blob: 1b8d1228c8fc044303facab966bd9777636cc32c [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 Kivity313dbd492008-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);
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002156 int cpu = raw_smp_processor_id();
Avi Kivity26bb0982009-09-07 11:14:12 +03002157 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002158
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002159 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002160 return;
2161
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002162 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002163 /*
2164 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2165 * allow segment selectors with cpl > 0 or ti == 1.
2166 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002167 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002168 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002169
2170#ifdef CONFIG_X86_64
2171 save_fsgs_for_kvm();
2172 vmx->host_state.fs_sel = current->thread.fsindex;
2173 vmx->host_state.gs_sel = current->thread.gsindex;
2174#else
Avi Kivity9581d442010-10-19 16:46:55 +02002175 savesegment(fs, vmx->host_state.fs_sel);
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002176 savesegment(gs, vmx->host_state.gs_sel);
2177#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002178 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002179 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002180 vmx->host_state.fs_reload_needed = 0;
2181 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002182 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002183 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002184 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002185 if (!(vmx->host_state.gs_sel & 7))
2186 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002187 else {
2188 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002189 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002190 }
2191
2192#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002193 savesegment(ds, vmx->host_state.ds_sel);
2194 savesegment(es, vmx->host_state.es_sel);
2195#endif
2196
2197#ifdef CONFIG_X86_64
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002198 vmcs_writel(HOST_FS_BASE, current->thread.fsbase);
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002199 vmcs_writel(HOST_GS_BASE, cpu_kernelmode_gs_base(cpu));
Avi Kivity33ed6322007-05-02 16:54:03 +03002200#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002201 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2202 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03002203#endif
Avi Kivity707c0872007-05-02 17:33:43 +03002204
2205#ifdef CONFIG_X86_64
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002206 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Avi Kivityc8770e72010-11-11 12:37:26 +02002207 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002208 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03002209#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002210 if (boot_cpu_has(X86_FEATURE_MPX))
2211 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002212 for (i = 0; i < vmx->save_nmsrs; ++i)
2213 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002214 vmx->guest_msrs[i].data,
2215 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002216}
2217
Avi Kivitya9b21b62008-06-24 11:48:49 +03002218static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002219{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002220 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002221 return;
2222
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002223 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002224 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002225#ifdef CONFIG_X86_64
2226 if (is_long_mode(&vmx->vcpu))
2227 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2228#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002229 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002230 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002231#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002232 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002233#else
2234 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002235#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002236 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002237 if (vmx->host_state.fs_reload_needed)
2238 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002239#ifdef CONFIG_X86_64
2240 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2241 loadsegment(ds, vmx->host_state.ds_sel);
2242 loadsegment(es, vmx->host_state.es_sel);
2243 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002244#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002245 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002246#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002247 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002248#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002249 if (vmx->host_state.msr_host_bndcfgs)
2250 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Thomas Garnier45fc8752017-03-14 10:05:08 -07002251 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002252}
2253
Avi Kivitya9b21b62008-06-24 11:48:49 +03002254static void vmx_load_host_state(struct vcpu_vmx *vmx)
2255{
2256 preempt_disable();
2257 __vmx_load_host_state(vmx);
2258 preempt_enable();
2259}
2260
Feng Wu28b835d2015-09-18 22:29:54 +08002261static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2262{
2263 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2264 struct pi_desc old, new;
2265 unsigned int dest;
2266
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002267 /*
2268 * In case of hot-plug or hot-unplug, we may have to undo
2269 * vmx_vcpu_pi_put even if there is no assigned device. And we
2270 * always keep PI.NDST up to date for simplicity: it makes the
2271 * code easier, and CPU migration is not a fast path.
2272 */
2273 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002274 return;
2275
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002276 /*
2277 * First handle the simple case where no cmpxchg is necessary; just
2278 * allow posting non-urgent interrupts.
2279 *
2280 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2281 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2282 * expects the VCPU to be on the blocked_vcpu_list that matches
2283 * PI.NDST.
2284 */
2285 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2286 vcpu->cpu == cpu) {
2287 pi_clear_sn(pi_desc);
2288 return;
2289 }
2290
2291 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002292 do {
2293 old.control = new.control = pi_desc->control;
2294
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002295 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002296
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002297 if (x2apic_enabled())
2298 new.ndst = dest;
2299 else
2300 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002301
Feng Wu28b835d2015-09-18 22:29:54 +08002302 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02002303 } while (cmpxchg64(&pi_desc->control, old.control,
2304 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002305}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002306
Peter Feinerc95ba922016-08-17 09:36:47 -07002307static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2308{
2309 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2310 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2311}
2312
Avi Kivity6aa8b732006-12-10 02:21:36 -08002313/*
2314 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2315 * vcpu mutex is already taken.
2316 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002317static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002318{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002319 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002320 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002321
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002322 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002323 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002324 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002325 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002326
2327 /*
2328 * Read loaded_vmcs->cpu should be before fetching
2329 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2330 * See the comments in __loaded_vmcs_clear().
2331 */
2332 smp_rmb();
2333
Nadav Har'Eld462b812011-05-24 15:26:10 +03002334 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2335 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002336 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002337 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002338 }
2339
2340 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2341 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2342 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01002343 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002344 }
2345
2346 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002347 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002348 unsigned long sysenter_esp;
2349
2350 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002351
Avi Kivity6aa8b732006-12-10 02:21:36 -08002352 /*
2353 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002354 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002355 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002356 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01002357 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002358 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002359
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002360 /*
2361 * VM exits change the host TR limit to 0x67 after a VM
2362 * exit. This is okay, since 0x67 covers everything except
2363 * the IO bitmap and have have code to handle the IO bitmap
2364 * being lost after a VM exit.
2365 */
2366 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2367
Avi Kivity6aa8b732006-12-10 02:21:36 -08002368 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2369 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002370
Nadav Har'Eld462b812011-05-24 15:26:10 +03002371 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002372 }
Feng Wu28b835d2015-09-18 22:29:54 +08002373
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002374 /* Setup TSC multiplier */
2375 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002376 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2377 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002378
Feng Wu28b835d2015-09-18 22:29:54 +08002379 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002380 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08002381 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08002382}
2383
2384static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2385{
2386 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2387
2388 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002389 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2390 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002391 return;
2392
2393 /* Set SN when the vCPU is preempted */
2394 if (vcpu->preempted)
2395 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002396}
2397
2398static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2399{
Feng Wu28b835d2015-09-18 22:29:54 +08002400 vmx_vcpu_pi_put(vcpu);
2401
Avi Kivitya9b21b62008-06-24 11:48:49 +03002402 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002403}
2404
Wanpeng Lif244dee2017-07-20 01:11:54 -07002405static bool emulation_required(struct kvm_vcpu *vcpu)
2406{
2407 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2408}
2409
Avi Kivityedcafe32009-12-30 18:07:40 +02002410static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2411
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002412/*
2413 * Return the cr0 value that a nested guest would read. This is a combination
2414 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2415 * its hypervisor (cr0_read_shadow).
2416 */
2417static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2418{
2419 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2420 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2421}
2422static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2423{
2424 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2425 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2426}
2427
Avi Kivity6aa8b732006-12-10 02:21:36 -08002428static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2429{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002430 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002431
Avi Kivity6de12732011-03-07 12:51:22 +02002432 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2433 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2434 rflags = vmcs_readl(GUEST_RFLAGS);
2435 if (to_vmx(vcpu)->rmode.vm86_active) {
2436 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2437 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2438 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2439 }
2440 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002441 }
Avi Kivity6de12732011-03-07 12:51:22 +02002442 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002443}
2444
2445static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2446{
Wanpeng Lif244dee2017-07-20 01:11:54 -07002447 unsigned long old_rflags = vmx_get_rflags(vcpu);
2448
Avi Kivity6de12732011-03-07 12:51:22 +02002449 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2450 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002451 if (to_vmx(vcpu)->rmode.vm86_active) {
2452 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002453 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002454 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002455 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07002456
2457 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2458 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002459}
2460
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002461static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002462{
2463 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2464 int ret = 0;
2465
2466 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002467 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002468 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002469 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002470
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002471 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002472}
2473
2474static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2475{
2476 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2477 u32 interruptibility = interruptibility_old;
2478
2479 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2480
Jan Kiszka48005f62010-02-19 19:38:07 +01002481 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002482 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002483 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002484 interruptibility |= GUEST_INTR_STATE_STI;
2485
2486 if ((interruptibility != interruptibility_old))
2487 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2488}
2489
Avi Kivity6aa8b732006-12-10 02:21:36 -08002490static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2491{
2492 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002493
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002494 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002495 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002496 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002497
Glauber Costa2809f5d2009-05-12 16:21:05 -04002498 /* skipping an emulated instruction also counts */
2499 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002500}
2501
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002502static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
2503 unsigned long exit_qual)
2504{
2505 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2506 unsigned int nr = vcpu->arch.exception.nr;
2507 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2508
2509 if (vcpu->arch.exception.has_error_code) {
2510 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
2511 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2512 }
2513
2514 if (kvm_exception_is_soft(nr))
2515 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2516 else
2517 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2518
2519 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
2520 vmx_get_nmi_mask(vcpu))
2521 intr_info |= INTR_INFO_UNBLOCK_NMI;
2522
2523 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
2524}
2525
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002526/*
2527 * KVM wants to inject page-faults which it got to the guest. This function
2528 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002529 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002530static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002531{
2532 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002533 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002534
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002535 if (nr == PF_VECTOR) {
2536 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002537 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002538 return 1;
2539 }
2540 /*
2541 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
2542 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2543 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
2544 * can be written only when inject_pending_event runs. This should be
2545 * conditional on a new capability---if the capability is disabled,
2546 * kvm_multiple_exception would write the ancillary information to
2547 * CR2 or DR6, for backwards ABI-compatibility.
2548 */
2549 if (nested_vmx_is_page_fault_vmexit(vmcs12,
2550 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002551 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002552 return 1;
2553 }
2554 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002555 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002556 if (nr == DB_VECTOR)
2557 *exit_qual = vcpu->arch.dr6;
2558 else
2559 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002560 return 1;
2561 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002562 }
2563
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002564 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002565}
2566
Wanpeng Licaa057a2018-03-12 04:53:03 -07002567static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
2568{
2569 /*
2570 * Ensure that we clear the HLT state in the VMCS. We don't need to
2571 * explicitly skip the instruction because if the HLT state is set,
2572 * then the instruction is already executing and RIP has already been
2573 * advanced.
2574 */
2575 if (kvm_hlt_in_guest(vcpu->kvm) &&
2576 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
2577 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
2578}
2579
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002580static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02002581{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002582 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002583 unsigned nr = vcpu->arch.exception.nr;
2584 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002585 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002586 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002587
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002588 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002589 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002590 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2591 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002592
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002593 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002594 int inc_eip = 0;
2595 if (kvm_exception_is_soft(nr))
2596 inc_eip = vcpu->arch.event_exit_inst_len;
2597 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002598 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002599 return;
2600 }
2601
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002602 if (kvm_exception_is_soft(nr)) {
2603 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2604 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002605 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2606 } else
2607 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2608
2609 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07002610
2611 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02002612}
2613
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002614static bool vmx_rdtscp_supported(void)
2615{
2616 return cpu_has_vmx_rdtscp();
2617}
2618
Mao, Junjiead756a12012-07-02 01:18:48 +00002619static bool vmx_invpcid_supported(void)
2620{
2621 return cpu_has_vmx_invpcid() && enable_ept;
2622}
2623
Avi Kivity6aa8b732006-12-10 02:21:36 -08002624/*
Eddie Donga75beee2007-05-17 18:55:15 +03002625 * Swap MSR entry in host/guest MSR entry array.
2626 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002627static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002628{
Avi Kivity26bb0982009-09-07 11:14:12 +03002629 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002630
2631 tmp = vmx->guest_msrs[to];
2632 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2633 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002634}
2635
2636/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002637 * Set up the vmcs to automatically save and restore system
2638 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2639 * mode, as fiddling with msrs is very expensive.
2640 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002641static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002642{
Avi Kivity26bb0982009-09-07 11:14:12 +03002643 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002644
Eddie Donga75beee2007-05-17 18:55:15 +03002645 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002646#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002647 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002648 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002649 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002650 move_msr_up(vmx, index, save_nmsrs++);
2651 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002652 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002653 move_msr_up(vmx, index, save_nmsrs++);
2654 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002655 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002656 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002657 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02002658 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002659 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002660 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002661 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002662 * if efer.sce is enabled.
2663 */
Brian Gerst8c065852010-07-17 09:03:26 -04002664 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002665 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002666 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002667 }
Eddie Donga75beee2007-05-17 18:55:15 +03002668#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002669 index = __find_msr_index(vmx, MSR_EFER);
2670 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002671 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002672
Avi Kivity26bb0982009-09-07 11:14:12 +03002673 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002674
Yang Zhang8d146952013-01-25 10:18:50 +08002675 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002676 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002677}
2678
2679/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002680 * reads and returns guest's timestamp counter "register"
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002681 * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
2682 * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3
Avi Kivity6aa8b732006-12-10 02:21:36 -08002683 */
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002684static u64 guest_read_tsc(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002685{
2686 u64 host_tsc, tsc_offset;
2687
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002688 host_tsc = rdtsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002689 tsc_offset = vmcs_read64(TSC_OFFSET);
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002690 return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002691}
2692
2693/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002694 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002695 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002696static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002697{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002698 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002699 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002700 * We're here if L1 chose not to trap WRMSR to TSC. According
2701 * to the spec, this should set L1's TSC; The offset that L1
2702 * set for L2 remains unchanged, and still needs to be added
2703 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002704 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002705 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002706 /* recalculate vmcs02.TSC_OFFSET: */
2707 vmcs12 = get_vmcs12(vcpu);
2708 vmcs_write64(TSC_OFFSET, offset +
2709 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2710 vmcs12->tsc_offset : 0));
2711 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002712 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2713 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002714 vmcs_write64(TSC_OFFSET, offset);
2715 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002716}
2717
Nadav Har'El801d3422011-05-25 23:02:23 +03002718/*
2719 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2720 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2721 * all guests if the "nested" module option is off, and can also be disabled
2722 * for a single guest by disabling its VMX cpuid bit.
2723 */
2724static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2725{
Radim Krčmářd6321d42017-08-05 00:12:49 +02002726 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03002727}
2728
Avi Kivity6aa8b732006-12-10 02:21:36 -08002729/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002730 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2731 * returned for the various VMX controls MSRs when nested VMX is enabled.
2732 * The same values should also be used to verify that vmcs12 control fields are
2733 * valid during nested entry from L1 to L2.
2734 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2735 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2736 * bit in the high half is on if the corresponding bit in the control field
2737 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002738 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002739static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002740{
Paolo Bonzini13893092018-02-26 13:40:09 +01002741 if (!nested) {
2742 memset(msrs, 0, sizeof(*msrs));
2743 return;
2744 }
2745
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002746 /*
2747 * Note that as a general rule, the high half of the MSRs (bits in
2748 * the control fields which may be 1) should be initialized by the
2749 * intersection of the underlying hardware's MSR (i.e., features which
2750 * can be supported) and the list of features we want to expose -
2751 * because they are known to be properly supported in our code.
2752 * Also, usually, the low half of the MSRs (bits which must be 1) can
2753 * be set to 0, meaning that L1 may turn off any of these bits. The
2754 * reason is that if one of these bits is necessary, it will appear
2755 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2756 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02002757 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002758 * These rules have exceptions below.
2759 */
2760
2761 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002762 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002763 msrs->pinbased_ctls_low,
2764 msrs->pinbased_ctls_high);
2765 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002766 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002767 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002768 PIN_BASED_EXT_INTR_MASK |
2769 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01002770 PIN_BASED_VIRTUAL_NMIS |
2771 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002772 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002773 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002774 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002775
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002776 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002777 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002778 msrs->exit_ctls_low,
2779 msrs->exit_ctls_high);
2780 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08002781 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04002782
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002783 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002784#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002785 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002786#endif
Jan Kiszkaf41245002014-03-07 20:03:13 +01002787 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002788 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002789 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf41245002014-03-07 20:03:13 +01002790 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04002791 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2792
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002793 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002794 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002795
Jan Kiszka2996fca2014-06-16 13:59:43 +02002796 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002797 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02002798
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002799 /* entry controls */
2800 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002801 msrs->entry_ctls_low,
2802 msrs->entry_ctls_high);
2803 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08002804 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002805 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02002806#ifdef CONFIG_X86_64
2807 VM_ENTRY_IA32E_MODE |
2808#endif
2809 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002810 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002811 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002812 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002813 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02002814
Jan Kiszka2996fca2014-06-16 13:59:43 +02002815 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002816 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02002817
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002818 /* cpu-based controls */
2819 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002820 msrs->procbased_ctls_low,
2821 msrs->procbased_ctls_high);
2822 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08002823 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002824 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01002825 CPU_BASED_VIRTUAL_INTR_PENDING |
2826 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002827 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2828 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2829 CPU_BASED_CR3_STORE_EXITING |
2830#ifdef CONFIG_X86_64
2831 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2832#endif
2833 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03002834 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
2835 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
2836 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
2837 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002838 /*
2839 * We can allow some features even when not supported by the
2840 * hardware. For example, L1 can specify an MSR bitmap - and we
2841 * can use it to avoid exits to L1 - even when L0 runs L2
2842 * without MSR bitmaps.
2843 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002844 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002845 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02002846 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002847
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002848 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002849 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002850 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
2851
Paolo Bonzini80154d72017-08-24 13:55:35 +02002852 /*
2853 * secondary cpu-based controls. Do not include those that
2854 * depend on CPUID bits, they are added later by vmx_cpuid_update.
2855 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002856 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002857 msrs->secondary_ctls_low,
2858 msrs->secondary_ctls_high);
2859 msrs->secondary_ctls_low = 0;
2860 msrs->secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002861 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02002862 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08002863 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08002864 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08002865 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02002866 SECONDARY_EXEC_WBINVD_EXITING;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002867
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002868 if (enable_ept) {
2869 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002870 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01002871 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002872 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01002873 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04002874 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002875 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04002876 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002877 msrs->ept_caps &= vmx_capability.ept;
2878 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01002879 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
2880 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04002881 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002882 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04002883 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002884 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04002885 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02002886 }
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002887
Bandan Das27c42a12017-08-03 15:54:42 -04002888 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002889 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04002890 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04002891 /*
2892 * Advertise EPTP switching unconditionally
2893 * since we emulate it
2894 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08002895 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002896 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08002897 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04002898 }
2899
Paolo Bonzinief697a72016-03-18 16:58:38 +01002900 /*
2901 * Old versions of KVM use the single-context version without
2902 * checking for support, so declare that it is supported even
2903 * though it is treated as global context. The alternative is
2904 * not failing the single-context invvpid, and it is worse.
2905 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07002906 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002907 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07002908 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002909 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03002910 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02002911 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07002912
Radim Krčmář0790ec12015-03-17 14:02:32 +01002913 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002914 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01002915 SECONDARY_EXEC_UNRESTRICTED_GUEST;
2916
Jan Kiszkac18911a2013-03-13 16:06:41 +01002917 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002918 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002919 msrs->misc_low,
2920 msrs->misc_high);
2921 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
2922 msrs->misc_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002923 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf41245002014-03-07 20:03:13 +01002924 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002925 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08002926
2927 /*
2928 * This MSR reports some information about VMX support. We
2929 * should return information about the VMX we emulate for the
2930 * guest, and the VMCS structure we give it - not about the
2931 * VMX support of the underlying hardware.
2932 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002933 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08002934 VMCS12_REVISION |
2935 VMX_BASIC_TRUE_CTLS |
2936 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
2937 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
2938
2939 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002940 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08002941
2942 /*
David Matlack8322ebb2016-11-29 18:14:09 -08002943 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08002944 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
2945 * We picked the standard core2 setting.
2946 */
2947#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
2948#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002949 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
2950 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08002951
2952 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002953 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
2954 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08002955
2956 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002957 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002958}
2959
David Matlack38991522016-11-29 18:14:08 -08002960/*
2961 * if fixed0[i] == 1: val[i] must be 1
2962 * if fixed1[i] == 0: val[i] must be 0
2963 */
2964static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
2965{
2966 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002967}
2968
2969static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
2970{
David Matlack38991522016-11-29 18:14:08 -08002971 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002972}
2973
2974static inline u64 vmx_control_msr(u32 low, u32 high)
2975{
2976 return low | ((u64)high << 32);
2977}
2978
David Matlack62cc6b9d2016-11-29 18:14:07 -08002979static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
2980{
2981 superset &= mask;
2982 subset &= mask;
2983
2984 return (superset | subset) == superset;
2985}
2986
2987static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
2988{
2989 const u64 feature_and_reserved =
2990 /* feature (except bit 48; see below) */
2991 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
2992 /* reserved */
2993 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002994 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08002995
2996 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
2997 return -EINVAL;
2998
2999 /*
3000 * KVM does not emulate a version of VMX that constrains physical
3001 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3002 */
3003 if (data & BIT_ULL(48))
3004 return -EINVAL;
3005
3006 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3007 vmx_basic_vmcs_revision_id(data))
3008 return -EINVAL;
3009
3010 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3011 return -EINVAL;
3012
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003013 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003014 return 0;
3015}
3016
3017static int
3018vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3019{
3020 u64 supported;
3021 u32 *lowp, *highp;
3022
3023 switch (msr_index) {
3024 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003025 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3026 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003027 break;
3028 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003029 lowp = &vmx->nested.msrs.procbased_ctls_low;
3030 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003031 break;
3032 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003033 lowp = &vmx->nested.msrs.exit_ctls_low;
3034 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003035 break;
3036 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003037 lowp = &vmx->nested.msrs.entry_ctls_low;
3038 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003039 break;
3040 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003041 lowp = &vmx->nested.msrs.secondary_ctls_low;
3042 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003043 break;
3044 default:
3045 BUG();
3046 }
3047
3048 supported = vmx_control_msr(*lowp, *highp);
3049
3050 /* Check must-be-1 bits are still 1. */
3051 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3052 return -EINVAL;
3053
3054 /* Check must-be-0 bits are still 0. */
3055 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3056 return -EINVAL;
3057
3058 *lowp = data;
3059 *highp = data >> 32;
3060 return 0;
3061}
3062
3063static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3064{
3065 const u64 feature_and_reserved_bits =
3066 /* feature */
3067 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3068 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3069 /* reserved */
3070 GENMASK_ULL(13, 9) | BIT_ULL(31);
3071 u64 vmx_misc;
3072
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003073 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3074 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003075
3076 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3077 return -EINVAL;
3078
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003079 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003080 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3081 vmx_misc_preemption_timer_rate(data) !=
3082 vmx_misc_preemption_timer_rate(vmx_misc))
3083 return -EINVAL;
3084
3085 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3086 return -EINVAL;
3087
3088 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3089 return -EINVAL;
3090
3091 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3092 return -EINVAL;
3093
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003094 vmx->nested.msrs.misc_low = data;
3095 vmx->nested.msrs.misc_high = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003096 return 0;
3097}
3098
3099static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3100{
3101 u64 vmx_ept_vpid_cap;
3102
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003103 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3104 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003105
3106 /* Every bit is either reserved or a feature bit. */
3107 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3108 return -EINVAL;
3109
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003110 vmx->nested.msrs.ept_caps = data;
3111 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003112 return 0;
3113}
3114
3115static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3116{
3117 u64 *msr;
3118
3119 switch (msr_index) {
3120 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003121 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003122 break;
3123 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003124 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003125 break;
3126 default:
3127 BUG();
3128 }
3129
3130 /*
3131 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3132 * must be 1 in the restored value.
3133 */
3134 if (!is_bitwise_subset(data, *msr, -1ULL))
3135 return -EINVAL;
3136
3137 *msr = data;
3138 return 0;
3139}
3140
3141/*
3142 * Called when userspace is restoring VMX MSRs.
3143 *
3144 * Returns 0 on success, non-0 otherwise.
3145 */
3146static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3147{
3148 struct vcpu_vmx *vmx = to_vmx(vcpu);
3149
3150 switch (msr_index) {
3151 case MSR_IA32_VMX_BASIC:
3152 return vmx_restore_vmx_basic(vmx, data);
3153 case MSR_IA32_VMX_PINBASED_CTLS:
3154 case MSR_IA32_VMX_PROCBASED_CTLS:
3155 case MSR_IA32_VMX_EXIT_CTLS:
3156 case MSR_IA32_VMX_ENTRY_CTLS:
3157 /*
3158 * The "non-true" VMX capability MSRs are generated from the
3159 * "true" MSRs, so we do not support restoring them directly.
3160 *
3161 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3162 * should restore the "true" MSRs with the must-be-1 bits
3163 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3164 * DEFAULT SETTINGS".
3165 */
3166 return -EINVAL;
3167 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3168 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3169 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3170 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3171 case MSR_IA32_VMX_PROCBASED_CTLS2:
3172 return vmx_restore_control_msr(vmx, msr_index, data);
3173 case MSR_IA32_VMX_MISC:
3174 return vmx_restore_vmx_misc(vmx, data);
3175 case MSR_IA32_VMX_CR0_FIXED0:
3176 case MSR_IA32_VMX_CR4_FIXED0:
3177 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3178 case MSR_IA32_VMX_CR0_FIXED1:
3179 case MSR_IA32_VMX_CR4_FIXED1:
3180 /*
3181 * These MSRs are generated based on the vCPU's CPUID, so we
3182 * do not support restoring them directly.
3183 */
3184 return -EINVAL;
3185 case MSR_IA32_VMX_EPT_VPID_CAP:
3186 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3187 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003188 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003189 return 0;
3190 default:
3191 /*
3192 * The rest of the VMX capability MSRs do not support restore.
3193 */
3194 return -EINVAL;
3195 }
3196}
3197
Jan Kiszkacae50132014-01-04 18:47:22 +01003198/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003199static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003200{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003201 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003202 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003203 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003204 break;
3205 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3206 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003207 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003208 msrs->pinbased_ctls_low,
3209 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003210 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3211 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003212 break;
3213 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3214 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003215 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003216 msrs->procbased_ctls_low,
3217 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003218 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3219 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003220 break;
3221 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3222 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003223 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003224 msrs->exit_ctls_low,
3225 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003226 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3227 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003228 break;
3229 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3230 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003231 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003232 msrs->entry_ctls_low,
3233 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003234 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3235 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003236 break;
3237 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003238 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003239 msrs->misc_low,
3240 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003241 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003242 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003243 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003244 break;
3245 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003246 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003247 break;
3248 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003249 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003250 break;
3251 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003252 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003253 break;
3254 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003255 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003256 break;
3257 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003258 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003259 msrs->secondary_ctls_low,
3260 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003261 break;
3262 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003263 *pdata = msrs->ept_caps |
3264 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003265 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003266 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003267 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04003268 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003269 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003270 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003271 }
3272
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003273 return 0;
3274}
3275
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003276static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3277 uint64_t val)
3278{
3279 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3280
3281 return !(val & ~valid_bits);
3282}
3283
Tom Lendacky801e4592018-02-21 13:39:51 -06003284static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3285{
Paolo Bonzini13893092018-02-26 13:40:09 +01003286 switch (msr->index) {
3287 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3288 if (!nested)
3289 return 1;
3290 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
3291 default:
3292 return 1;
3293 }
3294
3295 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06003296}
3297
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003298/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003299 * Reads an msr value (of 'msr_index') into 'pdata'.
3300 * Returns 0 on success, non-0 otherwise.
3301 * Assumes vcpu_load() was already called.
3302 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003303static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003304{
Borislav Petkova6cb0992017-12-20 12:50:28 +01003305 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003306 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003307
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003308 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003309#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003310 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003311 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003312 break;
3313 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003314 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003315 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003316 case MSR_KERNEL_GS_BASE:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003317 vmx_load_host_state(vmx);
3318 msr_info->data = vmx->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003319 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003320#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003321 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003322 return kvm_get_msr_common(vcpu, msr_info);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303323 case MSR_IA32_TSC:
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08003324 msr_info->data = guest_read_tsc(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003325 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003326 case MSR_IA32_SPEC_CTRL:
3327 if (!msr_info->host_initiated &&
3328 !guest_cpuid_has(vcpu, X86_FEATURE_IBRS) &&
3329 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3330 return 1;
3331
3332 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3333 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003334 case MSR_IA32_ARCH_CAPABILITIES:
3335 if (!msr_info->host_initiated &&
3336 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3337 return 1;
3338 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3339 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003340 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003341 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003342 break;
3343 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003344 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003345 break;
3346 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003347 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003348 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003349 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003350 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003351 (!msr_info->host_initiated &&
3352 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003353 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003354 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003355 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003356 case MSR_IA32_MCG_EXT_CTL:
3357 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01003358 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08003359 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003360 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003361 msr_info->data = vcpu->arch.mcg_ext_ctl;
3362 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003363 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003364 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003365 break;
3366 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3367 if (!nested_vmx_allowed(vcpu))
3368 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003369 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
3370 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003371 case MSR_IA32_XSS:
3372 if (!vmx_xsaves_supported())
3373 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003374 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003375 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003376 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003377 if (!msr_info->host_initiated &&
3378 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003379 return 1;
3380 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003381 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003382 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003383 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003384 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003385 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003386 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003387 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003388 }
3389
Avi Kivity6aa8b732006-12-10 02:21:36 -08003390 return 0;
3391}
3392
Jan Kiszkacae50132014-01-04 18:47:22 +01003393static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3394
Avi Kivity6aa8b732006-12-10 02:21:36 -08003395/*
3396 * Writes msr value into into the appropriate "register".
3397 * Returns 0 on success, non-0 otherwise.
3398 * Assumes vcpu_load() was already called.
3399 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003400static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003401{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003402 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003403 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003404 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003405 u32 msr_index = msr_info->index;
3406 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003407
Avi Kivity6aa8b732006-12-10 02:21:36 -08003408 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003409 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003410 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003411 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003412#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003413 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003414 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003415 vmcs_writel(GUEST_FS_BASE, data);
3416 break;
3417 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003418 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003419 vmcs_writel(GUEST_GS_BASE, data);
3420 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003421 case MSR_KERNEL_GS_BASE:
3422 vmx_load_host_state(vmx);
3423 vmx->msr_guest_kernel_gs_base = data;
3424 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003425#endif
3426 case MSR_IA32_SYSENTER_CS:
3427 vmcs_write32(GUEST_SYSENTER_CS, data);
3428 break;
3429 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003430 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003431 break;
3432 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003433 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003434 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003435 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003436 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003437 (!msr_info->host_initiated &&
3438 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003439 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08003440 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07003441 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003442 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003443 vmcs_write64(GUEST_BNDCFGS, data);
3444 break;
3445 case MSR_IA32_TSC:
3446 kvm_write_tsc(vcpu, msr_info);
3447 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003448 case MSR_IA32_SPEC_CTRL:
3449 if (!msr_info->host_initiated &&
3450 !guest_cpuid_has(vcpu, X86_FEATURE_IBRS) &&
3451 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3452 return 1;
3453
3454 /* The STIBP bit doesn't fault even if it's not advertised */
3455 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP))
3456 return 1;
3457
3458 vmx->spec_ctrl = data;
3459
3460 if (!data)
3461 break;
3462
3463 /*
3464 * For non-nested:
3465 * When it's written (to non-zero) for the first time, pass
3466 * it through.
3467 *
3468 * For nested:
3469 * The handling of the MSR bitmap for L2 guests is done in
3470 * nested_vmx_merge_msr_bitmap. We should not touch the
3471 * vmcs02.msr_bitmap here since it gets completely overwritten
3472 * in the merging. We update the vmcs01 here for L1 as well
3473 * since it will end up touching the MSR anyway now.
3474 */
3475 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3476 MSR_IA32_SPEC_CTRL,
3477 MSR_TYPE_RW);
3478 break;
Ashok Raj15d45072018-02-01 22:59:43 +01003479 case MSR_IA32_PRED_CMD:
3480 if (!msr_info->host_initiated &&
3481 !guest_cpuid_has(vcpu, X86_FEATURE_IBPB) &&
3482 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3483 return 1;
3484
3485 if (data & ~PRED_CMD_IBPB)
3486 return 1;
3487
3488 if (!data)
3489 break;
3490
3491 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3492
3493 /*
3494 * For non-nested:
3495 * When it's written (to non-zero) for the first time, pass
3496 * it through.
3497 *
3498 * For nested:
3499 * The handling of the MSR bitmap for L2 guests is done in
3500 * nested_vmx_merge_msr_bitmap. We should not touch the
3501 * vmcs02.msr_bitmap here since it gets completely overwritten
3502 * in the merging.
3503 */
3504 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3505 MSR_TYPE_W);
3506 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003507 case MSR_IA32_ARCH_CAPABILITIES:
3508 if (!msr_info->host_initiated)
3509 return 1;
3510 vmx->arch_capabilities = data;
3511 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003512 case MSR_IA32_CR_PAT:
3513 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003514 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3515 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003516 vmcs_write64(GUEST_IA32_PAT, data);
3517 vcpu->arch.pat = data;
3518 break;
3519 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003520 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003521 break;
Will Auldba904632012-11-29 12:42:50 -08003522 case MSR_IA32_TSC_ADJUST:
3523 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003524 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003525 case MSR_IA32_MCG_EXT_CTL:
3526 if ((!msr_info->host_initiated &&
3527 !(to_vmx(vcpu)->msr_ia32_feature_control &
3528 FEATURE_CONTROL_LMCE)) ||
3529 (data & ~MCG_EXT_CTL_LMCE_EN))
3530 return 1;
3531 vcpu->arch.mcg_ext_ctl = data;
3532 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003533 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003534 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003535 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003536 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3537 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003538 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003539 if (msr_info->host_initiated && data == 0)
3540 vmx_leave_nested(vcpu);
3541 break;
3542 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003543 if (!msr_info->host_initiated)
3544 return 1; /* they are read-only */
3545 if (!nested_vmx_allowed(vcpu))
3546 return 1;
3547 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08003548 case MSR_IA32_XSS:
3549 if (!vmx_xsaves_supported())
3550 return 1;
3551 /*
3552 * The only supported bit as of Skylake is bit 8, but
3553 * it is not supported on KVM.
3554 */
3555 if (data != 0)
3556 return 1;
3557 vcpu->arch.ia32_xss = data;
3558 if (vcpu->arch.ia32_xss != host_xss)
3559 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
3560 vcpu->arch.ia32_xss, host_xss);
3561 else
3562 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3563 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003564 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003565 if (!msr_info->host_initiated &&
3566 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003567 return 1;
3568 /* Check reserved bit, higher 32 bits should be zero */
3569 if ((data >> 32) != 0)
3570 return 1;
3571 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003572 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10003573 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003574 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07003575 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003576 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003577 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3578 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003579 ret = kvm_set_shared_msr(msr->index, msr->data,
3580 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03003581 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003582 if (ret)
3583 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003584 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08003585 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003586 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003587 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003588 }
3589
Eddie Dong2cc51562007-05-21 07:28:09 +03003590 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003591}
3592
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003593static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003594{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003595 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3596 switch (reg) {
3597 case VCPU_REGS_RSP:
3598 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3599 break;
3600 case VCPU_REGS_RIP:
3601 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3602 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003603 case VCPU_EXREG_PDPTR:
3604 if (enable_ept)
3605 ept_save_pdptrs(vcpu);
3606 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003607 default:
3608 break;
3609 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003610}
3611
Avi Kivity6aa8b732006-12-10 02:21:36 -08003612static __init int cpu_has_kvm_support(void)
3613{
Eduardo Habkost6210e372008-11-17 19:03:16 -02003614 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003615}
3616
3617static __init int vmx_disabled_by_bios(void)
3618{
3619 u64 msr;
3620
3621 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04003622 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08003623 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04003624 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3625 && tboot_enabled())
3626 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08003627 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04003628 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08003629 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08003630 && !tboot_enabled()) {
3631 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08003632 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04003633 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08003634 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08003635 /* launched w/o TXT and VMX disabled */
3636 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3637 && !tboot_enabled())
3638 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04003639 }
3640
3641 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003642}
3643
Dongxiao Xu7725b892010-05-11 18:29:38 +08003644static void kvm_cpu_vmxon(u64 addr)
3645{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003646 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003647 intel_pt_handle_vmx(1);
3648
Dongxiao Xu7725b892010-05-11 18:29:38 +08003649 asm volatile (ASM_VMX_VMXON_RAX
3650 : : "a"(&addr), "m"(addr)
3651 : "memory", "cc");
3652}
3653
Radim Krčmář13a34e02014-08-28 15:13:03 +02003654static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003655{
3656 int cpu = raw_smp_processor_id();
3657 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04003658 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003659
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003660 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02003661 return -EBUSY;
3662
Nadav Har'Eld462b812011-05-24 15:26:10 +03003663 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08003664 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3665 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003666
3667 /*
3668 * Now we can enable the vmclear operation in kdump
3669 * since the loaded_vmcss_on_cpu list on this cpu
3670 * has been initialized.
3671 *
3672 * Though the cpu is not in VMX operation now, there
3673 * is no problem to enable the vmclear operation
3674 * for the loaded_vmcss_on_cpu list is empty!
3675 */
3676 crash_enable_local_vmclear(cpu);
3677
Avi Kivity6aa8b732006-12-10 02:21:36 -08003678 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04003679
3680 test_bits = FEATURE_CONTROL_LOCKED;
3681 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3682 if (tboot_enabled())
3683 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3684
3685 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003686 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04003687 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3688 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003689 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02003690 if (enable_ept)
3691 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02003692
3693 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003694}
3695
Nadav Har'Eld462b812011-05-24 15:26:10 +03003696static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03003697{
3698 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03003699 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03003700
Nadav Har'Eld462b812011-05-24 15:26:10 +03003701 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3702 loaded_vmcss_on_cpu_link)
3703 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03003704}
3705
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003706
3707/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3708 * tricks.
3709 */
3710static void kvm_cpu_vmxoff(void)
3711{
3712 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003713
3714 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003715 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003716}
3717
Radim Krčmář13a34e02014-08-28 15:13:03 +02003718static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003719{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003720 vmclear_local_loaded_vmcss();
3721 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003722}
3723
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003724static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04003725 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003726{
3727 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003728 u32 ctl = ctl_min | ctl_opt;
3729
3730 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3731
3732 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3733 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3734
3735 /* Ensure minimum (required) set of control bits are supported. */
3736 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003737 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003738
3739 *result = ctl;
3740 return 0;
3741}
3742
Avi Kivity110312c2010-12-21 12:54:20 +02003743static __init bool allow_1_setting(u32 msr, u32 ctl)
3744{
3745 u32 vmx_msr_low, vmx_msr_high;
3746
3747 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3748 return vmx_msr_high & ctl;
3749}
3750
Yang, Sheng002c7f72007-07-31 14:23:01 +03003751static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003752{
3753 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08003754 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003755 u32 _pin_based_exec_control = 0;
3756 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003757 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003758 u32 _vmexit_control = 0;
3759 u32 _vmentry_control = 0;
3760
Paolo Bonzini13893092018-02-26 13:40:09 +01003761 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05303762 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003763#ifdef CONFIG_X86_64
3764 CPU_BASED_CR8_LOAD_EXITING |
3765 CPU_BASED_CR8_STORE_EXITING |
3766#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08003767 CPU_BASED_CR3_LOAD_EXITING |
3768 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e22017-12-12 16:44:21 +08003769 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003770 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03003771 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003772 CPU_BASED_MWAIT_EXITING |
3773 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02003774 CPU_BASED_INVLPG_EXITING |
3775 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06003776
Sheng Yangf78e0e22007-10-29 09:40:42 +08003777 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08003778 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08003779 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003780 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3781 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003782 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003783#ifdef CONFIG_X86_64
3784 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3785 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3786 ~CPU_BASED_CR8_STORE_EXITING;
3787#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08003788 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08003789 min2 = 0;
3790 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08003791 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08003792 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08003793 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003794 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003795 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003796 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02003797 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00003798 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08003799 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003800 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03003801 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08003802 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08003803 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02003804 SECONDARY_EXEC_RDSEED_EXITING |
3805 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08003806 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04003807 SECONDARY_EXEC_TSC_SCALING |
3808 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08003809 if (adjust_vmx_controls(min2, opt2,
3810 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003811 &_cpu_based_2nd_exec_control) < 0)
3812 return -EIO;
3813 }
3814#ifndef CONFIG_X86_64
3815 if (!(_cpu_based_2nd_exec_control &
3816 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
3817 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
3818#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08003819
3820 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3821 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08003822 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003823 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3824 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08003825
Wanpeng Li61f1dd92017-10-18 16:02:19 -07003826 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
3827 &vmx_capability.ept, &vmx_capability.vpid);
3828
Sheng Yangd56f5462008-04-25 10:13:16 +08003829 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03003830 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
3831 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03003832 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
3833 CPU_BASED_CR3_STORE_EXITING |
3834 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07003835 } else if (vmx_capability.ept) {
3836 vmx_capability.ept = 0;
3837 pr_warn_once("EPT CAP should not exist if not support "
3838 "1-setting enable EPT VM-execution control\n");
3839 }
3840 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
3841 vmx_capability.vpid) {
3842 vmx_capability.vpid = 0;
3843 pr_warn_once("VPID CAP should not exist if not support "
3844 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08003845 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003846
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003847 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003848#ifdef CONFIG_X86_64
3849 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
3850#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08003851 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003852 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003853 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
3854 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003855 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003856
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01003857 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
3858 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
3859 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003860 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
3861 &_pin_based_exec_control) < 0)
3862 return -EIO;
3863
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02003864 if (cpu_has_broken_vmx_preemption_timer())
3865 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003866 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003867 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08003868 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
3869
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01003870 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00003871 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003872 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
3873 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003874 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003875
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003876 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003877
3878 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
3879 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003880 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003881
3882#ifdef CONFIG_X86_64
3883 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
3884 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03003885 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003886#endif
3887
3888 /* Require Write-Back (WB) memory type for VMCS accesses. */
3889 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003890 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003891
Yang, Sheng002c7f72007-07-31 14:23:01 +03003892 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02003893 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03003894 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003895 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003896
Yang, Sheng002c7f72007-07-31 14:23:01 +03003897 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
3898 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003899 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003900 vmcs_conf->vmexit_ctrl = _vmexit_control;
3901 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003902
Avi Kivity110312c2010-12-21 12:54:20 +02003903 cpu_has_load_ia32_efer =
3904 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3905 VM_ENTRY_LOAD_IA32_EFER)
3906 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3907 VM_EXIT_LOAD_IA32_EFER);
3908
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003909 cpu_has_load_perf_global_ctrl =
3910 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3911 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
3912 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3913 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
3914
3915 /*
3916 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02003917 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003918 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
3919 *
3920 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
3921 *
3922 * AAK155 (model 26)
3923 * AAP115 (model 30)
3924 * AAT100 (model 37)
3925 * BC86,AAY89,BD102 (model 44)
3926 * BA97 (model 46)
3927 *
3928 */
3929 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
3930 switch (boot_cpu_data.x86_model) {
3931 case 26:
3932 case 30:
3933 case 37:
3934 case 44:
3935 case 46:
3936 cpu_has_load_perf_global_ctrl = false;
3937 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
3938 "does not work properly. Using workaround\n");
3939 break;
3940 default:
3941 break;
3942 }
3943 }
3944
Borislav Petkov782511b2016-04-04 22:25:03 +02003945 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08003946 rdmsrl(MSR_IA32_XSS, host_xss);
3947
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003948 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003949}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003950
3951static struct vmcs *alloc_vmcs_cpu(int cpu)
3952{
3953 int node = cpu_to_node(cpu);
3954 struct page *pages;
3955 struct vmcs *vmcs;
3956
Vlastimil Babka96db8002015-09-08 15:03:50 -07003957 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003958 if (!pages)
3959 return NULL;
3960 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003961 memset(vmcs, 0, vmcs_config.size);
3962 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003963 return vmcs;
3964}
3965
Avi Kivity6aa8b732006-12-10 02:21:36 -08003966static void free_vmcs(struct vmcs *vmcs)
3967{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003968 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003969}
3970
Nadav Har'Eld462b812011-05-24 15:26:10 +03003971/*
3972 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
3973 */
3974static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3975{
3976 if (!loaded_vmcs->vmcs)
3977 return;
3978 loaded_vmcs_clear(loaded_vmcs);
3979 free_vmcs(loaded_vmcs->vmcs);
3980 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003981 if (loaded_vmcs->msr_bitmap)
3982 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07003983 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03003984}
3985
Paolo Bonzinif21f1652018-01-11 12:16:15 +01003986static struct vmcs *alloc_vmcs(void)
3987{
3988 return alloc_vmcs_cpu(raw_smp_processor_id());
3989}
3990
3991static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3992{
3993 loaded_vmcs->vmcs = alloc_vmcs();
3994 if (!loaded_vmcs->vmcs)
3995 return -ENOMEM;
3996
3997 loaded_vmcs->shadow_vmcs = NULL;
3998 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003999
4000 if (cpu_has_vmx_msr_bitmap()) {
4001 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4002 if (!loaded_vmcs->msr_bitmap)
4003 goto out_vmcs;
4004 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
4005 }
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004006 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004007
4008out_vmcs:
4009 free_loaded_vmcs(loaded_vmcs);
4010 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004011}
4012
Sam Ravnborg39959582007-06-01 00:47:13 -07004013static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004014{
4015 int cpu;
4016
Zachary Amsden3230bb42009-09-29 11:38:37 -10004017 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004018 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004019 per_cpu(vmxarea, cpu) = NULL;
4020 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004021}
4022
Jim Mattsond37f4262017-12-22 12:12:16 -08004023enum vmcs_field_width {
4024 VMCS_FIELD_WIDTH_U16 = 0,
4025 VMCS_FIELD_WIDTH_U64 = 1,
4026 VMCS_FIELD_WIDTH_U32 = 2,
4027 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004028};
4029
Jim Mattsond37f4262017-12-22 12:12:16 -08004030static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004031{
4032 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004033 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004034 return (field >> 13) & 0x3 ;
4035}
4036
4037static inline int vmcs_field_readonly(unsigned long field)
4038{
4039 return (((field >> 10) & 0x3) == 1);
4040}
4041
Bandan Dasfe2b2012014-04-21 15:20:14 -04004042static void init_vmcs_shadow_fields(void)
4043{
4044 int i, j;
4045
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004046 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4047 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004048 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004049 (i + 1 == max_shadow_read_only_fields ||
4050 shadow_read_only_fields[i + 1] != field + 1))
4051 pr_err("Missing field from shadow_read_only_field %x\n",
4052 field + 1);
4053
4054 clear_bit(field, vmx_vmread_bitmap);
4055#ifdef CONFIG_X86_64
4056 if (field & 1)
4057 continue;
4058#endif
4059 if (j < i)
4060 shadow_read_only_fields[j] = field;
4061 j++;
4062 }
4063 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004064
4065 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004066 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004067 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004068 (i + 1 == max_shadow_read_write_fields ||
4069 shadow_read_write_fields[i + 1] != field + 1))
4070 pr_err("Missing field from shadow_read_write_field %x\n",
4071 field + 1);
4072
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004073 /*
4074 * PML and the preemption timer can be emulated, but the
4075 * processor cannot vmwrite to fields that don't exist
4076 * on bare metal.
4077 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004078 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004079 case GUEST_PML_INDEX:
4080 if (!cpu_has_vmx_pml())
4081 continue;
4082 break;
4083 case VMX_PREEMPTION_TIMER_VALUE:
4084 if (!cpu_has_vmx_preemption_timer())
4085 continue;
4086 break;
4087 case GUEST_INTR_STATUS:
4088 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004089 continue;
4090 break;
4091 default:
4092 break;
4093 }
4094
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004095 clear_bit(field, vmx_vmwrite_bitmap);
4096 clear_bit(field, vmx_vmread_bitmap);
4097#ifdef CONFIG_X86_64
4098 if (field & 1)
4099 continue;
4100#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004101 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004102 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004103 j++;
4104 }
4105 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004106}
4107
Avi Kivity6aa8b732006-12-10 02:21:36 -08004108static __init int alloc_kvm_area(void)
4109{
4110 int cpu;
4111
Zachary Amsden3230bb42009-09-29 11:38:37 -10004112 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004113 struct vmcs *vmcs;
4114
4115 vmcs = alloc_vmcs_cpu(cpu);
4116 if (!vmcs) {
4117 free_kvm_area();
4118 return -ENOMEM;
4119 }
4120
4121 per_cpu(vmxarea, cpu) = vmcs;
4122 }
4123 return 0;
4124}
4125
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004126static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004127 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004128{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004129 if (!emulate_invalid_guest_state) {
4130 /*
4131 * CS and SS RPL should be equal during guest entry according
4132 * to VMX spec, but in reality it is not always so. Since vcpu
4133 * is in the middle of the transition from real mode to
4134 * protected mode it is safe to assume that RPL 0 is a good
4135 * default value.
4136 */
4137 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004138 save->selector &= ~SEGMENT_RPL_MASK;
4139 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004140 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004141 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004142 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004143}
4144
4145static void enter_pmode(struct kvm_vcpu *vcpu)
4146{
4147 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004148 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004149
Gleb Natapovd99e4152012-12-20 16:57:45 +02004150 /*
4151 * Update real mode segment cache. It may be not up-to-date if sement
4152 * register was written while vcpu was in a guest mode.
4153 */
4154 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4155 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4156 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4157 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4158 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4159 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4160
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004161 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004162
Avi Kivity2fb92db2011-04-27 19:42:18 +03004163 vmx_segment_cache_clear(vmx);
4164
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004165 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004166
4167 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004168 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4169 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004170 vmcs_writel(GUEST_RFLAGS, flags);
4171
Rusty Russell66aee912007-07-17 23:34:16 +10004172 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4173 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004174
4175 update_exception_bitmap(vcpu);
4176
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004177 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4178 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4179 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4180 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4181 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4182 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004183}
4184
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004185static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004186{
Mathias Krause772e0312012-08-30 01:30:19 +02004187 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004188 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004189
Gleb Natapovd99e4152012-12-20 16:57:45 +02004190 var.dpl = 0x3;
4191 if (seg == VCPU_SREG_CS)
4192 var.type = 0x3;
4193
4194 if (!emulate_invalid_guest_state) {
4195 var.selector = var.base >> 4;
4196 var.base = var.base & 0xffff0;
4197 var.limit = 0xffff;
4198 var.g = 0;
4199 var.db = 0;
4200 var.present = 1;
4201 var.s = 1;
4202 var.l = 0;
4203 var.unusable = 0;
4204 var.type = 0x3;
4205 var.avl = 0;
4206 if (save->base & 0xf)
4207 printk_once(KERN_WARNING "kvm: segment base is not "
4208 "paragraph aligned when entering "
4209 "protected mode (seg=%d)", seg);
4210 }
4211
4212 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004213 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004214 vmcs_write32(sf->limit, var.limit);
4215 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004216}
4217
4218static void enter_rmode(struct kvm_vcpu *vcpu)
4219{
4220 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004221 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004222
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004223 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4224 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4225 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4226 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4227 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004228 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4229 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004230
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004231 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004232
Gleb Natapov776e58e2011-03-13 12:34:27 +02004233 /*
4234 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004235 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004236 */
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004237 if (!vcpu->kvm->arch.tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004238 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4239 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004240
Avi Kivity2fb92db2011-04-27 19:42:18 +03004241 vmx_segment_cache_clear(vmx);
4242
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004243 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004244 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004245 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4246
4247 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004248 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004249
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004250 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004251
4252 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004253 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004254 update_exception_bitmap(vcpu);
4255
Gleb Natapovd99e4152012-12-20 16:57:45 +02004256 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4257 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4258 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4259 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4260 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4261 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004262
Eddie Dong8668a3c2007-10-10 14:26:45 +08004263 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004264}
4265
Amit Shah401d10d2009-02-20 22:53:37 +05304266static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4267{
4268 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004269 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4270
4271 if (!msr)
4272 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304273
Avi Kivity44ea2b12009-09-06 15:55:37 +03004274 /*
4275 * Force kernel_gs_base reloading before EFER changes, as control
4276 * of this msr depends on is_long_mode().
4277 */
4278 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004279 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304280 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004281 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304282 msr->data = efer;
4283 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004284 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304285
4286 msr->data = efer & ~EFER_LME;
4287 }
4288 setup_msrs(vmx);
4289}
4290
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004291#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004292
4293static void enter_lmode(struct kvm_vcpu *vcpu)
4294{
4295 u32 guest_tr_ar;
4296
Avi Kivity2fb92db2011-04-27 19:42:18 +03004297 vmx_segment_cache_clear(to_vmx(vcpu));
4298
Avi Kivity6aa8b732006-12-10 02:21:36 -08004299 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004300 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004301 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4302 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004303 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004304 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4305 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004306 }
Avi Kivityda38f432010-07-06 11:30:49 +03004307 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004308}
4309
4310static void exit_lmode(struct kvm_vcpu *vcpu)
4311{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004312 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004313 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004314}
4315
4316#endif
4317
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004318static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4319 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004320{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004321 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004322 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4323 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004324 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004325 } else {
4326 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004327 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004328}
4329
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004330static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004331{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004332 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004333}
4334
Jim Mattsonfb6c8192017-03-16 13:53:59 -07004335static void vmx_flush_tlb_ept_only(struct kvm_vcpu *vcpu)
4336{
4337 if (enable_ept)
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004338 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07004339}
4340
Avi Kivitye8467fd2009-12-29 18:43:06 +02004341static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4342{
4343 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4344
4345 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4346 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4347}
4348
Avi Kivityaff48ba2010-12-05 18:56:11 +02004349static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4350{
Sean Christophersonb4d18512018-03-05 12:04:40 -08004351 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02004352 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4353 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4354}
4355
Anthony Liguori25c4c272007-04-27 09:29:21 +03004356static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004357{
Avi Kivityfc78f512009-12-07 12:16:48 +02004358 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4359
4360 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4361 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004362}
4363
Sheng Yang14394422008-04-28 12:24:45 +08004364static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4365{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004366 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4367
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004368 if (!test_bit(VCPU_EXREG_PDPTR,
4369 (unsigned long *)&vcpu->arch.regs_dirty))
4370 return;
4371
Sheng Yang14394422008-04-28 12:24:45 +08004372 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004373 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4374 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4375 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4376 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004377 }
4378}
4379
Avi Kivity8f5d5492009-05-31 18:41:29 +03004380static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4381{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004382 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4383
Avi Kivity8f5d5492009-05-31 18:41:29 +03004384 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004385 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4386 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4387 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4388 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004389 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004390
4391 __set_bit(VCPU_EXREG_PDPTR,
4392 (unsigned long *)&vcpu->arch.regs_avail);
4393 __set_bit(VCPU_EXREG_PDPTR,
4394 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004395}
4396
David Matlack38991522016-11-29 18:14:08 -08004397static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4398{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004399 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4400 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004401 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4402
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004403 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08004404 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4405 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4406 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4407
4408 return fixed_bits_valid(val, fixed0, fixed1);
4409}
4410
4411static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4412{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004413 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4414 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004415
4416 return fixed_bits_valid(val, fixed0, fixed1);
4417}
4418
4419static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4420{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004421 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
4422 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004423
4424 return fixed_bits_valid(val, fixed0, fixed1);
4425}
4426
4427/* No difference in the restrictions on guest and host CR4 in VMX operation. */
4428#define nested_guest_cr4_valid nested_cr4_valid
4429#define nested_host_cr4_valid nested_cr4_valid
4430
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004431static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004432
4433static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4434 unsigned long cr0,
4435 struct kvm_vcpu *vcpu)
4436{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004437 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4438 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004439 if (!(cr0 & X86_CR0_PG)) {
4440 /* From paging/starting to nonpaging */
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 } else if (!is_paging(vcpu)) {
4448 /* From nonpaging to paging */
4449 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004450 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004451 ~(CPU_BASED_CR3_LOAD_EXITING |
4452 CPU_BASED_CR3_STORE_EXITING));
4453 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004454 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004455 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004456
4457 if (!(cr0 & X86_CR0_WP))
4458 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004459}
4460
Avi Kivity6aa8b732006-12-10 02:21:36 -08004461static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4462{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004463 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004464 unsigned long hw_cr0;
4465
Gleb Natapov50378782013-02-04 16:00:28 +02004466 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004467 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004468 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004469 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004470 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004471
Gleb Natapov218e7632013-01-21 15:36:45 +02004472 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4473 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004474
Gleb Natapov218e7632013-01-21 15:36:45 +02004475 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4476 enter_rmode(vcpu);
4477 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004478
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004479#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004480 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10004481 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004482 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10004483 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004484 exit_lmode(vcpu);
4485 }
4486#endif
4487
Sean Christophersonb4d18512018-03-05 12:04:40 -08004488 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08004489 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4490
Avi Kivity6aa8b732006-12-10 02:21:36 -08004491 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004492 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004493 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004494
4495 /* depends on vcpu->arch.cr0 to be set to a new value */
4496 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004497}
4498
Yu Zhang855feb62017-08-24 20:27:55 +08004499static int get_ept_level(struct kvm_vcpu *vcpu)
4500{
4501 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
4502 return 5;
4503 return 4;
4504}
4505
Peter Feiner995f00a2017-06-30 17:26:32 -07004506static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08004507{
Yu Zhang855feb62017-08-24 20:27:55 +08004508 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08004509
Yu Zhang855feb62017-08-24 20:27:55 +08004510 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08004511
Peter Feiner995f00a2017-06-30 17:26:32 -07004512 if (enable_ept_ad_bits &&
4513 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02004514 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08004515 eptp |= (root_hpa & PAGE_MASK);
4516
4517 return eptp;
4518}
4519
Avi Kivity6aa8b732006-12-10 02:21:36 -08004520static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4521{
Sheng Yang14394422008-04-28 12:24:45 +08004522 unsigned long guest_cr3;
4523 u64 eptp;
4524
4525 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02004526 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07004527 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08004528 vmcs_write64(EPT_POINTER, eptp);
Sean Christophersone90008d2018-03-05 12:04:37 -08004529 if (enable_unrestricted_guest || is_paging(vcpu) ||
4530 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02004531 guest_cr3 = kvm_read_cr3(vcpu);
4532 else
4533 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02004534 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004535 }
4536
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004537 vmx_flush_tlb(vcpu, true);
Sheng Yang14394422008-04-28 12:24:45 +08004538 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004539}
4540
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004541static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004542{
Ben Serebrin085e68e2015-04-16 11:58:05 -07004543 /*
4544 * Pass through host's Machine Check Enable value to hw_cr4, which
4545 * is in force while we are in guest mode. Do not let guests control
4546 * this bit, even if host CR4.MCE == 0.
4547 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08004548 unsigned long hw_cr4;
4549
4550 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
4551 if (enable_unrestricted_guest)
4552 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
4553 else if (to_vmx(vcpu)->rmode.vm86_active)
4554 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
4555 else
4556 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004557
Paolo Bonzini0367f202016-07-12 10:44:55 +02004558 if ((cr4 & X86_CR4_UMIP) && !boot_cpu_has(X86_FEATURE_UMIP)) {
4559 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
4560 SECONDARY_EXEC_DESC);
4561 hw_cr4 &= ~X86_CR4_UMIP;
Radim Krčmář99158242018-01-31 18:12:50 +01004562 } else if (!is_guest_mode(vcpu) ||
4563 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
Paolo Bonzini0367f202016-07-12 10:44:55 +02004564 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
4565 SECONDARY_EXEC_DESC);
4566
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004567 if (cr4 & X86_CR4_VMXE) {
4568 /*
4569 * To use VMXON (and later other VMX instructions), a guest
4570 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4571 * So basically the check on whether to allow nested VMX
4572 * is here.
4573 */
4574 if (!nested_vmx_allowed(vcpu))
4575 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004576 }
David Matlack38991522016-11-29 18:14:08 -08004577
4578 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004579 return 1;
4580
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004581 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08004582
Sean Christopherson5dc1f042018-03-05 12:04:39 -08004583 if (!enable_unrestricted_guest) {
4584 if (enable_ept) {
4585 if (!is_paging(vcpu)) {
4586 hw_cr4 &= ~X86_CR4_PAE;
4587 hw_cr4 |= X86_CR4_PSE;
4588 } else if (!(cr4 & X86_CR4_PAE)) {
4589 hw_cr4 &= ~X86_CR4_PAE;
4590 }
4591 }
4592
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004593 /*
Huaitong Handdba2622016-03-22 16:51:15 +08004594 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4595 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4596 * to be manually disabled when guest switches to non-paging
4597 * mode.
4598 *
4599 * If !enable_unrestricted_guest, the CPU is always running
4600 * with CR0.PG=1 and CR4 needs to be modified.
4601 * If enable_unrestricted_guest, the CPU automatically
4602 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004603 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08004604 if (!is_paging(vcpu))
4605 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
4606 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004607
Sheng Yang14394422008-04-28 12:24:45 +08004608 vmcs_writel(CR4_READ_SHADOW, cr4);
4609 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004610 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004611}
4612
Avi Kivity6aa8b732006-12-10 02:21:36 -08004613static void vmx_get_segment(struct kvm_vcpu *vcpu,
4614 struct kvm_segment *var, int seg)
4615{
Avi Kivitya9179492011-01-03 14:28:52 +02004616 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004617 u32 ar;
4618
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004619 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004620 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02004621 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03004622 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004623 return;
Avi Kivity1390a282012-08-21 17:07:08 +03004624 var->base = vmx_read_guest_seg_base(vmx, seg);
4625 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4626 return;
Avi Kivitya9179492011-01-03 14:28:52 +02004627 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004628 var->base = vmx_read_guest_seg_base(vmx, seg);
4629 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4630 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4631 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03004632 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004633 var->type = ar & 15;
4634 var->s = (ar >> 4) & 1;
4635 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03004636 /*
4637 * Some userspaces do not preserve unusable property. Since usable
4638 * segment has to be present according to VMX spec we can use present
4639 * property to amend userspace bug by making unusable segment always
4640 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4641 * segment as unusable.
4642 */
4643 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004644 var->avl = (ar >> 12) & 1;
4645 var->l = (ar >> 13) & 1;
4646 var->db = (ar >> 14) & 1;
4647 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004648}
4649
Avi Kivitya9179492011-01-03 14:28:52 +02004650static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4651{
Avi Kivitya9179492011-01-03 14:28:52 +02004652 struct kvm_segment s;
4653
4654 if (to_vmx(vcpu)->rmode.vm86_active) {
4655 vmx_get_segment(vcpu, &s, seg);
4656 return s.base;
4657 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004658 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02004659}
4660
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004661static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02004662{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004663 struct vcpu_vmx *vmx = to_vmx(vcpu);
4664
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004665 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02004666 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004667 else {
4668 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004669 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02004670 }
Avi Kivity69c73022011-03-07 15:26:44 +02004671}
4672
Avi Kivity653e3102007-05-07 10:55:37 +03004673static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004674{
Avi Kivity6aa8b732006-12-10 02:21:36 -08004675 u32 ar;
4676
Avi Kivityf0495f92012-06-07 17:06:10 +03004677 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004678 ar = 1 << 16;
4679 else {
4680 ar = var->type & 15;
4681 ar |= (var->s & 1) << 4;
4682 ar |= (var->dpl & 3) << 5;
4683 ar |= (var->present & 1) << 7;
4684 ar |= (var->avl & 1) << 12;
4685 ar |= (var->l & 1) << 13;
4686 ar |= (var->db & 1) << 14;
4687 ar |= (var->g & 1) << 15;
4688 }
Avi Kivity653e3102007-05-07 10:55:37 +03004689
4690 return ar;
4691}
4692
4693static void vmx_set_segment(struct kvm_vcpu *vcpu,
4694 struct kvm_segment *var, int seg)
4695{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004696 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02004697 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03004698
Avi Kivity2fb92db2011-04-27 19:42:18 +03004699 vmx_segment_cache_clear(vmx);
4700
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004701 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4702 vmx->rmode.segs[seg] = *var;
4703 if (seg == VCPU_SREG_TR)
4704 vmcs_write16(sf->selector, var->selector);
4705 else if (var->s)
4706 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004707 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03004708 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004709
Avi Kivity653e3102007-05-07 10:55:37 +03004710 vmcs_writel(sf->base, var->base);
4711 vmcs_write32(sf->limit, var->limit);
4712 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004713
4714 /*
4715 * Fix the "Accessed" bit in AR field of segment registers for older
4716 * qemu binaries.
4717 * IA32 arch specifies that at the time of processor reset the
4718 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08004719 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004720 * state vmexit when "unrestricted guest" mode is turned on.
4721 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4722 * tree. Newer qemu binaries with that qemu fix would not need this
4723 * kvm hack.
4724 */
4725 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02004726 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004727
Gleb Natapovf924d662012-12-12 19:10:55 +02004728 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02004729
4730out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01004731 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004732}
4733
Avi Kivity6aa8b732006-12-10 02:21:36 -08004734static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4735{
Avi Kivity2fb92db2011-04-27 19:42:18 +03004736 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004737
4738 *db = (ar >> 14) & 1;
4739 *l = (ar >> 13) & 1;
4740}
4741
Gleb Natapov89a27f42010-02-16 10:51:48 +02004742static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004743{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004744 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4745 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004746}
4747
Gleb Natapov89a27f42010-02-16 10:51:48 +02004748static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004749{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004750 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4751 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004752}
4753
Gleb Natapov89a27f42010-02-16 10:51:48 +02004754static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004755{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004756 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4757 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004758}
4759
Gleb Natapov89a27f42010-02-16 10:51:48 +02004760static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004761{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004762 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4763 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004764}
4765
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004766static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4767{
4768 struct kvm_segment var;
4769 u32 ar;
4770
4771 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02004772 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02004773 if (seg == VCPU_SREG_CS)
4774 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004775 ar = vmx_segment_access_rights(&var);
4776
4777 if (var.base != (var.selector << 4))
4778 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02004779 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004780 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02004781 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004782 return false;
4783
4784 return true;
4785}
4786
4787static bool code_segment_valid(struct kvm_vcpu *vcpu)
4788{
4789 struct kvm_segment cs;
4790 unsigned int cs_rpl;
4791
4792 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004793 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004794
Avi Kivity1872a3f2009-01-04 23:26:52 +02004795 if (cs.unusable)
4796 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004797 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004798 return false;
4799 if (!cs.s)
4800 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004801 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004802 if (cs.dpl > cs_rpl)
4803 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004804 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004805 if (cs.dpl != cs_rpl)
4806 return false;
4807 }
4808 if (!cs.present)
4809 return false;
4810
4811 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
4812 return true;
4813}
4814
4815static bool stack_segment_valid(struct kvm_vcpu *vcpu)
4816{
4817 struct kvm_segment ss;
4818 unsigned int ss_rpl;
4819
4820 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004821 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004822
Avi Kivity1872a3f2009-01-04 23:26:52 +02004823 if (ss.unusable)
4824 return true;
4825 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004826 return false;
4827 if (!ss.s)
4828 return false;
4829 if (ss.dpl != ss_rpl) /* DPL != RPL */
4830 return false;
4831 if (!ss.present)
4832 return false;
4833
4834 return true;
4835}
4836
4837static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
4838{
4839 struct kvm_segment var;
4840 unsigned int rpl;
4841
4842 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03004843 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004844
Avi Kivity1872a3f2009-01-04 23:26:52 +02004845 if (var.unusable)
4846 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004847 if (!var.s)
4848 return false;
4849 if (!var.present)
4850 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004851 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004852 if (var.dpl < rpl) /* DPL < RPL */
4853 return false;
4854 }
4855
4856 /* TODO: Add other members to kvm_segment_field to allow checking for other access
4857 * rights flags
4858 */
4859 return true;
4860}
4861
4862static bool tr_valid(struct kvm_vcpu *vcpu)
4863{
4864 struct kvm_segment tr;
4865
4866 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
4867
Avi Kivity1872a3f2009-01-04 23:26:52 +02004868 if (tr.unusable)
4869 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03004870 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004871 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004872 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004873 return false;
4874 if (!tr.present)
4875 return false;
4876
4877 return true;
4878}
4879
4880static bool ldtr_valid(struct kvm_vcpu *vcpu)
4881{
4882 struct kvm_segment ldtr;
4883
4884 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
4885
Avi Kivity1872a3f2009-01-04 23:26:52 +02004886 if (ldtr.unusable)
4887 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03004888 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004889 return false;
4890 if (ldtr.type != 2)
4891 return false;
4892 if (!ldtr.present)
4893 return false;
4894
4895 return true;
4896}
4897
4898static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
4899{
4900 struct kvm_segment cs, ss;
4901
4902 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4903 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4904
Nadav Amitb32a9912015-03-29 16:33:04 +03004905 return ((cs.selector & SEGMENT_RPL_MASK) ==
4906 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004907}
4908
4909/*
4910 * Check if guest state is valid. Returns true if valid, false if
4911 * not.
4912 * We assume that registers are always usable
4913 */
4914static bool guest_state_valid(struct kvm_vcpu *vcpu)
4915{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02004916 if (enable_unrestricted_guest)
4917 return true;
4918
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004919 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03004920 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004921 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
4922 return false;
4923 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
4924 return false;
4925 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
4926 return false;
4927 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
4928 return false;
4929 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
4930 return false;
4931 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
4932 return false;
4933 } else {
4934 /* protected mode guest state checks */
4935 if (!cs_ss_rpl_check(vcpu))
4936 return false;
4937 if (!code_segment_valid(vcpu))
4938 return false;
4939 if (!stack_segment_valid(vcpu))
4940 return false;
4941 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
4942 return false;
4943 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
4944 return false;
4945 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
4946 return false;
4947 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
4948 return false;
4949 if (!tr_valid(vcpu))
4950 return false;
4951 if (!ldtr_valid(vcpu))
4952 return false;
4953 }
4954 /* TODO:
4955 * - Add checks on RIP
4956 * - Add checks on RFLAGS
4957 */
4958
4959 return true;
4960}
4961
Jim Mattson5fa99cb2017-07-06 16:33:07 -07004962static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
4963{
4964 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
4965}
4966
Mike Dayd77c26f2007-10-08 09:02:08 -04004967static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004968{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004969 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02004970 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004971 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004972
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004973 idx = srcu_read_lock(&kvm->srcu);
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004974 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
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 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08004979 r = kvm_write_guest_page(kvm, fn++, &data,
4980 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02004981 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004982 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004983 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
4984 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004985 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004986 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4987 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004988 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004989 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004990 r = kvm_write_guest_page(kvm, fn, &data,
4991 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
4992 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004993out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004994 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004995 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004996}
4997
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004998static int init_rmode_identity_map(struct kvm *kvm)
4999{
Tang Chenf51770e2014-09-16 18:41:59 +08005000 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005001 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005002 u32 tmp;
5003
Tang Chena255d472014-09-16 18:41:58 +08005004 /* Protect kvm->arch.ept_identity_pagetable_done. */
5005 mutex_lock(&kvm->slots_lock);
5006
Tang Chenf51770e2014-09-16 18:41:59 +08005007 if (likely(kvm->arch.ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005008 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005009
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005010 if (!kvm->arch.ept_identity_map_addr)
5011 kvm->arch.ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Sheng Yangb927a3c2009-07-21 10:42:48 +08005012 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005013
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005014 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
5015 kvm->arch.ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005016 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005017 goto out2;
5018
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005019 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005020 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5021 if (r < 0)
5022 goto out;
5023 /* Set up identity-mapping pagetable for EPT in real mode */
5024 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5025 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5026 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5027 r = kvm_write_guest_page(kvm, identity_map_pfn,
5028 &tmp, i * sizeof(tmp), sizeof(tmp));
5029 if (r < 0)
5030 goto out;
5031 }
5032 kvm->arch.ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005033
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005034out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005035 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005036
5037out2:
5038 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005039 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005040}
5041
Avi Kivity6aa8b732006-12-10 02:21:36 -08005042static void seg_setup(int seg)
5043{
Mathias Krause772e0312012-08-30 01:30:19 +02005044 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005045 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005046
5047 vmcs_write16(sf->selector, 0);
5048 vmcs_writel(sf->base, 0);
5049 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005050 ar = 0x93;
5051 if (seg == VCPU_SREG_CS)
5052 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005053
5054 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005055}
5056
Sheng Yangf78e0e22007-10-29 09:40:42 +08005057static int alloc_apic_access_page(struct kvm *kvm)
5058{
Xiao Guangrong44841412012-09-07 14:14:20 +08005059 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005060 int r = 0;
5061
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005062 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005063 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005064 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005065 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5066 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005067 if (r)
5068 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005069
Tang Chen73a6d942014-09-11 13:38:00 +08005070 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005071 if (is_error_page(page)) {
5072 r = -EFAULT;
5073 goto out;
5074 }
5075
Tang Chenc24ae0d2014-09-24 15:57:58 +08005076 /*
5077 * Do not pin the page in memory, so that memory hot-unplug
5078 * is able to migrate it.
5079 */
5080 put_page(page);
5081 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005082out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005083 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005084 return r;
5085}
5086
Wanpeng Li991e7a02015-09-16 17:30:05 +08005087static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005088{
5089 int vpid;
5090
Avi Kivity919818a2009-03-23 18:01:29 +02005091 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005092 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005093 spin_lock(&vmx_vpid_lock);
5094 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005095 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005096 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005097 else
5098 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005099 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005100 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005101}
5102
Wanpeng Li991e7a02015-09-16 17:30:05 +08005103static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005104{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005105 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005106 return;
5107 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005108 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005109 spin_unlock(&vmx_vpid_lock);
5110}
5111
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005112static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5113 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005114{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005115 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005116
5117 if (!cpu_has_vmx_msr_bitmap())
5118 return;
5119
5120 /*
5121 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5122 * have the write-low and read-high bitmap offsets the wrong way round.
5123 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5124 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005125 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005126 if (type & MSR_TYPE_R)
5127 /* read-low */
5128 __clear_bit(msr, msr_bitmap + 0x000 / f);
5129
5130 if (type & MSR_TYPE_W)
5131 /* write-low */
5132 __clear_bit(msr, msr_bitmap + 0x800 / f);
5133
Sheng Yang25c5f222008-03-28 13:18:56 +08005134 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5135 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005136 if (type & MSR_TYPE_R)
5137 /* read-high */
5138 __clear_bit(msr, msr_bitmap + 0x400 / f);
5139
5140 if (type & MSR_TYPE_W)
5141 /* write-high */
5142 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5143
5144 }
5145}
5146
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005147static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5148 u32 msr, int type)
5149{
5150 int f = sizeof(unsigned long);
5151
5152 if (!cpu_has_vmx_msr_bitmap())
5153 return;
5154
5155 /*
5156 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5157 * have the write-low and read-high bitmap offsets the wrong way round.
5158 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5159 */
5160 if (msr <= 0x1fff) {
5161 if (type & MSR_TYPE_R)
5162 /* read-low */
5163 __set_bit(msr, msr_bitmap + 0x000 / f);
5164
5165 if (type & MSR_TYPE_W)
5166 /* write-low */
5167 __set_bit(msr, msr_bitmap + 0x800 / f);
5168
5169 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5170 msr &= 0x1fff;
5171 if (type & MSR_TYPE_R)
5172 /* read-high */
5173 __set_bit(msr, msr_bitmap + 0x400 / f);
5174
5175 if (type & MSR_TYPE_W)
5176 /* write-high */
5177 __set_bit(msr, msr_bitmap + 0xc00 / f);
5178
5179 }
5180}
5181
5182static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5183 u32 msr, int type, bool value)
5184{
5185 if (value)
5186 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5187 else
5188 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5189}
5190
Wincy Vanf2b93282015-02-03 23:56:03 +08005191/*
5192 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5193 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5194 */
5195static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5196 unsigned long *msr_bitmap_nested,
5197 u32 msr, int type)
5198{
5199 int f = sizeof(unsigned long);
5200
Wincy Vanf2b93282015-02-03 23:56:03 +08005201 /*
5202 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5203 * have the write-low and read-high bitmap offsets the wrong way round.
5204 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5205 */
5206 if (msr <= 0x1fff) {
5207 if (type & MSR_TYPE_R &&
5208 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5209 /* read-low */
5210 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5211
5212 if (type & MSR_TYPE_W &&
5213 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5214 /* write-low */
5215 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5216
5217 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5218 msr &= 0x1fff;
5219 if (type & MSR_TYPE_R &&
5220 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5221 /* read-high */
5222 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5223
5224 if (type & MSR_TYPE_W &&
5225 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5226 /* write-high */
5227 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5228
5229 }
5230}
5231
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005232static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02005233{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005234 u8 mode = 0;
5235
5236 if (cpu_has_secondary_exec_ctrls() &&
5237 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5238 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5239 mode |= MSR_BITMAP_MODE_X2APIC;
5240 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5241 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5242 }
5243
5244 if (is_long_mode(vcpu))
5245 mode |= MSR_BITMAP_MODE_LM;
5246
5247 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005248}
5249
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005250#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5251
5252static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5253 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005254{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005255 int msr;
5256
5257 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5258 unsigned word = msr / BITS_PER_LONG;
5259 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5260 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005261 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005262
5263 if (mode & MSR_BITMAP_MODE_X2APIC) {
5264 /*
5265 * TPR reads and writes can be virtualized even if virtual interrupt
5266 * delivery is not in use.
5267 */
5268 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5269 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5270 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5271 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5272 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5273 }
5274 }
5275}
5276
5277static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5278{
5279 struct vcpu_vmx *vmx = to_vmx(vcpu);
5280 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5281 u8 mode = vmx_msr_bitmap_mode(vcpu);
5282 u8 changed = mode ^ vmx->msr_bitmap_mode;
5283
5284 if (!changed)
5285 return;
5286
5287 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5288 !(mode & MSR_BITMAP_MODE_LM));
5289
5290 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5291 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5292
5293 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005294}
5295
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005296static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005297{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005298 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005299}
5300
David Matlackc9f04402017-08-01 14:00:40 -07005301static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5302{
5303 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5304 gfn_t gfn;
5305
5306 /*
5307 * Don't need to mark the APIC access page dirty; it is never
5308 * written to by the CPU during APIC virtualization.
5309 */
5310
5311 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5312 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5313 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5314 }
5315
5316 if (nested_cpu_has_posted_intr(vmcs12)) {
5317 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5318 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5319 }
5320}
5321
5322
David Hildenbrand6342c502017-01-25 11:58:58 +01005323static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005324{
5325 struct vcpu_vmx *vmx = to_vmx(vcpu);
5326 int max_irr;
5327 void *vapic_page;
5328 u16 status;
5329
David Matlackc9f04402017-08-01 14:00:40 -07005330 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5331 return;
Wincy Van705699a2015-02-03 23:58:17 +08005332
David Matlackc9f04402017-08-01 14:00:40 -07005333 vmx->nested.pi_pending = false;
5334 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5335 return;
Wincy Van705699a2015-02-03 23:58:17 +08005336
David Matlackc9f04402017-08-01 14:00:40 -07005337 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5338 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005339 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02005340 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5341 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08005342 kunmap(vmx->nested.virtual_apic_page);
5343
5344 status = vmcs_read16(GUEST_INTR_STATUS);
5345 if ((u8)max_irr > ((u8)status & 0xff)) {
5346 status &= ~0xff;
5347 status |= (u8)max_irr;
5348 vmcs_write16(GUEST_INTR_STATUS, status);
5349 }
5350 }
David Matlackc9f04402017-08-01 14:00:40 -07005351
5352 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005353}
5354
Wincy Van06a55242017-04-28 13:13:59 +08005355static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5356 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005357{
5358#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005359 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5360
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005361 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005362 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005363 * The vector of interrupt to be delivered to vcpu had
5364 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005365 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005366 * Following cases will be reached in this block, and
5367 * we always send a notification event in all cases as
5368 * explained below.
5369 *
5370 * Case 1: vcpu keeps in non-root mode. Sending a
5371 * notification event posts the interrupt to vcpu.
5372 *
5373 * Case 2: vcpu exits to root mode and is still
5374 * runnable. PIR will be synced to vIRR before the
5375 * next vcpu entry. Sending a notification event in
5376 * this case has no effect, as vcpu is not in root
5377 * mode.
5378 *
5379 * Case 3: vcpu exits to root mode and is blocked.
5380 * vcpu_block() has already synced PIR to vIRR and
5381 * never blocks vcpu if vIRR is not cleared. Therefore,
5382 * a blocked vcpu here does not wait for any requested
5383 * interrupts in PIR, and sending a notification event
5384 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005385 */
Feng Wu28b835d2015-09-18 22:29:54 +08005386
Wincy Van06a55242017-04-28 13:13:59 +08005387 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005388 return true;
5389 }
5390#endif
5391 return false;
5392}
5393
Wincy Van705699a2015-02-03 23:58:17 +08005394static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5395 int vector)
5396{
5397 struct vcpu_vmx *vmx = to_vmx(vcpu);
5398
5399 if (is_guest_mode(vcpu) &&
5400 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005401 /*
5402 * If a posted intr is not recognized by hardware,
5403 * we will accomplish it in the next vmentry.
5404 */
5405 vmx->nested.pi_pending = true;
5406 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02005407 /* the PIR and ON have been set by L1. */
5408 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
5409 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005410 return 0;
5411 }
5412 return -1;
5413}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005414/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005415 * Send interrupt to vcpu via posted interrupt way.
5416 * 1. If target vcpu is running(non-root mode), send posted interrupt
5417 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5418 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5419 * interrupt from PIR in next vmentry.
5420 */
5421static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5422{
5423 struct vcpu_vmx *vmx = to_vmx(vcpu);
5424 int r;
5425
Wincy Van705699a2015-02-03 23:58:17 +08005426 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5427 if (!r)
5428 return;
5429
Yang Zhanga20ed542013-04-11 19:25:15 +08005430 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5431 return;
5432
Paolo Bonzinib95234c2016-12-19 13:57:33 +01005433 /* If a previous notification has sent the IPI, nothing to do. */
5434 if (pi_test_and_set_on(&vmx->pi_desc))
5435 return;
5436
Wincy Van06a55242017-04-28 13:13:59 +08005437 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08005438 kvm_vcpu_kick(vcpu);
5439}
5440
Avi Kivity6aa8b732006-12-10 02:21:36 -08005441/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005442 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5443 * will not change in the lifetime of the guest.
5444 * Note that host-state that does change is set elsewhere. E.g., host-state
5445 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5446 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005447static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005448{
5449 u32 low32, high32;
5450 unsigned long tmpl;
5451 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005452 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005453
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07005454 cr0 = read_cr0();
5455 WARN_ON(cr0 & X86_CR0_TS);
5456 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005457
5458 /*
5459 * Save the most likely value for this task's CR3 in the VMCS.
5460 * We can't use __get_current_cr3_fast() because we're not atomic.
5461 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07005462 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005463 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02005464 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005465
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005466 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005467 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005468 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02005469 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005470
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005471 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005472#ifdef CONFIG_X86_64
5473 /*
5474 * Load null selectors, so we can avoid reloading them in
5475 * __vmx_load_host_state(), in case userspace uses the null selectors
5476 * too (the expected case).
5477 */
5478 vmcs_write16(HOST_DS_SELECTOR, 0);
5479 vmcs_write16(HOST_ES_SELECTOR, 0);
5480#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005481 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5482 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005483#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005484 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5485 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5486
Juergen Gross87930012017-09-04 12:25:27 +02005487 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005488 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005489 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005490
Avi Kivity83287ea422012-09-16 15:10:57 +03005491 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005492
5493 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5494 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5495 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5496 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5497
5498 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5499 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5500 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5501 }
5502}
5503
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005504static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5505{
5506 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5507 if (enable_ept)
5508 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005509 if (is_guest_mode(&vmx->vcpu))
5510 vmx->vcpu.arch.cr4_guest_owned_bits &=
5511 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005512 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5513}
5514
Yang Zhang01e439b2013-04-11 19:25:12 +08005515static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5516{
5517 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5518
Andrey Smetanind62caab2015-11-10 15:36:33 +03005519 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005520 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005521
5522 if (!enable_vnmi)
5523 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
5524
Yunhong Jiang64672c92016-06-13 14:19:59 -07005525 /* Enable the preemption timer dynamically */
5526 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08005527 return pin_based_exec_ctrl;
5528}
5529
Andrey Smetanind62caab2015-11-10 15:36:33 +03005530static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5531{
5532 struct vcpu_vmx *vmx = to_vmx(vcpu);
5533
5534 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03005535 if (cpu_has_secondary_exec_ctrls()) {
5536 if (kvm_vcpu_apicv_active(vcpu))
5537 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5538 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5539 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5540 else
5541 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5542 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5543 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5544 }
5545
5546 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005547 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03005548}
5549
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005550static u32 vmx_exec_control(struct vcpu_vmx *vmx)
5551{
5552 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01005553
5554 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
5555 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5556
Paolo Bonzini35754c92015-07-29 12:05:37 +02005557 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005558 exec_control &= ~CPU_BASED_TPR_SHADOW;
5559#ifdef CONFIG_X86_64
5560 exec_control |= CPU_BASED_CR8_STORE_EXITING |
5561 CPU_BASED_CR8_LOAD_EXITING;
5562#endif
5563 }
5564 if (!enable_ept)
5565 exec_control |= CPU_BASED_CR3_STORE_EXITING |
5566 CPU_BASED_CR3_LOAD_EXITING |
5567 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07005568 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
5569 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
5570 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07005571 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
5572 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005573 return exec_control;
5574}
5575
Jim Mattson45ec3682017-08-23 16:32:04 -07005576static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005577{
Jim Mattson45ec3682017-08-23 16:32:04 -07005578 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02005579 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005580}
5581
Jim Mattson75f4fc82017-08-23 16:32:03 -07005582static bool vmx_rdseed_supported(void)
5583{
5584 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02005585 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005586}
5587
Paolo Bonzini80154d72017-08-24 13:55:35 +02005588static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005589{
Paolo Bonzini80154d72017-08-24 13:55:35 +02005590 struct kvm_vcpu *vcpu = &vmx->vcpu;
5591
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005592 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02005593
Paolo Bonzini80154d72017-08-24 13:55:35 +02005594 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005595 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5596 if (vmx->vpid == 0)
5597 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
5598 if (!enable_ept) {
5599 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
5600 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00005601 /* Enable INVPCID for non-ept guests may cause performance regression. */
5602 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005603 }
5604 if (!enable_unrestricted_guest)
5605 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07005606 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005607 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02005608 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08005609 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
5610 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08005611 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02005612
5613 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
5614 * in vmx_set_cr4. */
5615 exec_control &= ~SECONDARY_EXEC_DESC;
5616
Abel Gordonabc4fc52013-04-18 14:35:25 +03005617 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
5618 (handle_vmptrld).
5619 We can NOT enable shadow_vmcs here because we don't have yet
5620 a current VMCS12
5621 */
5622 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08005623
5624 if (!enable_pml)
5625 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08005626
Paolo Bonzini3db13482017-08-24 14:48:03 +02005627 if (vmx_xsaves_supported()) {
5628 /* Exposing XSAVES only when XSAVE is exposed */
5629 bool xsaves_enabled =
5630 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
5631 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
5632
5633 if (!xsaves_enabled)
5634 exec_control &= ~SECONDARY_EXEC_XSAVES;
5635
5636 if (nested) {
5637 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005638 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02005639 SECONDARY_EXEC_XSAVES;
5640 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005641 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02005642 ~SECONDARY_EXEC_XSAVES;
5643 }
5644 }
5645
Paolo Bonzini80154d72017-08-24 13:55:35 +02005646 if (vmx_rdtscp_supported()) {
5647 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
5648 if (!rdtscp_enabled)
5649 exec_control &= ~SECONDARY_EXEC_RDTSCP;
5650
5651 if (nested) {
5652 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005653 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02005654 SECONDARY_EXEC_RDTSCP;
5655 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005656 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02005657 ~SECONDARY_EXEC_RDTSCP;
5658 }
5659 }
5660
5661 if (vmx_invpcid_supported()) {
5662 /* Exposing INVPCID only when PCID is exposed */
5663 bool invpcid_enabled =
5664 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
5665 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
5666
5667 if (!invpcid_enabled) {
5668 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
5669 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
5670 }
5671
5672 if (nested) {
5673 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005674 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02005675 SECONDARY_EXEC_ENABLE_INVPCID;
5676 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005677 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02005678 ~SECONDARY_EXEC_ENABLE_INVPCID;
5679 }
5680 }
5681
Jim Mattson45ec3682017-08-23 16:32:04 -07005682 if (vmx_rdrand_supported()) {
5683 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
5684 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02005685 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005686
5687 if (nested) {
5688 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005689 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005690 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005691 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005692 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005693 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005694 }
5695 }
5696
Jim Mattson75f4fc82017-08-23 16:32:03 -07005697 if (vmx_rdseed_supported()) {
5698 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
5699 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02005700 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005701
5702 if (nested) {
5703 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005704 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005705 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005706 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005707 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005708 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005709 }
5710 }
5711
Paolo Bonzini80154d72017-08-24 13:55:35 +02005712 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005713}
5714
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005715static void ept_set_mmio_spte_mask(void)
5716{
5717 /*
5718 * EPT Misconfigurations can be generated if the value of bits 2:0
5719 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005720 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07005721 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
5722 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005723}
5724
Wanpeng Lif53cd632014-12-02 19:14:58 +08005725#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005726/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005727 * Sets up the vmcs for emulated real mode.
5728 */
David Hildenbrand12d79912017-08-24 20:51:26 +02005729static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005730{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005731#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005732 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005733#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08005734 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005735
Abel Gordon4607c2d2013-04-18 14:35:55 +03005736 if (enable_shadow_vmcs) {
5737 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5738 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
5739 }
Sheng Yang25c5f222008-03-28 13:18:56 +08005740 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005741 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08005742
Avi Kivity6aa8b732006-12-10 02:21:36 -08005743 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5744
Avi Kivity6aa8b732006-12-10 02:21:36 -08005745 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08005746 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07005747 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005748
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005749 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005750
Dan Williamsdfa169b2016-06-02 11:17:24 -07005751 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02005752 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005753 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02005754 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07005755 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08005756
Andrey Smetanind62caab2015-11-10 15:36:33 +03005757 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08005758 vmcs_write64(EOI_EXIT_BITMAP0, 0);
5759 vmcs_write64(EOI_EXIT_BITMAP1, 0);
5760 vmcs_write64(EOI_EXIT_BITMAP2, 0);
5761 vmcs_write64(EOI_EXIT_BITMAP3, 0);
5762
5763 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08005764
Li RongQing0bcf2612015-12-03 13:29:34 +08005765 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08005766 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08005767 }
5768
Wanpeng Lib31c1142018-03-12 04:53:04 -07005769 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005770 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02005771 vmx->ple_window = ple_window;
5772 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005773 }
5774
Xiao Guangrongc3707952011-07-12 03:28:04 +08005775 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5776 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005777 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5778
Avi Kivity9581d442010-10-19 16:46:55 +02005779 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
5780 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005781 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005782#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005783 rdmsrl(MSR_FS_BASE, a);
5784 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
5785 rdmsrl(MSR_GS_BASE, a);
5786 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
5787#else
5788 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
5789 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
5790#endif
5791
Bandan Das2a499e42017-08-03 15:54:41 -04005792 if (cpu_has_vmx_vmfunc())
5793 vmcs_write64(VM_FUNCTION_CONTROL, 0);
5794
Eddie Dong2cc51562007-05-21 07:28:09 +03005795 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
5796 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03005797 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03005798 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03005799 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005800
Radim Krčmář74545702015-04-27 15:11:25 +02005801 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
5802 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08005803
Paolo Bonzini03916db2014-07-24 14:21:57 +02005804 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08005805 u32 index = vmx_msr_index[i];
5806 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005807 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005808
5809 if (rdmsr_safe(index, &data_low, &data_high) < 0)
5810 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08005811 if (wrmsr_safe(index, data_low, data_high) < 0)
5812 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03005813 vmx->guest_msrs[j].index = i;
5814 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02005815 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005816 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005817 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005818
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01005819 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
5820 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02005821
5822 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005823
5824 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02005825 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03005826
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08005827 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
5828 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
5829
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005830 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005831
Wanpeng Lif53cd632014-12-02 19:14:58 +08005832 if (vmx_xsaves_supported())
5833 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
5834
Peter Feiner4e595162016-07-07 14:49:58 -07005835 if (enable_pml) {
5836 ASSERT(vmx->pml_pg);
5837 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
5838 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5839 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005840}
5841
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005842static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005843{
5844 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01005845 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005846 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005847
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005848 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01005849 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005850
Wanpeng Li518e7b92018-02-28 14:03:31 +08005851 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005852 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005853 kvm_set_cr8(vcpu, 0);
5854
5855 if (!init_event) {
5856 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
5857 MSR_IA32_APICBASE_ENABLE;
5858 if (kvm_vcpu_is_reset_bsp(vcpu))
5859 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
5860 apic_base_msr.host_initiated = true;
5861 kvm_set_apic_base(vcpu, &apic_base_msr);
5862 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005863
Avi Kivity2fb92db2011-04-27 19:42:18 +03005864 vmx_segment_cache_clear(vmx);
5865
Avi Kivity5706be02008-08-20 15:07:31 +03005866 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01005867 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005868 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005869
5870 seg_setup(VCPU_SREG_DS);
5871 seg_setup(VCPU_SREG_ES);
5872 seg_setup(VCPU_SREG_FS);
5873 seg_setup(VCPU_SREG_GS);
5874 seg_setup(VCPU_SREG_SS);
5875
5876 vmcs_write16(GUEST_TR_SELECTOR, 0);
5877 vmcs_writel(GUEST_TR_BASE, 0);
5878 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
5879 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5880
5881 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
5882 vmcs_writel(GUEST_LDTR_BASE, 0);
5883 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
5884 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
5885
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005886 if (!init_event) {
5887 vmcs_write32(GUEST_SYSENTER_CS, 0);
5888 vmcs_writel(GUEST_SYSENTER_ESP, 0);
5889 vmcs_writel(GUEST_SYSENTER_EIP, 0);
5890 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
5891 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005892
Wanpeng Lic37c2872017-11-20 14:52:21 -08005893 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01005894 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005895
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005896 vmcs_writel(GUEST_GDTR_BASE, 0);
5897 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
5898
5899 vmcs_writel(GUEST_IDTR_BASE, 0);
5900 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
5901
Anthony Liguori443381a2010-12-06 10:53:38 -06005902 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005903 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005904 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07005905 if (kvm_mpx_supported())
5906 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005907
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005908 setup_msrs(vmx);
5909
Avi Kivity6aa8b732006-12-10 02:21:36 -08005910 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
5911
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005912 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08005913 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005914 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08005915 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005916 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08005917 vmcs_write32(TPR_THRESHOLD, 0);
5918 }
5919
Paolo Bonzinia73896c2014-11-02 07:54:30 +01005920 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005921
Sheng Yang2384d2b2008-01-17 15:14:33 +08005922 if (vmx->vpid != 0)
5923 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
5924
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005925 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005926 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06005927 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005928 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02005929 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08005930
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005931 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005932
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005933 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07005934 if (init_event)
5935 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005936}
5937
Nadav Har'Elb6f12502011-05-25 23:13:06 +03005938/*
5939 * In nested virtualization, check if L1 asked to exit on external interrupts.
5940 * For most existing hypervisors, this will always return true.
5941 */
5942static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
5943{
5944 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5945 PIN_BASED_EXT_INTR_MASK;
5946}
5947
Bandan Das77b0f5d2014-04-19 18:17:45 -04005948/*
5949 * In nested virtualization, check if L1 has set
5950 * VM_EXIT_ACK_INTR_ON_EXIT
5951 */
5952static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
5953{
5954 return get_vmcs12(vcpu)->vm_exit_controls &
5955 VM_EXIT_ACK_INTR_ON_EXIT;
5956}
5957
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005958static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
5959{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05005960 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005961}
5962
Jan Kiszkac9a79532014-03-07 20:03:15 +01005963static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005964{
Paolo Bonzini47c01522016-12-19 11:44:07 +01005965 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
5966 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005967}
5968
Jan Kiszkac9a79532014-03-07 20:03:15 +01005969static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005970{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005971 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005972 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01005973 enable_irq_window(vcpu);
5974 return;
5975 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02005976
Paolo Bonzini47c01522016-12-19 11:44:07 +01005977 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
5978 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005979}
5980
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005981static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03005982{
Avi Kivity9c8cba32007-11-22 11:42:59 +02005983 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005984 uint32_t intr;
5985 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02005986
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005987 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005988
Avi Kivityfa89a812008-09-01 15:57:51 +03005989 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005990 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005991 int inc_eip = 0;
5992 if (vcpu->arch.interrupt.soft)
5993 inc_eip = vcpu->arch.event_exit_inst_len;
5994 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005995 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005996 return;
5997 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005998 intr = irq | INTR_INFO_VALID_MASK;
5999 if (vcpu->arch.interrupt.soft) {
6000 intr |= INTR_TYPE_SOFT_INTR;
6001 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6002 vmx->vcpu.arch.event_exit_inst_len);
6003 } else
6004 intr |= INTR_TYPE_EXT_INTR;
6005 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006006
6007 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006008}
6009
Sheng Yangf08864b2008-05-15 18:23:25 +08006010static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6011{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006012 struct vcpu_vmx *vmx = to_vmx(vcpu);
6013
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006014 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006015 /*
6016 * Tracking the NMI-blocked state in software is built upon
6017 * finding the next open IRQ window. This, in turn, depends on
6018 * well-behaving guests: They have to keep IRQs disabled at
6019 * least as long as the NMI handler runs. Otherwise we may
6020 * cause NMI nesting, maybe breaking the guest. But as this is
6021 * highly unlikely, we can live with the residual risk.
6022 */
6023 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6024 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6025 }
6026
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006027 ++vcpu->stat.nmi_injections;
6028 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006029
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006030 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006031 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006032 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006033 return;
6034 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006035
Sheng Yangf08864b2008-05-15 18:23:25 +08006036 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6037 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006038
6039 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006040}
6041
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006042static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6043{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006044 struct vcpu_vmx *vmx = to_vmx(vcpu);
6045 bool masked;
6046
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006047 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006048 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006049 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006050 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006051 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6052 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6053 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006054}
6055
6056static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6057{
6058 struct vcpu_vmx *vmx = to_vmx(vcpu);
6059
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006060 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006061 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6062 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6063 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6064 }
6065 } else {
6066 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6067 if (masked)
6068 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6069 GUEST_INTR_STATE_NMI);
6070 else
6071 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6072 GUEST_INTR_STATE_NMI);
6073 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006074}
6075
Jan Kiszka2505dc92013-04-14 12:12:47 +02006076static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6077{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006078 if (to_vmx(vcpu)->nested.nested_run_pending)
6079 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006080
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006081 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006082 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6083 return 0;
6084
Jan Kiszka2505dc92013-04-14 12:12:47 +02006085 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6086 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6087 | GUEST_INTR_STATE_NMI));
6088}
6089
Gleb Natapov78646122009-03-23 12:12:11 +02006090static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6091{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006092 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6093 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006094 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6095 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006096}
6097
Izik Eiduscbc94022007-10-25 00:29:55 +02006098static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6099{
6100 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006101
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006102 if (enable_unrestricted_guest)
6103 return 0;
6104
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006105 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6106 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006107 if (ret)
6108 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08006109 kvm->arch.tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006110 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006111}
6112
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006113static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006114{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006115 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006116 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006117 /*
6118 * Update instruction length as we may reinject the exception
6119 * from user space while in guest debugging mode.
6120 */
6121 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6122 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006123 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006124 return false;
6125 /* fall through */
6126 case DB_VECTOR:
6127 if (vcpu->guest_debug &
6128 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6129 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006130 /* fall through */
6131 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006132 case OF_VECTOR:
6133 case BR_VECTOR:
6134 case UD_VECTOR:
6135 case DF_VECTOR:
6136 case SS_VECTOR:
6137 case GP_VECTOR:
6138 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006139 return true;
6140 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006141 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006142 return false;
6143}
6144
6145static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6146 int vec, u32 err_code)
6147{
6148 /*
6149 * Instruction with address size override prefix opcode 0x67
6150 * Cause the #SS fault with 0 error code in VM86 mode.
6151 */
6152 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6153 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6154 if (vcpu->arch.halt_request) {
6155 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006156 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006157 }
6158 return 1;
6159 }
6160 return 0;
6161 }
6162
6163 /*
6164 * Forward all other exceptions that are valid in real mode.
6165 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6166 * the required debugging infrastructure rework.
6167 */
6168 kvm_queue_exception(vcpu, vec);
6169 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006170}
6171
Andi Kleena0861c02009-06-08 17:37:09 +08006172/*
6173 * Trigger machine check on the host. We assume all the MSRs are already set up
6174 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6175 * We pass a fake environment to the machine check handler because we want
6176 * the guest to be always treated like user space, no matter what context
6177 * it used internally.
6178 */
6179static void kvm_machine_check(void)
6180{
6181#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6182 struct pt_regs regs = {
6183 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6184 .flags = X86_EFLAGS_IF,
6185 };
6186
6187 do_machine_check(&regs, 0);
6188#endif
6189}
6190
Avi Kivity851ba692009-08-24 11:10:17 +03006191static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08006192{
6193 /* already handled by vcpu_run */
6194 return 1;
6195}
6196
Avi Kivity851ba692009-08-24 11:10:17 +03006197static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006198{
Avi Kivity1155f762007-11-22 11:30:47 +02006199 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006200 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006201 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006202 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006203 u32 vect_info;
6204 enum emulation_result er;
6205
Avi Kivity1155f762007-11-22 11:30:47 +02006206 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02006207 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006208
Andi Kleena0861c02009-06-08 17:37:09 +08006209 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03006210 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006211
Jim Mattsonef85b672016-12-12 11:01:37 -08006212 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02006213 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03006214
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006215 if (is_invalid_opcode(intr_info)) {
Andre Przywara51d8b662010-12-21 11:12:02 +01006216 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Liran Alon61cb57c2017-11-05 16:56:32 +02006217 if (er == EMULATE_USER_EXIT)
6218 return 0;
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006219 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02006220 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006221 return 1;
6222 }
6223
Avi Kivity6aa8b732006-12-10 02:21:36 -08006224 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06006225 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006226 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006227
Liran Alon9e869482018-03-12 13:12:51 +02006228 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
6229 WARN_ON_ONCE(!enable_vmware_backdoor);
6230 er = emulate_instruction(vcpu,
6231 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
6232 if (er == EMULATE_USER_EXIT)
6233 return 0;
6234 else if (er != EMULATE_DONE)
6235 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
6236 return 1;
6237 }
6238
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006239 /*
6240 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6241 * MMIO, it is better to report an internal error.
6242 * See the comments in vmx_handle_exit.
6243 */
6244 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6245 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6246 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6247 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006248 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006249 vcpu->run->internal.data[0] = vect_info;
6250 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006251 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006252 return 0;
6253 }
6254
Avi Kivity6aa8b732006-12-10 02:21:36 -08006255 if (is_page_fault(intr_info)) {
6256 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006257 /* EPT won't cause page fault directly */
6258 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02006259 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006260 }
6261
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006262 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006263
6264 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6265 return handle_rmode_exception(vcpu, ex_no, error_code);
6266
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006267 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01006268 case AC_VECTOR:
6269 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6270 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006271 case DB_VECTOR:
6272 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6273 if (!(vcpu->guest_debug &
6274 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01006275 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006276 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Huw Daviesfd2a4452014-04-16 10:02:51 +01006277 if (!(dr6 & ~DR6_RESERVED)) /* icebp */
6278 skip_emulated_instruction(vcpu);
6279
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006280 kvm_queue_exception(vcpu, DB_VECTOR);
6281 return 1;
6282 }
6283 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6284 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6285 /* fall through */
6286 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006287 /*
6288 * Update instruction length as we may reinject #BP from
6289 * user space while in guest debugging mode. Reading it for
6290 * #DB as well causes no harm, it is not used in that case.
6291 */
6292 vmx->vcpu.arch.event_exit_inst_len =
6293 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006294 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006295 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006296 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6297 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006298 break;
6299 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006300 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6301 kvm_run->ex.exception = ex_no;
6302 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006303 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006304 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006305 return 0;
6306}
6307
Avi Kivity851ba692009-08-24 11:10:17 +03006308static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006309{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006310 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006311 return 1;
6312}
6313
Avi Kivity851ba692009-08-24 11:10:17 +03006314static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006315{
Avi Kivity851ba692009-08-24 11:10:17 +03006316 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006317 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006318 return 0;
6319}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006320
Avi Kivity851ba692009-08-24 11:10:17 +03006321static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006322{
He, Qingbfdaab02007-09-12 14:18:28 +08006323 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08006324 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02006325 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006326
He, Qingbfdaab02007-09-12 14:18:28 +08006327 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006328 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006329
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006330 ++vcpu->stat.io_exits;
6331
Sean Christopherson432baf62018-03-08 08:57:26 -08006332 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01006333 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006334
6335 port = exit_qualification >> 16;
6336 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08006337 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006338
Sean Christophersondca7f122018-03-08 08:57:27 -08006339 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006340}
6341
Ingo Molnar102d8322007-02-19 14:37:47 +02006342static void
6343vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6344{
6345 /*
6346 * Patch in the VMCALL instruction:
6347 */
6348 hypercall[0] = 0x0f;
6349 hypercall[1] = 0x01;
6350 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006351}
6352
Guo Chao0fa06072012-06-28 15:16:19 +08006353/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006354static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6355{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006356 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006357 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6358 unsigned long orig_val = val;
6359
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006360 /*
6361 * We get here when L2 changed cr0 in a way that did not change
6362 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006363 * but did change L0 shadowed bits. So we first calculate the
6364 * effective cr0 value that L1 would like to write into the
6365 * hardware. It consists of the L2-owned bits from the new
6366 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006367 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006368 val = (val & ~vmcs12->cr0_guest_host_mask) |
6369 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6370
David Matlack38991522016-11-29 18:14:08 -08006371 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006372 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006373
6374 if (kvm_set_cr0(vcpu, val))
6375 return 1;
6376 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006377 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006378 } else {
6379 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08006380 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006381 return 1;
David Matlack38991522016-11-29 18:14:08 -08006382
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006383 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006384 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006385}
6386
6387static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6388{
6389 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006390 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6391 unsigned long orig_val = val;
6392
6393 /* analogously to handle_set_cr0 */
6394 val = (val & ~vmcs12->cr4_guest_host_mask) |
6395 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6396 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006397 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006398 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006399 return 0;
6400 } else
6401 return kvm_set_cr4(vcpu, val);
6402}
6403
Paolo Bonzini0367f202016-07-12 10:44:55 +02006404static int handle_desc(struct kvm_vcpu *vcpu)
6405{
6406 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
6407 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6408}
6409
Avi Kivity851ba692009-08-24 11:10:17 +03006410static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006411{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006412 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006413 int cr;
6414 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03006415 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006416 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006417
He, Qingbfdaab02007-09-12 14:18:28 +08006418 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006419 cr = exit_qualification & 15;
6420 reg = (exit_qualification >> 8) & 15;
6421 switch ((exit_qualification >> 4) & 3) {
6422 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03006423 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006424 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006425 switch (cr) {
6426 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006427 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006428 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006429 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006430 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03006431 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006432 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006433 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006434 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006435 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006436 case 8: {
6437 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03006438 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006439 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006440 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006441 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08006442 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006443 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006444 return ret;
6445 /*
6446 * TODO: we might be squashing a
6447 * KVM_GUESTDBG_SINGLESTEP-triggered
6448 * KVM_EXIT_DEBUG here.
6449 */
Avi Kivity851ba692009-08-24 11:10:17 +03006450 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006451 return 0;
6452 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02006453 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006454 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03006455 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006456 WARN_ONCE(1, "Guest should always own CR0.TS");
6457 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02006458 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08006459 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006460 case 1: /*mov from cr*/
6461 switch (cr) {
6462 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006463 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02006464 val = kvm_read_cr3(vcpu);
6465 kvm_register_write(vcpu, reg, val);
6466 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006467 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006468 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006469 val = kvm_get_cr8(vcpu);
6470 kvm_register_write(vcpu, reg, val);
6471 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006472 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006473 }
6474 break;
6475 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02006476 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02006477 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02006478 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006479
Kyle Huey6affcbe2016-11-29 12:40:40 -08006480 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006481 default:
6482 break;
6483 }
Avi Kivity851ba692009-08-24 11:10:17 +03006484 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03006485 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08006486 (int)(exit_qualification >> 4) & 3, cr);
6487 return 0;
6488}
6489
Avi Kivity851ba692009-08-24 11:10:17 +03006490static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006491{
He, Qingbfdaab02007-09-12 14:18:28 +08006492 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006493 int dr, dr7, reg;
6494
6495 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6496 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6497
6498 /* First, if DR does not exist, trigger UD */
6499 if (!kvm_require_dr(vcpu, dr))
6500 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006501
Jan Kiszkaf2483412010-01-20 18:20:20 +01006502 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03006503 if (!kvm_require_cpl(vcpu, 0))
6504 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006505 dr7 = vmcs_readl(GUEST_DR7);
6506 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006507 /*
6508 * As the vm-exit takes precedence over the debug trap, we
6509 * need to emulate the latter, either for the host or the
6510 * guest debugging itself.
6511 */
6512 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03006513 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006514 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02006515 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006516 vcpu->run->debug.arch.exception = DB_VECTOR;
6517 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006518 return 0;
6519 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02006520 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006521 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006522 kvm_queue_exception(vcpu, DB_VECTOR);
6523 return 1;
6524 }
6525 }
6526
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006527 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01006528 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6529 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006530
6531 /*
6532 * No more DR vmexits; force a reload of the debug registers
6533 * and reenter on this instruction. The next vmexit will
6534 * retrieve the full state of the debug registers.
6535 */
6536 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
6537 return 1;
6538 }
6539
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006540 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
6541 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03006542 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006543
6544 if (kvm_get_dr(vcpu, dr, &val))
6545 return 1;
6546 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03006547 } else
Nadav Amit57773922014-06-18 17:19:23 +03006548 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006549 return 1;
6550
Kyle Huey6affcbe2016-11-29 12:40:40 -08006551 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006552}
6553
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01006554static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
6555{
6556 return vcpu->arch.dr6;
6557}
6558
6559static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
6560{
6561}
6562
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006563static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
6564{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006565 get_debugreg(vcpu->arch.db[0], 0);
6566 get_debugreg(vcpu->arch.db[1], 1);
6567 get_debugreg(vcpu->arch.db[2], 2);
6568 get_debugreg(vcpu->arch.db[3], 3);
6569 get_debugreg(vcpu->arch.dr6, 6);
6570 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
6571
6572 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01006573 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006574}
6575
Gleb Natapov020df072010-04-13 10:05:23 +03006576static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
6577{
6578 vmcs_writel(GUEST_DR7, val);
6579}
6580
Avi Kivity851ba692009-08-24 11:10:17 +03006581static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006582{
Kyle Huey6a908b62016-11-29 12:40:37 -08006583 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006584}
6585
Avi Kivity851ba692009-08-24 11:10:17 +03006586static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006587{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006588 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006589 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006590
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006591 msr_info.index = ecx;
6592 msr_info.host_initiated = false;
6593 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02006594 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006595 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006596 return 1;
6597 }
6598
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006599 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006600
Avi Kivity6aa8b732006-12-10 02:21:36 -08006601 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006602 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
6603 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006604 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006605}
6606
Avi Kivity851ba692009-08-24 11:10:17 +03006607static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006608{
Will Auld8fe8ab42012-11-29 12:42:12 -08006609 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006610 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6611 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
6612 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006613
Will Auld8fe8ab42012-11-29 12:42:12 -08006614 msr.data = data;
6615 msr.index = ecx;
6616 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03006617 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02006618 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006619 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006620 return 1;
6621 }
6622
Avi Kivity59200272010-01-25 19:47:02 +02006623 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006624 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006625}
6626
Avi Kivity851ba692009-08-24 11:10:17 +03006627static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006628{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01006629 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006630 return 1;
6631}
6632
Avi Kivity851ba692009-08-24 11:10:17 +03006633static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006634{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006635 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6636 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006637
Avi Kivity3842d132010-07-27 12:30:24 +03006638 kvm_make_request(KVM_REQ_EVENT, vcpu);
6639
Jan Kiszkaa26bf122008-09-26 09:30:45 +02006640 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006641 return 1;
6642}
6643
Avi Kivity851ba692009-08-24 11:10:17 +03006644static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006645{
Avi Kivityd3bef152007-06-05 15:53:05 +03006646 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006647}
6648
Avi Kivity851ba692009-08-24 11:10:17 +03006649static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02006650{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03006651 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02006652}
6653
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006654static int handle_invd(struct kvm_vcpu *vcpu)
6655{
Andre Przywara51d8b662010-12-21 11:12:02 +01006656 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006657}
6658
Avi Kivity851ba692009-08-24 11:10:17 +03006659static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03006660{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006661 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006662
6663 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006664 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006665}
6666
Avi Kivityfee84b02011-11-10 14:57:25 +02006667static int handle_rdpmc(struct kvm_vcpu *vcpu)
6668{
6669 int err;
6670
6671 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006672 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02006673}
6674
Avi Kivity851ba692009-08-24 11:10:17 +03006675static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02006676{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006677 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02006678}
6679
Dexuan Cui2acf9232010-06-10 11:27:12 +08006680static int handle_xsetbv(struct kvm_vcpu *vcpu)
6681{
6682 u64 new_bv = kvm_read_edx_eax(vcpu);
6683 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
6684
6685 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006686 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08006687 return 1;
6688}
6689
Wanpeng Lif53cd632014-12-02 19:14:58 +08006690static int handle_xsaves(struct kvm_vcpu *vcpu)
6691{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006692 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08006693 WARN(1, "this should never happen\n");
6694 return 1;
6695}
6696
6697static int handle_xrstors(struct kvm_vcpu *vcpu)
6698{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006699 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08006700 WARN(1, "this should never happen\n");
6701 return 1;
6702}
6703
Avi Kivity851ba692009-08-24 11:10:17 +03006704static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08006705{
Kevin Tian58fbbf22011-08-30 13:56:17 +03006706 if (likely(fasteoi)) {
6707 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6708 int access_type, offset;
6709
6710 access_type = exit_qualification & APIC_ACCESS_TYPE;
6711 offset = exit_qualification & APIC_ACCESS_OFFSET;
6712 /*
6713 * Sane guest uses MOV to write EOI, with written value
6714 * not cared. So make a short-circuit here by avoiding
6715 * heavy instruction emulation.
6716 */
6717 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6718 (offset == APIC_EOI)) {
6719 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006720 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03006721 }
6722 }
Andre Przywara51d8b662010-12-21 11:12:02 +01006723 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08006724}
6725
Yang Zhangc7c9c562013-01-25 10:18:51 +08006726static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6727{
6728 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6729 int vector = exit_qualification & 0xff;
6730
6731 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6732 kvm_apic_set_eoi_accelerated(vcpu, vector);
6733 return 1;
6734}
6735
Yang Zhang83d4c282013-01-25 10:18:49 +08006736static int handle_apic_write(struct kvm_vcpu *vcpu)
6737{
6738 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6739 u32 offset = exit_qualification & 0xfff;
6740
6741 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6742 kvm_apic_write_nodecode(vcpu, offset);
6743 return 1;
6744}
6745
Avi Kivity851ba692009-08-24 11:10:17 +03006746static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02006747{
Jan Kiszka60637aa2008-09-26 09:30:47 +02006748 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02006749 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02006750 bool has_error_code = false;
6751 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02006752 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006753 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006754
6755 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006756 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006757 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02006758
6759 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6760
6761 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006762 if (reason == TASK_SWITCH_GATE && idt_v) {
6763 switch (type) {
6764 case INTR_TYPE_NMI_INTR:
6765 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02006766 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006767 break;
6768 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006769 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006770 kvm_clear_interrupt_queue(vcpu);
6771 break;
6772 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02006773 if (vmx->idt_vectoring_info &
6774 VECTORING_INFO_DELIVER_CODE_MASK) {
6775 has_error_code = true;
6776 error_code =
6777 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6778 }
6779 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006780 case INTR_TYPE_SOFT_EXCEPTION:
6781 kvm_clear_exception_queue(vcpu);
6782 break;
6783 default:
6784 break;
6785 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02006786 }
Izik Eidus37817f22008-03-24 23:14:53 +02006787 tss_selector = exit_qualification;
6788
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006789 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
6790 type != INTR_TYPE_EXT_INTR &&
6791 type != INTR_TYPE_NMI_INTR))
6792 skip_emulated_instruction(vcpu);
6793
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006794 if (kvm_task_switch(vcpu, tss_selector,
6795 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
6796 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03006797 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6798 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6799 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006800 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03006801 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006802
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006803 /*
6804 * TODO: What about debug traps on tss switch?
6805 * Are we supposed to inject them and update dr6?
6806 */
6807
6808 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02006809}
6810
Avi Kivity851ba692009-08-24 11:10:17 +03006811static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08006812{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006813 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08006814 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01006815 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08006816
Sheng Yangf9c617f2009-03-25 10:08:52 +08006817 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08006818
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006819 /*
6820 * EPT violation happened while executing iret from NMI,
6821 * "blocked by NMI" bit has to be set before next VM entry.
6822 * There are errata that may cause this bit to not be set:
6823 * AAK134, BY25.
6824 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006825 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006826 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006827 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006828 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
6829
Sheng Yang14394422008-04-28 12:24:45 +08006830 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006831 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006832
Junaid Shahid27959a42016-12-06 16:46:10 -08006833 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08006834 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08006835 ? PFERR_USER_MASK : 0;
6836 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08006837 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08006838 ? PFERR_WRITE_MASK : 0;
6839 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08006840 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08006841 ? PFERR_FETCH_MASK : 0;
6842 /* ept page table entry is present? */
6843 error_code |= (exit_qualification &
6844 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
6845 EPT_VIOLATION_EXECUTABLE))
6846 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006847
Paolo Bonzinieebed242016-11-28 14:39:58 +01006848 error_code |= (exit_qualification & 0x100) != 0 ?
6849 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03006850
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006851 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006852 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08006853}
6854
Avi Kivity851ba692009-08-24 11:10:17 +03006855static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006856{
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006857 int ret;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006858 gpa_t gpa;
6859
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02006860 /*
6861 * A nested guest cannot optimize MMIO vmexits, because we have an
6862 * nGPA here instead of the required GPA.
6863 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006864 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02006865 if (!is_guest_mode(vcpu) &&
6866 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08006867 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01006868 /*
6869 * Doing kvm_skip_emulated_instruction() depends on undefined
6870 * behavior: Intel's manual doesn't mandate
6871 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
6872 * occurs and while on real hardware it was observed to be set,
6873 * other hypervisors (namely Hyper-V) don't set it, we end up
6874 * advancing IP with some random value. Disable fast mmio when
6875 * running nested and keep it for real hardware in hope that
6876 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
6877 */
6878 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
6879 return kvm_skip_emulated_instruction(vcpu);
6880 else
6881 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
6882 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006883 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006884
Paolo Bonzinie08d26f2017-08-17 18:36:56 +02006885 ret = kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
6886 if (ret >= 0)
6887 return ret;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006888
6889 /* It is the real ept misconfig */
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006890 WARN_ON(1);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006891
Avi Kivity851ba692009-08-24 11:10:17 +03006892 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6893 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006894
6895 return 0;
6896}
6897
Avi Kivity851ba692009-08-24 11:10:17 +03006898static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08006899{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006900 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01006901 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6902 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08006903 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03006904 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006905
6906 return 1;
6907}
6908
Mohammed Gamal80ced182009-09-01 12:48:18 +02006909static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006910{
Avi Kivity8b3079a2009-01-05 12:10:54 +02006911 struct vcpu_vmx *vmx = to_vmx(vcpu);
6912 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006913 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02006914 u32 cpu_exec_ctrl;
6915 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03006916 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02006917
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07006918 /*
6919 * We should never reach the point where we are emulating L2
6920 * due to invalid guest state as that means we incorrectly
6921 * allowed a nested VMEntry with an invalid vmcs12.
6922 */
6923 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
6924
Avi Kivity49e9d552010-09-19 14:34:08 +02006925 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6926 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006927
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01006928 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03006929 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02006930 return handle_interrupt_window(&vmx->vcpu);
6931
Radim Krčmář72875d82017-04-26 22:32:19 +02006932 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03006933 return 1;
6934
Liran Alon9b8ae632017-11-05 16:56:34 +02006935 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006936
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02006937 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02006938 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006939 ret = 0;
6940 goto out;
6941 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01006942
Avi Kivityde5f70e2012-06-12 20:22:28 +03006943 if (err != EMULATE_DONE) {
6944 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6945 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6946 vcpu->run->internal.ndata = 0;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006947 return 0;
Avi Kivityde5f70e2012-06-12 20:22:28 +03006948 }
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006949
Gleb Natapov8d76c492013-05-08 18:38:44 +03006950 if (vcpu->arch.halt_request) {
6951 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006952 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03006953 goto out;
6954 }
6955
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006956 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02006957 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006958 if (need_resched())
6959 schedule();
6960 }
6961
Mohammed Gamal80ced182009-09-01 12:48:18 +02006962out:
6963 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006964}
6965
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006966static int __grow_ple_window(int val)
6967{
6968 if (ple_window_grow < 1)
6969 return ple_window;
6970
6971 val = min(val, ple_window_actual_max);
6972
6973 if (ple_window_grow < ple_window)
6974 val *= ple_window_grow;
6975 else
6976 val += ple_window_grow;
6977
6978 return val;
6979}
6980
6981static int __shrink_ple_window(int val, int modifier, int minimum)
6982{
6983 if (modifier < 1)
6984 return ple_window;
6985
6986 if (modifier < ple_window)
6987 val /= modifier;
6988 else
6989 val -= modifier;
6990
6991 return max(val, minimum);
6992}
6993
6994static void grow_ple_window(struct kvm_vcpu *vcpu)
6995{
6996 struct vcpu_vmx *vmx = to_vmx(vcpu);
6997 int old = vmx->ple_window;
6998
6999 vmx->ple_window = __grow_ple_window(old);
7000
7001 if (vmx->ple_window != old)
7002 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007003
7004 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007005}
7006
7007static void shrink_ple_window(struct kvm_vcpu *vcpu)
7008{
7009 struct vcpu_vmx *vmx = to_vmx(vcpu);
7010 int old = vmx->ple_window;
7011
7012 vmx->ple_window = __shrink_ple_window(old,
7013 ple_window_shrink, ple_window);
7014
7015 if (vmx->ple_window != old)
7016 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007017
7018 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007019}
7020
7021/*
7022 * ple_window_actual_max is computed to be one grow_ple_window() below
7023 * ple_window_max. (See __grow_ple_window for the reason.)
7024 * This prevents overflows, because ple_window_max is int.
7025 * ple_window_max effectively rounded down to a multiple of ple_window_grow in
7026 * this process.
7027 * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
7028 */
7029static void update_ple_window_actual_max(void)
7030{
7031 ple_window_actual_max =
7032 __shrink_ple_window(max(ple_window_max, ple_window),
7033 ple_window_grow, INT_MIN);
7034}
7035
Feng Wubf9f6ac2015-09-18 22:29:55 +08007036/*
7037 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7038 */
7039static void wakeup_handler(void)
7040{
7041 struct kvm_vcpu *vcpu;
7042 int cpu = smp_processor_id();
7043
7044 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7045 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7046 blocked_vcpu_list) {
7047 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7048
7049 if (pi_test_on(pi_desc) == 1)
7050 kvm_vcpu_kick(vcpu);
7051 }
7052 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7053}
7054
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007055void vmx_enable_tdp(void)
7056{
7057 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7058 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7059 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7060 0ull, VMX_EPT_EXECUTABLE_MASK,
7061 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007062 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007063
7064 ept_set_mmio_spte_mask();
7065 kvm_enable_tdp();
7066}
7067
Tiejun Chenf2c76482014-10-28 10:14:47 +08007068static __init int hardware_setup(void)
7069{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007070 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007071
7072 rdmsrl_safe(MSR_EFER, &host_efer);
7073
7074 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7075 kvm_define_shared_msr(i, vmx_msr_index[i]);
7076
Radim Krčmář23611332016-09-29 22:41:33 +02007077 for (i = 0; i < VMX_BITMAP_NR; i++) {
7078 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7079 if (!vmx_bitmap[i])
7080 goto out;
7081 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007082
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007083 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7084 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7085
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007086 if (setup_vmcs_config(&vmcs_config) < 0) {
7087 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007088 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007089 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007090
7091 if (boot_cpu_has(X86_FEATURE_NX))
7092 kvm_enable_efer_bits(EFER_NX);
7093
Wanpeng Li08d839c2017-03-23 05:30:08 -07007094 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7095 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007096 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007097
Tiejun Chenf2c76482014-10-28 10:14:47 +08007098 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007099 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007100 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007101 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007102 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007103
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007104 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007105 enable_ept_ad_bits = 0;
7106
Wanpeng Li8ad81822017-10-09 15:51:53 -07007107 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007108 enable_unrestricted_guest = 0;
7109
Paolo Bonziniad15a292015-01-30 16:18:49 +01007110 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007111 flexpriority_enabled = 0;
7112
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007113 if (!cpu_has_virtual_nmis())
7114 enable_vnmi = 0;
7115
Paolo Bonziniad15a292015-01-30 16:18:49 +01007116 /*
7117 * set_apic_access_page_addr() is used to reload apic access
7118 * page upon invalidation. No need to do anything if not
7119 * using the APIC_ACCESS_ADDR VMCS field.
7120 */
7121 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007122 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007123
7124 if (!cpu_has_vmx_tpr_shadow())
7125 kvm_x86_ops->update_cr8_intercept = NULL;
7126
7127 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7128 kvm_disable_largepages();
7129
Wanpeng Li0f107682017-09-28 18:06:24 -07007130 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007131 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007132 ple_window = 0;
7133 ple_window_grow = 0;
7134 ple_window_max = 0;
7135 ple_window_shrink = 0;
7136 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007137
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007138 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007139 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007140 kvm_x86_ops->sync_pir_to_irr = NULL;
7141 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007142
Haozhong Zhang64903d62015-10-20 15:39:09 +08007143 if (cpu_has_vmx_tsc_scaling()) {
7144 kvm_has_tsc_control = true;
7145 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7146 kvm_tsc_scaling_ratio_frac_bits = 48;
7147 }
7148
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007149 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7150
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007151 if (enable_ept)
7152 vmx_enable_tdp();
7153 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007154 kvm_disable_tdp();
7155
7156 update_ple_window_actual_max();
7157
Kai Huang843e4332015-01-28 10:54:28 +08007158 /*
7159 * Only enable PML when hardware supports PML feature, and both EPT
7160 * and EPT A/D bit features are enabled -- PML depends on them to work.
7161 */
7162 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7163 enable_pml = 0;
7164
7165 if (!enable_pml) {
7166 kvm_x86_ops->slot_enable_log_dirty = NULL;
7167 kvm_x86_ops->slot_disable_log_dirty = NULL;
7168 kvm_x86_ops->flush_log_dirty = NULL;
7169 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7170 }
7171
Yunhong Jiang64672c92016-06-13 14:19:59 -07007172 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7173 u64 vmx_msr;
7174
7175 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7176 cpu_preemption_timer_multi =
7177 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7178 } else {
7179 kvm_x86_ops->set_hv_timer = NULL;
7180 kvm_x86_ops->cancel_hv_timer = NULL;
7181 }
7182
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007183 if (!cpu_has_vmx_shadow_vmcs())
7184 enable_shadow_vmcs = 0;
7185 if (enable_shadow_vmcs)
7186 init_vmcs_shadow_fields();
7187
Feng Wubf9f6ac2015-09-18 22:29:55 +08007188 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007189 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007190
Ashok Rajc45dcc72016-06-22 14:59:56 +08007191 kvm_mce_cap_supported |= MCG_LMCE_P;
7192
Tiejun Chenf2c76482014-10-28 10:14:47 +08007193 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007194
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007195out:
Radim Krčmář23611332016-09-29 22:41:33 +02007196 for (i = 0; i < VMX_BITMAP_NR; i++)
7197 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007198
7199 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007200}
7201
7202static __exit void hardware_unsetup(void)
7203{
Radim Krčmář23611332016-09-29 22:41:33 +02007204 int i;
7205
7206 for (i = 0; i < VMX_BITMAP_NR; i++)
7207 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007208
Tiejun Chenf2c76482014-10-28 10:14:47 +08007209 free_kvm_area();
7210}
7211
Avi Kivity6aa8b732006-12-10 02:21:36 -08007212/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007213 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7214 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7215 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03007216static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007217{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007218 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007219 grow_ple_window(vcpu);
7220
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08007221 /*
7222 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7223 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7224 * never set PAUSE_EXITING and just set PLE if supported,
7225 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7226 */
7227 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007228 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007229}
7230
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007231static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08007232{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007233 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08007234}
7235
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007236static int handle_mwait(struct kvm_vcpu *vcpu)
7237{
7238 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7239 return handle_nop(vcpu);
7240}
7241
Jim Mattson45ec3682017-08-23 16:32:04 -07007242static int handle_invalid_op(struct kvm_vcpu *vcpu)
7243{
7244 kvm_queue_exception(vcpu, UD_VECTOR);
7245 return 1;
7246}
7247
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007248static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7249{
7250 return 1;
7251}
7252
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007253static int handle_monitor(struct kvm_vcpu *vcpu)
7254{
7255 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7256 return handle_nop(vcpu);
7257}
7258
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007259/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007260 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7261 * set the success or error code of an emulated VMX instruction, as specified
7262 * by Vol 2B, VMX Instruction Reference, "Conventions".
7263 */
7264static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7265{
7266 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7267 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7268 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7269}
7270
7271static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7272{
7273 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7274 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7275 X86_EFLAGS_SF | X86_EFLAGS_OF))
7276 | X86_EFLAGS_CF);
7277}
7278
Abel Gordon145c28d2013-04-18 14:36:55 +03007279static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007280 u32 vm_instruction_error)
7281{
7282 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7283 /*
7284 * failValid writes the error number to the current VMCS, which
7285 * can't be done there isn't a current VMCS.
7286 */
7287 nested_vmx_failInvalid(vcpu);
7288 return;
7289 }
7290 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7291 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7292 X86_EFLAGS_SF | X86_EFLAGS_OF))
7293 | X86_EFLAGS_ZF);
7294 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7295 /*
7296 * We don't need to force a shadow sync because
7297 * VM_INSTRUCTION_ERROR is not shadowed
7298 */
7299}
Abel Gordon145c28d2013-04-18 14:36:55 +03007300
Wincy Vanff651cb2014-12-11 08:52:58 +03007301static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7302{
7303 /* TODO: not to reset guest simply here. */
7304 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007305 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007306}
7307
Jan Kiszkaf41245002014-03-07 20:03:13 +01007308static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7309{
7310 struct vcpu_vmx *vmx =
7311 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7312
7313 vmx->nested.preemption_timer_expired = true;
7314 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7315 kvm_vcpu_kick(&vmx->vcpu);
7316
7317 return HRTIMER_NORESTART;
7318}
7319
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007320/*
Bandan Das19677e32014-05-06 02:19:15 -04007321 * Decode the memory-address operand of a vmx instruction, as recorded on an
7322 * exit caused by such an instruction (run by a guest hypervisor).
7323 * On success, returns 0. When the operand is invalid, returns 1 and throws
7324 * #UD or #GP.
7325 */
7326static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7327 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007328 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007329{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007330 gva_t off;
7331 bool exn;
7332 struct kvm_segment s;
7333
Bandan Das19677e32014-05-06 02:19:15 -04007334 /*
7335 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7336 * Execution", on an exit, vmx_instruction_info holds most of the
7337 * addressing components of the operand. Only the displacement part
7338 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7339 * For how an actual address is calculated from all these components,
7340 * refer to Vol. 1, "Operand Addressing".
7341 */
7342 int scaling = vmx_instruction_info & 3;
7343 int addr_size = (vmx_instruction_info >> 7) & 7;
7344 bool is_reg = vmx_instruction_info & (1u << 10);
7345 int seg_reg = (vmx_instruction_info >> 15) & 7;
7346 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7347 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7348 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7349 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7350
7351 if (is_reg) {
7352 kvm_queue_exception(vcpu, UD_VECTOR);
7353 return 1;
7354 }
7355
7356 /* Addr = segment_base + offset */
7357 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007358 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007359 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007360 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007361 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007362 off += kvm_register_read(vcpu, index_reg)<<scaling;
7363 vmx_get_segment(vcpu, &s, seg_reg);
7364 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007365
7366 if (addr_size == 1) /* 32 bit */
7367 *ret &= 0xffffffff;
7368
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007369 /* Checks for #GP/#SS exceptions. */
7370 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007371 if (is_long_mode(vcpu)) {
7372 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7373 * non-canonical form. This is the only check on the memory
7374 * destination for long mode!
7375 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007376 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007377 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007378 /* Protected mode: apply checks for segment validity in the
7379 * following order:
7380 * - segment type check (#GP(0) may be thrown)
7381 * - usability check (#GP(0)/#SS(0))
7382 * - limit check (#GP(0)/#SS(0))
7383 */
7384 if (wr)
7385 /* #GP(0) if the destination operand is located in a
7386 * read-only data segment or any code segment.
7387 */
7388 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7389 else
7390 /* #GP(0) if the source operand is located in an
7391 * execute-only code segment
7392 */
7393 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007394 if (exn) {
7395 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7396 return 1;
7397 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007398 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7399 */
7400 exn = (s.unusable != 0);
7401 /* Protected mode: #GP(0)/#SS(0) if the memory
7402 * operand is outside the segment limit.
7403 */
7404 exn = exn || (off + sizeof(u64) > s.limit);
7405 }
7406 if (exn) {
7407 kvm_queue_exception_e(vcpu,
7408 seg_reg == VCPU_SREG_SS ?
7409 SS_VECTOR : GP_VECTOR,
7410 0);
7411 return 1;
7412 }
7413
Bandan Das19677e32014-05-06 02:19:15 -04007414 return 0;
7415}
7416
Radim Krčmářcbf71272017-05-19 15:48:51 +02007417static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007418{
7419 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04007420 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04007421
7422 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007423 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007424 return 1;
7425
Radim Krčmářcbf71272017-05-19 15:48:51 +02007426 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, vmpointer,
7427 sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007428 kvm_inject_page_fault(vcpu, &e);
7429 return 1;
7430 }
7431
Bandan Das3573e222014-05-06 02:19:16 -04007432 return 0;
7433}
7434
Jim Mattsone29acc52016-11-30 12:03:43 -08007435static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7436{
7437 struct vcpu_vmx *vmx = to_vmx(vcpu);
7438 struct vmcs *shadow_vmcs;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007439 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08007440
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007441 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7442 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06007443 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08007444
7445 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7446 if (!vmx->nested.cached_vmcs12)
7447 goto out_cached_vmcs12;
7448
7449 if (enable_shadow_vmcs) {
7450 shadow_vmcs = alloc_vmcs();
7451 if (!shadow_vmcs)
7452 goto out_shadow_vmcs;
7453 /* mark vmcs as shadow */
7454 shadow_vmcs->revision_id |= (1u << 31);
7455 /* init shadow vmcs */
7456 vmcs_clear(shadow_vmcs);
7457 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7458 }
7459
Jim Mattsone29acc52016-11-30 12:03:43 -08007460 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7461 HRTIMER_MODE_REL_PINNED);
7462 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7463
7464 vmx->nested.vmxon = true;
7465 return 0;
7466
7467out_shadow_vmcs:
7468 kfree(vmx->nested.cached_vmcs12);
7469
7470out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06007471 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08007472
Jim Mattsonde3a0022017-11-27 17:22:25 -06007473out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08007474 return -ENOMEM;
7475}
7476
Bandan Das3573e222014-05-06 02:19:16 -04007477/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007478 * Emulate the VMXON instruction.
7479 * Currently, we just remember that VMX is active, and do not save or even
7480 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7481 * do not currently need to store anything in that guest-allocated memory
7482 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7483 * argument is different from the VMXON pointer (which the spec says they do).
7484 */
7485static int handle_vmon(struct kvm_vcpu *vcpu)
7486{
Jim Mattsone29acc52016-11-30 12:03:43 -08007487 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007488 gpa_t vmptr;
7489 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007490 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007491 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7492 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007493
Jim Mattson70f3aac2017-04-26 08:53:46 -07007494 /*
7495 * The Intel VMX Instruction Reference lists a bunch of bits that are
7496 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
7497 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
7498 * Otherwise, we should fail with #UD. But most faulting conditions
7499 * have already been checked by hardware, prior to the VM-exit for
7500 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
7501 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007502 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07007503 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007504 kvm_queue_exception(vcpu, UD_VECTOR);
7505 return 1;
7506 }
7507
Abel Gordon145c28d2013-04-18 14:36:55 +03007508 if (vmx->nested.vmxon) {
7509 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007510 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03007511 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007512
Haozhong Zhang3b840802016-06-22 14:59:54 +08007513 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007514 != VMXON_NEEDED_FEATURES) {
7515 kvm_inject_gp(vcpu, 0);
7516 return 1;
7517 }
7518
Radim Krčmářcbf71272017-05-19 15:48:51 +02007519 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08007520 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007521
7522 /*
7523 * SDM 3: 24.11.5
7524 * The first 4 bytes of VMXON region contain the supported
7525 * VMCS revision identifier
7526 *
7527 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
7528 * which replaces physical address width with 32
7529 */
7530 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7531 nested_vmx_failInvalid(vcpu);
7532 return kvm_skip_emulated_instruction(vcpu);
7533 }
7534
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02007535 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7536 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02007537 nested_vmx_failInvalid(vcpu);
7538 return kvm_skip_emulated_instruction(vcpu);
7539 }
7540 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
7541 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007542 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007543 nested_vmx_failInvalid(vcpu);
7544 return kvm_skip_emulated_instruction(vcpu);
7545 }
7546 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007547 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007548
7549 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08007550 ret = enter_vmx_operation(vcpu);
7551 if (ret)
7552 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007553
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007554 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007555 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007556}
7557
7558/*
7559 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7560 * for running VMX instructions (except VMXON, whose prerequisites are
7561 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07007562 * Note that many of these exceptions have priority over VM exits, so they
7563 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007564 */
7565static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7566{
Jim Mattson70f3aac2017-04-26 08:53:46 -07007567 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007568 kvm_queue_exception(vcpu, UD_VECTOR);
7569 return 0;
7570 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007571 return 1;
7572}
7573
David Matlack8ca44e82017-08-01 14:00:39 -07007574static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
7575{
7576 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
7577 vmcs_write64(VMCS_LINK_POINTER, -1ull);
7578}
7579
Abel Gordone7953d72013-04-18 14:37:55 +03007580static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7581{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007582 if (vmx->nested.current_vmptr == -1ull)
7583 return;
7584
Abel Gordon012f83c2013-04-18 14:39:25 +03007585 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007586 /* copy to memory all shadowed fields in case
7587 they were modified */
7588 copy_shadow_to_vmcs12(vmx);
7589 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07007590 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03007591 }
Wincy Van705699a2015-02-03 23:58:17 +08007592 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07007593
7594 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02007595 kvm_vcpu_write_guest_page(&vmx->vcpu,
7596 vmx->nested.current_vmptr >> PAGE_SHIFT,
7597 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07007598
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007599 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03007600}
7601
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007602/*
7603 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7604 * just stops using VMX.
7605 */
7606static void free_nested(struct vcpu_vmx *vmx)
7607{
Wanpeng Lib7455822017-11-22 14:04:00 -08007608 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007609 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007610
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007611 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08007612 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07007613 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07007614 vmx->nested.posted_intr_nv = -1;
7615 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007616 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07007617 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007618 vmcs_clear(vmx->vmcs01.shadow_vmcs);
7619 free_vmcs(vmx->vmcs01.shadow_vmcs);
7620 vmx->vmcs01.shadow_vmcs = NULL;
7621 }
David Matlack4f2777b2016-07-13 17:16:37 -07007622 kfree(vmx->nested.cached_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06007623 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007624 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02007625 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007626 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007627 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007628 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02007629 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007630 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007631 }
Wincy Van705699a2015-02-03 23:58:17 +08007632 if (vmx->nested.pi_desc_page) {
7633 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007634 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08007635 vmx->nested.pi_desc_page = NULL;
7636 vmx->nested.pi_desc = NULL;
7637 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007638
Jim Mattsonde3a0022017-11-27 17:22:25 -06007639 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007640}
7641
7642/* Emulate the VMXOFF instruction */
7643static int handle_vmoff(struct kvm_vcpu *vcpu)
7644{
7645 if (!nested_vmx_check_permission(vcpu))
7646 return 1;
7647 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007648 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007649 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007650}
7651
Nadav Har'El27d6c862011-05-25 23:06:59 +03007652/* Emulate the VMCLEAR instruction */
7653static int handle_vmclear(struct kvm_vcpu *vcpu)
7654{
7655 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08007656 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007657 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007658
7659 if (!nested_vmx_check_permission(vcpu))
7660 return 1;
7661
Radim Krčmářcbf71272017-05-19 15:48:51 +02007662 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03007663 return 1;
7664
Radim Krčmářcbf71272017-05-19 15:48:51 +02007665 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7666 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
7667 return kvm_skip_emulated_instruction(vcpu);
7668 }
7669
7670 if (vmptr == vmx->nested.vmxon_ptr) {
7671 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
7672 return kvm_skip_emulated_instruction(vcpu);
7673 }
7674
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007675 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03007676 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007677
Jim Mattson587d7e722017-03-02 12:41:48 -08007678 kvm_vcpu_write_guest(vcpu,
7679 vmptr + offsetof(struct vmcs12, launch_state),
7680 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03007681
Nadav Har'El27d6c862011-05-25 23:06:59 +03007682 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007683 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007684}
7685
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007686static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7687
7688/* Emulate the VMLAUNCH instruction */
7689static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7690{
7691 return nested_vmx_run(vcpu, true);
7692}
7693
7694/* Emulate the VMRESUME instruction */
7695static int handle_vmresume(struct kvm_vcpu *vcpu)
7696{
7697
7698 return nested_vmx_run(vcpu, false);
7699}
7700
Nadav Har'El49f705c2011-05-25 23:08:30 +03007701/*
7702 * Read a vmcs12 field. Since these can have varying lengths and we return
7703 * one type, we chose the biggest type (u64) and zero-extend the return value
7704 * to that size. Note that the caller, handle_vmread, might need to use only
7705 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7706 * 64-bit fields are to be returned).
7707 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007708static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7709 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03007710{
7711 short offset = vmcs_field_to_offset(field);
7712 char *p;
7713
7714 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007715 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007716
7717 p = ((char *)(get_vmcs12(vcpu))) + offset;
7718
Jim Mattsond37f4262017-12-22 12:12:16 -08007719 switch (vmcs_field_width(field)) {
7720 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007721 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007722 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007723 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007724 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007725 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007726 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007727 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007728 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007729 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007730 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007731 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007732 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007733 WARN_ON(1);
7734 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007735 }
7736}
7737
Abel Gordon20b97fe2013-04-18 14:36:25 +03007738
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007739static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7740 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03007741 short offset = vmcs_field_to_offset(field);
7742 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7743 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007744 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007745
Jim Mattsond37f4262017-12-22 12:12:16 -08007746 switch (vmcs_field_width(field)) {
7747 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007748 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007749 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007750 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007751 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007752 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007753 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007754 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007755 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007756 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007757 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007758 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007759 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007760 WARN_ON(1);
7761 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007762 }
7763
7764}
7765
Abel Gordon16f5b902013-04-18 14:38:25 +03007766static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7767{
7768 int i;
7769 unsigned long field;
7770 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007771 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007772 const u16 *fields = shadow_read_write_fields;
Mathias Krausec2bae892013-06-26 20:36:21 +02007773 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03007774
Jan Kiszka282da872014-10-08 18:05:39 +02007775 preempt_disable();
7776
Abel Gordon16f5b902013-04-18 14:38:25 +03007777 vmcs_load(shadow_vmcs);
7778
7779 for (i = 0; i < num_fields; i++) {
7780 field = fields[i];
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007781 field_value = __vmcs_readl(field);
Abel Gordon16f5b902013-04-18 14:38:25 +03007782 vmcs12_write_any(&vmx->vcpu, field, field_value);
7783 }
7784
7785 vmcs_clear(shadow_vmcs);
7786 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02007787
7788 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03007789}
7790
Abel Gordonc3114422013-04-18 14:38:55 +03007791static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7792{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007793 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02007794 shadow_read_write_fields,
7795 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03007796 };
Mathias Krausec2bae892013-06-26 20:36:21 +02007797 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03007798 max_shadow_read_write_fields,
7799 max_shadow_read_only_fields
7800 };
7801 int i, q;
7802 unsigned long field;
7803 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007804 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03007805
7806 vmcs_load(shadow_vmcs);
7807
Mathias Krausec2bae892013-06-26 20:36:21 +02007808 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03007809 for (i = 0; i < max_fields[q]; i++) {
7810 field = fields[q][i];
7811 vmcs12_read_any(&vmx->vcpu, field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007812 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03007813 }
7814 }
7815
7816 vmcs_clear(shadow_vmcs);
7817 vmcs_load(vmx->loaded_vmcs->vmcs);
7818}
7819
Nadav Har'El49f705c2011-05-25 23:08:30 +03007820/*
7821 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7822 * used before) all generate the same failure when it is missing.
7823 */
7824static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
7825{
7826 struct vcpu_vmx *vmx = to_vmx(vcpu);
7827 if (vmx->nested.current_vmptr == -1ull) {
7828 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007829 return 0;
7830 }
7831 return 1;
7832}
7833
7834static int handle_vmread(struct kvm_vcpu *vcpu)
7835{
7836 unsigned long field;
7837 u64 field_value;
7838 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7839 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7840 gva_t gva = 0;
7841
Kyle Hueyeb277562016-11-29 12:40:39 -08007842 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007843 return 1;
7844
Kyle Huey6affcbe2016-11-29 12:40:40 -08007845 if (!nested_vmx_check_vmcs12(vcpu))
7846 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08007847
Nadav Har'El49f705c2011-05-25 23:08:30 +03007848 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03007849 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007850 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007851 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007852 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007853 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007854 }
7855 /*
7856 * Now copy part of this value to register or memory, as requested.
7857 * Note that the number of bits actually copied is 32 or 64 depending
7858 * on the guest's mode (32 or 64 bit), not on the given field's length.
7859 */
7860 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03007861 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03007862 field_value);
7863 } else {
7864 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007865 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007866 return 1;
Jim Mattson70f3aac2017-04-26 08:53:46 -07007867 /* _system ok, as hardware has verified cpl=0 */
Nadav Har'El49f705c2011-05-25 23:08:30 +03007868 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
7869 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
7870 }
7871
7872 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007873 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007874}
7875
7876
7877static int handle_vmwrite(struct kvm_vcpu *vcpu)
7878{
7879 unsigned long field;
7880 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01007881 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007882 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7883 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01007884
Nadav Har'El49f705c2011-05-25 23:08:30 +03007885 /* The value to write might be 32 or 64 bits, depending on L1's long
7886 * mode, and eventually we need to write that into a field of several
7887 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08007888 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03007889 * bits into the vmcs12 field.
7890 */
7891 u64 field_value = 0;
7892 struct x86_exception e;
7893
Kyle Hueyeb277562016-11-29 12:40:39 -08007894 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007895 return 1;
7896
Kyle Huey6affcbe2016-11-29 12:40:40 -08007897 if (!nested_vmx_check_vmcs12(vcpu))
7898 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08007899
Nadav Har'El49f705c2011-05-25 23:08:30 +03007900 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03007901 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007902 (((vmx_instruction_info) >> 3) & 0xf));
7903 else {
7904 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007905 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007906 return 1;
7907 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
Nadav Amit27e6fb52014-06-18 17:19:26 +03007908 &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007909 kvm_inject_page_fault(vcpu, &e);
7910 return 1;
7911 }
7912 }
7913
7914
Nadav Amit27e6fb52014-06-18 17:19:26 +03007915 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007916 if (vmcs_field_readonly(field)) {
7917 nested_vmx_failValid(vcpu,
7918 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007919 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007920 }
7921
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007922 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007923 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007924 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007925 }
7926
Paolo Bonzini74a497f2017-12-20 13:55:39 +01007927 switch (field) {
7928#define SHADOW_FIELD_RW(x) case x:
7929#include "vmx_shadow_fields.h"
7930 /*
7931 * The fields that can be updated by L1 without a vmexit are
7932 * always updated in the vmcs02, the others go down the slow
7933 * path of prepare_vmcs02.
7934 */
7935 break;
7936 default:
7937 vmx->nested.dirty_vmcs12 = true;
7938 break;
7939 }
7940
Nadav Har'El49f705c2011-05-25 23:08:30 +03007941 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007942 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007943}
7944
Jim Mattsona8bc2842016-11-30 12:03:44 -08007945static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
7946{
7947 vmx->nested.current_vmptr = vmptr;
7948 if (enable_shadow_vmcs) {
7949 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
7950 SECONDARY_EXEC_SHADOW_VMCS);
7951 vmcs_write64(VMCS_LINK_POINTER,
7952 __pa(vmx->vmcs01.shadow_vmcs));
7953 vmx->nested.sync_shadow_vmcs = true;
7954 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01007955 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08007956}
7957
Nadav Har'El63846662011-05-25 23:07:29 +03007958/* Emulate the VMPTRLD instruction */
7959static int handle_vmptrld(struct kvm_vcpu *vcpu)
7960{
7961 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007962 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03007963
7964 if (!nested_vmx_check_permission(vcpu))
7965 return 1;
7966
Radim Krčmářcbf71272017-05-19 15:48:51 +02007967 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03007968 return 1;
7969
Radim Krčmářcbf71272017-05-19 15:48:51 +02007970 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7971 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
7972 return kvm_skip_emulated_instruction(vcpu);
7973 }
7974
7975 if (vmptr == vmx->nested.vmxon_ptr) {
7976 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
7977 return kvm_skip_emulated_instruction(vcpu);
7978 }
7979
Nadav Har'El63846662011-05-25 23:07:29 +03007980 if (vmx->nested.current_vmptr != vmptr) {
7981 struct vmcs12 *new_vmcs12;
7982 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02007983 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7984 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03007985 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007986 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007987 }
7988 new_vmcs12 = kmap(page);
7989 if (new_vmcs12->revision_id != VMCS12_REVISION) {
7990 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007991 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03007992 nested_vmx_failValid(vcpu,
7993 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007994 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007995 }
Nadav Har'El63846662011-05-25 23:07:29 +03007996
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007997 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07007998 /*
7999 * Load VMCS12 from guest memory since it is not already
8000 * cached.
8001 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008002 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8003 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008004 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008005
Jim Mattsona8bc2842016-11-30 12:03:44 -08008006 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008007 }
8008
8009 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008010 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008011}
8012
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008013/* Emulate the VMPTRST instruction */
8014static int handle_vmptrst(struct kvm_vcpu *vcpu)
8015{
8016 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8017 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8018 gva_t vmcs_gva;
8019 struct x86_exception e;
8020
8021 if (!nested_vmx_check_permission(vcpu))
8022 return 1;
8023
8024 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008025 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008026 return 1;
Jim Mattson70f3aac2017-04-26 08:53:46 -07008027 /* ok to use *_system, as hardware has verified cpl=0 */
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008028 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
8029 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8030 sizeof(u64), &e)) {
8031 kvm_inject_page_fault(vcpu, &e);
8032 return 1;
8033 }
8034 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008035 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008036}
8037
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008038/* Emulate the INVEPT instruction */
8039static int handle_invept(struct kvm_vcpu *vcpu)
8040{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008041 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008042 u32 vmx_instruction_info, types;
8043 unsigned long type;
8044 gva_t gva;
8045 struct x86_exception e;
8046 struct {
8047 u64 eptp, gpa;
8048 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008049
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008050 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008051 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008052 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008053 kvm_queue_exception(vcpu, UD_VECTOR);
8054 return 1;
8055 }
8056
8057 if (!nested_vmx_check_permission(vcpu))
8058 return 1;
8059
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008060 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008061 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008062
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008063 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008064
Jim Mattson85c856b2016-10-26 08:38:38 -07008065 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008066 nested_vmx_failValid(vcpu,
8067 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008068 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008069 }
8070
8071 /* According to the Intel VMX instruction reference, the memory
8072 * operand is read even if it isn't needed (e.g., for type==global)
8073 */
8074 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008075 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008076 return 1;
8077 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
8078 sizeof(operand), &e)) {
8079 kvm_inject_page_fault(vcpu, &e);
8080 return 1;
8081 }
8082
8083 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008084 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008085 /*
8086 * TODO: track mappings and invalidate
8087 * single context requests appropriately
8088 */
8089 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008090 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008091 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008092 nested_vmx_succeed(vcpu);
8093 break;
8094 default:
8095 BUG_ON(1);
8096 break;
8097 }
8098
Kyle Huey6affcbe2016-11-29 12:40:40 -08008099 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008100}
8101
Petr Matouseka642fc32014-09-23 20:22:30 +02008102static int handle_invvpid(struct kvm_vcpu *vcpu)
8103{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008104 struct vcpu_vmx *vmx = to_vmx(vcpu);
8105 u32 vmx_instruction_info;
8106 unsigned long type, types;
8107 gva_t gva;
8108 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008109 struct {
8110 u64 vpid;
8111 u64 gla;
8112 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008113
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008114 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008115 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008116 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008117 kvm_queue_exception(vcpu, UD_VECTOR);
8118 return 1;
8119 }
8120
8121 if (!nested_vmx_check_permission(vcpu))
8122 return 1;
8123
8124 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8125 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8126
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008127 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008128 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008129
Jim Mattson85c856b2016-10-26 08:38:38 -07008130 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008131 nested_vmx_failValid(vcpu,
8132 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008133 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008134 }
8135
8136 /* according to the intel vmx instruction reference, the memory
8137 * operand is read even if it isn't needed (e.g., for type==global)
8138 */
8139 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8140 vmx_instruction_info, false, &gva))
8141 return 1;
Jim Mattson40352602017-06-28 09:37:37 -07008142 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
8143 sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008144 kvm_inject_page_fault(vcpu, &e);
8145 return 1;
8146 }
Jim Mattson40352602017-06-28 09:37:37 -07008147 if (operand.vpid >> 16) {
8148 nested_vmx_failValid(vcpu,
8149 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8150 return kvm_skip_emulated_instruction(vcpu);
8151 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008152
8153 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008154 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Yu Zhangfd8cb432017-08-24 20:27:56 +08008155 if (is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07008156 nested_vmx_failValid(vcpu,
8157 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8158 return kvm_skip_emulated_instruction(vcpu);
8159 }
8160 /* fall through */
Paolo Bonzinief697a72016-03-18 16:58:38 +01008161 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008162 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07008163 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008164 nested_vmx_failValid(vcpu,
8165 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008166 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008167 }
8168 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008169 case VMX_VPID_EXTENT_ALL_CONTEXT:
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008170 break;
8171 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008172 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008173 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008174 }
8175
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08008176 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008177 nested_vmx_succeed(vcpu);
8178
Kyle Huey6affcbe2016-11-29 12:40:40 -08008179 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02008180}
8181
Kai Huang843e4332015-01-28 10:54:28 +08008182static int handle_pml_full(struct kvm_vcpu *vcpu)
8183{
8184 unsigned long exit_qualification;
8185
8186 trace_kvm_pml_full(vcpu->vcpu_id);
8187
8188 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8189
8190 /*
8191 * PML buffer FULL happened while executing iret from NMI,
8192 * "blocked by NMI" bit has to be set before next VM entry.
8193 */
8194 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008195 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08008196 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8197 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8198 GUEST_INTR_STATE_NMI);
8199
8200 /*
8201 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8202 * here.., and there's no userspace involvement needed for PML.
8203 */
8204 return 1;
8205}
8206
Yunhong Jiang64672c92016-06-13 14:19:59 -07008207static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8208{
8209 kvm_lapic_expired_hv_timer(vcpu);
8210 return 1;
8211}
8212
Bandan Das41ab9372017-08-03 15:54:43 -04008213static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8214{
8215 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04008216 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8217
8218 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008219 switch (address & VMX_EPTP_MT_MASK) {
8220 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008221 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008222 return false;
8223 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02008224 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008225 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008226 return false;
8227 break;
8228 default:
8229 return false;
8230 }
8231
David Hildenbrandbb97a012017-08-10 23:15:28 +02008232 /* only 4 levels page-walk length are valid */
8233 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04008234 return false;
8235
8236 /* Reserved bits should not be set */
8237 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8238 return false;
8239
8240 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008241 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008242 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008243 return false;
8244 }
8245
8246 return true;
8247}
8248
8249static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8250 struct vmcs12 *vmcs12)
8251{
8252 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8253 u64 address;
8254 bool accessed_dirty;
8255 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8256
8257 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8258 !nested_cpu_has_ept(vmcs12))
8259 return 1;
8260
8261 if (index >= VMFUNC_EPTP_ENTRIES)
8262 return 1;
8263
8264
8265 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8266 &address, index * 8, 8))
8267 return 1;
8268
David Hildenbrandbb97a012017-08-10 23:15:28 +02008269 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04008270
8271 /*
8272 * If the (L2) guest does a vmfunc to the currently
8273 * active ept pointer, we don't have to do anything else
8274 */
8275 if (vmcs12->ept_pointer != address) {
8276 if (!valid_ept_address(vcpu, address))
8277 return 1;
8278
8279 kvm_mmu_unload(vcpu);
8280 mmu->ept_ad = accessed_dirty;
8281 mmu->base_role.ad_disabled = !accessed_dirty;
8282 vmcs12->ept_pointer = address;
8283 /*
8284 * TODO: Check what's the correct approach in case
8285 * mmu reload fails. Currently, we just let the next
8286 * reload potentially fail
8287 */
8288 kvm_mmu_reload(vcpu);
8289 }
8290
8291 return 0;
8292}
8293
Bandan Das2a499e42017-08-03 15:54:41 -04008294static int handle_vmfunc(struct kvm_vcpu *vcpu)
8295{
Bandan Das27c42a12017-08-03 15:54:42 -04008296 struct vcpu_vmx *vmx = to_vmx(vcpu);
8297 struct vmcs12 *vmcs12;
8298 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8299
8300 /*
8301 * VMFUNC is only supported for nested guests, but we always enable the
8302 * secondary control for simplicity; for non-nested mode, fake that we
8303 * didn't by injecting #UD.
8304 */
8305 if (!is_guest_mode(vcpu)) {
8306 kvm_queue_exception(vcpu, UD_VECTOR);
8307 return 1;
8308 }
8309
8310 vmcs12 = get_vmcs12(vcpu);
8311 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8312 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04008313
8314 switch (function) {
8315 case 0:
8316 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8317 goto fail;
8318 break;
8319 default:
8320 goto fail;
8321 }
8322 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04008323
8324fail:
8325 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8326 vmcs_read32(VM_EXIT_INTR_INFO),
8327 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04008328 return 1;
8329}
8330
Nadav Har'El0140cae2011-05-25 23:06:28 +03008331/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08008332 * The exit handlers return 1 if the exit was handled fully and guest execution
8333 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8334 * to be done to userspace and return 0.
8335 */
Mathias Krause772e0312012-08-30 01:30:19 +02008336static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08008337 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8338 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08008339 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08008340 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008341 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008342 [EXIT_REASON_CR_ACCESS] = handle_cr,
8343 [EXIT_REASON_DR_ACCESS] = handle_dr,
8344 [EXIT_REASON_CPUID] = handle_cpuid,
8345 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8346 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8347 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8348 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02008349 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03008350 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02008351 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02008352 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03008353 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008354 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03008355 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008356 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008357 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008358 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008359 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008360 [EXIT_REASON_VMOFF] = handle_vmoff,
8361 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08008362 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8363 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08008364 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008365 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02008366 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08008367 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02008368 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08008369 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02008370 [EXIT_REASON_GDTR_IDTR] = handle_desc,
8371 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03008372 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8373 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008374 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008375 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008376 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008377 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008378 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02008379 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07008380 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07008381 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08008382 [EXIT_REASON_XSAVES] = handle_xsaves,
8383 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08008384 [EXIT_REASON_PML_FULL] = handle_pml_full,
Bandan Das2a499e42017-08-03 15:54:41 -04008385 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07008386 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008387};
8388
8389static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04008390 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008391
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008392static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8393 struct vmcs12 *vmcs12)
8394{
8395 unsigned long exit_qualification;
8396 gpa_t bitmap, last_bitmap;
8397 unsigned int port;
8398 int size;
8399 u8 b;
8400
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008401 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05008402 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008403
8404 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8405
8406 port = exit_qualification >> 16;
8407 size = (exit_qualification & 7) + 1;
8408
8409 last_bitmap = (gpa_t)-1;
8410 b = -1;
8411
8412 while (size > 0) {
8413 if (port < 0x8000)
8414 bitmap = vmcs12->io_bitmap_a;
8415 else if (port < 0x10000)
8416 bitmap = vmcs12->io_bitmap_b;
8417 else
Joe Perches1d804d02015-03-30 16:46:09 -07008418 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008419 bitmap += (port & 0x7fff) / 8;
8420
8421 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008422 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008423 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008424 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07008425 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008426
8427 port++;
8428 size--;
8429 last_bitmap = bitmap;
8430 }
8431
Joe Perches1d804d02015-03-30 16:46:09 -07008432 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008433}
8434
Nadav Har'El644d7112011-05-25 23:12:35 +03008435/*
8436 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8437 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8438 * disinterest in the current event (read or write a specific MSR) by using an
8439 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8440 */
8441static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8442 struct vmcs12 *vmcs12, u32 exit_reason)
8443{
8444 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8445 gpa_t bitmap;
8446
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01008447 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07008448 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008449
8450 /*
8451 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8452 * for the four combinations of read/write and low/high MSR numbers.
8453 * First we need to figure out which of the four to use:
8454 */
8455 bitmap = vmcs12->msr_bitmap;
8456 if (exit_reason == EXIT_REASON_MSR_WRITE)
8457 bitmap += 2048;
8458 if (msr_index >= 0xc0000000) {
8459 msr_index -= 0xc0000000;
8460 bitmap += 1024;
8461 }
8462
8463 /* Then read the msr_index'th bit from this bitmap: */
8464 if (msr_index < 1024*8) {
8465 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008466 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008467 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008468 return 1 & (b >> (msr_index & 7));
8469 } else
Joe Perches1d804d02015-03-30 16:46:09 -07008470 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03008471}
8472
8473/*
8474 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8475 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8476 * intercept (via guest_host_mask etc.) the current event.
8477 */
8478static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8479 struct vmcs12 *vmcs12)
8480{
8481 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8482 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008483 int reg;
8484 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03008485
8486 switch ((exit_qualification >> 4) & 3) {
8487 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008488 reg = (exit_qualification >> 8) & 15;
8489 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03008490 switch (cr) {
8491 case 0:
8492 if (vmcs12->cr0_guest_host_mask &
8493 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008494 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008495 break;
8496 case 3:
8497 if ((vmcs12->cr3_target_count >= 1 &&
8498 vmcs12->cr3_target_value0 == val) ||
8499 (vmcs12->cr3_target_count >= 2 &&
8500 vmcs12->cr3_target_value1 == val) ||
8501 (vmcs12->cr3_target_count >= 3 &&
8502 vmcs12->cr3_target_value2 == val) ||
8503 (vmcs12->cr3_target_count >= 4 &&
8504 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07008505 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008506 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008507 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008508 break;
8509 case 4:
8510 if (vmcs12->cr4_guest_host_mask &
8511 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07008512 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008513 break;
8514 case 8:
8515 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008516 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008517 break;
8518 }
8519 break;
8520 case 2: /* clts */
8521 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
8522 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008523 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008524 break;
8525 case 1: /* mov from cr */
8526 switch (cr) {
8527 case 3:
8528 if (vmcs12->cpu_based_vm_exec_control &
8529 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008530 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008531 break;
8532 case 8:
8533 if (vmcs12->cpu_based_vm_exec_control &
8534 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008535 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008536 break;
8537 }
8538 break;
8539 case 3: /* lmsw */
8540 /*
8541 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
8542 * cr0. Other attempted changes are ignored, with no exit.
8543 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008544 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03008545 if (vmcs12->cr0_guest_host_mask & 0xe &
8546 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008547 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008548 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
8549 !(vmcs12->cr0_read_shadow & 0x1) &&
8550 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07008551 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008552 break;
8553 }
Joe Perches1d804d02015-03-30 16:46:09 -07008554 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008555}
8556
8557/*
8558 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8559 * should handle it ourselves in L0 (and then continue L2). Only call this
8560 * when in is_guest_mode (L2).
8561 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02008562static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03008563{
Nadav Har'El644d7112011-05-25 23:12:35 +03008564 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8565 struct vcpu_vmx *vmx = to_vmx(vcpu);
8566 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8567
Jim Mattson4f350c62017-09-14 16:31:44 -07008568 if (vmx->nested.nested_run_pending)
8569 return false;
8570
8571 if (unlikely(vmx->fail)) {
8572 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8573 vmcs_read32(VM_INSTRUCTION_ERROR));
8574 return true;
8575 }
Jan Kiszka542060e2014-01-04 18:47:21 +01008576
David Matlackc9f04402017-08-01 14:00:40 -07008577 /*
8578 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06008579 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
8580 * Page). The CPU may write to these pages via their host
8581 * physical address while L2 is running, bypassing any
8582 * address-translation-based dirty tracking (e.g. EPT write
8583 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07008584 *
8585 * Mark them dirty on every exit from L2 to prevent them from
8586 * getting out of sync with dirty tracking.
8587 */
8588 nested_mark_vmcs12_pages_dirty(vcpu);
8589
Jim Mattson4f350c62017-09-14 16:31:44 -07008590 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
8591 vmcs_readl(EXIT_QUALIFICATION),
8592 vmx->idt_vectoring_info,
8593 intr_info,
8594 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8595 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03008596
8597 switch (exit_reason) {
8598 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08008599 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07008600 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008601 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07008602 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01008603 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01008604 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008605 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01008606 else if (is_debug(intr_info) &&
8607 vcpu->guest_debug &
8608 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8609 return false;
8610 else if (is_breakpoint(intr_info) &&
8611 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8612 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008613 return vmcs12->exception_bitmap &
8614 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8615 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07008616 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008617 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07008618 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008619 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008620 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008621 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008622 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008623 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07008624 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008625 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07008626 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008627 case EXIT_REASON_HLT:
8628 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8629 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07008630 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008631 case EXIT_REASON_INVLPG:
8632 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8633 case EXIT_REASON_RDPMC:
8634 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02008635 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02008636 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02008637 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02008638 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01008639 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03008640 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8641 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8642 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8643 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8644 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8645 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02008646 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03008647 /*
8648 * VMX instructions trap unconditionally. This allows L1 to
8649 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8650 */
Joe Perches1d804d02015-03-30 16:46:09 -07008651 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008652 case EXIT_REASON_CR_ACCESS:
8653 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8654 case EXIT_REASON_DR_ACCESS:
8655 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8656 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008657 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02008658 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
8659 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03008660 case EXIT_REASON_MSR_READ:
8661 case EXIT_REASON_MSR_WRITE:
8662 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8663 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07008664 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008665 case EXIT_REASON_MWAIT_INSTRUCTION:
8666 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008667 case EXIT_REASON_MONITOR_TRAP_FLAG:
8668 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03008669 case EXIT_REASON_MONITOR_INSTRUCTION:
8670 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8671 case EXIT_REASON_PAUSE_INSTRUCTION:
8672 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8673 nested_cpu_has2(vmcs12,
8674 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8675 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07008676 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008677 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008678 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03008679 case EXIT_REASON_APIC_ACCESS:
8680 return nested_cpu_has2(vmcs12,
8681 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Wincy Van82f0dd42015-02-03 23:57:18 +08008682 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08008683 case EXIT_REASON_EOI_INDUCED:
8684 /* apic_write and eoi_induced should exit unconditionally. */
Joe Perches1d804d02015-03-30 16:46:09 -07008685 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008686 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008687 /*
8688 * L0 always deals with the EPT violation. If nested EPT is
8689 * used, and the nested mmu code discovers that the address is
8690 * missing in the guest EPT table (EPT12), the EPT violation
8691 * will be injected with nested_ept_inject_page_fault()
8692 */
Joe Perches1d804d02015-03-30 16:46:09 -07008693 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008694 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008695 /*
8696 * L2 never uses directly L1's EPT, but rather L0's own EPT
8697 * table (shadow on EPT) or a merged EPT table that L0 built
8698 * (EPT on EPT). So any problems with the structure of the
8699 * table is L0's fault.
8700 */
Joe Perches1d804d02015-03-30 16:46:09 -07008701 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02008702 case EXIT_REASON_INVPCID:
8703 return
8704 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
8705 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008706 case EXIT_REASON_WBINVD:
8707 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8708 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07008709 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08008710 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8711 /*
8712 * This should never happen, since it is not possible to
8713 * set XSS to a non-zero value---neither in L1 nor in L2.
8714 * If if it were, XSS would have to be checked against
8715 * the XSS exit bitmap in vmcs12.
8716 */
8717 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08008718 case EXIT_REASON_PREEMPTION_TIMER:
8719 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02008720 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04008721 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02008722 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04008723 case EXIT_REASON_VMFUNC:
8724 /* VM functions are emulated through L2->L0 vmexits. */
8725 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008726 default:
Joe Perches1d804d02015-03-30 16:46:09 -07008727 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008728 }
8729}
8730
Paolo Bonzini7313c692017-07-27 10:31:25 +02008731static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
8732{
8733 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8734
8735 /*
8736 * At this point, the exit interruption info in exit_intr_info
8737 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
8738 * we need to query the in-kernel LAPIC.
8739 */
8740 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
8741 if ((exit_intr_info &
8742 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
8743 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
8744 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8745 vmcs12->vm_exit_intr_error_code =
8746 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
8747 }
8748
8749 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
8750 vmcs_readl(EXIT_QUALIFICATION));
8751 return 1;
8752}
8753
Avi Kivity586f9602010-11-18 13:09:54 +02008754static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8755{
8756 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8757 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8758}
8759
Kai Huanga3eaa862015-11-04 13:46:05 +08008760static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08008761{
Kai Huanga3eaa862015-11-04 13:46:05 +08008762 if (vmx->pml_pg) {
8763 __free_page(vmx->pml_pg);
8764 vmx->pml_pg = NULL;
8765 }
Kai Huang843e4332015-01-28 10:54:28 +08008766}
8767
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008768static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08008769{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008770 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008771 u64 *pml_buf;
8772 u16 pml_idx;
8773
8774 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8775
8776 /* Do nothing if PML buffer is empty */
8777 if (pml_idx == (PML_ENTITY_NUM - 1))
8778 return;
8779
8780 /* PML index always points to next available PML buffer entity */
8781 if (pml_idx >= PML_ENTITY_NUM)
8782 pml_idx = 0;
8783 else
8784 pml_idx++;
8785
8786 pml_buf = page_address(vmx->pml_pg);
8787 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8788 u64 gpa;
8789
8790 gpa = pml_buf[pml_idx];
8791 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008792 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08008793 }
8794
8795 /* reset PML index */
8796 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8797}
8798
8799/*
8800 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8801 * Called before reporting dirty_bitmap to userspace.
8802 */
8803static void kvm_flush_pml_buffers(struct kvm *kvm)
8804{
8805 int i;
8806 struct kvm_vcpu *vcpu;
8807 /*
8808 * We only need to kick vcpu out of guest mode here, as PML buffer
8809 * is flushed at beginning of all VMEXITs, and it's obvious that only
8810 * vcpus running in guest are possible to have unflushed GPAs in PML
8811 * buffer.
8812 */
8813 kvm_for_each_vcpu(i, vcpu, kvm)
8814 kvm_vcpu_kick(vcpu);
8815}
8816
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008817static void vmx_dump_sel(char *name, uint32_t sel)
8818{
8819 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05008820 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008821 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8822 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
8823 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
8824}
8825
8826static void vmx_dump_dtsel(char *name, uint32_t limit)
8827{
8828 pr_err("%s limit=0x%08x, base=0x%016lx\n",
8829 name, vmcs_read32(limit),
8830 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
8831}
8832
8833static void dump_vmcs(void)
8834{
8835 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
8836 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
8837 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
8838 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
8839 u32 secondary_exec_control = 0;
8840 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01008841 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008842 int i, n;
8843
8844 if (cpu_has_secondary_exec_ctrls())
8845 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8846
8847 pr_err("*** Guest State ***\n");
8848 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8849 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
8850 vmcs_readl(CR0_GUEST_HOST_MASK));
8851 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8852 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
8853 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
8854 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
8855 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
8856 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008857 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
8858 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
8859 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
8860 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008861 }
8862 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
8863 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
8864 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
8865 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
8866 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8867 vmcs_readl(GUEST_SYSENTER_ESP),
8868 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
8869 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
8870 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
8871 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
8872 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
8873 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
8874 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
8875 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
8876 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
8877 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
8878 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
8879 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
8880 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008881 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8882 efer, vmcs_read64(GUEST_IA32_PAT));
8883 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
8884 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008885 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
8886 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008887 pr_err("PerfGlobCtl = 0x%016llx\n",
8888 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008889 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008890 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008891 pr_err("Interruptibility = %08x ActivityState = %08x\n",
8892 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
8893 vmcs_read32(GUEST_ACTIVITY_STATE));
8894 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
8895 pr_err("InterruptStatus = %04x\n",
8896 vmcs_read16(GUEST_INTR_STATUS));
8897
8898 pr_err("*** Host State ***\n");
8899 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
8900 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
8901 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
8902 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
8903 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
8904 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
8905 vmcs_read16(HOST_TR_SELECTOR));
8906 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
8907 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
8908 vmcs_readl(HOST_TR_BASE));
8909 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
8910 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
8911 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
8912 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
8913 vmcs_readl(HOST_CR4));
8914 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8915 vmcs_readl(HOST_IA32_SYSENTER_ESP),
8916 vmcs_read32(HOST_IA32_SYSENTER_CS),
8917 vmcs_readl(HOST_IA32_SYSENTER_EIP));
8918 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008919 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8920 vmcs_read64(HOST_IA32_EFER),
8921 vmcs_read64(HOST_IA32_PAT));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008922 if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008923 pr_err("PerfGlobCtl = 0x%016llx\n",
8924 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008925
8926 pr_err("*** Control State ***\n");
8927 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
8928 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
8929 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
8930 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
8931 vmcs_read32(EXCEPTION_BITMAP),
8932 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
8933 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
8934 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
8935 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8936 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
8937 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
8938 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
8939 vmcs_read32(VM_EXIT_INTR_INFO),
8940 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8941 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
8942 pr_err(" reason=%08x qualification=%016lx\n",
8943 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
8944 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
8945 vmcs_read32(IDT_VECTORING_INFO_FIELD),
8946 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008947 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08008948 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008949 pr_err("TSC Multiplier = 0x%016llx\n",
8950 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008951 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
8952 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
8953 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
8954 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
8955 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008956 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008957 n = vmcs_read32(CR3_TARGET_COUNT);
8958 for (i = 0; i + 1 < n; i += 4)
8959 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
8960 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
8961 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
8962 if (i < n)
8963 pr_err("CR3 target%u=%016lx\n",
8964 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
8965 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
8966 pr_err("PLE Gap=%08x Window=%08x\n",
8967 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
8968 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
8969 pr_err("Virtual processor ID = 0x%04x\n",
8970 vmcs_read16(VIRTUAL_PROCESSOR_ID));
8971}
8972
Avi Kivity6aa8b732006-12-10 02:21:36 -08008973/*
8974 * The guest has exited. See if we can fix it or if we need userspace
8975 * assistance.
8976 */
Avi Kivity851ba692009-08-24 11:10:17 +03008977static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008978{
Avi Kivity29bd8a72007-09-10 17:27:03 +03008979 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08008980 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02008981 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03008982
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008983 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
8984
Kai Huang843e4332015-01-28 10:54:28 +08008985 /*
8986 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
8987 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
8988 * querying dirty_bitmap, we only need to kick all vcpus out of guest
8989 * mode as if vcpus is in root mode, the PML buffer must has been
8990 * flushed already.
8991 */
8992 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008993 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008994
Mohammed Gamal80ced182009-09-01 12:48:18 +02008995 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02008996 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02008997 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01008998
Paolo Bonzini7313c692017-07-27 10:31:25 +02008999 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
9000 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03009001
Mohammed Gamal51207022010-05-31 22:40:54 +03009002 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009003 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03009004 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9005 vcpu->run->fail_entry.hardware_entry_failure_reason
9006 = exit_reason;
9007 return 0;
9008 }
9009
Avi Kivity29bd8a72007-09-10 17:27:03 +03009010 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009011 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9012 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03009013 = vmcs_read32(VM_INSTRUCTION_ERROR);
9014 return 0;
9015 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009016
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009017 /*
9018 * Note:
9019 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9020 * delivery event since it indicates guest is accessing MMIO.
9021 * The vm-exit can be triggered again after return to guest that
9022 * will cause infinite loop.
9023 */
Mike Dayd77c26f2007-10-08 09:02:08 -04009024 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08009025 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02009026 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00009027 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009028 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9029 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9030 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009031 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009032 vcpu->run->internal.data[0] = vectoring_info;
9033 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009034 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9035 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9036 vcpu->run->internal.ndata++;
9037 vcpu->run->internal.data[3] =
9038 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9039 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009040 return 0;
9041 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009042
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009043 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009044 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9045 if (vmx_interrupt_allowed(vcpu)) {
9046 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9047 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9048 vcpu->arch.nmi_pending) {
9049 /*
9050 * This CPU don't support us in finding the end of an
9051 * NMI-blocked window if the guest runs with IRQs
9052 * disabled. So we pull the trigger after 1 s of
9053 * futile waiting, but inform the user about this.
9054 */
9055 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9056 "state on VCPU %d after 1 s timeout\n",
9057 __func__, vcpu->vcpu_id);
9058 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9059 }
9060 }
9061
Avi Kivity6aa8b732006-12-10 02:21:36 -08009062 if (exit_reason < kvm_vmx_max_exit_handlers
9063 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03009064 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009065 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01009066 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9067 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03009068 kvm_queue_exception(vcpu, UD_VECTOR);
9069 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009070 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009071}
9072
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009073static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009074{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009075 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9076
9077 if (is_guest_mode(vcpu) &&
9078 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9079 return;
9080
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009081 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009082 vmcs_write32(TPR_THRESHOLD, 0);
9083 return;
9084 }
9085
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009086 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009087}
9088
Yang Zhang8d146952013-01-25 10:18:50 +08009089static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
9090{
9091 u32 sec_exec_control;
9092
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009093 /* Postpone execution until vmcs01 is the current VMCS. */
9094 if (is_guest_mode(vcpu)) {
9095 to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
9096 return;
9097 }
9098
Wanpeng Lif6e90f92016-09-22 07:43:25 +08009099 if (!cpu_has_vmx_virtualize_x2apic_mode())
Yang Zhang8d146952013-01-25 10:18:50 +08009100 return;
9101
Paolo Bonzini35754c92015-07-29 12:05:37 +02009102 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08009103 return;
9104
9105 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9106
9107 if (set) {
9108 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9109 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9110 } else {
9111 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9112 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009113 vmx_flush_tlb_ept_only(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009114 }
9115 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9116
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009117 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009118}
9119
Tang Chen38b99172014-09-24 15:57:54 +08009120static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9121{
9122 struct vcpu_vmx *vmx = to_vmx(vcpu);
9123
9124 /*
9125 * Currently we do not handle the nested case where L2 has an
9126 * APIC access page of its own; that page is still pinned.
9127 * Hence, we skip the case where the VCPU is in guest mode _and_
9128 * L1 prepared an APIC access page for L2.
9129 *
9130 * For the case where L1 and L2 share the same APIC access page
9131 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
9132 * in the vmcs12), this function will only update either the vmcs01
9133 * or the vmcs02. If the former, the vmcs02 will be updated by
9134 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
9135 * the next L2->L1 exit.
9136 */
9137 if (!is_guest_mode(vcpu) ||
David Matlack4f2777b2016-07-13 17:16:37 -07009138 !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009139 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Tang Chen38b99172014-09-24 15:57:54 +08009140 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009141 vmx_flush_tlb_ept_only(vcpu);
9142 }
Tang Chen38b99172014-09-24 15:57:54 +08009143}
9144
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009145static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009146{
9147 u16 status;
9148 u8 old;
9149
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009150 if (max_isr == -1)
9151 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009152
9153 status = vmcs_read16(GUEST_INTR_STATUS);
9154 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009155 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08009156 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009157 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009158 vmcs_write16(GUEST_INTR_STATUS, status);
9159 }
9160}
9161
9162static void vmx_set_rvi(int vector)
9163{
9164 u16 status;
9165 u8 old;
9166
Wei Wang4114c272014-11-05 10:53:43 +08009167 if (vector == -1)
9168 vector = 0;
9169
Yang Zhangc7c9c562013-01-25 10:18:51 +08009170 status = vmcs_read16(GUEST_INTR_STATUS);
9171 old = (u8)status & 0xff;
9172 if ((u8)vector != old) {
9173 status &= ~0xff;
9174 status |= (u8)vector;
9175 vmcs_write16(GUEST_INTR_STATUS, status);
9176 }
9177}
9178
9179static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9180{
Liran Alon851c1a182017-12-24 18:12:56 +02009181 /*
9182 * When running L2, updating RVI is only relevant when
9183 * vmcs12 virtual-interrupt-delivery enabled.
9184 * However, it can be enabled only when L1 also
9185 * intercepts external-interrupts and in that case
9186 * we should not update vmcs02 RVI but instead intercept
9187 * interrupt. Therefore, do nothing when running L2.
9188 */
9189 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08009190 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009191}
9192
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009193static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009194{
9195 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009196 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02009197 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009198
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009199 WARN_ON(!vcpu->arch.apicv_active);
9200 if (pi_test_on(&vmx->pi_desc)) {
9201 pi_clear_on(&vmx->pi_desc);
9202 /*
9203 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9204 * But on x86 this is just a compiler barrier anyway.
9205 */
9206 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02009207 max_irr_updated =
9208 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
9209
9210 /*
9211 * If we are running L2 and L1 has a new pending interrupt
9212 * which can be injected, we should re-evaluate
9213 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02009214 * If L1 intercepts external-interrupts, we should
9215 * exit from L2 to L1. Otherwise, interrupt should be
9216 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02009217 */
Liran Alon851c1a182017-12-24 18:12:56 +02009218 if (is_guest_mode(vcpu) && max_irr_updated) {
9219 if (nested_exit_on_intr(vcpu))
9220 kvm_vcpu_exiting_guest_mode(vcpu);
9221 else
9222 kvm_make_request(KVM_REQ_EVENT, vcpu);
9223 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009224 } else {
9225 max_irr = kvm_lapic_find_highest_irr(vcpu);
9226 }
9227 vmx_hwapic_irr_update(vcpu, max_irr);
9228 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009229}
9230
Andrey Smetanin63086302015-11-10 15:36:32 +03009231static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009232{
Andrey Smetanind62caab2015-11-10 15:36:33 +03009233 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08009234 return;
9235
Yang Zhangc7c9c562013-01-25 10:18:51 +08009236 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9237 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9238 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9239 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9240}
9241
Paolo Bonzini967235d2016-12-19 14:03:45 +01009242static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9243{
9244 struct vcpu_vmx *vmx = to_vmx(vcpu);
9245
9246 pi_clear_on(&vmx->pi_desc);
9247 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9248}
9249
Avi Kivity51aa01d2010-07-20 14:31:20 +03009250static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03009251{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009252 u32 exit_intr_info = 0;
9253 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02009254
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009255 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9256 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02009257 return;
9258
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009259 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9260 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9261 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08009262
Wanpeng Li1261bfa2017-07-13 18:30:40 -07009263 /* if exit due to PF check for async PF */
9264 if (is_page_fault(exit_intr_info))
9265 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9266
Andi Kleena0861c02009-06-08 17:37:09 +08009267 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009268 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9269 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08009270 kvm_machine_check();
9271
Gleb Natapov20f65982009-05-11 13:35:55 +03009272 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08009273 if (is_nmi(exit_intr_info)) {
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009274 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03009275 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009276 kvm_after_handle_nmi(&vmx->vcpu);
9277 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03009278}
Gleb Natapov20f65982009-05-11 13:35:55 +03009279
Yang Zhanga547c6d2013-04-11 19:25:10 +08009280static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9281{
9282 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9283
Yang Zhanga547c6d2013-04-11 19:25:10 +08009284 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9285 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9286 unsigned int vector;
9287 unsigned long entry;
9288 gate_desc *desc;
9289 struct vcpu_vmx *vmx = to_vmx(vcpu);
9290#ifdef CONFIG_X86_64
9291 unsigned long tmp;
9292#endif
9293
9294 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9295 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02009296 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009297 asm volatile(
9298#ifdef CONFIG_X86_64
9299 "mov %%" _ASM_SP ", %[sp]\n\t"
9300 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
9301 "push $%c[ss]\n\t"
9302 "push %[sp]\n\t"
9303#endif
9304 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08009305 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009306 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08009307 :
9308#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06009309 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009310#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -05009311 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +08009312 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009313 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009314 [ss]"i"(__KERNEL_DS),
9315 [cs]"i"(__KERNEL_CS)
9316 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02009317 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08009318}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009319STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009320
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009321static bool vmx_has_high_real_mode_segbase(void)
9322{
9323 return enable_unrestricted_guest || emulate_invalid_guest_state;
9324}
9325
Liu, Jinsongda8999d2014-02-24 10:55:46 +00009326static bool vmx_mpx_supported(void)
9327{
9328 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
9329 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
9330}
9331
Wanpeng Li55412b22014-12-02 19:21:30 +08009332static bool vmx_xsaves_supported(void)
9333{
9334 return vmcs_config.cpu_based_2nd_exec_ctrl &
9335 SECONDARY_EXEC_XSAVES;
9336}
9337
Paolo Bonzini66336ca2016-07-12 10:36:41 +02009338static bool vmx_umip_emulated(void)
9339{
Paolo Bonzini0367f202016-07-12 10:44:55 +02009340 return vmcs_config.cpu_based_2nd_exec_ctrl &
9341 SECONDARY_EXEC_DESC;
Paolo Bonzini66336ca2016-07-12 10:36:41 +02009342}
9343
Avi Kivity51aa01d2010-07-20 14:31:20 +03009344static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9345{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02009346 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03009347 bool unblock_nmi;
9348 u8 vector;
9349 bool idtv_info_valid;
9350
9351 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03009352
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009353 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009354 if (vmx->loaded_vmcs->nmi_known_unmasked)
9355 return;
9356 /*
9357 * Can't use vmx->exit_intr_info since we're not sure what
9358 * the exit reason is.
9359 */
9360 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9361 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9362 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9363 /*
9364 * SDM 3: 27.7.1.2 (September 2008)
9365 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9366 * a guest IRET fault.
9367 * SDM 3: 23.2.2 (September 2008)
9368 * Bit 12 is undefined in any of the following cases:
9369 * If the VM exit sets the valid bit in the IDT-vectoring
9370 * information field.
9371 * If the VM exit is due to a double fault.
9372 */
9373 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9374 vector != DF_VECTOR && !idtv_info_valid)
9375 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9376 GUEST_INTR_STATE_NMI);
9377 else
9378 vmx->loaded_vmcs->nmi_known_unmasked =
9379 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9380 & GUEST_INTR_STATE_NMI);
9381 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
9382 vmx->loaded_vmcs->vnmi_blocked_time +=
9383 ktime_to_ns(ktime_sub(ktime_get(),
9384 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03009385}
9386
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009387static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03009388 u32 idt_vectoring_info,
9389 int instr_len_field,
9390 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03009391{
Avi Kivity51aa01d2010-07-20 14:31:20 +03009392 u8 vector;
9393 int type;
9394 bool idtv_info_valid;
9395
9396 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03009397
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009398 vcpu->arch.nmi_injected = false;
9399 kvm_clear_exception_queue(vcpu);
9400 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009401
9402 if (!idtv_info_valid)
9403 return;
9404
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009405 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03009406
Avi Kivity668f6122008-07-02 09:28:55 +03009407 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
9408 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009409
Gleb Natapov64a7ec02009-03-30 16:03:29 +03009410 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03009411 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009412 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03009413 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009414 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03009415 * Clear bit "block by NMI" before VM entry if a NMI
9416 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03009417 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009418 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009419 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009420 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009421 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009422 /* fall through */
9423 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03009424 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03009425 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03009426 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03009427 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03009428 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009429 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009430 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009431 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009432 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03009433 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009434 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009435 break;
9436 default:
9437 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03009438 }
Avi Kivitycf393f72008-07-01 16:20:21 +03009439}
9440
Avi Kivity83422e12010-07-20 14:43:23 +03009441static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
9442{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009443 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03009444 VM_EXIT_INSTRUCTION_LEN,
9445 IDT_VECTORING_ERROR_CODE);
9446}
9447
Avi Kivityb463a6f2010-07-20 15:06:17 +03009448static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
9449{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009450 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03009451 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9452 VM_ENTRY_INSTRUCTION_LEN,
9453 VM_ENTRY_EXCEPTION_ERROR_CODE);
9454
9455 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
9456}
9457
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009458static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
9459{
9460 int i, nr_msrs;
9461 struct perf_guest_switch_msr *msrs;
9462
9463 msrs = perf_guest_get_msrs(&nr_msrs);
9464
9465 if (!msrs)
9466 return;
9467
9468 for (i = 0; i < nr_msrs; i++)
9469 if (msrs[i].host == msrs[i].guest)
9470 clear_atomic_switch_msr(vmx, msrs[i].msr);
9471 else
9472 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
9473 msrs[i].host);
9474}
9475
Jiang Biao33365e72016-11-03 15:03:37 +08009476static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07009477{
9478 struct vcpu_vmx *vmx = to_vmx(vcpu);
9479 u64 tscl;
9480 u32 delta_tsc;
9481
9482 if (vmx->hv_deadline_tsc == -1)
9483 return;
9484
9485 tscl = rdtsc();
9486 if (vmx->hv_deadline_tsc > tscl)
9487 /* sure to be 32 bit only because checked on set_hv_timer */
9488 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
9489 cpu_preemption_timer_multi);
9490 else
9491 delta_tsc = 0;
9492
9493 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
9494}
9495
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08009496static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009497{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009498 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Li74c55932017-11-29 01:31:20 -08009499 unsigned long cr3, cr4;
Avi Kivity104f2262010-11-18 13:12:52 +02009500
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009501 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009502 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009503 vmx->loaded_vmcs->soft_vnmi_blocked))
9504 vmx->loaded_vmcs->entry_time = ktime_get();
9505
Avi Kivity104f2262010-11-18 13:12:52 +02009506 /* Don't enter VMX if guest state is invalid, let the exit handler
9507 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02009508 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02009509 return;
9510
Radim Krčmářa7653ec2014-08-21 18:08:07 +02009511 if (vmx->ple_window_dirty) {
9512 vmx->ple_window_dirty = false;
9513 vmcs_write32(PLE_WINDOW, vmx->ple_window);
9514 }
9515
Abel Gordon012f83c2013-04-18 14:39:25 +03009516 if (vmx->nested.sync_shadow_vmcs) {
9517 copy_vmcs12_to_shadow(vmx);
9518 vmx->nested.sync_shadow_vmcs = false;
9519 }
9520
Avi Kivity104f2262010-11-18 13:12:52 +02009521 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
9522 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
9523 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
9524 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
9525
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009526 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +02009527 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009528 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +02009529 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009530 }
9531
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07009532 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +02009533 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009534 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +02009535 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009536 }
9537
Avi Kivity104f2262010-11-18 13:12:52 +02009538 /* When single-stepping over STI and MOV SS, we must clear the
9539 * corresponding interruptibility bits in the guest state. Otherwise
9540 * vmentry fails as it then expects bit 14 (BS) in pending debug
9541 * exceptions being set, but that's not correct for the guest debugging
9542 * case. */
9543 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9544 vmx_set_interrupt_shadow(vcpu, 0);
9545
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02009546 if (static_cpu_has(X86_FEATURE_PKU) &&
9547 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
9548 vcpu->arch.pkru != vmx->host_pkru)
9549 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009550
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009551 atomic_switch_perf_msrs(vmx);
9552
Yunhong Jiang64672c92016-06-13 14:19:59 -07009553 vmx_arm_hv_timer(vcpu);
9554
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009555 /*
9556 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
9557 * it's non-zero. Since vmentry is serialising on affected CPUs, there
9558 * is no need to worry about the conditional branch over the wrmsr
9559 * being speculatively taken.
9560 */
9561 if (vmx->spec_ctrl)
Paolo Bonziniecb586b2018-02-22 16:43:17 +01009562 native_wrmsrl(MSR_IA32_SPEC_CTRL, vmx->spec_ctrl);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009563
Nadav Har'Eld462b812011-05-24 15:26:10 +03009564 vmx->__launched = vmx->loaded_vmcs->launched;
Avi Kivity104f2262010-11-18 13:12:52 +02009565 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08009566 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009567 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
9568 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
9569 "push %%" _ASM_CX " \n\t"
9570 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03009571 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009572 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009573 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03009574 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009575 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009576 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
9577 "mov %%cr2, %%" _ASM_DX " \n\t"
9578 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009579 "je 2f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009580 "mov %%" _ASM_AX", %%cr2 \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009581 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009582 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02009583 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009584 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009585 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
9586 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
9587 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
9588 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
9589 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
9590 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009591#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009592 "mov %c[r8](%0), %%r8 \n\t"
9593 "mov %c[r9](%0), %%r9 \n\t"
9594 "mov %c[r10](%0), %%r10 \n\t"
9595 "mov %c[r11](%0), %%r11 \n\t"
9596 "mov %c[r12](%0), %%r12 \n\t"
9597 "mov %c[r13](%0), %%r13 \n\t"
9598 "mov %c[r14](%0), %%r14 \n\t"
9599 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009600#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009601 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03009602
Avi Kivity6aa8b732006-12-10 02:21:36 -08009603 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03009604 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009605 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009606 "jmp 2f \n\t"
9607 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
9608 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08009609 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009610 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02009611 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -08009612 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009613 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
9614 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
9615 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
9616 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
9617 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
9618 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
9619 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009620#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009621 "mov %%r8, %c[r8](%0) \n\t"
9622 "mov %%r9, %c[r9](%0) \n\t"
9623 "mov %%r10, %c[r10](%0) \n\t"
9624 "mov %%r11, %c[r11](%0) \n\t"
9625 "mov %%r12, %c[r12](%0) \n\t"
9626 "mov %%r13, %c[r13](%0) \n\t"
9627 "mov %%r14, %c[r14](%0) \n\t"
9628 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -08009629 "xor %%r8d, %%r8d \n\t"
9630 "xor %%r9d, %%r9d \n\t"
9631 "xor %%r10d, %%r10d \n\t"
9632 "xor %%r11d, %%r11d \n\t"
9633 "xor %%r12d, %%r12d \n\t"
9634 "xor %%r13d, %%r13d \n\t"
9635 "xor %%r14d, %%r14d \n\t"
9636 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009637#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009638 "mov %%cr2, %%" _ASM_AX " \n\t"
9639 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03009640
Jim Mattson0cb5b302018-01-03 14:31:38 -08009641 "xor %%eax, %%eax \n\t"
9642 "xor %%ebx, %%ebx \n\t"
9643 "xor %%esi, %%esi \n\t"
9644 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009645 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009646 ".pushsection .rodata \n\t"
9647 ".global vmx_return \n\t"
9648 "vmx_return: " _ASM_PTR " 2b \n\t"
9649 ".popsection"
Avi Kivitye08aa782007-11-15 18:06:18 +02009650 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03009651 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02009652 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +03009653 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009654 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
9655 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
9656 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
9657 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
9658 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
9659 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
9660 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009661#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009662 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
9663 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
9664 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
9665 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
9666 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
9667 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
9668 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
9669 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08009670#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02009671 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
9672 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02009673 : "cc", "memory"
9674#ifdef CONFIG_X86_64
Avi Kivityb188c81f2012-09-16 15:10:58 +03009675 , "rax", "rbx", "rdi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009676 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009677#else
9678 , "eax", "ebx", "edi", "esi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009679#endif
9680 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08009681
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009682 /*
9683 * We do not use IBRS in the kernel. If this vCPU has used the
9684 * SPEC_CTRL MSR it may have left it on; save the value and
9685 * turn it off. This is much more efficient than blindly adding
9686 * it to the atomic save/restore list. Especially as the former
9687 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
9688 *
9689 * For non-nested case:
9690 * If the L01 MSR bitmap does not intercept the MSR, then we need to
9691 * save it.
9692 *
9693 * For nested case:
9694 * If the L02 MSR bitmap does not intercept the MSR, then we need to
9695 * save it.
9696 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +01009697 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +01009698 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009699
9700 if (vmx->spec_ctrl)
Paolo Bonziniecb586b2018-02-22 16:43:17 +01009701 native_wrmsrl(MSR_IA32_SPEC_CTRL, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009702
David Woodhouse117cc7a2018-01-12 11:11:27 +00009703 /* Eliminate branch target predictions from guest mode */
9704 vmexit_fill_RSB();
9705
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009706 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -08009707 if (vmx->host_debugctlmsr)
9708 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009709
Avi Kivityaa67f602012-08-01 16:48:03 +03009710#ifndef CONFIG_X86_64
9711 /*
9712 * The sysexit path does not restore ds/es, so we must set them to
9713 * a reasonable value ourselves.
9714 *
9715 * We can't defer this to vmx_load_host_state() since that function
9716 * may be executed in interrupt context, which saves and restore segments
9717 * around it, nullifying its effect.
9718 */
9719 loadsegment(ds, __USER_DS);
9720 loadsegment(es, __USER_DS);
9721#endif
9722
Avi Kivity6de4f3a2009-05-31 22:58:47 +03009723 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02009724 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009725 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03009726 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009727 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009728 vcpu->arch.regs_dirty = 0;
9729
Gleb Natapove0b890d2013-09-25 12:51:33 +03009730 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009731 * eager fpu is enabled if PKEY is supported and CR4 is switched
9732 * back on host, so it is safe to read guest PKRU from current
9733 * XSAVE.
9734 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02009735 if (static_cpu_has(X86_FEATURE_PKU) &&
9736 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
9737 vcpu->arch.pkru = __read_pkru();
9738 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009739 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009740 }
9741
9742 /*
Gleb Natapove0b890d2013-09-25 12:51:33 +03009743 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
9744 * we did not inject a still-pending event to L1 now because of
9745 * nested_run_pending, we need to re-enable this bit.
9746 */
9747 if (vmx->nested.nested_run_pending)
9748 kvm_make_request(KVM_REQ_EVENT, vcpu);
9749
9750 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -07009751 vmx->idt_vectoring_info = 0;
9752
9753 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
9754 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9755 return;
9756
9757 vmx->loaded_vmcs->launched = 1;
9758 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +03009759
Avi Kivity51aa01d2010-07-20 14:31:20 +03009760 vmx_complete_atomic_exit(vmx);
9761 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03009762 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009763}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009764STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009765
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009766static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009767{
9768 struct vcpu_vmx *vmx = to_vmx(vcpu);
9769 int cpu;
9770
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009771 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009772 return;
9773
9774 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009775 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009776 vmx_vcpu_put(vcpu);
9777 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009778 put_cpu();
9779}
9780
Jim Mattson2f1fe812016-07-08 15:36:06 -07009781/*
9782 * Ensure that the current vmcs of the logical processor is the
9783 * vmcs01 of the vcpu before calling free_nested().
9784 */
9785static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9786{
9787 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009788
Christoffer Dallec7660c2017-12-04 21:35:23 +01009789 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009790 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009791 free_nested(vmx);
9792 vcpu_put(vcpu);
9793}
9794
Avi Kivity6aa8b732006-12-10 02:21:36 -08009795static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9796{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009797 struct vcpu_vmx *vmx = to_vmx(vcpu);
9798
Kai Huang843e4332015-01-28 10:54:28 +08009799 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08009800 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08009801 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009802 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009803 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009804 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009805 kfree(vmx->guest_msrs);
9806 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10009807 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009808}
9809
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009810static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009811{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009812 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10009813 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009814 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +03009815 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009816
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009817 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009818 return ERR_PTR(-ENOMEM);
9819
Wanpeng Li991e7a02015-09-16 17:30:05 +08009820 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +08009821
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009822 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
9823 if (err)
9824 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009825
Peter Feiner4e595162016-07-07 14:49:58 -07009826 err = -ENOMEM;
9827
9828 /*
9829 * If PML is turned on, failure on enabling PML just results in failure
9830 * of creating the vcpu, therefore we can simplify PML logic (by
9831 * avoiding dealing with cases, such as enabling PML partially on vcpus
9832 * for the guest, etc.
9833 */
9834 if (enable_pml) {
9835 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
9836 if (!vmx->pml_pg)
9837 goto uninit_vcpu;
9838 }
9839
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009840 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +02009841 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
9842 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +03009843
Peter Feiner4e595162016-07-07 14:49:58 -07009844 if (!vmx->guest_msrs)
9845 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009846
Paolo Bonzinif21f1652018-01-11 12:16:15 +01009847 err = alloc_loaded_vmcs(&vmx->vmcs01);
9848 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009849 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009850
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009851 msr_bitmap = vmx->vmcs01.msr_bitmap;
9852 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
9853 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
9854 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
9855 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
9856 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
9857 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
9858 vmx->msr_bitmap_mode = 0;
9859
Paolo Bonzinif21f1652018-01-11 12:16:15 +01009860 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +03009861 cpu = get_cpu();
9862 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10009863 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +02009864 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009865 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03009866 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +02009867 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02009868 err = alloc_apic_access_page(kvm);
9869 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02009870 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02009871 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08009872
Sean Christophersone90008d2018-03-05 12:04:37 -08009873 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +08009874 err = init_rmode_identity_map(kvm);
9875 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02009876 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08009877 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08009878
Wanpeng Li5c614b32015-10-13 09:18:36 -07009879 if (nested) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009880 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
9881 kvm_vcpu_apicv_active(&vmx->vcpu));
Wanpeng Li5c614b32015-10-13 09:18:36 -07009882 vmx->nested.vpid02 = allocate_vpid();
9883 }
Wincy Vanb9c237b2015-02-03 23:56:30 +08009884
Wincy Van705699a2015-02-03 23:58:17 +08009885 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009886 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009887
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009888 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
9889
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02009890 /*
9891 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
9892 * or POSTED_INTR_WAKEUP_VECTOR.
9893 */
9894 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
9895 vmx->pi_desc.sn = 1;
9896
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009897 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009898
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009899free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -07009900 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08009901 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009902free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009903 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -07009904free_pml:
9905 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009906uninit_vcpu:
9907 kvm_vcpu_uninit(&vmx->vcpu);
9908free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +08009909 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +10009910 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009911 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009912}
9913
Wanpeng Lib31c1142018-03-12 04:53:04 -07009914static int vmx_vm_init(struct kvm *kvm)
9915{
9916 if (!ple_gap)
9917 kvm->arch.pause_in_guest = true;
9918 return 0;
9919}
9920
Yang, Sheng002c7f72007-07-31 14:23:01 +03009921static void __init vmx_check_processor_compat(void *rtn)
9922{
9923 struct vmcs_config vmcs_conf;
9924
9925 *(int *)rtn = 0;
9926 if (setup_vmcs_config(&vmcs_conf) < 0)
9927 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +01009928 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +03009929 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
9930 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
9931 smp_processor_id());
9932 *(int *)rtn = -EIO;
9933 }
9934}
9935
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009936static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08009937{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009938 u8 cache;
9939 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009940
Sheng Yang522c68c2009-04-27 20:35:43 +08009941 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +02009942 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +08009943 * 2. EPT with VT-d:
9944 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +02009945 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +08009946 * b. VT-d with snooping control feature: snooping control feature of
9947 * VT-d engine can guarantee the cache correctness. Just set it
9948 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08009949 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08009950 * consistent with host MTRR
9951 */
Paolo Bonzini606decd2015-10-01 13:12:47 +02009952 if (is_mmio) {
9953 cache = MTRR_TYPE_UNCACHABLE;
9954 goto exit;
9955 }
9956
9957 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009958 ipat = VMX_EPT_IPAT_BIT;
9959 cache = MTRR_TYPE_WRBACK;
9960 goto exit;
9961 }
9962
9963 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
9964 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02009965 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08009966 cache = MTRR_TYPE_WRBACK;
9967 else
9968 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009969 goto exit;
9970 }
9971
Xiao Guangrongff536042015-06-15 16:55:22 +08009972 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009973
9974exit:
9975 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08009976}
9977
Sheng Yang17cc3932010-01-05 19:02:27 +08009978static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02009979{
Sheng Yang878403b2010-01-05 19:02:29 +08009980 if (enable_ept && !cpu_has_vmx_ept_1g_page())
9981 return PT_DIRECTORY_LEVEL;
9982 else
9983 /* For shadow and EPT supported 1GB page */
9984 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02009985}
9986
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009987static void vmcs_set_secondary_exec_control(u32 new_ctl)
9988{
9989 /*
9990 * These bits in the secondary execution controls field
9991 * are dynamic, the others are mostly based on the hypervisor
9992 * architecture and the guest's CPUID. Do not touch the
9993 * dynamic bits.
9994 */
9995 u32 mask =
9996 SECONDARY_EXEC_SHADOW_VMCS |
9997 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +02009998 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9999 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010000
10001 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10002
10003 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10004 (new_ctl & ~mask) | (cur_ctl & mask));
10005}
10006
David Matlack8322ebb2016-11-29 18:14:09 -080010007/*
10008 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10009 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10010 */
10011static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10012{
10013 struct vcpu_vmx *vmx = to_vmx(vcpu);
10014 struct kvm_cpuid_entry2 *entry;
10015
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010016 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
10017 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080010018
10019#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10020 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010021 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080010022} while (0)
10023
10024 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10025 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10026 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10027 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10028 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10029 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10030 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10031 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10032 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10033 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10034 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10035 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10036 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10037 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10038 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10039
10040 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10041 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10042 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10043 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10044 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010010045 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080010046
10047#undef cr4_fixed1_update
10048}
10049
Sheng Yang0e851882009-12-18 16:48:46 +080010050static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10051{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010052 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010053
Paolo Bonzini80154d72017-08-24 13:55:35 +020010054 if (cpu_has_secondary_exec_ctrls()) {
10055 vmx_compute_secondary_exec_control(vmx);
10056 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010057 }
Mao, Junjiead756a12012-07-02 01:18:48 +000010058
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010059 if (nested_vmx_allowed(vcpu))
10060 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10061 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10062 else
10063 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10064 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080010065
10066 if (nested_vmx_allowed(vcpu))
10067 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +080010068}
10069
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010070static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10071{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030010072 if (func == 1 && nested)
10073 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010074}
10075
Yang Zhang25d92082013-08-06 12:00:32 +030010076static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10077 struct x86_exception *fault)
10078{
Jan Kiszka533558b2014-01-04 18:47:20 +010010079 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040010080 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010081 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010082 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030010083
Bandan Dasc5f983f2017-05-05 15:25:14 -040010084 if (vmx->nested.pml_full) {
10085 exit_reason = EXIT_REASON_PML_FULL;
10086 vmx->nested.pml_full = false;
10087 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10088 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010010089 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030010090 else
Jan Kiszka533558b2014-01-04 18:47:20 +010010091 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010092
10093 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030010094 vmcs12->guest_physical_address = fault->address;
10095}
10096
Peter Feiner995f00a2017-06-30 17:26:32 -070010097static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10098{
David Hildenbrandbb97a012017-08-10 23:15:28 +020010099 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070010100}
10101
Nadav Har'El155a97a2013-08-05 11:07:16 +030010102/* Callbacks for nested_ept_init_mmu_context: */
10103
10104static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10105{
10106 /* return the page table to be shadowed - in our case, EPT12 */
10107 return get_vmcs12(vcpu)->ept_pointer;
10108}
10109
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010110static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030010111{
Paolo Bonziniad896af2013-10-02 16:56:14 +020010112 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020010113 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010114 return 1;
10115
10116 kvm_mmu_unload(vcpu);
Paolo Bonziniad896af2013-10-02 16:56:14 +020010117 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010118 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010119 VMX_EPT_EXECUTE_ONLY_BIT,
David Hildenbranda057e0e2017-08-10 23:36:54 +020010120 nested_ept_ad_enabled(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030010121 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10122 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10123 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10124
10125 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010126 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030010127}
10128
10129static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10130{
10131 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10132}
10133
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030010134static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10135 u16 error_code)
10136{
10137 bool inequality, bit;
10138
10139 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10140 inequality =
10141 (error_code & vmcs12->page_fault_error_code_mask) !=
10142 vmcs12->page_fault_error_code_match;
10143 return inequality ^ bit;
10144}
10145
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010146static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10147 struct x86_exception *fault)
10148{
10149 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10150
10151 WARN_ON(!is_guest_mode(vcpu));
10152
Wanpeng Li305d0ab2017-09-28 18:16:44 -070010153 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10154 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020010155 vmcs12->vm_exit_intr_error_code = fault->error_code;
10156 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10157 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10158 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10159 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010160 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010161 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010162 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010163}
10164
Paolo Bonzinic9923842017-12-13 14:16:30 +010010165static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10166 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010167
10168static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010169 struct vmcs12 *vmcs12)
10170{
10171 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010172 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010173 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010174
10175 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010176 /*
10177 * Translate L1 physical address to host physical
10178 * address for vmcs02. Keep the page pinned, so this
10179 * physical address remains valid. We keep a reference
10180 * to it so we can release it later.
10181 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010182 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010183 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010184 vmx->nested.apic_access_page = NULL;
10185 }
10186 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010187 /*
10188 * If translation failed, no matter: This feature asks
10189 * to exit when accessing the given address, and if it
10190 * can never be accessed, this feature won't do
10191 * anything anyway.
10192 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010193 if (!is_error_page(page)) {
10194 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010195 hpa = page_to_phys(vmx->nested.apic_access_page);
10196 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10197 } else {
10198 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
10199 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10200 }
10201 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
10202 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
10203 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
10204 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10205 kvm_vcpu_reload_apic_access_page(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010206 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010207
10208 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010209 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010210 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010211 vmx->nested.virtual_apic_page = NULL;
10212 }
10213 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010214
10215 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010216 * If translation failed, VM entry will fail because
10217 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10218 * Failing the vm entry is _not_ what the processor
10219 * does but it's basically the only possibility we
10220 * have. We could still enter the guest if CR8 load
10221 * exits are enabled, CR8 store exits are enabled, and
10222 * virtualize APIC access is disabled; in this case
10223 * the processor would never use the TPR shadow and we
10224 * could simply clear the bit from the execution
10225 * control. But such a configuration is useless, so
10226 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010227 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010228 if (!is_error_page(page)) {
10229 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010230 hpa = page_to_phys(vmx->nested.virtual_apic_page);
10231 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
10232 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010233 }
10234
Wincy Van705699a2015-02-03 23:58:17 +080010235 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010236 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
10237 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010238 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010239 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080010240 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010241 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
10242 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010243 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010244 vmx->nested.pi_desc_page = page;
10245 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080010246 vmx->nested.pi_desc =
10247 (struct pi_desc *)((void *)vmx->nested.pi_desc +
10248 (unsigned long)(vmcs12->posted_intr_desc_addr &
10249 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010250 vmcs_write64(POSTED_INTR_DESC_ADDR,
10251 page_to_phys(vmx->nested.pi_desc_page) +
10252 (unsigned long)(vmcs12->posted_intr_desc_addr &
10253 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080010254 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080010255 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000010256 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
10257 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010258 else
10259 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
10260 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010261}
10262
Jan Kiszkaf41245002014-03-07 20:03:13 +010010263static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
10264{
10265 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
10266 struct vcpu_vmx *vmx = to_vmx(vcpu);
10267
10268 if (vcpu->arch.virtual_tsc_khz == 0)
10269 return;
10270
10271 /* Make sure short timeouts reliably trigger an immediate vmexit.
10272 * hrtimer_start does not guarantee this. */
10273 if (preemption_timeout <= 1) {
10274 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
10275 return;
10276 }
10277
10278 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10279 preemption_timeout *= 1000000;
10280 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
10281 hrtimer_start(&vmx->nested.preemption_timer,
10282 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
10283}
10284
Jim Mattson56a20512017-07-06 16:33:06 -070010285static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
10286 struct vmcs12 *vmcs12)
10287{
10288 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
10289 return 0;
10290
10291 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
10292 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
10293 return -EINVAL;
10294
10295 return 0;
10296}
10297
Wincy Van3af18d92015-02-03 23:49:31 +080010298static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
10299 struct vmcs12 *vmcs12)
10300{
Wincy Van3af18d92015-02-03 23:49:31 +080010301 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10302 return 0;
10303
Jim Mattson5fa99cb2017-07-06 16:33:07 -070010304 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080010305 return -EINVAL;
10306
10307 return 0;
10308}
10309
Jim Mattson712b12d2017-08-24 13:24:47 -070010310static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
10311 struct vmcs12 *vmcs12)
10312{
10313 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10314 return 0;
10315
10316 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
10317 return -EINVAL;
10318
10319 return 0;
10320}
10321
Wincy Van3af18d92015-02-03 23:49:31 +080010322/*
10323 * Merge L0's and L1's MSR bitmap, return false to indicate that
10324 * we do not use the hardware.
10325 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010010326static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10327 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080010328{
Wincy Van82f0dd42015-02-03 23:57:18 +080010329 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080010330 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020010331 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010332 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010010333 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010334 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010010335 *
10336 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
10337 * ensures that we do not accidentally generate an L02 MSR bitmap
10338 * from the L12 MSR bitmap that is too permissive.
10339 * 2. That L1 or L2s have actually used the MSR. This avoids
10340 * unnecessarily merging of the bitmap if the MSR is unused. This
10341 * works properly because we only update the L01 MSR bitmap lazily.
10342 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
10343 * updated to reflect this when L1 (or its L2s) actually write to
10344 * the MSR.
10345 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000010346 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
10347 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080010348
Paolo Bonzinic9923842017-12-13 14:16:30 +010010349 /* Nothing to do if the MSR bitmap is not in use. */
10350 if (!cpu_has_vmx_msr_bitmap() ||
10351 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10352 return false;
10353
Ashok Raj15d45072018-02-01 22:59:43 +010010354 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010355 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080010356 return false;
10357
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010358 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
10359 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080010360 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010010361
Radim Krčmářd048c092016-08-08 20:16:22 +020010362 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010010363 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
10364 /*
10365 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
10366 * just lets the processor take the value from the virtual-APIC page;
10367 * take those 256 bits directly from the L1 bitmap.
10368 */
10369 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10370 unsigned word = msr / BITS_PER_LONG;
10371 msr_bitmap_l0[word] = msr_bitmap_l1[word];
10372 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080010373 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010010374 } else {
10375 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10376 unsigned word = msr / BITS_PER_LONG;
10377 msr_bitmap_l0[word] = ~0;
10378 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
10379 }
10380 }
10381
10382 nested_vmx_disable_intercept_for_msr(
10383 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010384 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010385 MSR_TYPE_W);
10386
10387 if (nested_cpu_has_vid(vmcs12)) {
10388 nested_vmx_disable_intercept_for_msr(
10389 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010390 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010391 MSR_TYPE_W);
10392 nested_vmx_disable_intercept_for_msr(
10393 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010394 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010395 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080010396 }
Ashok Raj15d45072018-02-01 22:59:43 +010010397
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010398 if (spec_ctrl)
10399 nested_vmx_disable_intercept_for_msr(
10400 msr_bitmap_l1, msr_bitmap_l0,
10401 MSR_IA32_SPEC_CTRL,
10402 MSR_TYPE_R | MSR_TYPE_W);
10403
Ashok Raj15d45072018-02-01 22:59:43 +010010404 if (pred_cmd)
10405 nested_vmx_disable_intercept_for_msr(
10406 msr_bitmap_l1, msr_bitmap_l0,
10407 MSR_IA32_PRED_CMD,
10408 MSR_TYPE_W);
10409
Wincy Vanf2b93282015-02-03 23:56:03 +080010410 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010411 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080010412
10413 return true;
10414}
10415
10416static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
10417 struct vmcs12 *vmcs12)
10418{
Wincy Van82f0dd42015-02-03 23:57:18 +080010419 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080010420 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080010421 !nested_cpu_has_vid(vmcs12) &&
10422 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080010423 return 0;
10424
10425 /*
10426 * If virtualize x2apic mode is enabled,
10427 * virtualize apic access must be disabled.
10428 */
Wincy Van82f0dd42015-02-03 23:57:18 +080010429 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10430 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080010431 return -EINVAL;
10432
Wincy Van608406e2015-02-03 23:57:51 +080010433 /*
10434 * If virtual interrupt delivery is enabled,
10435 * we must exit on external interrupts.
10436 */
10437 if (nested_cpu_has_vid(vmcs12) &&
10438 !nested_exit_on_intr(vcpu))
10439 return -EINVAL;
10440
Wincy Van705699a2015-02-03 23:58:17 +080010441 /*
10442 * bits 15:8 should be zero in posted_intr_nv,
10443 * the descriptor address has been already checked
10444 * in nested_get_vmcs12_pages.
10445 */
10446 if (nested_cpu_has_posted_intr(vmcs12) &&
10447 (!nested_cpu_has_vid(vmcs12) ||
10448 !nested_exit_intr_ack_set(vcpu) ||
10449 vmcs12->posted_intr_nv & 0xff00))
10450 return -EINVAL;
10451
Wincy Vanf2b93282015-02-03 23:56:03 +080010452 /* tpr shadow is needed by all apicv features. */
10453 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10454 return -EINVAL;
10455
10456 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080010457}
10458
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010459static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
10460 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010461 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030010462{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010463 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010464 u64 count, addr;
10465
10466 if (vmcs12_read_any(vcpu, count_field, &count) ||
10467 vmcs12_read_any(vcpu, addr_field, &addr)) {
10468 WARN_ON(1);
10469 return -EINVAL;
10470 }
10471 if (count == 0)
10472 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010473 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010474 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
10475 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010476 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010477 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
10478 addr_field, maxphyaddr, count, addr);
10479 return -EINVAL;
10480 }
10481 return 0;
10482}
10483
10484static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
10485 struct vmcs12 *vmcs12)
10486{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010487 if (vmcs12->vm_exit_msr_load_count == 0 &&
10488 vmcs12->vm_exit_msr_store_count == 0 &&
10489 vmcs12->vm_entry_msr_load_count == 0)
10490 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010491 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010492 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010493 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010494 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010495 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010496 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030010497 return -EINVAL;
10498 return 0;
10499}
10500
Bandan Dasc5f983f2017-05-05 15:25:14 -040010501static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
10502 struct vmcs12 *vmcs12)
10503{
10504 u64 address = vmcs12->pml_address;
10505 int maxphyaddr = cpuid_maxphyaddr(vcpu);
10506
10507 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
10508 if (!nested_cpu_has_ept(vmcs12) ||
10509 !IS_ALIGNED(address, 4096) ||
10510 address >> maxphyaddr)
10511 return -EINVAL;
10512 }
10513
10514 return 0;
10515}
10516
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010517static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
10518 struct vmx_msr_entry *e)
10519{
10520 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020010521 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010522 return -EINVAL;
10523 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
10524 e->index == MSR_IA32_UCODE_REV)
10525 return -EINVAL;
10526 if (e->reserved != 0)
10527 return -EINVAL;
10528 return 0;
10529}
10530
10531static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
10532 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030010533{
10534 if (e->index == MSR_FS_BASE ||
10535 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010536 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
10537 nested_vmx_msr_check_common(vcpu, e))
10538 return -EINVAL;
10539 return 0;
10540}
10541
10542static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
10543 struct vmx_msr_entry *e)
10544{
10545 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
10546 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030010547 return -EINVAL;
10548 return 0;
10549}
10550
10551/*
10552 * Load guest's/host's msr at nested entry/exit.
10553 * return 0 for success, entry index for failure.
10554 */
10555static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10556{
10557 u32 i;
10558 struct vmx_msr_entry e;
10559 struct msr_data msr;
10560
10561 msr.host_initiated = false;
10562 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010563 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
10564 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010565 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010566 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10567 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010568 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010569 }
10570 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010571 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010572 "%s check failed (%u, 0x%x, 0x%x)\n",
10573 __func__, i, e.index, e.reserved);
10574 goto fail;
10575 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010576 msr.index = e.index;
10577 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010578 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010579 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010580 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10581 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030010582 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010583 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010584 }
10585 return 0;
10586fail:
10587 return i + 1;
10588}
10589
10590static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10591{
10592 u32 i;
10593 struct vmx_msr_entry e;
10594
10595 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010596 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010597 if (kvm_vcpu_read_guest(vcpu,
10598 gpa + i * sizeof(e),
10599 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010600 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010601 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10602 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010603 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010604 }
10605 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010606 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010607 "%s check failed (%u, 0x%x, 0x%x)\n",
10608 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030010609 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010610 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010611 msr_info.host_initiated = false;
10612 msr_info.index = e.index;
10613 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010614 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010615 "%s cannot read MSR (%u, 0x%x)\n",
10616 __func__, i, e.index);
10617 return -EINVAL;
10618 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010619 if (kvm_vcpu_write_guest(vcpu,
10620 gpa + i * sizeof(e) +
10621 offsetof(struct vmx_msr_entry, value),
10622 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010623 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010624 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010625 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010626 return -EINVAL;
10627 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010628 }
10629 return 0;
10630}
10631
Ladi Prosek1dc35da2016-11-30 16:03:11 +010010632static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
10633{
10634 unsigned long invalid_mask;
10635
10636 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
10637 return (val & invalid_mask) == 0;
10638}
10639
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010640/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010641 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
10642 * emulating VM entry into a guest with EPT enabled.
10643 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10644 * is assigned to entry_failure_code on failure.
10645 */
10646static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080010647 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010648{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010649 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010010650 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010651 *entry_failure_code = ENTRY_FAIL_DEFAULT;
10652 return 1;
10653 }
10654
10655 /*
10656 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
10657 * must not be dereferenced.
10658 */
10659 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
10660 !nested_ept) {
10661 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
10662 *entry_failure_code = ENTRY_FAIL_PDPTE;
10663 return 1;
10664 }
10665 }
10666
10667 vcpu->arch.cr3 = cr3;
10668 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
10669 }
10670
10671 kvm_mmu_reset_context(vcpu);
10672 return 0;
10673}
10674
Paolo Bonzini74a497f2017-12-20 13:55:39 +010010675static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10676 bool from_vmentry)
10677{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010010678 struct vcpu_vmx *vmx = to_vmx(vcpu);
10679
10680 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
10681 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
10682 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
10683 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
10684 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
10685 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
10686 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
10687 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
10688 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
10689 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
10690 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
10691 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
10692 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
10693 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
10694 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
10695 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
10696 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
10697 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
10698 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
10699 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
10700 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
10701 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
10702 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
10703 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
10704 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
10705 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
10706 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
10707 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
10708 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
10709 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
10710 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010010711
10712 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
10713 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
10714 vmcs12->guest_pending_dbg_exceptions);
10715 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
10716 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
10717
10718 if (nested_cpu_has_xsaves(vmcs12))
10719 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
10720 vmcs_write64(VMCS_LINK_POINTER, -1ull);
10721
10722 if (cpu_has_vmx_posted_intr())
10723 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
10724
10725 /*
10726 * Whether page-faults are trapped is determined by a combination of
10727 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
10728 * If enable_ept, L0 doesn't care about page faults and we should
10729 * set all of these to L1's desires. However, if !enable_ept, L0 does
10730 * care about (at least some) page faults, and because it is not easy
10731 * (if at all possible?) to merge L0 and L1's desires, we simply ask
10732 * to exit on each and every L2 page fault. This is done by setting
10733 * MASK=MATCH=0 and (see below) EB.PF=1.
10734 * Note that below we don't need special code to set EB.PF beyond the
10735 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
10736 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
10737 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
10738 */
10739 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
10740 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
10741 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
10742 enable_ept ? vmcs12->page_fault_error_code_match : 0);
10743
10744 /* All VMFUNCs are currently emulated through L0 vmexits. */
10745 if (cpu_has_vmx_vmfunc())
10746 vmcs_write64(VM_FUNCTION_CONTROL, 0);
10747
10748 if (cpu_has_vmx_apicv()) {
10749 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
10750 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
10751 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
10752 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
10753 }
10754
10755 /*
10756 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
10757 * Some constant fields are set here by vmx_set_constant_host_state().
10758 * Other fields are different per CPU, and will be set later when
10759 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
10760 */
10761 vmx_set_constant_host_state(vmx);
10762
10763 /*
10764 * Set the MSR load/store lists to match L0's settings.
10765 */
10766 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
10767 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10768 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
10769 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10770 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
10771
10772 set_cr4_guest_host_mask(vmx);
10773
10774 if (vmx_mpx_supported())
10775 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
10776
10777 if (enable_vpid) {
10778 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
10779 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
10780 else
10781 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
10782 }
10783
10784 /*
10785 * L1 may access the L2's PDPTR, so save them to construct vmcs12
10786 */
10787 if (enable_ept) {
10788 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
10789 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
10790 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
10791 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
10792 }
Radim Krčmář80132f42018-02-02 18:26:58 +010010793
10794 if (cpu_has_vmx_msr_bitmap())
10795 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010010796}
10797
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010798/*
10799 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
10800 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080010801 * 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 +030010802 * guest in a way that will both be appropriate to L1's requests, and our
10803 * needs. In addition to modifying the active vmcs (which is vmcs02), this
10804 * function also has additional necessary side-effects, like setting various
10805 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010010806 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10807 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010808 */
Ladi Prosekee146c12016-11-30 16:03:09 +010010809static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattsonca0bde22016-11-30 12:03:46 -080010810 bool from_vmentry, u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010811{
10812 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040010813 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010814
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010010815 /*
10816 * First, the fields that are shadowed. This must be kept in sync
10817 * with vmx_shadow_fields.h.
10818 */
10819
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010820 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010821 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010822 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010823 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
10824 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010010825
10826 /*
10827 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
10828 * HOST_FS_BASE, HOST_GS_BASE.
10829 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010830
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010831 if (from_vmentry &&
10832 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020010833 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
10834 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
10835 } else {
10836 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
10837 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
10838 }
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010839 if (from_vmentry) {
10840 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
10841 vmcs12->vm_entry_intr_info_field);
10842 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
10843 vmcs12->vm_entry_exception_error_code);
10844 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
10845 vmcs12->vm_entry_instruction_len);
10846 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
10847 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070010848 vmx->loaded_vmcs->nmi_known_unmasked =
10849 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010850 } else {
10851 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10852 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030010853 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010854
Jan Kiszkaf41245002014-03-07 20:03:13 +010010855 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080010856
Paolo Bonzini9314006db2016-07-06 13:23:51 +020010857 /* Preemption timer setting is only taken from vmcs01. */
10858 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10859 exec_control |= vmcs_config.pin_based_exec_ctrl;
10860 if (vmx->hv_deadline_tsc == -1)
10861 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10862
10863 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080010864 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010865 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
10866 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010867 } else {
Wincy Van705699a2015-02-03 23:58:17 +080010868 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010869 }
Wincy Van705699a2015-02-03 23:58:17 +080010870
Jan Kiszkaf41245002014-03-07 20:03:13 +010010871 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010872
Jan Kiszkaf41245002014-03-07 20:03:13 +010010873 vmx->nested.preemption_timer_expired = false;
10874 if (nested_cpu_has_preemption_timer(vmcs12))
10875 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010010876
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010877 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020010878 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080010879
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010880 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010881 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020010882 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010010883 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020010884 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010885 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040010886 SECONDARY_EXEC_APIC_REGISTER_VIRT |
10887 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010888 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040010889 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
10890 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
10891 ~SECONDARY_EXEC_ENABLE_PML;
10892 exec_control |= vmcs12_exec_ctrl;
10893 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010894
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010010895 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080010896 vmcs_write16(GUEST_INTR_STATUS,
10897 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080010898
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010899 /*
10900 * Write an illegal value to APIC_ACCESS_ADDR. Later,
10901 * nested_get_vmcs12_pages will either fix it up or
10902 * remove the VM execution control.
10903 */
10904 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
10905 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
10906
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010907 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
10908 }
10909
Jim Mattson83bafef2016-10-04 10:48:38 -070010910 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010911 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
10912 * entry, but only if the current (host) sp changed from the value
10913 * we wrote last (vmx->host_rsp). This cache is no longer relevant
10914 * if we switch vmcs, and rather than hold a separate cache per vmcs,
10915 * here we just force the write to happen on entry.
10916 */
10917 vmx->host_rsp = 0;
10918
10919 exec_control = vmx_exec_control(vmx); /* L0's desires */
10920 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
10921 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
10922 exec_control &= ~CPU_BASED_TPR_SHADOW;
10923 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010924
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010925 /*
10926 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
10927 * nested_get_vmcs12_pages can't fix it up, the illegal value
10928 * will result in a VM entry failure.
10929 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010930 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010931 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010932 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070010933 } else {
10934#ifdef CONFIG_X86_64
10935 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
10936 CPU_BASED_CR8_STORE_EXITING;
10937#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010938 }
10939
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010940 /*
Quan Xu8eb73e22017-12-12 16:44:21 +080010941 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
10942 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010943 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010944 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
10945 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
10946
10947 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
10948
10949 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
10950 * bitwise-or of what L1 wants to trap for L2, and what we want to
10951 * trap. Note that CR0.TS also needs updating - we do this later.
10952 */
10953 update_exception_bitmap(vcpu);
10954 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
10955 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10956
Nadav Har'El8049d652013-08-05 11:07:06 +030010957 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
10958 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
10959 * bits are further modified by vmx_set_efer() below.
10960 */
Jan Kiszkaf41245002014-03-07 20:03:13 +010010961 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030010962
10963 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
10964 * emulated by vmx_set_efer(), below.
10965 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020010966 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030010967 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
10968 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010969 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
10970
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010971 if (from_vmentry &&
10972 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010973 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020010974 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010975 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010976 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010977 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010978
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010979 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
10980 vmcs_write64(TSC_OFFSET,
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010981 vcpu->arch.tsc_offset + vmcs12->tsc_offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010982 else
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010983 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Peter Feinerc95ba922016-08-17 09:36:47 -070010984 if (kvm_has_tsc_control)
10985 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010986
10987 if (enable_vpid) {
10988 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070010989 * There is no direct mapping between vpid02 and vpid12, the
10990 * vpid02 is per-vCPU for L0 and reused while the value of
10991 * vpid12 is changed w/ one invvpid during nested vmentry.
10992 * The vpid12 is allocated by L1 for L2, so it will not
10993 * influence global bitmap(for vpid01 and vpid02 allocation)
10994 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010995 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070010996 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070010997 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
10998 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080010999 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011000 }
11001 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011002 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011003 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011004 }
11005
Ladi Prosek1fb883b2017-04-04 14:18:53 +020011006 if (enable_pml) {
11007 /*
11008 * Conceptually we want to copy the PML address and index from
11009 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11010 * since we always flush the log on each vmexit, this happens
11011 * to be equivalent to simply resetting the fields in vmcs02.
11012 */
11013 ASSERT(vmx->pml_pg);
11014 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11015 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11016 }
11017
Nadav Har'El155a97a2013-08-05 11:07:16 +030011018 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011019 if (nested_ept_init_mmu_context(vcpu)) {
11020 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11021 return 1;
11022 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011023 } else if (nested_cpu_has2(vmcs12,
11024 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
11025 vmx_flush_tlb_ept_only(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011026 }
11027
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011028 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011029 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11030 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011031 * The CR0_READ_SHADOW is what L2 should have expected to read given
11032 * the specifications by L1; It's not enough to take
11033 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11034 * have more bits than L1 expected.
11035 */
11036 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11037 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11038
11039 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11040 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11041
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011042 if (from_vmentry &&
11043 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080011044 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11045 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11046 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11047 else
11048 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11049 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11050 vmx_set_efer(vcpu, vcpu->arch.efer);
11051
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011052 if (vmx->nested.dirty_vmcs12) {
11053 prepare_vmcs02_full(vcpu, vmcs12, from_vmentry);
11054 vmx->nested.dirty_vmcs12 = false;
11055 }
11056
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011057 /*
11058 * Guest state is invalid and unrestricted guest is disabled,
11059 * which means L1 attempted VMEntry to L2 with invalid state.
11060 * Fail the VMEntry.
11061 */
11062 if (vmx->emulation_required)
11063 return 1;
11064
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011065 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010011066 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011067 entry_failure_code))
11068 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010011069
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011070 if (!enable_ept)
11071 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11072
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011073 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11074 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010011075 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011076}
11077
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011078static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11079{
11080 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11081 nested_cpu_has_virtual_nmis(vmcs12))
11082 return -EINVAL;
11083
11084 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11085 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
11086 return -EINVAL;
11087
11088 return 0;
11089}
11090
Jim Mattsonca0bde22016-11-30 12:03:46 -080011091static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11092{
11093 struct vcpu_vmx *vmx = to_vmx(vcpu);
11094
11095 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
11096 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11097 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11098
Jim Mattson56a20512017-07-06 16:33:06 -070011099 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11100 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11101
Jim Mattsonca0bde22016-11-30 12:03:46 -080011102 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11103 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11104
Jim Mattson712b12d2017-08-24 13:24:47 -070011105 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11106 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11107
Jim Mattsonca0bde22016-11-30 12:03:46 -080011108 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
11109 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11110
11111 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11112 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11113
Bandan Dasc5f983f2017-05-05 15:25:14 -040011114 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
11115 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11116
Jim Mattsonca0bde22016-11-30 12:03:46 -080011117 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011118 vmx->nested.msrs.procbased_ctls_low,
11119 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070011120 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
11121 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011122 vmx->nested.msrs.secondary_ctls_low,
11123 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011124 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011125 vmx->nested.msrs.pinbased_ctls_low,
11126 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011127 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011128 vmx->nested.msrs.exit_ctls_low,
11129 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011130 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011131 vmx->nested.msrs.entry_ctls_low,
11132 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011133 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11134
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011135 if (nested_vmx_check_nmi_controls(vmcs12))
11136 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11137
Bandan Das41ab9372017-08-03 15:54:43 -040011138 if (nested_cpu_has_vmfunc(vmcs12)) {
11139 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011140 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040011141 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11142
11143 if (nested_cpu_has_eptp_switching(vmcs12)) {
11144 if (!nested_cpu_has_ept(vmcs12) ||
11145 !page_address_valid(vcpu, vmcs12->eptp_list_address))
11146 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11147 }
11148 }
Bandan Das27c42a12017-08-03 15:54:42 -040011149
Jim Mattsonc7c2c7092017-05-05 11:28:09 -070011150 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
11151 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11152
Jim Mattsonca0bde22016-11-30 12:03:46 -080011153 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
11154 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
11155 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
11156 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11157
11158 return 0;
11159}
11160
11161static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11162 u32 *exit_qual)
11163{
11164 bool ia32e;
11165
11166 *exit_qual = ENTRY_FAIL_DEFAULT;
11167
11168 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
11169 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
11170 return 1;
11171
11172 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
11173 vmcs12->vmcs_link_pointer != -1ull) {
11174 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
11175 return 1;
11176 }
11177
11178 /*
11179 * If the load IA32_EFER VM-entry control is 1, the following checks
11180 * are performed on the field for the IA32_EFER MSR:
11181 * - Bits reserved in the IA32_EFER MSR must be 0.
11182 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
11183 * the IA-32e mode guest VM-exit control. It must also be identical
11184 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
11185 * CR0.PG) is 1.
11186 */
11187 if (to_vmx(vcpu)->nested.nested_run_pending &&
11188 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
11189 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
11190 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
11191 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
11192 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
11193 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
11194 return 1;
11195 }
11196
11197 /*
11198 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
11199 * IA32_EFER MSR must be 0 in the field for that register. In addition,
11200 * the values of the LMA and LME bits in the field must each be that of
11201 * the host address-space size VM-exit control.
11202 */
11203 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
11204 ia32e = (vmcs12->vm_exit_controls &
11205 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
11206 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
11207 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
11208 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
11209 return 1;
11210 }
11211
Wanpeng Lif1b026a2017-11-05 16:54:48 -080011212 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
11213 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
11214 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
11215 return 1;
11216
Jim Mattsonca0bde22016-11-30 12:03:46 -080011217 return 0;
11218}
11219
Jim Mattson858e25c2016-11-30 12:03:47 -080011220static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry)
11221{
11222 struct vcpu_vmx *vmx = to_vmx(vcpu);
11223 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080011224 u32 msr_entry_idx;
11225 u32 exit_qual;
11226
Jim Mattson858e25c2016-11-30 12:03:47 -080011227 enter_guest_mode(vcpu);
11228
11229 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
11230 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11231
Jim Mattsonde3a0022017-11-27 17:22:25 -060011232 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080011233 vmx_segment_cache_clear(vmx);
11234
11235 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &exit_qual)) {
11236 leave_guest_mode(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010011237 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson858e25c2016-11-30 12:03:47 -080011238 nested_vmx_entry_failure(vcpu, vmcs12,
11239 EXIT_REASON_INVALID_STATE, exit_qual);
11240 return 1;
11241 }
11242
11243 nested_get_vmcs12_pages(vcpu, vmcs12);
11244
11245 msr_entry_idx = nested_vmx_load_msr(vcpu,
11246 vmcs12->vm_entry_msr_load_addr,
11247 vmcs12->vm_entry_msr_load_count);
11248 if (msr_entry_idx) {
11249 leave_guest_mode(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010011250 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson858e25c2016-11-30 12:03:47 -080011251 nested_vmx_entry_failure(vcpu, vmcs12,
11252 EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
11253 return 1;
11254 }
11255
Jim Mattson858e25c2016-11-30 12:03:47 -080011256 /*
11257 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
11258 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
11259 * returned as far as L1 is concerned. It will only return (and set
11260 * the success flag) when L2 exits (see nested_vmx_vmexit()).
11261 */
11262 return 0;
11263}
11264
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011265/*
11266 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
11267 * for running an L2 nested guest.
11268 */
11269static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
11270{
11271 struct vmcs12 *vmcs12;
11272 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011273 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080011274 u32 exit_qual;
11275 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011276
Kyle Hueyeb277562016-11-29 12:40:39 -080011277 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011278 return 1;
11279
Kyle Hueyeb277562016-11-29 12:40:39 -080011280 if (!nested_vmx_check_vmcs12(vcpu))
11281 goto out;
11282
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011283 vmcs12 = get_vmcs12(vcpu);
11284
Abel Gordon012f83c2013-04-18 14:39:25 +030011285 if (enable_shadow_vmcs)
11286 copy_shadow_to_vmcs12(vmx);
11287
Nadav Har'El7c177932011-05-25 23:12:04 +030011288 /*
11289 * The nested entry process starts with enforcing various prerequisites
11290 * on vmcs12 as required by the Intel SDM, and act appropriately when
11291 * they fail: As the SDM explains, some conditions should cause the
11292 * instruction to fail, while others will cause the instruction to seem
11293 * to succeed, but return an EXIT_REASON_INVALID_STATE.
11294 * To speed up the normal (success) code path, we should avoid checking
11295 * for misconfigurations which will anyway be caught by the processor
11296 * when using the merged vmcs02.
11297 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011298 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
11299 nested_vmx_failValid(vcpu,
11300 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
11301 goto out;
11302 }
11303
Nadav Har'El7c177932011-05-25 23:12:04 +030011304 if (vmcs12->launch_state == launch) {
11305 nested_vmx_failValid(vcpu,
11306 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
11307 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080011308 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030011309 }
11310
Jim Mattsonca0bde22016-11-30 12:03:46 -080011311 ret = check_vmentry_prereqs(vcpu, vmcs12);
11312 if (ret) {
11313 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080011314 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020011315 }
11316
Nadav Har'El7c177932011-05-25 23:12:04 +030011317 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080011318 * After this point, the trap flag no longer triggers a singlestep trap
11319 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
11320 * This is not 100% correct; for performance reasons, we delegate most
11321 * of the checks on host state to the processor. If those fail,
11322 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020011323 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080011324 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020011325
Jim Mattsonca0bde22016-11-30 12:03:46 -080011326 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
11327 if (ret) {
11328 nested_vmx_entry_failure(vcpu, vmcs12,
11329 EXIT_REASON_INVALID_STATE, exit_qual);
11330 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020011331 }
11332
11333 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030011334 * We're finally done with prerequisite checking, and can start with
11335 * the nested entry.
11336 */
11337
Jim Mattson858e25c2016-11-30 12:03:47 -080011338 ret = enter_vmx_non_root_mode(vcpu, true);
11339 if (ret)
11340 return ret;
Wincy Vanff651cb2014-12-11 08:52:58 +030011341
Chao Gao135a06c2018-02-11 10:06:30 +080011342 /*
11343 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
11344 * by event injection, halt vcpu.
11345 */
11346 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
11347 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK))
Joel Schopp5cb56052015-03-02 13:43:31 -060011348 return kvm_vcpu_halt(vcpu);
Jan Kiszka6dfacad2013-12-04 08:58:54 +010011349
Jan Kiszka7af40ad32014-01-04 18:47:23 +010011350 vmx->nested.nested_run_pending = 1;
11351
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011352 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080011353
11354out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080011355 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011356}
11357
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011358/*
11359 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
11360 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
11361 * This function returns the new value we should put in vmcs12.guest_cr0.
11362 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
11363 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
11364 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
11365 * didn't trap the bit, because if L1 did, so would L0).
11366 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
11367 * been modified by L2, and L1 knows it. So just leave the old value of
11368 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
11369 * isn't relevant, because if L0 traps this bit it can set it to anything.
11370 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
11371 * changed these bits, and therefore they need to be updated, but L0
11372 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
11373 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
11374 */
11375static inline unsigned long
11376vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11377{
11378 return
11379 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
11380 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
11381 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
11382 vcpu->arch.cr0_guest_owned_bits));
11383}
11384
11385static inline unsigned long
11386vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11387{
11388 return
11389 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
11390 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
11391 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
11392 vcpu->arch.cr4_guest_owned_bits));
11393}
11394
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011395static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
11396 struct vmcs12 *vmcs12)
11397{
11398 u32 idt_vectoring;
11399 unsigned int nr;
11400
Wanpeng Li664f8e22017-08-24 03:35:09 -070011401 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011402 nr = vcpu->arch.exception.nr;
11403 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11404
11405 if (kvm_exception_is_soft(nr)) {
11406 vmcs12->vm_exit_instruction_len =
11407 vcpu->arch.event_exit_inst_len;
11408 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
11409 } else
11410 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
11411
11412 if (vcpu->arch.exception.has_error_code) {
11413 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
11414 vmcs12->idt_vectoring_error_code =
11415 vcpu->arch.exception.error_code;
11416 }
11417
11418 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010011419 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011420 vmcs12->idt_vectoring_info_field =
11421 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
11422 } else if (vcpu->arch.interrupt.pending) {
11423 nr = vcpu->arch.interrupt.nr;
11424 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11425
11426 if (vcpu->arch.interrupt.soft) {
11427 idt_vectoring |= INTR_TYPE_SOFT_INTR;
11428 vmcs12->vm_entry_instruction_len =
11429 vcpu->arch.event_exit_inst_len;
11430 } else
11431 idt_vectoring |= INTR_TYPE_EXT_INTR;
11432
11433 vmcs12->idt_vectoring_info_field = idt_vectoring;
11434 }
11435}
11436
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011437static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
11438{
11439 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011440 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020011441 bool block_nested_events =
11442 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080011443
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011444 if (vcpu->arch.exception.pending &&
11445 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020011446 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011447 return -EBUSY;
11448 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011449 return 0;
11450 }
11451
Jan Kiszkaf41245002014-03-07 20:03:13 +010011452 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
11453 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020011454 if (block_nested_events)
Jan Kiszkaf41245002014-03-07 20:03:13 +010011455 return -EBUSY;
11456 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
11457 return 0;
11458 }
11459
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011460 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020011461 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011462 return -EBUSY;
11463 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11464 NMI_VECTOR | INTR_TYPE_NMI_INTR |
11465 INTR_INFO_VALID_MASK, 0);
11466 /*
11467 * The NMI-triggered VM exit counts as injection:
11468 * clear this one and block further NMIs.
11469 */
11470 vcpu->arch.nmi_pending = 0;
11471 vmx_set_nmi_mask(vcpu, true);
11472 return 0;
11473 }
11474
11475 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
11476 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020011477 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011478 return -EBUSY;
11479 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080011480 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011481 }
11482
David Hildenbrand6342c502017-01-25 11:58:58 +010011483 vmx_complete_nested_posted_interrupt(vcpu);
11484 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011485}
11486
Jan Kiszkaf41245002014-03-07 20:03:13 +010011487static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
11488{
11489 ktime_t remaining =
11490 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
11491 u64 value;
11492
11493 if (ktime_to_ns(remaining) <= 0)
11494 return 0;
11495
11496 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
11497 do_div(value, 1000000);
11498 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11499}
11500
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011501/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011502 * Update the guest state fields of vmcs12 to reflect changes that
11503 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
11504 * VM-entry controls is also updated, since this is really a guest
11505 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011506 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011507static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011508{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011509 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
11510 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
11511
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011512 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
11513 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
11514 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
11515
11516 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
11517 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
11518 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
11519 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
11520 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
11521 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
11522 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
11523 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
11524 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
11525 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
11526 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
11527 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
11528 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
11529 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
11530 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
11531 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
11532 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
11533 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
11534 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
11535 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
11536 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
11537 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
11538 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
11539 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
11540 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
11541 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
11542 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
11543 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
11544 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
11545 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
11546 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
11547 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
11548 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
11549 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
11550 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
11551 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
11552
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011553 vmcs12->guest_interruptibility_info =
11554 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
11555 vmcs12->guest_pending_dbg_exceptions =
11556 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010011557 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
11558 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
11559 else
11560 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011561
Jan Kiszkaf41245002014-03-07 20:03:13 +010011562 if (nested_cpu_has_preemption_timer(vmcs12)) {
11563 if (vmcs12->vm_exit_controls &
11564 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
11565 vmcs12->vmx_preemption_timer_value =
11566 vmx_get_preemption_timer_value(vcpu);
11567 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
11568 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080011569
Nadav Har'El3633cfc2013-08-05 11:07:07 +030011570 /*
11571 * In some cases (usually, nested EPT), L2 is allowed to change its
11572 * own CR3 without exiting. If it has changed it, we must keep it.
11573 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
11574 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
11575 *
11576 * Additionally, restore L2's PDPTR to vmcs12.
11577 */
11578 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010011579 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030011580 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
11581 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
11582 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
11583 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
11584 }
11585
Jim Mattsond281e132017-06-01 12:44:46 -070011586 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030011587
Wincy Van608406e2015-02-03 23:57:51 +080011588 if (nested_cpu_has_vid(vmcs12))
11589 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
11590
Jan Kiszkac18911a2013-03-13 16:06:41 +010011591 vmcs12->vm_entry_controls =
11592 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020011593 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010011594
Jan Kiszka2996fca2014-06-16 13:59:43 +020011595 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
11596 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
11597 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11598 }
11599
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011600 /* TODO: These cannot have changed unless we have MSR bitmaps and
11601 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020011602 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011603 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020011604 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
11605 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011606 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
11607 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
11608 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010011609 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011610 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011611}
11612
11613/*
11614 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
11615 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
11616 * and this function updates it to reflect the changes to the guest state while
11617 * L2 was running (and perhaps made some exits which were handled directly by L0
11618 * without going back to L1), and to reflect the exit reason.
11619 * Note that we do not have to copy here all VMCS fields, just those that
11620 * could have changed by the L2 guest or the exit - i.e., the guest-state and
11621 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
11622 * which already writes to vmcs12 directly.
11623 */
11624static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11625 u32 exit_reason, u32 exit_intr_info,
11626 unsigned long exit_qualification)
11627{
11628 /* update guest state fields: */
11629 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011630
11631 /* update exit information fields: */
11632
Jan Kiszka533558b2014-01-04 18:47:20 +010011633 vmcs12->vm_exit_reason = exit_reason;
11634 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010011635 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020011636
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011637 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011638 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
11639 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
11640
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011641 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070011642 vmcs12->launch_state = 1;
11643
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011644 /* vm_entry_intr_info_field is cleared on exit. Emulate this
11645 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011646 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011647
11648 /*
11649 * Transfer the event that L0 or L1 may wanted to inject into
11650 * L2 to IDT_VECTORING_INFO_FIELD.
11651 */
11652 vmcs12_save_pending_event(vcpu, vmcs12);
11653 }
11654
11655 /*
11656 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
11657 * preserved above and would only end up incorrectly in L1.
11658 */
11659 vcpu->arch.nmi_injected = false;
11660 kvm_clear_exception_queue(vcpu);
11661 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011662}
11663
Wanpeng Li5af41572017-11-05 16:54:49 -080011664static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
11665 struct vmcs12 *vmcs12)
11666{
11667 u32 entry_failure_code;
11668
11669 nested_ept_uninit_mmu_context(vcpu);
11670
11671 /*
11672 * Only PDPTE load can fail as the value of cr3 was checked on entry and
11673 * couldn't have changed.
11674 */
11675 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
11676 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
11677
11678 if (!enable_ept)
11679 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
11680}
11681
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011682/*
11683 * A part of what we need to when the nested L2 guest exits and we want to
11684 * run its L1 parent, is to reset L1's guest state to the host state specified
11685 * in vmcs12.
11686 * This function is to be called not only on normal nested exit, but also on
11687 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
11688 * Failures During or After Loading Guest State").
11689 * This function should be called when the active VMCS is L1's (vmcs01).
11690 */
Jan Kiszka733568f2013-02-23 15:07:47 +010011691static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
11692 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011693{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011694 struct kvm_segment seg;
11695
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011696 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
11697 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020011698 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011699 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11700 else
11701 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11702 vmx_set_efer(vcpu, vcpu->arch.efer);
11703
11704 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
11705 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070011706 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011707 /*
11708 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011709 * actually changed, because vmx_set_cr0 refers to efer set above.
11710 *
11711 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
11712 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011713 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011714 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020011715 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011716
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011717 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011718 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080011719 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011720
Wanpeng Li5af41572017-11-05 16:54:49 -080011721 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011722
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011723 if (enable_vpid) {
11724 /*
11725 * Trivially support vpid by letting L2s share their parent
11726 * L1's vpid. TODO: move to a more elaborate solution, giving
11727 * each L2 its own vpid and exposing the vpid feature to L1.
11728 */
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011729 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011730 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011731
11732 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
11733 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
11734 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
11735 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
11736 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d5512017-10-11 16:54:42 +020011737 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
11738 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011739
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011740 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
11741 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
11742 vmcs_write64(GUEST_BNDCFGS, 0);
11743
Jan Kiszka44811c02013-08-04 17:17:27 +020011744 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011745 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011746 vcpu->arch.pat = vmcs12->host_ia32_pat;
11747 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011748 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
11749 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
11750 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011751
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011752 /* Set L1 segment info according to Intel SDM
11753 27.5.2 Loading Host Segment and Descriptor-Table Registers */
11754 seg = (struct kvm_segment) {
11755 .base = 0,
11756 .limit = 0xFFFFFFFF,
11757 .selector = vmcs12->host_cs_selector,
11758 .type = 11,
11759 .present = 1,
11760 .s = 1,
11761 .g = 1
11762 };
11763 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
11764 seg.l = 1;
11765 else
11766 seg.db = 1;
11767 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
11768 seg = (struct kvm_segment) {
11769 .base = 0,
11770 .limit = 0xFFFFFFFF,
11771 .type = 3,
11772 .present = 1,
11773 .s = 1,
11774 .db = 1,
11775 .g = 1
11776 };
11777 seg.selector = vmcs12->host_ds_selector;
11778 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
11779 seg.selector = vmcs12->host_es_selector;
11780 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
11781 seg.selector = vmcs12->host_ss_selector;
11782 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
11783 seg.selector = vmcs12->host_fs_selector;
11784 seg.base = vmcs12->host_fs_base;
11785 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
11786 seg.selector = vmcs12->host_gs_selector;
11787 seg.base = vmcs12->host_gs_base;
11788 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
11789 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030011790 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011791 .limit = 0x67,
11792 .selector = vmcs12->host_tr_selector,
11793 .type = 11,
11794 .present = 1
11795 };
11796 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
11797
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011798 kvm_set_dr(vcpu, 7, 0x400);
11799 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030011800
Wincy Van3af18d92015-02-03 23:49:31 +080011801 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011802 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080011803
Wincy Vanff651cb2014-12-11 08:52:58 +030011804 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
11805 vmcs12->vm_exit_msr_load_count))
11806 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011807}
11808
11809/*
11810 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
11811 * and modify vmcs12 to make it see what it would expect to see there if
11812 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
11813 */
Jan Kiszka533558b2014-01-04 18:47:20 +010011814static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
11815 u32 exit_intr_info,
11816 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011817{
11818 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011819 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11820
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011821 /* trying to cancel vmlaunch/vmresume is a bug */
11822 WARN_ON_ONCE(vmx->nested.nested_run_pending);
11823
Wanpeng Li6550c4d2017-07-31 19:25:27 -070011824 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070011825 * The only expected VM-instruction error is "VM entry with
11826 * invalid control field(s)." Anything else indicates a
11827 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070011828 */
Jim Mattson4f350c62017-09-14 16:31:44 -070011829 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
11830 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
11831
11832 leave_guest_mode(vcpu);
11833
11834 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020011835 if (exit_reason == -1)
11836 sync_vmcs12(vcpu, vmcs12);
11837 else
11838 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
11839 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070011840
11841 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
11842 vmcs12->vm_exit_msr_store_count))
11843 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040011844 }
11845
Jim Mattson4f350c62017-09-14 16:31:44 -070011846 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020011847 vm_entry_controls_reset_shadow(vmx);
11848 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010011849 vmx_segment_cache_clear(vmx);
11850
Paolo Bonzini9314006db2016-07-06 13:23:51 +020011851 /* Update any VMCS fields that might have changed while L2 ran */
Jim Mattson83bafef2016-10-04 10:48:38 -070011852 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11853 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010011854 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini9314006db2016-07-06 13:23:51 +020011855 if (vmx->hv_deadline_tsc == -1)
11856 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11857 PIN_BASED_VMX_PREEMPTION_TIMER);
11858 else
11859 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11860 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070011861 if (kvm_has_tsc_control)
11862 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011863
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020011864 if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
11865 vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
11866 vmx_set_virtual_x2apic_mode(vcpu,
11867 vcpu->arch.apic_base & X2APIC_ENABLE);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011868 } else if (!nested_cpu_has_ept(vmcs12) &&
11869 nested_cpu_has2(vmcs12,
11870 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
11871 vmx_flush_tlb_ept_only(vcpu);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020011872 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011873
11874 /* This is needed for same reason as it was needed in prepare_vmcs02 */
11875 vmx->host_rsp = 0;
11876
11877 /* Unpin physical memory we referred to in vmcs02 */
11878 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020011879 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011880 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011881 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011882 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020011883 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011884 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011885 }
Wincy Van705699a2015-02-03 23:58:17 +080011886 if (vmx->nested.pi_desc_page) {
11887 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011888 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080011889 vmx->nested.pi_desc_page = NULL;
11890 vmx->nested.pi_desc = NULL;
11891 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011892
11893 /*
Tang Chen38b99172014-09-24 15:57:54 +080011894 * We are now running in L2, mmu_notifier will force to reload the
11895 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
11896 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080011897 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080011898
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020011899 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030011900 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011901
11902 /* in case we halted in L2 */
11903 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070011904
11905 if (likely(!vmx->fail)) {
11906 /*
11907 * TODO: SDM says that with acknowledge interrupt on
11908 * exit, bit 31 of the VM-exit interrupt information
11909 * (valid interrupt) is always set to 1 on
11910 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
11911 * need kvm_cpu_has_interrupt(). See the commit
11912 * message for details.
11913 */
11914 if (nested_exit_intr_ack_set(vcpu) &&
11915 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
11916 kvm_cpu_has_interrupt(vcpu)) {
11917 int irq = kvm_cpu_get_interrupt(vcpu);
11918 WARN_ON(irq < 0);
11919 vmcs12->vm_exit_intr_info = irq |
11920 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
11921 }
11922
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020011923 if (exit_reason != -1)
11924 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
11925 vmcs12->exit_qualification,
11926 vmcs12->idt_vectoring_info_field,
11927 vmcs12->vm_exit_intr_info,
11928 vmcs12->vm_exit_intr_error_code,
11929 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070011930
11931 load_vmcs12_host_state(vcpu, vmcs12);
11932
11933 return;
11934 }
11935
11936 /*
11937 * After an early L2 VM-entry failure, we're now back
11938 * in L1 which thinks it just finished a VMLAUNCH or
11939 * VMRESUME instruction, so we need to set the failure
11940 * flag and the VM-instruction error field of the VMCS
11941 * accordingly.
11942 */
11943 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080011944
11945 load_vmcs12_mmu_host_state(vcpu, vmcs12);
11946
Jim Mattson4f350c62017-09-14 16:31:44 -070011947 /*
11948 * The emulated instruction was already skipped in
11949 * nested_vmx_run, but the updated RIP was never
11950 * written back to the vmcs01.
11951 */
11952 skip_emulated_instruction(vcpu);
11953 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011954}
11955
Nadav Har'El7c177932011-05-25 23:12:04 +030011956/*
Jan Kiszka42124922014-01-04 18:47:19 +010011957 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
11958 */
11959static void vmx_leave_nested(struct kvm_vcpu *vcpu)
11960{
Wanpeng Li2f707d92017-03-06 04:03:28 -080011961 if (is_guest_mode(vcpu)) {
11962 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010011963 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080011964 }
Jan Kiszka42124922014-01-04 18:47:19 +010011965 free_nested(to_vmx(vcpu));
11966}
11967
11968/*
Nadav Har'El7c177932011-05-25 23:12:04 +030011969 * L1's failure to enter L2 is a subset of a normal exit, as explained in
11970 * 23.7 "VM-entry failures during or after loading guest state" (this also
11971 * lists the acceptable exit-reason and exit-qualification parameters).
11972 * It should only be called before L2 actually succeeded to run, and when
11973 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
11974 */
11975static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
11976 struct vmcs12 *vmcs12,
11977 u32 reason, unsigned long qualification)
11978{
11979 load_vmcs12_host_state(vcpu, vmcs12);
11980 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
11981 vmcs12->exit_qualification = qualification;
11982 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030011983 if (enable_shadow_vmcs)
11984 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030011985}
11986
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011987static int vmx_check_intercept(struct kvm_vcpu *vcpu,
11988 struct x86_instruction_info *info,
11989 enum x86_intercept_stage stage)
11990{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020011991 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11992 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
11993
11994 /*
11995 * RDPID causes #UD if disabled through secondary execution controls.
11996 * Because it is marked as EmulateOnUD, we need to intercept it here.
11997 */
11998 if (info->intercept == x86_intercept_rdtscp &&
11999 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
12000 ctxt->exception.vector = UD_VECTOR;
12001 ctxt->exception.error_code_valid = false;
12002 return X86EMUL_PROPAGATE_FAULT;
12003 }
12004
12005 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012006 return X86EMUL_CONTINUE;
12007}
12008
Yunhong Jiang64672c92016-06-13 14:19:59 -070012009#ifdef CONFIG_X86_64
12010/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
12011static inline int u64_shl_div_u64(u64 a, unsigned int shift,
12012 u64 divisor, u64 *result)
12013{
12014 u64 low = a << shift, high = a >> (64 - shift);
12015
12016 /* To avoid the overflow on divq */
12017 if (high >= divisor)
12018 return 1;
12019
12020 /* Low hold the result, high hold rem which is discarded */
12021 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
12022 "rm" (divisor), "0" (low), "1" (high));
12023 *result = low;
12024
12025 return 0;
12026}
12027
12028static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
12029{
12030 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini9175d2e2016-06-27 15:08:01 +020012031 u64 tscl = rdtsc();
12032 u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
12033 u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012034
12035 /* Convert to host delta tsc if tsc scaling is enabled */
12036 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
12037 u64_shl_div_u64(delta_tsc,
12038 kvm_tsc_scaling_ratio_frac_bits,
12039 vcpu->arch.tsc_scaling_ratio,
12040 &delta_tsc))
12041 return -ERANGE;
12042
12043 /*
12044 * If the delta tsc can't fit in the 32 bit after the multi shift,
12045 * we can't use the preemption timer.
12046 * It's possible that it fits on later vmentries, but checking
12047 * on every vmentry is costly so we just use an hrtimer.
12048 */
12049 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
12050 return -ERANGE;
12051
12052 vmx->hv_deadline_tsc = tscl + delta_tsc;
12053 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12054 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070012055
12056 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012057}
12058
12059static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
12060{
12061 struct vcpu_vmx *vmx = to_vmx(vcpu);
12062 vmx->hv_deadline_tsc = -1;
12063 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12064 PIN_BASED_VMX_PREEMPTION_TIMER);
12065}
12066#endif
12067
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012068static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012069{
Wanpeng Lib31c1142018-03-12 04:53:04 -070012070 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020012071 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012072}
12073
Kai Huang843e4332015-01-28 10:54:28 +080012074static void vmx_slot_enable_log_dirty(struct kvm *kvm,
12075 struct kvm_memory_slot *slot)
12076{
12077 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
12078 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
12079}
12080
12081static void vmx_slot_disable_log_dirty(struct kvm *kvm,
12082 struct kvm_memory_slot *slot)
12083{
12084 kvm_mmu_slot_set_dirty(kvm, slot);
12085}
12086
12087static void vmx_flush_log_dirty(struct kvm *kvm)
12088{
12089 kvm_flush_pml_buffers(kvm);
12090}
12091
Bandan Dasc5f983f2017-05-05 15:25:14 -040012092static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
12093{
12094 struct vmcs12 *vmcs12;
12095 struct vcpu_vmx *vmx = to_vmx(vcpu);
12096 gpa_t gpa;
12097 struct page *page = NULL;
12098 u64 *pml_address;
12099
12100 if (is_guest_mode(vcpu)) {
12101 WARN_ON_ONCE(vmx->nested.pml_full);
12102
12103 /*
12104 * Check if PML is enabled for the nested guest.
12105 * Whether eptp bit 6 is set is already checked
12106 * as part of A/D emulation.
12107 */
12108 vmcs12 = get_vmcs12(vcpu);
12109 if (!nested_cpu_has_pml(vmcs12))
12110 return 0;
12111
Dan Carpenter47698862017-05-10 22:43:17 +030012112 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040012113 vmx->nested.pml_full = true;
12114 return 1;
12115 }
12116
12117 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
12118
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020012119 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
12120 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040012121 return 0;
12122
12123 pml_address = kmap(page);
12124 pml_address[vmcs12->guest_pml_index--] = gpa;
12125 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012126 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040012127 }
12128
12129 return 0;
12130}
12131
Kai Huang843e4332015-01-28 10:54:28 +080012132static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
12133 struct kvm_memory_slot *memslot,
12134 gfn_t offset, unsigned long mask)
12135{
12136 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
12137}
12138
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012139static void __pi_post_block(struct kvm_vcpu *vcpu)
12140{
12141 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12142 struct pi_desc old, new;
12143 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012144
12145 do {
12146 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012147 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
12148 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012149
12150 dest = cpu_physical_id(vcpu->cpu);
12151
12152 if (x2apic_enabled())
12153 new.ndst = dest;
12154 else
12155 new.ndst = (dest << 8) & 0xFF00;
12156
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012157 /* set 'NV' to 'notification vector' */
12158 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012159 } while (cmpxchg64(&pi_desc->control, old.control,
12160 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012161
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012162 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
12163 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012164 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012165 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012166 vcpu->pre_pcpu = -1;
12167 }
12168}
12169
Feng Wuefc64402015-09-18 22:29:51 +080012170/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080012171 * This routine does the following things for vCPU which is going
12172 * to be blocked if VT-d PI is enabled.
12173 * - Store the vCPU to the wakeup list, so when interrupts happen
12174 * we can find the right vCPU to wake up.
12175 * - Change the Posted-interrupt descriptor as below:
12176 * 'NDST' <-- vcpu->pre_pcpu
12177 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
12178 * - If 'ON' is set during this process, which means at least one
12179 * interrupt is posted for this vCPU, we cannot block it, in
12180 * this case, return 1, otherwise, return 0.
12181 *
12182 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070012183static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012184{
Feng Wubf9f6ac2015-09-18 22:29:55 +080012185 unsigned int dest;
12186 struct pi_desc old, new;
12187 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12188
12189 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012190 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12191 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080012192 return 0;
12193
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012194 WARN_ON(irqs_disabled());
12195 local_irq_disable();
12196 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
12197 vcpu->pre_pcpu = vcpu->cpu;
12198 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12199 list_add_tail(&vcpu->blocked_vcpu_list,
12200 &per_cpu(blocked_vcpu_on_cpu,
12201 vcpu->pre_pcpu));
12202 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12203 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080012204
12205 do {
12206 old.control = new.control = pi_desc->control;
12207
Feng Wubf9f6ac2015-09-18 22:29:55 +080012208 WARN((pi_desc->sn == 1),
12209 "Warning: SN field of posted-interrupts "
12210 "is set before blocking\n");
12211
12212 /*
12213 * Since vCPU can be preempted during this process,
12214 * vcpu->cpu could be different with pre_pcpu, we
12215 * need to set pre_pcpu as the destination of wakeup
12216 * notification event, then we can find the right vCPU
12217 * to wakeup in wakeup handler if interrupts happen
12218 * when the vCPU is in blocked state.
12219 */
12220 dest = cpu_physical_id(vcpu->pre_pcpu);
12221
12222 if (x2apic_enabled())
12223 new.ndst = dest;
12224 else
12225 new.ndst = (dest << 8) & 0xFF00;
12226
12227 /* set 'NV' to 'wakeup vector' */
12228 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012229 } while (cmpxchg64(&pi_desc->control, old.control,
12230 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012231
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012232 /* We should not block the vCPU if an interrupt is posted for it. */
12233 if (pi_test_on(pi_desc) == 1)
12234 __pi_post_block(vcpu);
12235
12236 local_irq_enable();
12237 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012238}
12239
Yunhong Jiangbc225122016-06-13 14:19:58 -070012240static int vmx_pre_block(struct kvm_vcpu *vcpu)
12241{
12242 if (pi_pre_block(vcpu))
12243 return 1;
12244
Yunhong Jiang64672c92016-06-13 14:19:59 -070012245 if (kvm_lapic_hv_timer_in_use(vcpu))
12246 kvm_lapic_switch_to_sw_timer(vcpu);
12247
Yunhong Jiangbc225122016-06-13 14:19:58 -070012248 return 0;
12249}
12250
12251static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012252{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012253 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012254 return;
12255
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012256 WARN_ON(irqs_disabled());
12257 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012258 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012259 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080012260}
12261
Yunhong Jiangbc225122016-06-13 14:19:58 -070012262static void vmx_post_block(struct kvm_vcpu *vcpu)
12263{
Yunhong Jiang64672c92016-06-13 14:19:59 -070012264 if (kvm_x86_ops->set_hv_timer)
12265 kvm_lapic_switch_to_hv_timer(vcpu);
12266
Yunhong Jiangbc225122016-06-13 14:19:58 -070012267 pi_post_block(vcpu);
12268}
12269
Feng Wubf9f6ac2015-09-18 22:29:55 +080012270/*
Feng Wuefc64402015-09-18 22:29:51 +080012271 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
12272 *
12273 * @kvm: kvm
12274 * @host_irq: host irq of the interrupt
12275 * @guest_irq: gsi of the interrupt
12276 * @set: set or unset PI
12277 * returns 0 on success, < 0 on failure
12278 */
12279static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
12280 uint32_t guest_irq, bool set)
12281{
12282 struct kvm_kernel_irq_routing_entry *e;
12283 struct kvm_irq_routing_table *irq_rt;
12284 struct kvm_lapic_irq irq;
12285 struct kvm_vcpu *vcpu;
12286 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012287 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080012288
12289 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012290 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12291 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080012292 return 0;
12293
12294 idx = srcu_read_lock(&kvm->irq_srcu);
12295 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012296 if (guest_irq >= irq_rt->nr_rt_entries ||
12297 hlist_empty(&irq_rt->map[guest_irq])) {
12298 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
12299 guest_irq, irq_rt->nr_rt_entries);
12300 goto out;
12301 }
Feng Wuefc64402015-09-18 22:29:51 +080012302
12303 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
12304 if (e->type != KVM_IRQ_ROUTING_MSI)
12305 continue;
12306 /*
12307 * VT-d PI cannot support posting multicast/broadcast
12308 * interrupts to a vCPU, we still use interrupt remapping
12309 * for these kind of interrupts.
12310 *
12311 * For lowest-priority interrupts, we only support
12312 * those with single CPU as the destination, e.g. user
12313 * configures the interrupts via /proc/irq or uses
12314 * irqbalance to make the interrupts single-CPU.
12315 *
12316 * We will support full lowest-priority interrupt later.
12317 */
12318
Radim Krčmář371313132016-07-12 22:09:27 +020012319 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080012320 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
12321 /*
12322 * Make sure the IRTE is in remapped mode if
12323 * we don't handle it in posted mode.
12324 */
12325 ret = irq_set_vcpu_affinity(host_irq, NULL);
12326 if (ret < 0) {
12327 printk(KERN_INFO
12328 "failed to back to remapped mode, irq: %u\n",
12329 host_irq);
12330 goto out;
12331 }
12332
Feng Wuefc64402015-09-18 22:29:51 +080012333 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080012334 }
Feng Wuefc64402015-09-18 22:29:51 +080012335
12336 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
12337 vcpu_info.vector = irq.vector;
12338
Feng Wub6ce9782016-01-25 16:53:35 +080012339 trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080012340 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
12341
12342 if (set)
12343 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2eb2017-09-18 09:56:49 +080012344 else
Feng Wuefc64402015-09-18 22:29:51 +080012345 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080012346
12347 if (ret < 0) {
12348 printk(KERN_INFO "%s: failed to update PI IRTE\n",
12349 __func__);
12350 goto out;
12351 }
12352 }
12353
12354 ret = 0;
12355out:
12356 srcu_read_unlock(&kvm->irq_srcu, idx);
12357 return ret;
12358}
12359
Ashok Rajc45dcc72016-06-22 14:59:56 +080012360static void vmx_setup_mce(struct kvm_vcpu *vcpu)
12361{
12362 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
12363 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
12364 FEATURE_CONTROL_LMCE;
12365 else
12366 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
12367 ~FEATURE_CONTROL_LMCE;
12368}
12369
Ladi Prosek72d7b372017-10-11 16:54:41 +020012370static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
12371{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012372 /* we need a nested vmexit to enter SMM, postpone if run is pending */
12373 if (to_vmx(vcpu)->nested.nested_run_pending)
12374 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020012375 return 1;
12376}
12377
Ladi Prosek0234bf82017-10-11 16:54:40 +020012378static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
12379{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012380 struct vcpu_vmx *vmx = to_vmx(vcpu);
12381
12382 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
12383 if (vmx->nested.smm.guest_mode)
12384 nested_vmx_vmexit(vcpu, -1, 0, 0);
12385
12386 vmx->nested.smm.vmxon = vmx->nested.vmxon;
12387 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070012388 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020012389 return 0;
12390}
12391
12392static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
12393{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012394 struct vcpu_vmx *vmx = to_vmx(vcpu);
12395 int ret;
12396
12397 if (vmx->nested.smm.vmxon) {
12398 vmx->nested.vmxon = true;
12399 vmx->nested.smm.vmxon = false;
12400 }
12401
12402 if (vmx->nested.smm.guest_mode) {
12403 vcpu->arch.hflags &= ~HF_SMM_MASK;
12404 ret = enter_vmx_non_root_mode(vcpu, false);
12405 vcpu->arch.hflags |= HF_SMM_MASK;
12406 if (ret)
12407 return ret;
12408
12409 vmx->nested.smm.guest_mode = false;
12410 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020012411 return 0;
12412}
12413
Ladi Prosekcc3d9672017-10-17 16:02:39 +020012414static int enable_smi_window(struct kvm_vcpu *vcpu)
12415{
12416 return 0;
12417}
12418
Kees Cook404f6aa2016-08-08 16:29:06 -070012419static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080012420 .cpu_has_kvm_support = cpu_has_kvm_support,
12421 .disabled_by_bios = vmx_disabled_by_bios,
12422 .hardware_setup = hardware_setup,
12423 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030012424 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012425 .hardware_enable = hardware_enable,
12426 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080012427 .cpu_has_accelerated_tpr = report_flexpriority,
Paolo Bonzini6d396b52015-04-01 14:25:33 +020012428 .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012429
Wanpeng Lib31c1142018-03-12 04:53:04 -070012430 .vm_init = vmx_vm_init,
12431
Avi Kivity6aa8b732006-12-10 02:21:36 -080012432 .vcpu_create = vmx_create_vcpu,
12433 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030012434 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012435
Avi Kivity04d2cc72007-09-10 18:10:54 +030012436 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012437 .vcpu_load = vmx_vcpu_load,
12438 .vcpu_put = vmx_vcpu_put,
12439
Paolo Bonzinia96036b2015-11-10 11:55:36 +010012440 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060012441 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012442 .get_msr = vmx_get_msr,
12443 .set_msr = vmx_set_msr,
12444 .get_segment_base = vmx_get_segment_base,
12445 .get_segment = vmx_get_segment,
12446 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020012447 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012448 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020012449 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020012450 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030012451 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012452 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012453 .set_cr3 = vmx_set_cr3,
12454 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012455 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012456 .get_idt = vmx_get_idt,
12457 .set_idt = vmx_set_idt,
12458 .get_gdt = vmx_get_gdt,
12459 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010012460 .get_dr6 = vmx_get_dr6,
12461 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030012462 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010012463 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030012464 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012465 .get_rflags = vmx_get_rflags,
12466 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080012467
Avi Kivity6aa8b732006-12-10 02:21:36 -080012468 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012469
Avi Kivity6aa8b732006-12-10 02:21:36 -080012470 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020012471 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012472 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040012473 .set_interrupt_shadow = vmx_set_interrupt_shadow,
12474 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020012475 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030012476 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012477 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020012478 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030012479 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020012480 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012481 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010012482 .get_nmi_mask = vmx_get_nmi_mask,
12483 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012484 .enable_nmi_window = enable_nmi_window,
12485 .enable_irq_window = enable_irq_window,
12486 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +080012487 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080012488 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030012489 .get_enable_apicv = vmx_get_enable_apicv,
12490 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080012491 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010012492 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080012493 .hwapic_irr_update = vmx_hwapic_irr_update,
12494 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080012495 .sync_pir_to_irr = vmx_sync_pir_to_irr,
12496 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012497
Izik Eiduscbc94022007-10-25 00:29:55 +020012498 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080012499 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080012500 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030012501
Avi Kivity586f9602010-11-18 13:09:54 +020012502 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020012503
Sheng Yang17cc3932010-01-05 19:02:27 +080012504 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080012505
12506 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080012507
12508 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000012509 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020012510
12511 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080012512
12513 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100012514
12515 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020012516
12517 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012518
12519 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080012520 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000012521 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080012522 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020012523 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012524
12525 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012526
12527 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080012528
12529 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
12530 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
12531 .flush_log_dirty = vmx_flush_log_dirty,
12532 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040012533 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020012534
Feng Wubf9f6ac2015-09-18 22:29:55 +080012535 .pre_block = vmx_pre_block,
12536 .post_block = vmx_post_block,
12537
Wei Huang25462f72015-06-19 15:45:05 +020012538 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080012539
12540 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070012541
12542#ifdef CONFIG_X86_64
12543 .set_hv_timer = vmx_set_hv_timer,
12544 .cancel_hv_timer = vmx_cancel_hv_timer,
12545#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080012546
12547 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020012548
Ladi Prosek72d7b372017-10-11 16:54:41 +020012549 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020012550 .pre_enter_smm = vmx_pre_enter_smm,
12551 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020012552 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012553};
12554
12555static int __init vmx_init(void)
12556{
Tiejun Chen34a1cd62014-10-28 10:14:48 +080012557 int r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
12558 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030012559 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080012560 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080012561
Dave Young2965faa2015-09-09 15:38:55 -070012562#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080012563 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
12564 crash_vmclear_local_loaded_vmcss);
12565#endif
12566
He, Qingfdef3ad2007-04-30 09:45:24 +030012567 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080012568}
12569
12570static void __exit vmx_exit(void)
12571{
Dave Young2965faa2015-09-09 15:38:55 -070012572#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053012573 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080012574 synchronize_rcu();
12575#endif
12576
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080012577 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -080012578}
12579
12580module_init(vmx_init)
12581module_exit(vmx_exit)