blob: 0cbdd4cf9ba0f3a5126e87c289a026c403c58e17 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020021#include "cpuid.h"
Andrey Smetanind62caab2015-11-10 15:36:33 +030022#include "lapic.h"
Avi Kivitye4956062007-06-28 14:15:57 -040023
Avi Kivityedf88412007-12-16 11:02:48 +020024#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020026#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/mm.h>
28#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040029#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020030#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070031#include <linux/mod_devicetable.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040032#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040034#include <linux/tboot.h>
Jan Kiszkaf41245002014-03-07 20:03:13 +010035#include <linux/hrtimer.h>
Josh Poimboeufc207aee2017-06-28 10:11:06 -050036#include <linux/frame.h>
Dan Williams085331d2018-01-31 17:47:03 -080037#include <linux/nospec.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030038#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030039#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040040
Feng Wu28b835d2015-09-18 22:29:54 +080041#include <asm/cpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080042#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080043#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020044#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020045#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080046#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020047#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020048#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010049#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080050#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010051#include <asm/apic.h>
Feng Wuefc64402015-09-18 22:29:51 +080052#include <asm/irq_remapping.h>
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070053#include <asm/mmu_context.h>
Paolo Bonziniecb586b2018-02-22 16:43:17 +010054#include <asm/microcode.h>
David Woodhouse117cc7a2018-01-12 11:11:27 +000055#include <asm/nospec-branch.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080056
Marcelo Tosatti229456f2009-06-17 09:22:14 -030057#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020058#include "pmu.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030059
Avi Kivity4ecac3f2008-05-13 13:23:38 +030060#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040061#define __ex_clear(x, reg) \
62 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030063
Avi Kivity6aa8b732006-12-10 02:21:36 -080064MODULE_AUTHOR("Qumranet");
65MODULE_LICENSE("GPL");
66
Josh Triplette9bda3b2012-03-20 23:33:51 -070067static const struct x86_cpu_id vmx_cpu_id[] = {
68 X86_FEATURE_MATCH(X86_FEATURE_VMX),
69 {}
70};
71MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
72
Rusty Russell476bc002012-01-13 09:32:18 +103073static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020074module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080075
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010076static bool __read_mostly enable_vnmi = 1;
77module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
78
Rusty Russell476bc002012-01-13 09:32:18 +103079static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020080module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020081
Rusty Russell476bc002012-01-13 09:32:18 +103082static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020083module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080084
Rusty Russell476bc002012-01-13 09:32:18 +103085static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070086module_param_named(unrestricted_guest,
87 enable_unrestricted_guest, bool, S_IRUGO);
88
Xudong Hao83c3a332012-05-28 19:33:35 +080089static bool __read_mostly enable_ept_ad_bits = 1;
90module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
91
Avi Kivitya27685c2012-06-12 20:30:18 +030092static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020093module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030094
Rusty Russell476bc002012-01-13 09:32:18 +103095static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030096module_param(fasteoi, bool, S_IRUGO);
97
Yang Zhang5a717852013-04-11 19:25:16 +080098static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +080099module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800100
Abel Gordonabc4fc52013-04-18 14:35:25 +0300101static bool __read_mostly enable_shadow_vmcs = 1;
102module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300103/*
104 * If nested=1, nested virtualization is supported, i.e., guests may use
105 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
106 * use VMX instructions.
107 */
Rusty Russell476bc002012-01-13 09:32:18 +1030108static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300109module_param(nested, bool, S_IRUGO);
110
Wanpeng Li20300092014-12-02 19:14:59 +0800111static u64 __read_mostly host_xss;
112
Kai Huang843e4332015-01-28 10:54:28 +0800113static bool __read_mostly enable_pml = 1;
114module_param_named(pml, enable_pml, bool, S_IRUGO);
115
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100116#define MSR_TYPE_R 1
117#define MSR_TYPE_W 2
118#define MSR_TYPE_RW 3
119
120#define MSR_BITMAP_MODE_X2APIC 1
121#define MSR_BITMAP_MODE_X2APIC_APICV 2
122#define MSR_BITMAP_MODE_LM 4
123
Haozhong Zhang64903d62015-10-20 15:39:09 +0800124#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
125
Yunhong Jiang64672c92016-06-13 14:19:59 -0700126/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
127static int __read_mostly cpu_preemption_timer_multi;
128static bool __read_mostly enable_preemption_timer = 1;
129#ifdef CONFIG_X86_64
130module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
131#endif
132
Gleb Natapov50378782013-02-04 16:00:28 +0200133#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
Sean Christopherson1706bd02018-03-05 12:04:38 -0800134#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
135#define KVM_VM_CR0_ALWAYS_ON \
136 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
137 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200138#define KVM_CR4_GUEST_OWNED_BITS \
139 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Yu Zhangfd8cb432017-08-24 20:27:56 +0800140 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200141
Sean Christopherson5dc1f042018-03-05 12:04:39 -0800142#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
Avi Kivitycdc0e242009-12-06 17:21:14 +0200143#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
144#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
145
Avi Kivity78ac8b42010-04-08 18:19:35 +0300146#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
147
Jan Kiszkaf41245002014-03-07 20:03:13 +0100148#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
149
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800150/*
Jan Dakinevich16c2aec2016-10-28 07:00:30 +0300151 * Hyper-V requires all of these, so mark them as supported even though
152 * they are just treated the same as all-context.
153 */
154#define VMX_VPID_EXTENT_SUPPORTED_MASK \
155 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
156 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
157 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
158 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
159
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800160/*
161 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
162 * ple_gap: upper bound on the amount of time between two successive
163 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500164 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800165 * ple_window: upper bound on the amount of time a guest is allowed to execute
166 * in a PAUSE loop. Tests indicate that most spinlocks are held for
167 * less than 2^12 cycles
168 * Time is measured based on a counter that runs at the same rate as the TSC,
169 * refer SDM volume 3b section 21.6.13 & 22.1.3.
170 */
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200171#define KVM_VMX_DEFAULT_PLE_GAP 128
172#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
173#define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2
174#define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0
175#define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \
176 INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW
177
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800178static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
179module_param(ple_gap, int, S_IRUGO);
180
181static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
182module_param(ple_window, int, S_IRUGO);
183
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200184/* Default doubles per-vcpu window every exit. */
185static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW;
186module_param(ple_window_grow, int, S_IRUGO);
187
188/* Default resets per-vcpu window every exit to ple_window. */
189static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK;
190module_param(ple_window_shrink, int, S_IRUGO);
191
192/* Default is to compute the maximum so we can never overflow. */
193static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
194static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
195module_param(ple_window_max, int, S_IRUGO);
196
Avi Kivity83287ea422012-09-16 15:10:57 +0300197extern const ulong vmx_return;
198
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200199#define NR_AUTOLOAD_MSRS 8
Avi Kivity61d2ef22010-04-28 16:40:38 +0300200
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400201struct vmcs {
202 u32 revision_id;
203 u32 abort;
204 char data[0];
205};
206
Nadav Har'Eld462b812011-05-24 15:26:10 +0300207/*
208 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
209 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
210 * loaded on this CPU (so we can clear them if the CPU goes down).
211 */
212struct loaded_vmcs {
213 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700214 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300215 int cpu;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +0200216 bool launched;
217 bool nmi_known_unmasked;
Ladi Prosek44889942017-09-22 07:53:15 +0200218 unsigned long vmcs_host_cr3; /* May not match real cr3 */
219 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Paolo Bonzini8a1b4392017-11-06 13:31:12 +0100220 /* Support for vnmi-less CPUs */
221 int soft_vnmi_blocked;
222 ktime_t entry_time;
223 s64 vnmi_blocked_time;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100224 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300225 struct list_head loaded_vmcss_on_cpu_link;
226};
227
Avi Kivity26bb0982009-09-07 11:14:12 +0300228struct shared_msr_entry {
229 unsigned index;
230 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200231 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300232};
233
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300234/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300235 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
236 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
237 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
238 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
239 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
240 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattsonde3a0022017-11-27 17:22:25 -0600241 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300242 * underlying hardware which will be used to run L2.
243 * This structure is packed to ensure that its layout is identical across
244 * machines (necessary for live migration).
245 * If there are changes in this struct, VMCS12_REVISION must be changed.
246 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300247typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300248struct __packed vmcs12 {
249 /* According to the Intel spec, a VMCS region must start with the
250 * following two fields. Then follow implementation-specific data.
251 */
252 u32 revision_id;
253 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300254
Nadav Har'El27d6c862011-05-25 23:06:59 +0300255 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
256 u32 padding[7]; /* room for future expansion */
257
Nadav Har'El22bd0352011-05-25 23:05:57 +0300258 u64 io_bitmap_a;
259 u64 io_bitmap_b;
260 u64 msr_bitmap;
261 u64 vm_exit_msr_store_addr;
262 u64 vm_exit_msr_load_addr;
263 u64 vm_entry_msr_load_addr;
264 u64 tsc_offset;
265 u64 virtual_apic_page_addr;
266 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800267 u64 posted_intr_desc_addr;
Bandan Das27c42a12017-08-03 15:54:42 -0400268 u64 vm_function_control;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300269 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800270 u64 eoi_exit_bitmap0;
271 u64 eoi_exit_bitmap1;
272 u64 eoi_exit_bitmap2;
273 u64 eoi_exit_bitmap3;
Bandan Das41ab9372017-08-03 15:54:43 -0400274 u64 eptp_list_address;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800275 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300276 u64 guest_physical_address;
277 u64 vmcs_link_pointer;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400278 u64 pml_address;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300279 u64 guest_ia32_debugctl;
280 u64 guest_ia32_pat;
281 u64 guest_ia32_efer;
282 u64 guest_ia32_perf_global_ctrl;
283 u64 guest_pdptr0;
284 u64 guest_pdptr1;
285 u64 guest_pdptr2;
286 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100287 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300288 u64 host_ia32_pat;
289 u64 host_ia32_efer;
290 u64 host_ia32_perf_global_ctrl;
291 u64 padding64[8]; /* room for future expansion */
292 /*
293 * To allow migration of L1 (complete with its L2 guests) between
294 * machines of different natural widths (32 or 64 bit), we cannot have
295 * unsigned long fields with no explict size. We use u64 (aliased
296 * natural_width) instead. Luckily, x86 is little-endian.
297 */
298 natural_width cr0_guest_host_mask;
299 natural_width cr4_guest_host_mask;
300 natural_width cr0_read_shadow;
301 natural_width cr4_read_shadow;
302 natural_width cr3_target_value0;
303 natural_width cr3_target_value1;
304 natural_width cr3_target_value2;
305 natural_width cr3_target_value3;
306 natural_width exit_qualification;
307 natural_width guest_linear_address;
308 natural_width guest_cr0;
309 natural_width guest_cr3;
310 natural_width guest_cr4;
311 natural_width guest_es_base;
312 natural_width guest_cs_base;
313 natural_width guest_ss_base;
314 natural_width guest_ds_base;
315 natural_width guest_fs_base;
316 natural_width guest_gs_base;
317 natural_width guest_ldtr_base;
318 natural_width guest_tr_base;
319 natural_width guest_gdtr_base;
320 natural_width guest_idtr_base;
321 natural_width guest_dr7;
322 natural_width guest_rsp;
323 natural_width guest_rip;
324 natural_width guest_rflags;
325 natural_width guest_pending_dbg_exceptions;
326 natural_width guest_sysenter_esp;
327 natural_width guest_sysenter_eip;
328 natural_width host_cr0;
329 natural_width host_cr3;
330 natural_width host_cr4;
331 natural_width host_fs_base;
332 natural_width host_gs_base;
333 natural_width host_tr_base;
334 natural_width host_gdtr_base;
335 natural_width host_idtr_base;
336 natural_width host_ia32_sysenter_esp;
337 natural_width host_ia32_sysenter_eip;
338 natural_width host_rsp;
339 natural_width host_rip;
340 natural_width paddingl[8]; /* room for future expansion */
341 u32 pin_based_vm_exec_control;
342 u32 cpu_based_vm_exec_control;
343 u32 exception_bitmap;
344 u32 page_fault_error_code_mask;
345 u32 page_fault_error_code_match;
346 u32 cr3_target_count;
347 u32 vm_exit_controls;
348 u32 vm_exit_msr_store_count;
349 u32 vm_exit_msr_load_count;
350 u32 vm_entry_controls;
351 u32 vm_entry_msr_load_count;
352 u32 vm_entry_intr_info_field;
353 u32 vm_entry_exception_error_code;
354 u32 vm_entry_instruction_len;
355 u32 tpr_threshold;
356 u32 secondary_vm_exec_control;
357 u32 vm_instruction_error;
358 u32 vm_exit_reason;
359 u32 vm_exit_intr_info;
360 u32 vm_exit_intr_error_code;
361 u32 idt_vectoring_info_field;
362 u32 idt_vectoring_error_code;
363 u32 vm_exit_instruction_len;
364 u32 vmx_instruction_info;
365 u32 guest_es_limit;
366 u32 guest_cs_limit;
367 u32 guest_ss_limit;
368 u32 guest_ds_limit;
369 u32 guest_fs_limit;
370 u32 guest_gs_limit;
371 u32 guest_ldtr_limit;
372 u32 guest_tr_limit;
373 u32 guest_gdtr_limit;
374 u32 guest_idtr_limit;
375 u32 guest_es_ar_bytes;
376 u32 guest_cs_ar_bytes;
377 u32 guest_ss_ar_bytes;
378 u32 guest_ds_ar_bytes;
379 u32 guest_fs_ar_bytes;
380 u32 guest_gs_ar_bytes;
381 u32 guest_ldtr_ar_bytes;
382 u32 guest_tr_ar_bytes;
383 u32 guest_interruptibility_info;
384 u32 guest_activity_state;
385 u32 guest_sysenter_cs;
386 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100387 u32 vmx_preemption_timer_value;
388 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300389 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800390 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300391 u16 guest_es_selector;
392 u16 guest_cs_selector;
393 u16 guest_ss_selector;
394 u16 guest_ds_selector;
395 u16 guest_fs_selector;
396 u16 guest_gs_selector;
397 u16 guest_ldtr_selector;
398 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800399 u16 guest_intr_status;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400400 u16 guest_pml_index;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300401 u16 host_es_selector;
402 u16 host_cs_selector;
403 u16 host_ss_selector;
404 u16 host_ds_selector;
405 u16 host_fs_selector;
406 u16 host_gs_selector;
407 u16 host_tr_selector;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300408};
409
410/*
411 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
412 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
413 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
414 */
415#define VMCS12_REVISION 0x11e57ed0
416
417/*
418 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
419 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
420 * current implementation, 4K are reserved to avoid future complications.
421 */
422#define VMCS12_SIZE 0x1000
423
424/*
Jim Mattson5b157062017-12-22 12:11:12 -0800425 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
426 * supported VMCS12 field encoding.
427 */
428#define VMCS12_MAX_FIELD_INDEX 0x17
429
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100430struct nested_vmx_msrs {
431 /*
432 * We only store the "true" versions of the VMX capability MSRs. We
433 * generate the "non-true" versions by setting the must-be-1 bits
434 * according to the SDM.
435 */
436 u32 procbased_ctls_low;
437 u32 procbased_ctls_high;
438 u32 secondary_ctls_low;
439 u32 secondary_ctls_high;
440 u32 pinbased_ctls_low;
441 u32 pinbased_ctls_high;
442 u32 exit_ctls_low;
443 u32 exit_ctls_high;
444 u32 entry_ctls_low;
445 u32 entry_ctls_high;
446 u32 misc_low;
447 u32 misc_high;
448 u32 ept_caps;
449 u32 vpid_caps;
450 u64 basic;
451 u64 cr0_fixed0;
452 u64 cr0_fixed1;
453 u64 cr4_fixed0;
454 u64 cr4_fixed1;
455 u64 vmcs_enum;
456 u64 vmfunc_controls;
457};
458
Jim Mattson5b157062017-12-22 12:11:12 -0800459/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300460 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
461 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
462 */
463struct nested_vmx {
464 /* Has the level1 guest done vmxon? */
465 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400466 gpa_t vmxon_ptr;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400467 bool pml_full;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300468
469 /* The guest-physical address of the current VMCS L1 keeps for L2 */
470 gpa_t current_vmptr;
David Matlack4f2777b2016-07-13 17:16:37 -0700471 /*
472 * Cache of the guest's VMCS, existing outside of guest memory.
473 * Loaded from guest memory during VMPTRLD. Flushed to guest
David Matlack8ca44e82017-08-01 14:00:39 -0700474 * memory during VMCLEAR and VMPTRLD.
David Matlack4f2777b2016-07-13 17:16:37 -0700475 */
476 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300477 /*
478 * Indicates if the shadow vmcs must be updated with the
479 * data hold by vmcs12
480 */
481 bool sync_shadow_vmcs;
Paolo Bonzini74a497f2017-12-20 13:55:39 +0100482 bool dirty_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300483
Radim Krčmářdccbfcf2016-08-08 20:16:23 +0200484 bool change_vmcs01_virtual_x2apic_mode;
Nadav Har'El644d7112011-05-25 23:12:35 +0300485 /* L2 must run next, and mustn't decide to exit to L1. */
486 bool nested_run_pending;
Jim Mattsonde3a0022017-11-27 17:22:25 -0600487
488 struct loaded_vmcs vmcs02;
489
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300490 /*
Jim Mattsonde3a0022017-11-27 17:22:25 -0600491 * Guest pages referred to in the vmcs02 with host-physical
492 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300493 */
494 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800495 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800496 struct page *pi_desc_page;
497 struct pi_desc *pi_desc;
498 bool pi_pending;
499 u16 posted_intr_nv;
Jan Kiszkaf41245002014-03-07 20:03:13 +0100500
501 struct hrtimer preemption_timer;
502 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200503
504 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
505 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800506
Wanpeng Li5c614b32015-10-13 09:18:36 -0700507 u16 vpid02;
508 u16 last_vpid;
509
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100510 struct nested_vmx_msrs msrs;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200511
512 /* SMM related state */
513 struct {
514 /* in VMX operation on SMM entry? */
515 bool vmxon;
516 /* in guest mode on SMM entry? */
517 bool guest_mode;
518 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300519};
520
Yang Zhang01e439b2013-04-11 19:25:12 +0800521#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800522#define POSTED_INTR_SN 1
523
Yang Zhang01e439b2013-04-11 19:25:12 +0800524/* Posted-Interrupt Descriptor */
525struct pi_desc {
526 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800527 union {
528 struct {
529 /* bit 256 - Outstanding Notification */
530 u16 on : 1,
531 /* bit 257 - Suppress Notification */
532 sn : 1,
533 /* bit 271:258 - Reserved */
534 rsvd_1 : 14;
535 /* bit 279:272 - Notification Vector */
536 u8 nv;
537 /* bit 287:280 - Reserved */
538 u8 rsvd_2;
539 /* bit 319:288 - Notification Destination */
540 u32 ndst;
541 };
542 u64 control;
543 };
544 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800545} __aligned(64);
546
Yang Zhanga20ed542013-04-11 19:25:15 +0800547static bool pi_test_and_set_on(struct pi_desc *pi_desc)
548{
549 return test_and_set_bit(POSTED_INTR_ON,
550 (unsigned long *)&pi_desc->control);
551}
552
553static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
554{
555 return test_and_clear_bit(POSTED_INTR_ON,
556 (unsigned long *)&pi_desc->control);
557}
558
559static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
560{
561 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
562}
563
Feng Wuebbfc762015-09-18 22:29:46 +0800564static inline void pi_clear_sn(struct pi_desc *pi_desc)
565{
566 return clear_bit(POSTED_INTR_SN,
567 (unsigned long *)&pi_desc->control);
568}
569
570static inline void pi_set_sn(struct pi_desc *pi_desc)
571{
572 return set_bit(POSTED_INTR_SN,
573 (unsigned long *)&pi_desc->control);
574}
575
Paolo Bonziniad361092016-09-20 16:15:05 +0200576static inline void pi_clear_on(struct pi_desc *pi_desc)
577{
578 clear_bit(POSTED_INTR_ON,
579 (unsigned long *)&pi_desc->control);
580}
581
Feng Wuebbfc762015-09-18 22:29:46 +0800582static inline int pi_test_on(struct pi_desc *pi_desc)
583{
584 return test_bit(POSTED_INTR_ON,
585 (unsigned long *)&pi_desc->control);
586}
587
588static inline int pi_test_sn(struct pi_desc *pi_desc)
589{
590 return test_bit(POSTED_INTR_SN,
591 (unsigned long *)&pi_desc->control);
592}
593
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400594struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000595 struct kvm_vcpu vcpu;
Avi Kivity313dbd42008-07-17 18:04:30 +0300596 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300597 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100598 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300599 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200600 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200601 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300602 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400603 int nmsrs;
604 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800605 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400606#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300607 u64 msr_host_kernel_gs_base;
608 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400609#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100610
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100611 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100612 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100613
Gleb Natapov2961e8762013-11-25 15:37:13 +0200614 u32 vm_entry_controls_shadow;
615 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200616 u32 secondary_exec_control;
617
Nadav Har'Eld462b812011-05-24 15:26:10 +0300618 /*
619 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
620 * non-nested (L1) guest, it always points to vmcs01. For a nested
621 * guest (L2), it points to a different VMCS.
622 */
623 struct loaded_vmcs vmcs01;
624 struct loaded_vmcs *loaded_vmcs;
625 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300626 struct msr_autoload {
627 unsigned nr;
628 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
629 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
630 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400631 struct {
632 int loaded;
633 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300634#ifdef CONFIG_X86_64
635 u16 ds_sel, es_sel;
636#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200637 int gs_ldt_reload_needed;
638 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000639 u64 msr_host_bndcfgs;
Mike Dayd77c26f2007-10-08 09:02:08 -0400640 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200641 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300642 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300643 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300644 struct kvm_segment segs[8];
645 } rmode;
646 struct {
647 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300648 struct kvm_save_segment {
649 u16 selector;
650 unsigned long base;
651 u32 limit;
652 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300653 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300654 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800655 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300656 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200657
Andi Kleena0861c02009-06-08 17:37:09 +0800658 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800659
Yang Zhang01e439b2013-04-11 19:25:12 +0800660 /* Posted interrupt descriptor */
661 struct pi_desc pi_desc;
662
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300663 /* Support for a guest hypervisor (nested VMX) */
664 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200665
666 /* Dynamic PLE window. */
667 int ple_window;
668 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800669
670 /* Support for PML */
671#define PML_ENTITY_NUM 512
672 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800673
Yunhong Jiang64672c92016-06-13 14:19:59 -0700674 /* apic deadline value in host tsc */
675 u64 hv_deadline_tsc;
676
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800677 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800678
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800679 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800680
Wanpeng Li74c55932017-11-29 01:31:20 -0800681 unsigned long host_debugctlmsr;
682
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800683 /*
684 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
685 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
686 * in msr_ia32_feature_control_valid_bits.
687 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800688 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800689 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400690};
691
Avi Kivity2fb92db2011-04-27 19:42:18 +0300692enum segment_cache_field {
693 SEG_FIELD_SEL = 0,
694 SEG_FIELD_BASE = 1,
695 SEG_FIELD_LIMIT = 2,
696 SEG_FIELD_AR = 3,
697
698 SEG_FIELD_NR = 4
699};
700
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400701static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
702{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000703 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400704}
705
Feng Wuefc64402015-09-18 22:29:51 +0800706static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
707{
708 return &(to_vmx(vcpu)->pi_desc);
709}
710
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800711#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +0300712#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800713#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
714#define FIELD64(number, name) \
715 FIELD(number, name), \
716 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +0300717
Abel Gordon4607c2d2013-04-18 14:35:55 +0300718
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100719static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100720#define SHADOW_FIELD_RO(x) x,
721#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300722};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400723static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300724 ARRAY_SIZE(shadow_read_only_fields);
725
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100726static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100727#define SHADOW_FIELD_RW(x) x,
728#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300729};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400730static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300731 ARRAY_SIZE(shadow_read_write_fields);
732
Mathias Krause772e0312012-08-30 01:30:19 +0200733static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300734 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800735 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300736 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
737 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
738 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
739 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
740 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
741 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
742 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
743 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800744 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -0400745 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300746 FIELD(HOST_ES_SELECTOR, host_es_selector),
747 FIELD(HOST_CS_SELECTOR, host_cs_selector),
748 FIELD(HOST_SS_SELECTOR, host_ss_selector),
749 FIELD(HOST_DS_SELECTOR, host_ds_selector),
750 FIELD(HOST_FS_SELECTOR, host_fs_selector),
751 FIELD(HOST_GS_SELECTOR, host_gs_selector),
752 FIELD(HOST_TR_SELECTOR, host_tr_selector),
753 FIELD64(IO_BITMAP_A, io_bitmap_a),
754 FIELD64(IO_BITMAP_B, io_bitmap_b),
755 FIELD64(MSR_BITMAP, msr_bitmap),
756 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
757 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
758 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
759 FIELD64(TSC_OFFSET, tsc_offset),
760 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
761 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800762 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -0400763 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300764 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800765 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
766 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
767 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
768 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -0400769 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800770 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300771 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
772 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
Bandan Dasc5f983f2017-05-05 15:25:14 -0400773 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300774 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
775 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
776 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
777 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
778 FIELD64(GUEST_PDPTR0, guest_pdptr0),
779 FIELD64(GUEST_PDPTR1, guest_pdptr1),
780 FIELD64(GUEST_PDPTR2, guest_pdptr2),
781 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100782 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300783 FIELD64(HOST_IA32_PAT, host_ia32_pat),
784 FIELD64(HOST_IA32_EFER, host_ia32_efer),
785 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
786 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
787 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
788 FIELD(EXCEPTION_BITMAP, exception_bitmap),
789 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
790 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
791 FIELD(CR3_TARGET_COUNT, cr3_target_count),
792 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
793 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
794 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
795 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
796 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
797 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
798 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
799 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
800 FIELD(TPR_THRESHOLD, tpr_threshold),
801 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
802 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
803 FIELD(VM_EXIT_REASON, vm_exit_reason),
804 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
805 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
806 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
807 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
808 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
809 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
810 FIELD(GUEST_ES_LIMIT, guest_es_limit),
811 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
812 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
813 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
814 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
815 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
816 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
817 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
818 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
819 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
820 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
821 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
822 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
823 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
824 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
825 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
826 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
827 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
828 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
829 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
830 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
831 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +0100832 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300833 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
834 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
835 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
836 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
837 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
838 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
839 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
840 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
841 FIELD(EXIT_QUALIFICATION, exit_qualification),
842 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
843 FIELD(GUEST_CR0, guest_cr0),
844 FIELD(GUEST_CR3, guest_cr3),
845 FIELD(GUEST_CR4, guest_cr4),
846 FIELD(GUEST_ES_BASE, guest_es_base),
847 FIELD(GUEST_CS_BASE, guest_cs_base),
848 FIELD(GUEST_SS_BASE, guest_ss_base),
849 FIELD(GUEST_DS_BASE, guest_ds_base),
850 FIELD(GUEST_FS_BASE, guest_fs_base),
851 FIELD(GUEST_GS_BASE, guest_gs_base),
852 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
853 FIELD(GUEST_TR_BASE, guest_tr_base),
854 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
855 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
856 FIELD(GUEST_DR7, guest_dr7),
857 FIELD(GUEST_RSP, guest_rsp),
858 FIELD(GUEST_RIP, guest_rip),
859 FIELD(GUEST_RFLAGS, guest_rflags),
860 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
861 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
862 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
863 FIELD(HOST_CR0, host_cr0),
864 FIELD(HOST_CR3, host_cr3),
865 FIELD(HOST_CR4, host_cr4),
866 FIELD(HOST_FS_BASE, host_fs_base),
867 FIELD(HOST_GS_BASE, host_gs_base),
868 FIELD(HOST_TR_BASE, host_tr_base),
869 FIELD(HOST_GDTR_BASE, host_gdtr_base),
870 FIELD(HOST_IDTR_BASE, host_idtr_base),
871 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
872 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
873 FIELD(HOST_RSP, host_rsp),
874 FIELD(HOST_RIP, host_rip),
875};
Nadav Har'El22bd0352011-05-25 23:05:57 +0300876
877static inline short vmcs_field_to_offset(unsigned long field)
878{
Dan Williams085331d2018-01-31 17:47:03 -0800879 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
880 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800881 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +0100882
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800883 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -0800884 return -ENOENT;
885
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800886 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -0800887 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -0800888 return -ENOENT;
889
Linus Torvalds15303ba2018-02-10 13:16:35 -0800890 index = array_index_nospec(index, size);
891 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -0800892 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +0100893 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -0800894 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300895}
896
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300897static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
898{
David Matlack4f2777b2016-07-13 17:16:37 -0700899 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300900}
901
Peter Feiner995f00a2017-06-30 17:26:32 -0700902static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +0300903static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -0700904static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +0800905static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +0300906static void vmx_set_segment(struct kvm_vcpu *vcpu,
907 struct kvm_segment *var, int seg);
908static void vmx_get_segment(struct kvm_vcpu *vcpu,
909 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +0200910static bool guest_state_valid(struct kvm_vcpu *vcpu);
911static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +0300912static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +0200913static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
914static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
915static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
916 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100917static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +0100918static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
919 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +0300920
Avi Kivity6aa8b732006-12-10 02:21:36 -0800921static DEFINE_PER_CPU(struct vmcs *, vmxarea);
922static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300923/*
924 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
925 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
926 */
927static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800928
Feng Wubf9f6ac2015-09-18 22:29:55 +0800929/*
930 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
931 * can find which vCPU should be waken up.
932 */
933static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
934static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
935
Radim Krčmář23611332016-09-29 22:41:33 +0200936enum {
Radim Krčmář23611332016-09-29 22:41:33 +0200937 VMX_VMREAD_BITMAP,
938 VMX_VMWRITE_BITMAP,
939 VMX_BITMAP_NR
940};
941
942static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
943
Radim Krčmář23611332016-09-29 22:41:33 +0200944#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
945#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +0300946
Avi Kivity110312c2010-12-21 12:54:20 +0200947static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200948static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +0200949
Sheng Yang2384d2b2008-01-17 15:14:33 +0800950static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
951static DEFINE_SPINLOCK(vmx_vpid_lock);
952
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300953static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800954 int size;
955 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +0300956 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800957 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300958 u32 pin_based_exec_ctrl;
959 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800960 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300961 u32 vmexit_ctrl;
962 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +0100963 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300964} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800965
Hannes Ederefff9e52008-11-28 17:02:06 +0100966static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800967 u32 ept;
968 u32 vpid;
969} vmx_capability;
970
Avi Kivity6aa8b732006-12-10 02:21:36 -0800971#define VMX_SEGMENT_FIELD(seg) \
972 [VCPU_SREG_##seg] = { \
973 .selector = GUEST_##seg##_SELECTOR, \
974 .base = GUEST_##seg##_BASE, \
975 .limit = GUEST_##seg##_LIMIT, \
976 .ar_bytes = GUEST_##seg##_AR_BYTES, \
977 }
978
Mathias Krause772e0312012-08-30 01:30:19 +0200979static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800980 unsigned selector;
981 unsigned base;
982 unsigned limit;
983 unsigned ar_bytes;
984} kvm_vmx_segment_fields[] = {
985 VMX_SEGMENT_FIELD(CS),
986 VMX_SEGMENT_FIELD(DS),
987 VMX_SEGMENT_FIELD(ES),
988 VMX_SEGMENT_FIELD(FS),
989 VMX_SEGMENT_FIELD(GS),
990 VMX_SEGMENT_FIELD(SS),
991 VMX_SEGMENT_FIELD(TR),
992 VMX_SEGMENT_FIELD(LDTR),
993};
994
Avi Kivity26bb0982009-09-07 11:14:12 +0300995static u64 host_efer;
996
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300997static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
998
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300999/*
Brian Gerst8c065852010-07-17 09:03:26 -04001000 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001001 * away by decrementing the array size.
1002 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001003static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001004#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001005 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001006#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001007 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001008};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001009
Jan Kiszka5bb16012016-02-09 20:14:21 +01001010static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001011{
1012 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1013 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001014 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1015}
1016
Jan Kiszka6f054852016-02-09 20:15:18 +01001017static inline bool is_debug(u32 intr_info)
1018{
1019 return is_exception_n(intr_info, DB_VECTOR);
1020}
1021
1022static inline bool is_breakpoint(u32 intr_info)
1023{
1024 return is_exception_n(intr_info, BP_VECTOR);
1025}
1026
Jan Kiszka5bb16012016-02-09 20:14:21 +01001027static inline bool is_page_fault(u32 intr_info)
1028{
1029 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001030}
1031
Gui Jianfeng31299942010-03-15 17:29:09 +08001032static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001033{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001034 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001035}
1036
Gui Jianfeng31299942010-03-15 17:29:09 +08001037static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001038{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001039 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001040}
1041
Liran Alon9e869482018-03-12 13:12:51 +02001042static inline bool is_gp_fault(u32 intr_info)
1043{
1044 return is_exception_n(intr_info, GP_VECTOR);
1045}
1046
Gui Jianfeng31299942010-03-15 17:29:09 +08001047static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001048{
1049 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1050 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1051}
1052
Gui Jianfeng31299942010-03-15 17:29:09 +08001053static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001054{
1055 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1056 INTR_INFO_VALID_MASK)) ==
1057 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1058}
1059
Gui Jianfeng31299942010-03-15 17:29:09 +08001060static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001061{
Sheng Yang04547152009-04-01 15:52:31 +08001062 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001063}
1064
Gui Jianfeng31299942010-03-15 17:29:09 +08001065static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001066{
Sheng Yang04547152009-04-01 15:52:31 +08001067 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001068}
1069
Paolo Bonzini35754c92015-07-29 12:05:37 +02001070static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001071{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001072 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001073}
1074
Gui Jianfeng31299942010-03-15 17:29:09 +08001075static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001076{
Sheng Yang04547152009-04-01 15:52:31 +08001077 return vmcs_config.cpu_based_exec_ctrl &
1078 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001079}
1080
Avi Kivity774ead32007-12-26 13:57:04 +02001081static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001082{
Sheng Yang04547152009-04-01 15:52:31 +08001083 return vmcs_config.cpu_based_2nd_exec_ctrl &
1084 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1085}
1086
Yang Zhang8d146952013-01-25 10:18:50 +08001087static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1088{
1089 return vmcs_config.cpu_based_2nd_exec_ctrl &
1090 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1091}
1092
Yang Zhang83d4c282013-01-25 10:18:49 +08001093static inline bool cpu_has_vmx_apic_register_virt(void)
1094{
1095 return vmcs_config.cpu_based_2nd_exec_ctrl &
1096 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1097}
1098
Yang Zhangc7c9c562013-01-25 10:18:51 +08001099static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1100{
1101 return vmcs_config.cpu_based_2nd_exec_ctrl &
1102 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1103}
1104
Yunhong Jiang64672c92016-06-13 14:19:59 -07001105/*
1106 * Comment's format: document - errata name - stepping - processor name.
1107 * Refer from
1108 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1109 */
1110static u32 vmx_preemption_cpu_tfms[] = {
1111/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
11120x000206E6,
1113/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1114/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1115/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
11160x00020652,
1117/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
11180x00020655,
1119/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1120/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1121/*
1122 * 320767.pdf - AAP86 - B1 -
1123 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1124 */
11250x000106E5,
1126/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
11270x000106A0,
1128/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
11290x000106A1,
1130/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
11310x000106A4,
1132 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1133 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1134 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
11350x000106A5,
1136};
1137
1138static inline bool cpu_has_broken_vmx_preemption_timer(void)
1139{
1140 u32 eax = cpuid_eax(0x00000001), i;
1141
1142 /* Clear the reserved bits */
1143 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001144 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001145 if (eax == vmx_preemption_cpu_tfms[i])
1146 return true;
1147
1148 return false;
1149}
1150
1151static inline bool cpu_has_vmx_preemption_timer(void)
1152{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001153 return vmcs_config.pin_based_exec_ctrl &
1154 PIN_BASED_VMX_PREEMPTION_TIMER;
1155}
1156
Yang Zhang01e439b2013-04-11 19:25:12 +08001157static inline bool cpu_has_vmx_posted_intr(void)
1158{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001159 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1160 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001161}
1162
1163static inline bool cpu_has_vmx_apicv(void)
1164{
1165 return cpu_has_vmx_apic_register_virt() &&
1166 cpu_has_vmx_virtual_intr_delivery() &&
1167 cpu_has_vmx_posted_intr();
1168}
1169
Sheng Yang04547152009-04-01 15:52:31 +08001170static inline bool cpu_has_vmx_flexpriority(void)
1171{
1172 return cpu_has_vmx_tpr_shadow() &&
1173 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001174}
1175
Marcelo Tosattie7997942009-06-11 12:07:40 -03001176static inline bool cpu_has_vmx_ept_execute_only(void)
1177{
Gui Jianfeng31299942010-03-15 17:29:09 +08001178 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001179}
1180
Marcelo Tosattie7997942009-06-11 12:07:40 -03001181static inline bool cpu_has_vmx_ept_2m_page(void)
1182{
Gui Jianfeng31299942010-03-15 17:29:09 +08001183 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001184}
1185
Sheng Yang878403b2010-01-05 19:02:29 +08001186static inline bool cpu_has_vmx_ept_1g_page(void)
1187{
Gui Jianfeng31299942010-03-15 17:29:09 +08001188 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001189}
1190
Sheng Yang4bc9b982010-06-02 14:05:24 +08001191static inline bool cpu_has_vmx_ept_4levels(void)
1192{
1193 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1194}
1195
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001196static inline bool cpu_has_vmx_ept_mt_wb(void)
1197{
1198 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1199}
1200
Yu Zhang855feb62017-08-24 20:27:55 +08001201static inline bool cpu_has_vmx_ept_5levels(void)
1202{
1203 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1204}
1205
Xudong Hao83c3a332012-05-28 19:33:35 +08001206static inline bool cpu_has_vmx_ept_ad_bits(void)
1207{
1208 return vmx_capability.ept & VMX_EPT_AD_BIT;
1209}
1210
Gui Jianfeng31299942010-03-15 17:29:09 +08001211static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001212{
Gui Jianfeng31299942010-03-15 17:29:09 +08001213 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001214}
1215
Gui Jianfeng31299942010-03-15 17:29:09 +08001216static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001217{
Gui Jianfeng31299942010-03-15 17:29:09 +08001218 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001219}
1220
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001221static inline bool cpu_has_vmx_invvpid_single(void)
1222{
1223 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1224}
1225
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001226static inline bool cpu_has_vmx_invvpid_global(void)
1227{
1228 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1229}
1230
Wanpeng Li08d839c2017-03-23 05:30:08 -07001231static inline bool cpu_has_vmx_invvpid(void)
1232{
1233 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1234}
1235
Gui Jianfeng31299942010-03-15 17:29:09 +08001236static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001237{
Sheng Yang04547152009-04-01 15:52:31 +08001238 return vmcs_config.cpu_based_2nd_exec_ctrl &
1239 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001240}
1241
Gui Jianfeng31299942010-03-15 17:29:09 +08001242static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001243{
1244 return vmcs_config.cpu_based_2nd_exec_ctrl &
1245 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1246}
1247
Gui Jianfeng31299942010-03-15 17:29:09 +08001248static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001249{
1250 return vmcs_config.cpu_based_2nd_exec_ctrl &
1251 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1252}
1253
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001254static inline bool cpu_has_vmx_basic_inout(void)
1255{
1256 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1257}
1258
Paolo Bonzini35754c92015-07-29 12:05:37 +02001259static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001260{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001261 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001262}
1263
Gui Jianfeng31299942010-03-15 17:29:09 +08001264static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001265{
Sheng Yang04547152009-04-01 15:52:31 +08001266 return vmcs_config.cpu_based_2nd_exec_ctrl &
1267 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001268}
1269
Gui Jianfeng31299942010-03-15 17:29:09 +08001270static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001271{
1272 return vmcs_config.cpu_based_2nd_exec_ctrl &
1273 SECONDARY_EXEC_RDTSCP;
1274}
1275
Mao, Junjiead756a12012-07-02 01:18:48 +00001276static inline bool cpu_has_vmx_invpcid(void)
1277{
1278 return vmcs_config.cpu_based_2nd_exec_ctrl &
1279 SECONDARY_EXEC_ENABLE_INVPCID;
1280}
1281
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001282static inline bool cpu_has_virtual_nmis(void)
1283{
1284 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1285}
1286
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001287static inline bool cpu_has_vmx_wbinvd_exit(void)
1288{
1289 return vmcs_config.cpu_based_2nd_exec_ctrl &
1290 SECONDARY_EXEC_WBINVD_EXITING;
1291}
1292
Abel Gordonabc4fc52013-04-18 14:35:25 +03001293static inline bool cpu_has_vmx_shadow_vmcs(void)
1294{
1295 u64 vmx_msr;
1296 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1297 /* check if the cpu supports writing r/o exit information fields */
1298 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1299 return false;
1300
1301 return vmcs_config.cpu_based_2nd_exec_ctrl &
1302 SECONDARY_EXEC_SHADOW_VMCS;
1303}
1304
Kai Huang843e4332015-01-28 10:54:28 +08001305static inline bool cpu_has_vmx_pml(void)
1306{
1307 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1308}
1309
Haozhong Zhang64903d62015-10-20 15:39:09 +08001310static inline bool cpu_has_vmx_tsc_scaling(void)
1311{
1312 return vmcs_config.cpu_based_2nd_exec_ctrl &
1313 SECONDARY_EXEC_TSC_SCALING;
1314}
1315
Bandan Das2a499e42017-08-03 15:54:41 -04001316static inline bool cpu_has_vmx_vmfunc(void)
1317{
1318 return vmcs_config.cpu_based_2nd_exec_ctrl &
1319 SECONDARY_EXEC_ENABLE_VMFUNC;
1320}
1321
Sheng Yang04547152009-04-01 15:52:31 +08001322static inline bool report_flexpriority(void)
1323{
1324 return flexpriority_enabled;
1325}
1326
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001327static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1328{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001329 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001330}
1331
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001332static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1333{
1334 return vmcs12->cpu_based_vm_exec_control & bit;
1335}
1336
1337static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1338{
1339 return (vmcs12->cpu_based_vm_exec_control &
1340 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1341 (vmcs12->secondary_vm_exec_control & bit);
1342}
1343
Jan Kiszkaf41245002014-03-07 20:03:13 +01001344static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1345{
1346 return vmcs12->pin_based_vm_exec_control &
1347 PIN_BASED_VMX_PREEMPTION_TIMER;
1348}
1349
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001350static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1351{
1352 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1353}
1354
1355static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1356{
1357 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1358}
1359
Nadav Har'El155a97a2013-08-05 11:07:16 +03001360static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1361{
1362 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1363}
1364
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001365static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1366{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001367 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001368}
1369
Bandan Dasc5f983f2017-05-05 15:25:14 -04001370static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1371{
1372 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1373}
1374
Wincy Vanf2b93282015-02-03 23:56:03 +08001375static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1376{
1377 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1378}
1379
Wanpeng Li5c614b32015-10-13 09:18:36 -07001380static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1381{
1382 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1383}
1384
Wincy Van82f0dd42015-02-03 23:57:18 +08001385static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1386{
1387 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1388}
1389
Wincy Van608406e2015-02-03 23:57:51 +08001390static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1391{
1392 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1393}
1394
Wincy Van705699a2015-02-03 23:58:17 +08001395static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1396{
1397 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1398}
1399
Bandan Das27c42a12017-08-03 15:54:42 -04001400static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1401{
1402 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1403}
1404
Bandan Das41ab9372017-08-03 15:54:43 -04001405static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1406{
1407 return nested_cpu_has_vmfunc(vmcs12) &&
1408 (vmcs12->vm_function_control &
1409 VMX_VMFUNC_EPTP_SWITCHING);
1410}
1411
Jim Mattsonef85b672016-12-12 11:01:37 -08001412static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001413{
1414 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08001415 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001416}
1417
Jan Kiszka533558b2014-01-04 18:47:20 +01001418static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1419 u32 exit_intr_info,
1420 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001421static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1422 struct vmcs12 *vmcs12,
1423 u32 reason, unsigned long qualification);
1424
Rusty Russell8b9cf982007-07-30 16:31:43 +10001425static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001426{
1427 int i;
1428
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001429 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001430 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001431 return i;
1432 return -1;
1433}
1434
Sheng Yang2384d2b2008-01-17 15:14:33 +08001435static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1436{
1437 struct {
1438 u64 vpid : 16;
1439 u64 rsvd : 48;
1440 u64 gva;
1441 } operand = { vpid, 0, gva };
1442
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001443 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001444 /* CF==1 or ZF==1 --> rc = -1 */
1445 "; ja 1f ; ud2 ; 1:"
1446 : : "a"(&operand), "c"(ext) : "cc", "memory");
1447}
1448
Sheng Yang14394422008-04-28 12:24:45 +08001449static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1450{
1451 struct {
1452 u64 eptp, gpa;
1453 } operand = {eptp, gpa};
1454
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001455 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001456 /* CF==1 or ZF==1 --> rc = -1 */
1457 "; ja 1f ; ud2 ; 1:\n"
1458 : : "a" (&operand), "c" (ext) : "cc", "memory");
1459}
1460
Avi Kivity26bb0982009-09-07 11:14:12 +03001461static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001462{
1463 int i;
1464
Rusty Russell8b9cf982007-07-30 16:31:43 +10001465 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001466 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001467 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001468 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001469}
1470
Avi Kivity6aa8b732006-12-10 02:21:36 -08001471static void vmcs_clear(struct vmcs *vmcs)
1472{
1473 u64 phys_addr = __pa(vmcs);
1474 u8 error;
1475
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001476 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001477 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001478 : "cc", "memory");
1479 if (error)
1480 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1481 vmcs, phys_addr);
1482}
1483
Nadav Har'Eld462b812011-05-24 15:26:10 +03001484static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1485{
1486 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001487 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1488 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001489 loaded_vmcs->cpu = -1;
1490 loaded_vmcs->launched = 0;
1491}
1492
Dongxiao Xu7725b892010-05-11 18:29:38 +08001493static void vmcs_load(struct vmcs *vmcs)
1494{
1495 u64 phys_addr = __pa(vmcs);
1496 u8 error;
1497
1498 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001499 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001500 : "cc", "memory");
1501 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001502 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001503 vmcs, phys_addr);
1504}
1505
Dave Young2965faa2015-09-09 15:38:55 -07001506#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001507/*
1508 * This bitmap is used to indicate whether the vmclear
1509 * operation is enabled on all cpus. All disabled by
1510 * default.
1511 */
1512static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1513
1514static inline void crash_enable_local_vmclear(int cpu)
1515{
1516 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1517}
1518
1519static inline void crash_disable_local_vmclear(int cpu)
1520{
1521 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1522}
1523
1524static inline int crash_local_vmclear_enabled(int cpu)
1525{
1526 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1527}
1528
1529static void crash_vmclear_local_loaded_vmcss(void)
1530{
1531 int cpu = raw_smp_processor_id();
1532 struct loaded_vmcs *v;
1533
1534 if (!crash_local_vmclear_enabled(cpu))
1535 return;
1536
1537 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1538 loaded_vmcss_on_cpu_link)
1539 vmcs_clear(v->vmcs);
1540}
1541#else
1542static inline void crash_enable_local_vmclear(int cpu) { }
1543static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001544#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001545
Nadav Har'Eld462b812011-05-24 15:26:10 +03001546static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001547{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001548 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001549 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001550
Nadav Har'Eld462b812011-05-24 15:26:10 +03001551 if (loaded_vmcs->cpu != cpu)
1552 return; /* vcpu migration can race with cpu offline */
1553 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001554 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001555 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001556 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001557
1558 /*
1559 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1560 * is before setting loaded_vmcs->vcpu to -1 which is done in
1561 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1562 * then adds the vmcs into percpu list before it is deleted.
1563 */
1564 smp_wmb();
1565
Nadav Har'Eld462b812011-05-24 15:26:10 +03001566 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001567 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001568}
1569
Nadav Har'Eld462b812011-05-24 15:26:10 +03001570static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001571{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001572 int cpu = loaded_vmcs->cpu;
1573
1574 if (cpu != -1)
1575 smp_call_function_single(cpu,
1576 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001577}
1578
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001579static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001580{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001581 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001582 return;
1583
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001584 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001585 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001586}
1587
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001588static inline void vpid_sync_vcpu_global(void)
1589{
1590 if (cpu_has_vmx_invvpid_global())
1591 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1592}
1593
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001594static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001595{
1596 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001597 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001598 else
1599 vpid_sync_vcpu_global();
1600}
1601
Sheng Yang14394422008-04-28 12:24:45 +08001602static inline void ept_sync_global(void)
1603{
David Hildenbrandf5f51582017-08-24 20:51:30 +02001604 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08001605}
1606
1607static inline void ept_sync_context(u64 eptp)
1608{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02001609 if (cpu_has_vmx_invept_context())
1610 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1611 else
1612 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08001613}
1614
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001615static __always_inline void vmcs_check16(unsigned long field)
1616{
1617 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1618 "16-bit accessor invalid for 64-bit field");
1619 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1620 "16-bit accessor invalid for 64-bit high field");
1621 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1622 "16-bit accessor invalid for 32-bit high field");
1623 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1624 "16-bit accessor invalid for natural width field");
1625}
1626
1627static __always_inline void vmcs_check32(unsigned long field)
1628{
1629 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1630 "32-bit accessor invalid for 16-bit field");
1631 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1632 "32-bit accessor invalid for natural width field");
1633}
1634
1635static __always_inline void vmcs_check64(unsigned long field)
1636{
1637 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1638 "64-bit accessor invalid for 16-bit field");
1639 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1640 "64-bit accessor invalid for 64-bit high field");
1641 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1642 "64-bit accessor invalid for 32-bit field");
1643 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1644 "64-bit accessor invalid for natural width field");
1645}
1646
1647static __always_inline void vmcs_checkl(unsigned long field)
1648{
1649 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1650 "Natural width accessor invalid for 16-bit field");
1651 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1652 "Natural width accessor invalid for 64-bit field");
1653 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1654 "Natural width accessor invalid for 64-bit high field");
1655 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1656 "Natural width accessor invalid for 32-bit field");
1657}
1658
1659static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001660{
Avi Kivity5e520e62011-05-15 10:13:12 -04001661 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001662
Avi Kivity5e520e62011-05-15 10:13:12 -04001663 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1664 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001665 return value;
1666}
1667
Avi Kivity96304212011-05-15 10:13:13 -04001668static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001669{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001670 vmcs_check16(field);
1671 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001672}
1673
Avi Kivity96304212011-05-15 10:13:13 -04001674static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001675{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001676 vmcs_check32(field);
1677 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001678}
1679
Avi Kivity96304212011-05-15 10:13:13 -04001680static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001681{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001682 vmcs_check64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001683#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001684 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001685#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001686 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001687#endif
1688}
1689
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001690static __always_inline unsigned long vmcs_readl(unsigned long field)
1691{
1692 vmcs_checkl(field);
1693 return __vmcs_readl(field);
1694}
1695
Avi Kivitye52de1b2007-01-05 16:36:56 -08001696static noinline void vmwrite_error(unsigned long field, unsigned long value)
1697{
1698 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1699 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1700 dump_stack();
1701}
1702
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001703static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001704{
1705 u8 error;
1706
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001707 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04001708 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08001709 if (unlikely(error))
1710 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001711}
1712
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001713static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001714{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001715 vmcs_check16(field);
1716 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001717}
1718
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001719static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001720{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001721 vmcs_check32(field);
1722 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001723}
1724
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001725static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001726{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001727 vmcs_check64(field);
1728 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03001729#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001730 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001731 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001732#endif
1733}
1734
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001735static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001736{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001737 vmcs_checkl(field);
1738 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001739}
1740
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001741static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001742{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001743 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1744 "vmcs_clear_bits does not support 64-bit fields");
1745 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
1746}
1747
1748static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
1749{
1750 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1751 "vmcs_set_bits does not support 64-bit fields");
1752 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001753}
1754
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001755static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
1756{
1757 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
1758}
1759
Gleb Natapov2961e8762013-11-25 15:37:13 +02001760static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1761{
1762 vmcs_write32(VM_ENTRY_CONTROLS, val);
1763 vmx->vm_entry_controls_shadow = val;
1764}
1765
1766static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1767{
1768 if (vmx->vm_entry_controls_shadow != val)
1769 vm_entry_controls_init(vmx, val);
1770}
1771
1772static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1773{
1774 return vmx->vm_entry_controls_shadow;
1775}
1776
1777
1778static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1779{
1780 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
1781}
1782
1783static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1784{
1785 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
1786}
1787
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001788static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
1789{
1790 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
1791}
1792
Gleb Natapov2961e8762013-11-25 15:37:13 +02001793static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
1794{
1795 vmcs_write32(VM_EXIT_CONTROLS, val);
1796 vmx->vm_exit_controls_shadow = val;
1797}
1798
1799static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
1800{
1801 if (vmx->vm_exit_controls_shadow != val)
1802 vm_exit_controls_init(vmx, val);
1803}
1804
1805static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
1806{
1807 return vmx->vm_exit_controls_shadow;
1808}
1809
1810
1811static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1812{
1813 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
1814}
1815
1816static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1817{
1818 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
1819}
1820
Avi Kivity2fb92db2011-04-27 19:42:18 +03001821static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
1822{
1823 vmx->segment_cache.bitmask = 0;
1824}
1825
1826static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1827 unsigned field)
1828{
1829 bool ret;
1830 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1831
1832 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
1833 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
1834 vmx->segment_cache.bitmask = 0;
1835 }
1836 ret = vmx->segment_cache.bitmask & mask;
1837 vmx->segment_cache.bitmask |= mask;
1838 return ret;
1839}
1840
1841static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1842{
1843 u16 *p = &vmx->segment_cache.seg[seg].selector;
1844
1845 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1846 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1847 return *p;
1848}
1849
1850static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1851{
1852 ulong *p = &vmx->segment_cache.seg[seg].base;
1853
1854 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1855 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1856 return *p;
1857}
1858
1859static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1860{
1861 u32 *p = &vmx->segment_cache.seg[seg].limit;
1862
1863 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1864 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1865 return *p;
1866}
1867
1868static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1869{
1870 u32 *p = &vmx->segment_cache.seg[seg].ar;
1871
1872 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1873 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1874 return *p;
1875}
1876
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001877static void update_exception_bitmap(struct kvm_vcpu *vcpu)
1878{
1879 u32 eb;
1880
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001881 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08001882 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02001883 /*
1884 * Guest access to VMware backdoor ports could legitimately
1885 * trigger #GP because of TSS I/O permission bitmap.
1886 * We intercept those #GP and allow access to them anyway
1887 * as VMware does.
1888 */
1889 if (enable_vmware_backdoor)
1890 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001891 if ((vcpu->guest_debug &
1892 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
1893 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
1894 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001895 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001896 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02001897 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001898 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001899
1900 /* When we are running a nested L2 guest and L1 specified for it a
1901 * certain exception bitmap, we must trap the same exceptions and pass
1902 * them to L1. When running L2, we will only handle the exceptions
1903 * specified above if L1 did not want them.
1904 */
1905 if (is_guest_mode(vcpu))
1906 eb |= get_vmcs12(vcpu)->exception_bitmap;
1907
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001908 vmcs_write32(EXCEPTION_BITMAP, eb);
1909}
1910
Ashok Raj15d45072018-02-01 22:59:43 +01001911/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001912 * Check if MSR is intercepted for currently loaded MSR bitmap.
1913 */
1914static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
1915{
1916 unsigned long *msr_bitmap;
1917 int f = sizeof(unsigned long);
1918
1919 if (!cpu_has_vmx_msr_bitmap())
1920 return true;
1921
1922 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
1923
1924 if (msr <= 0x1fff) {
1925 return !!test_bit(msr, msr_bitmap + 0x800 / f);
1926 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
1927 msr &= 0x1fff;
1928 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
1929 }
1930
1931 return true;
1932}
1933
1934/*
Ashok Raj15d45072018-02-01 22:59:43 +01001935 * Check if MSR is intercepted for L01 MSR bitmap.
1936 */
1937static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
1938{
1939 unsigned long *msr_bitmap;
1940 int f = sizeof(unsigned long);
1941
1942 if (!cpu_has_vmx_msr_bitmap())
1943 return true;
1944
1945 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
1946
1947 if (msr <= 0x1fff) {
1948 return !!test_bit(msr, msr_bitmap + 0x800 / f);
1949 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
1950 msr &= 0x1fff;
1951 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
1952 }
1953
1954 return true;
1955}
1956
Gleb Natapov2961e8762013-11-25 15:37:13 +02001957static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1958 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001959{
Gleb Natapov2961e8762013-11-25 15:37:13 +02001960 vm_entry_controls_clearbit(vmx, entry);
1961 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001962}
1963
Avi Kivity61d2ef22010-04-28 16:40:38 +03001964static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
1965{
1966 unsigned i;
1967 struct msr_autoload *m = &vmx->msr_autoload;
1968
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001969 switch (msr) {
1970 case MSR_EFER:
1971 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001972 clear_atomic_switch_msr_special(vmx,
1973 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001974 VM_EXIT_LOAD_IA32_EFER);
1975 return;
1976 }
1977 break;
1978 case MSR_CORE_PERF_GLOBAL_CTRL:
1979 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001980 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001981 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1982 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
1983 return;
1984 }
1985 break;
Avi Kivity110312c2010-12-21 12:54:20 +02001986 }
1987
Avi Kivity61d2ef22010-04-28 16:40:38 +03001988 for (i = 0; i < m->nr; ++i)
1989 if (m->guest[i].index == msr)
1990 break;
1991
1992 if (i == m->nr)
1993 return;
1994 --m->nr;
1995 m->guest[i] = m->guest[m->nr];
1996 m->host[i] = m->host[m->nr];
1997 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1998 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1999}
2000
Gleb Natapov2961e8762013-11-25 15:37:13 +02002001static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2002 unsigned long entry, unsigned long exit,
2003 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2004 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002005{
2006 vmcs_write64(guest_val_vmcs, guest_val);
2007 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002008 vm_entry_controls_setbit(vmx, entry);
2009 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002010}
2011
Avi Kivity61d2ef22010-04-28 16:40:38 +03002012static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
2013 u64 guest_val, u64 host_val)
2014{
2015 unsigned i;
2016 struct msr_autoload *m = &vmx->msr_autoload;
2017
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002018 switch (msr) {
2019 case MSR_EFER:
2020 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002021 add_atomic_switch_msr_special(vmx,
2022 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002023 VM_EXIT_LOAD_IA32_EFER,
2024 GUEST_IA32_EFER,
2025 HOST_IA32_EFER,
2026 guest_val, host_val);
2027 return;
2028 }
2029 break;
2030 case MSR_CORE_PERF_GLOBAL_CTRL:
2031 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002032 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002033 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2034 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2035 GUEST_IA32_PERF_GLOBAL_CTRL,
2036 HOST_IA32_PERF_GLOBAL_CTRL,
2037 guest_val, host_val);
2038 return;
2039 }
2040 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002041 case MSR_IA32_PEBS_ENABLE:
2042 /* PEBS needs a quiescent period after being disabled (to write
2043 * a record). Disabling PEBS through VMX MSR swapping doesn't
2044 * provide that period, so a CPU could write host's record into
2045 * guest's memory.
2046 */
2047 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002048 }
2049
Avi Kivity61d2ef22010-04-28 16:40:38 +03002050 for (i = 0; i < m->nr; ++i)
2051 if (m->guest[i].index == msr)
2052 break;
2053
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002054 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002055 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002056 "Can't add msr %x\n", msr);
2057 return;
2058 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03002059 ++m->nr;
2060 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2061 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2062 }
2063
2064 m->guest[i].index = msr;
2065 m->guest[i].value = guest_val;
2066 m->host[i].index = msr;
2067 m->host[i].value = host_val;
2068}
2069
Avi Kivity92c0d902009-10-29 11:00:16 +02002070static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002071{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002072 u64 guest_efer = vmx->vcpu.arch.efer;
2073 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002074
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002075 if (!enable_ept) {
2076 /*
2077 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2078 * host CPUID is more efficient than testing guest CPUID
2079 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2080 */
2081 if (boot_cpu_has(X86_FEATURE_SMEP))
2082 guest_efer |= EFER_NX;
2083 else if (!(guest_efer & EFER_NX))
2084 ignore_bits |= EFER_NX;
2085 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002086
Avi Kivity51c6cf62007-08-29 03:48:05 +03002087 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002088 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002089 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002090 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002091#ifdef CONFIG_X86_64
2092 ignore_bits |= EFER_LMA | EFER_LME;
2093 /* SCE is meaningful only in long mode on Intel */
2094 if (guest_efer & EFER_LMA)
2095 ignore_bits &= ~(u64)EFER_SCE;
2096#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002097
2098 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002099
2100 /*
2101 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2102 * On CPUs that support "load IA32_EFER", always switch EFER
2103 * atomically, since it's faster than switching it manually.
2104 */
2105 if (cpu_has_load_ia32_efer ||
2106 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002107 if (!(guest_efer & EFER_LMA))
2108 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002109 if (guest_efer != host_efer)
2110 add_atomic_switch_msr(vmx, MSR_EFER,
2111 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002112 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002113 } else {
2114 guest_efer &= ~ignore_bits;
2115 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002116
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002117 vmx->guest_msrs[efer_offset].data = guest_efer;
2118 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2119
2120 return true;
2121 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002122}
2123
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002124#ifdef CONFIG_X86_32
2125/*
2126 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2127 * VMCS rather than the segment table. KVM uses this helper to figure
2128 * out the current bases to poke them into the VMCS before entry.
2129 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002130static unsigned long segment_base(u16 selector)
2131{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002132 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002133 unsigned long v;
2134
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002135 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002136 return 0;
2137
Thomas Garnier45fc8752017-03-14 10:05:08 -07002138 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002139
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002140 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002141 u16 ldt_selector = kvm_read_ldt();
2142
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002143 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002144 return 0;
2145
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002146 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002147 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002148 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002149 return v;
2150}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002151#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002152
Avi Kivity04d2cc72007-09-10 18:10:54 +03002153static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002154{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002155 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002156 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002157
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002158 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002159 return;
2160
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002161 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002162 /*
2163 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2164 * allow segment selectors with cpl > 0 or ti == 1.
2165 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002166 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002167 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002168
2169#ifdef CONFIG_X86_64
2170 save_fsgs_for_kvm();
2171 vmx->host_state.fs_sel = current->thread.fsindex;
2172 vmx->host_state.gs_sel = current->thread.gsindex;
2173#else
Avi Kivity9581d442010-10-19 16:46:55 +02002174 savesegment(fs, vmx->host_state.fs_sel);
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002175 savesegment(gs, vmx->host_state.gs_sel);
2176#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002177 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002178 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002179 vmx->host_state.fs_reload_needed = 0;
2180 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002181 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002182 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002183 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002184 if (!(vmx->host_state.gs_sel & 7))
2185 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002186 else {
2187 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002188 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002189 }
2190
2191#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002192 savesegment(ds, vmx->host_state.ds_sel);
2193 savesegment(es, vmx->host_state.es_sel);
2194#endif
2195
2196#ifdef CONFIG_X86_64
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002197 vmcs_writel(HOST_FS_BASE, current->thread.fsbase);
Avi Kivity33ed6322007-05-02 16:54:03 +03002198 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
2199#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002200 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2201 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03002202#endif
Avi Kivity707c0872007-05-02 17:33:43 +03002203
2204#ifdef CONFIG_X86_64
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002205 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Avi Kivityc8770e72010-11-11 12:37:26 +02002206 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002207 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03002208#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002209 if (boot_cpu_has(X86_FEATURE_MPX))
2210 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002211 for (i = 0; i < vmx->save_nmsrs; ++i)
2212 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002213 vmx->guest_msrs[i].data,
2214 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002215}
2216
Avi Kivitya9b21b62008-06-24 11:48:49 +03002217static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002218{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002219 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002220 return;
2221
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002222 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002223 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002224#ifdef CONFIG_X86_64
2225 if (is_long_mode(&vmx->vcpu))
2226 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2227#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002228 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002229 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002230#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002231 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002232#else
2233 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002234#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002235 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002236 if (vmx->host_state.fs_reload_needed)
2237 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002238#ifdef CONFIG_X86_64
2239 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2240 loadsegment(ds, vmx->host_state.ds_sel);
2241 loadsegment(es, vmx->host_state.es_sel);
2242 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002243#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002244 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002245#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002246 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002247#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002248 if (vmx->host_state.msr_host_bndcfgs)
2249 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Thomas Garnier45fc8752017-03-14 10:05:08 -07002250 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002251}
2252
Avi Kivitya9b21b62008-06-24 11:48:49 +03002253static void vmx_load_host_state(struct vcpu_vmx *vmx)
2254{
2255 preempt_disable();
2256 __vmx_load_host_state(vmx);
2257 preempt_enable();
2258}
2259
Feng Wu28b835d2015-09-18 22:29:54 +08002260static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2261{
2262 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2263 struct pi_desc old, new;
2264 unsigned int dest;
2265
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002266 /*
2267 * In case of hot-plug or hot-unplug, we may have to undo
2268 * vmx_vcpu_pi_put even if there is no assigned device. And we
2269 * always keep PI.NDST up to date for simplicity: it makes the
2270 * code easier, and CPU migration is not a fast path.
2271 */
2272 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002273 return;
2274
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002275 /*
2276 * First handle the simple case where no cmpxchg is necessary; just
2277 * allow posting non-urgent interrupts.
2278 *
2279 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2280 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2281 * expects the VCPU to be on the blocked_vcpu_list that matches
2282 * PI.NDST.
2283 */
2284 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2285 vcpu->cpu == cpu) {
2286 pi_clear_sn(pi_desc);
2287 return;
2288 }
2289
2290 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002291 do {
2292 old.control = new.control = pi_desc->control;
2293
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002294 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002295
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002296 if (x2apic_enabled())
2297 new.ndst = dest;
2298 else
2299 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002300
Feng Wu28b835d2015-09-18 22:29:54 +08002301 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02002302 } while (cmpxchg64(&pi_desc->control, old.control,
2303 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002304}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002305
Peter Feinerc95ba922016-08-17 09:36:47 -07002306static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2307{
2308 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2309 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2310}
2311
Avi Kivity6aa8b732006-12-10 02:21:36 -08002312/*
2313 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2314 * vcpu mutex is already taken.
2315 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002316static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002317{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002318 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002319 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002320
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002321 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002322 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002323 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002324 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002325
2326 /*
2327 * Read loaded_vmcs->cpu should be before fetching
2328 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2329 * See the comments in __loaded_vmcs_clear().
2330 */
2331 smp_rmb();
2332
Nadav Har'Eld462b812011-05-24 15:26:10 +03002333 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2334 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002335 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002336 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002337 }
2338
2339 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2340 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2341 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01002342 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002343 }
2344
2345 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002346 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002347 unsigned long sysenter_esp;
2348
2349 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002350
Avi Kivity6aa8b732006-12-10 02:21:36 -08002351 /*
2352 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002353 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002354 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002355 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01002356 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002357 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002358
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002359 /*
2360 * VM exits change the host TR limit to 0x67 after a VM
2361 * exit. This is okay, since 0x67 covers everything except
2362 * the IO bitmap and have have code to handle the IO bitmap
2363 * being lost after a VM exit.
2364 */
2365 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2366
Avi Kivity6aa8b732006-12-10 02:21:36 -08002367 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2368 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002369
Nadav Har'Eld462b812011-05-24 15:26:10 +03002370 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002371 }
Feng Wu28b835d2015-09-18 22:29:54 +08002372
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002373 /* Setup TSC multiplier */
2374 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002375 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2376 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002377
Feng Wu28b835d2015-09-18 22:29:54 +08002378 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002379 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08002380 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08002381}
2382
2383static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2384{
2385 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2386
2387 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002388 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2389 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002390 return;
2391
2392 /* Set SN when the vCPU is preempted */
2393 if (vcpu->preempted)
2394 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002395}
2396
2397static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2398{
Feng Wu28b835d2015-09-18 22:29:54 +08002399 vmx_vcpu_pi_put(vcpu);
2400
Avi Kivitya9b21b62008-06-24 11:48:49 +03002401 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002402}
2403
Wanpeng Lif244dee2017-07-20 01:11:54 -07002404static bool emulation_required(struct kvm_vcpu *vcpu)
2405{
2406 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2407}
2408
Avi Kivityedcafe32009-12-30 18:07:40 +02002409static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2410
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002411/*
2412 * Return the cr0 value that a nested guest would read. This is a combination
2413 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2414 * its hypervisor (cr0_read_shadow).
2415 */
2416static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2417{
2418 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2419 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2420}
2421static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2422{
2423 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2424 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2425}
2426
Avi Kivity6aa8b732006-12-10 02:21:36 -08002427static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2428{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002429 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002430
Avi Kivity6de12732011-03-07 12:51:22 +02002431 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2432 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2433 rflags = vmcs_readl(GUEST_RFLAGS);
2434 if (to_vmx(vcpu)->rmode.vm86_active) {
2435 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2436 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2437 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2438 }
2439 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002440 }
Avi Kivity6de12732011-03-07 12:51:22 +02002441 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002442}
2443
2444static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2445{
Wanpeng Lif244dee2017-07-20 01:11:54 -07002446 unsigned long old_rflags = vmx_get_rflags(vcpu);
2447
Avi Kivity6de12732011-03-07 12:51:22 +02002448 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2449 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002450 if (to_vmx(vcpu)->rmode.vm86_active) {
2451 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002452 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002453 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002454 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07002455
2456 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2457 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002458}
2459
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002460static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002461{
2462 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2463 int ret = 0;
2464
2465 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002466 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002467 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002468 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002469
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002470 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002471}
2472
2473static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2474{
2475 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2476 u32 interruptibility = interruptibility_old;
2477
2478 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2479
Jan Kiszka48005f62010-02-19 19:38:07 +01002480 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002481 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002482 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002483 interruptibility |= GUEST_INTR_STATE_STI;
2484
2485 if ((interruptibility != interruptibility_old))
2486 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2487}
2488
Avi Kivity6aa8b732006-12-10 02:21:36 -08002489static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2490{
2491 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002492
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002493 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002494 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002495 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002496
Glauber Costa2809f5d2009-05-12 16:21:05 -04002497 /* skipping an emulated instruction also counts */
2498 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002499}
2500
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002501static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
2502 unsigned long exit_qual)
2503{
2504 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2505 unsigned int nr = vcpu->arch.exception.nr;
2506 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2507
2508 if (vcpu->arch.exception.has_error_code) {
2509 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
2510 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2511 }
2512
2513 if (kvm_exception_is_soft(nr))
2514 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2515 else
2516 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2517
2518 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
2519 vmx_get_nmi_mask(vcpu))
2520 intr_info |= INTR_INFO_UNBLOCK_NMI;
2521
2522 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
2523}
2524
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002525/*
2526 * KVM wants to inject page-faults which it got to the guest. This function
2527 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002528 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002529static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002530{
2531 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002532 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002533
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002534 if (nr == PF_VECTOR) {
2535 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002536 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002537 return 1;
2538 }
2539 /*
2540 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
2541 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2542 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
2543 * can be written only when inject_pending_event runs. This should be
2544 * conditional on a new capability---if the capability is disabled,
2545 * kvm_multiple_exception would write the ancillary information to
2546 * CR2 or DR6, for backwards ABI-compatibility.
2547 */
2548 if (nested_vmx_is_page_fault_vmexit(vmcs12,
2549 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002550 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002551 return 1;
2552 }
2553 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002554 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002555 if (nr == DB_VECTOR)
2556 *exit_qual = vcpu->arch.dr6;
2557 else
2558 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002559 return 1;
2560 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002561 }
2562
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002563 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002564}
2565
Wanpeng Licaa057a2018-03-12 04:53:03 -07002566static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
2567{
2568 /*
2569 * Ensure that we clear the HLT state in the VMCS. We don't need to
2570 * explicitly skip the instruction because if the HLT state is set,
2571 * then the instruction is already executing and RIP has already been
2572 * advanced.
2573 */
2574 if (kvm_hlt_in_guest(vcpu->kvm) &&
2575 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
2576 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
2577}
2578
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002579static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02002580{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002581 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002582 unsigned nr = vcpu->arch.exception.nr;
2583 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002584 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002585 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002586
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002587 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002588 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002589 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2590 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002591
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002592 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002593 int inc_eip = 0;
2594 if (kvm_exception_is_soft(nr))
2595 inc_eip = vcpu->arch.event_exit_inst_len;
2596 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002597 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002598 return;
2599 }
2600
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002601 if (kvm_exception_is_soft(nr)) {
2602 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2603 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002604 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2605 } else
2606 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2607
2608 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07002609
2610 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02002611}
2612
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002613static bool vmx_rdtscp_supported(void)
2614{
2615 return cpu_has_vmx_rdtscp();
2616}
2617
Mao, Junjiead756a12012-07-02 01:18:48 +00002618static bool vmx_invpcid_supported(void)
2619{
2620 return cpu_has_vmx_invpcid() && enable_ept;
2621}
2622
Avi Kivity6aa8b732006-12-10 02:21:36 -08002623/*
Eddie Donga75beee2007-05-17 18:55:15 +03002624 * Swap MSR entry in host/guest MSR entry array.
2625 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002626static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002627{
Avi Kivity26bb0982009-09-07 11:14:12 +03002628 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002629
2630 tmp = vmx->guest_msrs[to];
2631 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2632 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002633}
2634
2635/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002636 * Set up the vmcs to automatically save and restore system
2637 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2638 * mode, as fiddling with msrs is very expensive.
2639 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002640static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002641{
Avi Kivity26bb0982009-09-07 11:14:12 +03002642 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002643
Eddie Donga75beee2007-05-17 18:55:15 +03002644 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002645#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002646 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002647 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002648 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002649 move_msr_up(vmx, index, save_nmsrs++);
2650 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002651 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002652 move_msr_up(vmx, index, save_nmsrs++);
2653 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002654 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002655 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002656 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02002657 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002658 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002659 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002660 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002661 * if efer.sce is enabled.
2662 */
Brian Gerst8c065852010-07-17 09:03:26 -04002663 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002664 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002665 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002666 }
Eddie Donga75beee2007-05-17 18:55:15 +03002667#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002668 index = __find_msr_index(vmx, MSR_EFER);
2669 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002670 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002671
Avi Kivity26bb0982009-09-07 11:14:12 +03002672 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002673
Yang Zhang8d146952013-01-25 10:18:50 +08002674 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002675 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002676}
2677
2678/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002679 * reads and returns guest's timestamp counter "register"
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002680 * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
2681 * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3
Avi Kivity6aa8b732006-12-10 02:21:36 -08002682 */
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002683static u64 guest_read_tsc(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002684{
2685 u64 host_tsc, tsc_offset;
2686
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002687 host_tsc = rdtsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002688 tsc_offset = vmcs_read64(TSC_OFFSET);
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002689 return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002690}
2691
2692/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002693 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002694 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002695static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002696{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002697 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002698 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002699 * We're here if L1 chose not to trap WRMSR to TSC. According
2700 * to the spec, this should set L1's TSC; The offset that L1
2701 * set for L2 remains unchanged, and still needs to be added
2702 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002703 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002704 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002705 /* recalculate vmcs02.TSC_OFFSET: */
2706 vmcs12 = get_vmcs12(vcpu);
2707 vmcs_write64(TSC_OFFSET, offset +
2708 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2709 vmcs12->tsc_offset : 0));
2710 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002711 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2712 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002713 vmcs_write64(TSC_OFFSET, offset);
2714 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002715}
2716
Nadav Har'El801d3422011-05-25 23:02:23 +03002717/*
2718 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2719 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2720 * all guests if the "nested" module option is off, and can also be disabled
2721 * for a single guest by disabling its VMX cpuid bit.
2722 */
2723static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2724{
Radim Krčmářd6321d42017-08-05 00:12:49 +02002725 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03002726}
2727
Avi Kivity6aa8b732006-12-10 02:21:36 -08002728/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002729 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2730 * returned for the various VMX controls MSRs when nested VMX is enabled.
2731 * The same values should also be used to verify that vmcs12 control fields are
2732 * valid during nested entry from L1 to L2.
2733 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2734 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2735 * bit in the high half is on if the corresponding bit in the control field
2736 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002737 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002738static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002739{
Paolo Bonzini13893092018-02-26 13:40:09 +01002740 if (!nested) {
2741 memset(msrs, 0, sizeof(*msrs));
2742 return;
2743 }
2744
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002745 /*
2746 * Note that as a general rule, the high half of the MSRs (bits in
2747 * the control fields which may be 1) should be initialized by the
2748 * intersection of the underlying hardware's MSR (i.e., features which
2749 * can be supported) and the list of features we want to expose -
2750 * because they are known to be properly supported in our code.
2751 * Also, usually, the low half of the MSRs (bits which must be 1) can
2752 * be set to 0, meaning that L1 may turn off any of these bits. The
2753 * reason is that if one of these bits is necessary, it will appear
2754 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2755 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02002756 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002757 * These rules have exceptions below.
2758 */
2759
2760 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002761 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002762 msrs->pinbased_ctls_low,
2763 msrs->pinbased_ctls_high);
2764 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002765 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002766 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002767 PIN_BASED_EXT_INTR_MASK |
2768 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01002769 PIN_BASED_VIRTUAL_NMIS |
2770 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002771 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002772 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002773 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002774
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002775 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002776 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002777 msrs->exit_ctls_low,
2778 msrs->exit_ctls_high);
2779 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08002780 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04002781
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002782 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002783#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002784 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002785#endif
Jan Kiszkaf41245002014-03-07 20:03:13 +01002786 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002787 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002788 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf41245002014-03-07 20:03:13 +01002789 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04002790 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2791
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002792 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002793 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002794
Jan Kiszka2996fca2014-06-16 13:59:43 +02002795 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002796 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02002797
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002798 /* entry controls */
2799 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002800 msrs->entry_ctls_low,
2801 msrs->entry_ctls_high);
2802 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08002803 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002804 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02002805#ifdef CONFIG_X86_64
2806 VM_ENTRY_IA32E_MODE |
2807#endif
2808 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002809 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002810 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002811 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002812 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02002813
Jan Kiszka2996fca2014-06-16 13:59:43 +02002814 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002815 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02002816
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002817 /* cpu-based controls */
2818 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002819 msrs->procbased_ctls_low,
2820 msrs->procbased_ctls_high);
2821 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08002822 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002823 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01002824 CPU_BASED_VIRTUAL_INTR_PENDING |
2825 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002826 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2827 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2828 CPU_BASED_CR3_STORE_EXITING |
2829#ifdef CONFIG_X86_64
2830 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2831#endif
2832 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03002833 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
2834 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
2835 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
2836 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002837 /*
2838 * We can allow some features even when not supported by the
2839 * hardware. For example, L1 can specify an MSR bitmap - and we
2840 * can use it to avoid exits to L1 - even when L0 runs L2
2841 * without MSR bitmaps.
2842 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002843 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002844 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02002845 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002846
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002847 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002848 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002849 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
2850
Paolo Bonzini80154d72017-08-24 13:55:35 +02002851 /*
2852 * secondary cpu-based controls. Do not include those that
2853 * depend on CPUID bits, they are added later by vmx_cpuid_update.
2854 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002855 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002856 msrs->secondary_ctls_low,
2857 msrs->secondary_ctls_high);
2858 msrs->secondary_ctls_low = 0;
2859 msrs->secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002860 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02002861 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08002862 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08002863 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08002864 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02002865 SECONDARY_EXEC_WBINVD_EXITING;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002866
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002867 if (enable_ept) {
2868 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002869 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01002870 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002871 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01002872 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04002873 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002874 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04002875 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002876 msrs->ept_caps &= vmx_capability.ept;
2877 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01002878 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
2879 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04002880 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002881 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04002882 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002883 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04002884 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02002885 }
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002886
Bandan Das27c42a12017-08-03 15:54:42 -04002887 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002888 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04002889 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04002890 /*
2891 * Advertise EPTP switching unconditionally
2892 * since we emulate it
2893 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08002894 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002895 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08002896 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04002897 }
2898
Paolo Bonzinief697a72016-03-18 16:58:38 +01002899 /*
2900 * Old versions of KVM use the single-context version without
2901 * checking for support, so declare that it is supported even
2902 * though it is treated as global context. The alternative is
2903 * not failing the single-context invvpid, and it is worse.
2904 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07002905 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002906 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07002907 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002908 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03002909 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02002910 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07002911
Radim Krčmář0790ec12015-03-17 14:02:32 +01002912 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002913 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01002914 SECONDARY_EXEC_UNRESTRICTED_GUEST;
2915
Jan Kiszkac18911a2013-03-13 16:06:41 +01002916 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002917 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002918 msrs->misc_low,
2919 msrs->misc_high);
2920 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
2921 msrs->misc_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002922 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf41245002014-03-07 20:03:13 +01002923 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002924 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08002925
2926 /*
2927 * This MSR reports some information about VMX support. We
2928 * should return information about the VMX we emulate for the
2929 * guest, and the VMCS structure we give it - not about the
2930 * VMX support of the underlying hardware.
2931 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002932 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08002933 VMCS12_REVISION |
2934 VMX_BASIC_TRUE_CTLS |
2935 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
2936 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
2937
2938 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002939 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08002940
2941 /*
David Matlack8322ebb2016-11-29 18:14:09 -08002942 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08002943 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
2944 * We picked the standard core2 setting.
2945 */
2946#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
2947#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002948 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
2949 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08002950
2951 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002952 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
2953 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08002954
2955 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002956 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002957}
2958
David Matlack38991522016-11-29 18:14:08 -08002959/*
2960 * if fixed0[i] == 1: val[i] must be 1
2961 * if fixed1[i] == 0: val[i] must be 0
2962 */
2963static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
2964{
2965 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002966}
2967
2968static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
2969{
David Matlack38991522016-11-29 18:14:08 -08002970 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002971}
2972
2973static inline u64 vmx_control_msr(u32 low, u32 high)
2974{
2975 return low | ((u64)high << 32);
2976}
2977
David Matlack62cc6b9d2016-11-29 18:14:07 -08002978static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
2979{
2980 superset &= mask;
2981 subset &= mask;
2982
2983 return (superset | subset) == superset;
2984}
2985
2986static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
2987{
2988 const u64 feature_and_reserved =
2989 /* feature (except bit 48; see below) */
2990 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
2991 /* reserved */
2992 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002993 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08002994
2995 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
2996 return -EINVAL;
2997
2998 /*
2999 * KVM does not emulate a version of VMX that constrains physical
3000 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3001 */
3002 if (data & BIT_ULL(48))
3003 return -EINVAL;
3004
3005 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3006 vmx_basic_vmcs_revision_id(data))
3007 return -EINVAL;
3008
3009 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3010 return -EINVAL;
3011
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003012 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003013 return 0;
3014}
3015
3016static int
3017vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3018{
3019 u64 supported;
3020 u32 *lowp, *highp;
3021
3022 switch (msr_index) {
3023 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003024 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3025 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003026 break;
3027 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003028 lowp = &vmx->nested.msrs.procbased_ctls_low;
3029 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003030 break;
3031 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003032 lowp = &vmx->nested.msrs.exit_ctls_low;
3033 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003034 break;
3035 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003036 lowp = &vmx->nested.msrs.entry_ctls_low;
3037 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003038 break;
3039 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003040 lowp = &vmx->nested.msrs.secondary_ctls_low;
3041 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003042 break;
3043 default:
3044 BUG();
3045 }
3046
3047 supported = vmx_control_msr(*lowp, *highp);
3048
3049 /* Check must-be-1 bits are still 1. */
3050 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3051 return -EINVAL;
3052
3053 /* Check must-be-0 bits are still 0. */
3054 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3055 return -EINVAL;
3056
3057 *lowp = data;
3058 *highp = data >> 32;
3059 return 0;
3060}
3061
3062static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3063{
3064 const u64 feature_and_reserved_bits =
3065 /* feature */
3066 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3067 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3068 /* reserved */
3069 GENMASK_ULL(13, 9) | BIT_ULL(31);
3070 u64 vmx_misc;
3071
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003072 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3073 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003074
3075 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3076 return -EINVAL;
3077
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003078 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003079 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3080 vmx_misc_preemption_timer_rate(data) !=
3081 vmx_misc_preemption_timer_rate(vmx_misc))
3082 return -EINVAL;
3083
3084 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3085 return -EINVAL;
3086
3087 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3088 return -EINVAL;
3089
3090 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3091 return -EINVAL;
3092
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003093 vmx->nested.msrs.misc_low = data;
3094 vmx->nested.msrs.misc_high = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003095 return 0;
3096}
3097
3098static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3099{
3100 u64 vmx_ept_vpid_cap;
3101
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003102 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3103 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003104
3105 /* Every bit is either reserved or a feature bit. */
3106 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3107 return -EINVAL;
3108
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003109 vmx->nested.msrs.ept_caps = data;
3110 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003111 return 0;
3112}
3113
3114static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3115{
3116 u64 *msr;
3117
3118 switch (msr_index) {
3119 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003120 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003121 break;
3122 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003123 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003124 break;
3125 default:
3126 BUG();
3127 }
3128
3129 /*
3130 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3131 * must be 1 in the restored value.
3132 */
3133 if (!is_bitwise_subset(data, *msr, -1ULL))
3134 return -EINVAL;
3135
3136 *msr = data;
3137 return 0;
3138}
3139
3140/*
3141 * Called when userspace is restoring VMX MSRs.
3142 *
3143 * Returns 0 on success, non-0 otherwise.
3144 */
3145static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3146{
3147 struct vcpu_vmx *vmx = to_vmx(vcpu);
3148
3149 switch (msr_index) {
3150 case MSR_IA32_VMX_BASIC:
3151 return vmx_restore_vmx_basic(vmx, data);
3152 case MSR_IA32_VMX_PINBASED_CTLS:
3153 case MSR_IA32_VMX_PROCBASED_CTLS:
3154 case MSR_IA32_VMX_EXIT_CTLS:
3155 case MSR_IA32_VMX_ENTRY_CTLS:
3156 /*
3157 * The "non-true" VMX capability MSRs are generated from the
3158 * "true" MSRs, so we do not support restoring them directly.
3159 *
3160 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3161 * should restore the "true" MSRs with the must-be-1 bits
3162 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3163 * DEFAULT SETTINGS".
3164 */
3165 return -EINVAL;
3166 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3167 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3168 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3169 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3170 case MSR_IA32_VMX_PROCBASED_CTLS2:
3171 return vmx_restore_control_msr(vmx, msr_index, data);
3172 case MSR_IA32_VMX_MISC:
3173 return vmx_restore_vmx_misc(vmx, data);
3174 case MSR_IA32_VMX_CR0_FIXED0:
3175 case MSR_IA32_VMX_CR4_FIXED0:
3176 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3177 case MSR_IA32_VMX_CR0_FIXED1:
3178 case MSR_IA32_VMX_CR4_FIXED1:
3179 /*
3180 * These MSRs are generated based on the vCPU's CPUID, so we
3181 * do not support restoring them directly.
3182 */
3183 return -EINVAL;
3184 case MSR_IA32_VMX_EPT_VPID_CAP:
3185 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3186 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003187 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003188 return 0;
3189 default:
3190 /*
3191 * The rest of the VMX capability MSRs do not support restore.
3192 */
3193 return -EINVAL;
3194 }
3195}
3196
Jan Kiszkacae50132014-01-04 18:47:22 +01003197/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003198static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003199{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003200 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003201 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003202 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003203 break;
3204 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3205 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003206 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003207 msrs->pinbased_ctls_low,
3208 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003209 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3210 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003211 break;
3212 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3213 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003214 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003215 msrs->procbased_ctls_low,
3216 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003217 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3218 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003219 break;
3220 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3221 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003222 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003223 msrs->exit_ctls_low,
3224 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003225 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3226 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003227 break;
3228 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3229 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003230 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003231 msrs->entry_ctls_low,
3232 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003233 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3234 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003235 break;
3236 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003237 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003238 msrs->misc_low,
3239 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003240 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003241 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003242 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003243 break;
3244 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003245 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003246 break;
3247 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003248 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003249 break;
3250 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003251 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003252 break;
3253 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003254 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003255 break;
3256 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003257 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003258 msrs->secondary_ctls_low,
3259 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003260 break;
3261 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003262 *pdata = msrs->ept_caps |
3263 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003264 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003265 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003266 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04003267 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003268 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003269 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003270 }
3271
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003272 return 0;
3273}
3274
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003275static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3276 uint64_t val)
3277{
3278 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3279
3280 return !(val & ~valid_bits);
3281}
3282
Tom Lendacky801e4592018-02-21 13:39:51 -06003283static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3284{
Paolo Bonzini13893092018-02-26 13:40:09 +01003285 switch (msr->index) {
3286 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3287 if (!nested)
3288 return 1;
3289 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
3290 default:
3291 return 1;
3292 }
3293
3294 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06003295}
3296
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003297/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003298 * Reads an msr value (of 'msr_index') into 'pdata'.
3299 * Returns 0 on success, non-0 otherwise.
3300 * Assumes vcpu_load() was already called.
3301 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003302static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003303{
Borislav Petkova6cb0992017-12-20 12:50:28 +01003304 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003305 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003306
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003307 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003308#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003309 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003310 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003311 break;
3312 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003313 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003314 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003315 case MSR_KERNEL_GS_BASE:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003316 vmx_load_host_state(vmx);
3317 msr_info->data = vmx->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003318 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003319#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003320 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003321 return kvm_get_msr_common(vcpu, msr_info);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303322 case MSR_IA32_TSC:
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08003323 msr_info->data = guest_read_tsc(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003324 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003325 case MSR_IA32_SPEC_CTRL:
3326 if (!msr_info->host_initiated &&
3327 !guest_cpuid_has(vcpu, X86_FEATURE_IBRS) &&
3328 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3329 return 1;
3330
3331 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3332 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003333 case MSR_IA32_ARCH_CAPABILITIES:
3334 if (!msr_info->host_initiated &&
3335 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3336 return 1;
3337 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3338 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003339 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003340 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003341 break;
3342 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003343 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003344 break;
3345 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003346 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003347 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003348 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003349 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003350 (!msr_info->host_initiated &&
3351 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003352 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003353 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003354 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003355 case MSR_IA32_MCG_EXT_CTL:
3356 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01003357 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08003358 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003359 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003360 msr_info->data = vcpu->arch.mcg_ext_ctl;
3361 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003362 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003363 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003364 break;
3365 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3366 if (!nested_vmx_allowed(vcpu))
3367 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003368 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
3369 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003370 case MSR_IA32_XSS:
3371 if (!vmx_xsaves_supported())
3372 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003373 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003374 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003375 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003376 if (!msr_info->host_initiated &&
3377 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003378 return 1;
3379 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003380 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003381 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003382 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003383 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003384 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003385 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003386 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003387 }
3388
Avi Kivity6aa8b732006-12-10 02:21:36 -08003389 return 0;
3390}
3391
Jan Kiszkacae50132014-01-04 18:47:22 +01003392static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3393
Avi Kivity6aa8b732006-12-10 02:21:36 -08003394/*
3395 * Writes msr value into into the appropriate "register".
3396 * Returns 0 on success, non-0 otherwise.
3397 * Assumes vcpu_load() was already called.
3398 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003399static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003400{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003401 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003402 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003403 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003404 u32 msr_index = msr_info->index;
3405 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003406
Avi Kivity6aa8b732006-12-10 02:21:36 -08003407 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003408 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003409 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003410 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003411#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003412 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003413 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003414 vmcs_writel(GUEST_FS_BASE, data);
3415 break;
3416 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003417 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003418 vmcs_writel(GUEST_GS_BASE, data);
3419 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003420 case MSR_KERNEL_GS_BASE:
3421 vmx_load_host_state(vmx);
3422 vmx->msr_guest_kernel_gs_base = data;
3423 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003424#endif
3425 case MSR_IA32_SYSENTER_CS:
3426 vmcs_write32(GUEST_SYSENTER_CS, data);
3427 break;
3428 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003429 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003430 break;
3431 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003432 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003433 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003434 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003435 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003436 (!msr_info->host_initiated &&
3437 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003438 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08003439 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07003440 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003441 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003442 vmcs_write64(GUEST_BNDCFGS, data);
3443 break;
3444 case MSR_IA32_TSC:
3445 kvm_write_tsc(vcpu, msr_info);
3446 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003447 case MSR_IA32_SPEC_CTRL:
3448 if (!msr_info->host_initiated &&
3449 !guest_cpuid_has(vcpu, X86_FEATURE_IBRS) &&
3450 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3451 return 1;
3452
3453 /* The STIBP bit doesn't fault even if it's not advertised */
3454 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP))
3455 return 1;
3456
3457 vmx->spec_ctrl = data;
3458
3459 if (!data)
3460 break;
3461
3462 /*
3463 * For non-nested:
3464 * When it's written (to non-zero) for the first time, pass
3465 * it through.
3466 *
3467 * For nested:
3468 * The handling of the MSR bitmap for L2 guests is done in
3469 * nested_vmx_merge_msr_bitmap. We should not touch the
3470 * vmcs02.msr_bitmap here since it gets completely overwritten
3471 * in the merging. We update the vmcs01 here for L1 as well
3472 * since it will end up touching the MSR anyway now.
3473 */
3474 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3475 MSR_IA32_SPEC_CTRL,
3476 MSR_TYPE_RW);
3477 break;
Ashok Raj15d45072018-02-01 22:59:43 +01003478 case MSR_IA32_PRED_CMD:
3479 if (!msr_info->host_initiated &&
3480 !guest_cpuid_has(vcpu, X86_FEATURE_IBPB) &&
3481 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3482 return 1;
3483
3484 if (data & ~PRED_CMD_IBPB)
3485 return 1;
3486
3487 if (!data)
3488 break;
3489
3490 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3491
3492 /*
3493 * For non-nested:
3494 * When it's written (to non-zero) for the first time, pass
3495 * it through.
3496 *
3497 * For nested:
3498 * The handling of the MSR bitmap for L2 guests is done in
3499 * nested_vmx_merge_msr_bitmap. We should not touch the
3500 * vmcs02.msr_bitmap here since it gets completely overwritten
3501 * in the merging.
3502 */
3503 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3504 MSR_TYPE_W);
3505 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003506 case MSR_IA32_ARCH_CAPABILITIES:
3507 if (!msr_info->host_initiated)
3508 return 1;
3509 vmx->arch_capabilities = data;
3510 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003511 case MSR_IA32_CR_PAT:
3512 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003513 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3514 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003515 vmcs_write64(GUEST_IA32_PAT, data);
3516 vcpu->arch.pat = data;
3517 break;
3518 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003519 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003520 break;
Will Auldba904632012-11-29 12:42:50 -08003521 case MSR_IA32_TSC_ADJUST:
3522 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003523 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003524 case MSR_IA32_MCG_EXT_CTL:
3525 if ((!msr_info->host_initiated &&
3526 !(to_vmx(vcpu)->msr_ia32_feature_control &
3527 FEATURE_CONTROL_LMCE)) ||
3528 (data & ~MCG_EXT_CTL_LMCE_EN))
3529 return 1;
3530 vcpu->arch.mcg_ext_ctl = data;
3531 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003532 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003533 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003534 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003535 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3536 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003537 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003538 if (msr_info->host_initiated && data == 0)
3539 vmx_leave_nested(vcpu);
3540 break;
3541 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003542 if (!msr_info->host_initiated)
3543 return 1; /* they are read-only */
3544 if (!nested_vmx_allowed(vcpu))
3545 return 1;
3546 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08003547 case MSR_IA32_XSS:
3548 if (!vmx_xsaves_supported())
3549 return 1;
3550 /*
3551 * The only supported bit as of Skylake is bit 8, but
3552 * it is not supported on KVM.
3553 */
3554 if (data != 0)
3555 return 1;
3556 vcpu->arch.ia32_xss = data;
3557 if (vcpu->arch.ia32_xss != host_xss)
3558 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
3559 vcpu->arch.ia32_xss, host_xss);
3560 else
3561 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3562 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003563 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003564 if (!msr_info->host_initiated &&
3565 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003566 return 1;
3567 /* Check reserved bit, higher 32 bits should be zero */
3568 if ((data >> 32) != 0)
3569 return 1;
3570 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003571 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10003572 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003573 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07003574 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003575 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003576 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3577 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003578 ret = kvm_set_shared_msr(msr->index, msr->data,
3579 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03003580 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003581 if (ret)
3582 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003583 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08003584 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003585 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003586 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003587 }
3588
Eddie Dong2cc51562007-05-21 07:28:09 +03003589 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003590}
3591
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003592static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003593{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003594 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3595 switch (reg) {
3596 case VCPU_REGS_RSP:
3597 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3598 break;
3599 case VCPU_REGS_RIP:
3600 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3601 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003602 case VCPU_EXREG_PDPTR:
3603 if (enable_ept)
3604 ept_save_pdptrs(vcpu);
3605 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003606 default:
3607 break;
3608 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003609}
3610
Avi Kivity6aa8b732006-12-10 02:21:36 -08003611static __init int cpu_has_kvm_support(void)
3612{
Eduardo Habkost6210e372008-11-17 19:03:16 -02003613 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003614}
3615
3616static __init int vmx_disabled_by_bios(void)
3617{
3618 u64 msr;
3619
3620 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04003621 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08003622 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04003623 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3624 && tboot_enabled())
3625 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08003626 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04003627 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08003628 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08003629 && !tboot_enabled()) {
3630 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08003631 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04003632 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08003633 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08003634 /* launched w/o TXT and VMX disabled */
3635 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3636 && !tboot_enabled())
3637 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04003638 }
3639
3640 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003641}
3642
Dongxiao Xu7725b892010-05-11 18:29:38 +08003643static void kvm_cpu_vmxon(u64 addr)
3644{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003645 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003646 intel_pt_handle_vmx(1);
3647
Dongxiao Xu7725b892010-05-11 18:29:38 +08003648 asm volatile (ASM_VMX_VMXON_RAX
3649 : : "a"(&addr), "m"(addr)
3650 : "memory", "cc");
3651}
3652
Radim Krčmář13a34e02014-08-28 15:13:03 +02003653static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003654{
3655 int cpu = raw_smp_processor_id();
3656 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04003657 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003658
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003659 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02003660 return -EBUSY;
3661
Nadav Har'Eld462b812011-05-24 15:26:10 +03003662 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08003663 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3664 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003665
3666 /*
3667 * Now we can enable the vmclear operation in kdump
3668 * since the loaded_vmcss_on_cpu list on this cpu
3669 * has been initialized.
3670 *
3671 * Though the cpu is not in VMX operation now, there
3672 * is no problem to enable the vmclear operation
3673 * for the loaded_vmcss_on_cpu list is empty!
3674 */
3675 crash_enable_local_vmclear(cpu);
3676
Avi Kivity6aa8b732006-12-10 02:21:36 -08003677 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04003678
3679 test_bits = FEATURE_CONTROL_LOCKED;
3680 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3681 if (tboot_enabled())
3682 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3683
3684 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003685 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04003686 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3687 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003688 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02003689 if (enable_ept)
3690 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02003691
3692 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003693}
3694
Nadav Har'Eld462b812011-05-24 15:26:10 +03003695static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03003696{
3697 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03003698 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03003699
Nadav Har'Eld462b812011-05-24 15:26:10 +03003700 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3701 loaded_vmcss_on_cpu_link)
3702 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03003703}
3704
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003705
3706/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3707 * tricks.
3708 */
3709static void kvm_cpu_vmxoff(void)
3710{
3711 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003712
3713 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003714 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003715}
3716
Radim Krčmář13a34e02014-08-28 15:13:03 +02003717static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003718{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003719 vmclear_local_loaded_vmcss();
3720 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003721}
3722
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003723static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04003724 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003725{
3726 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003727 u32 ctl = ctl_min | ctl_opt;
3728
3729 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3730
3731 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3732 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3733
3734 /* Ensure minimum (required) set of control bits are supported. */
3735 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003736 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003737
3738 *result = ctl;
3739 return 0;
3740}
3741
Avi Kivity110312c2010-12-21 12:54:20 +02003742static __init bool allow_1_setting(u32 msr, u32 ctl)
3743{
3744 u32 vmx_msr_low, vmx_msr_high;
3745
3746 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3747 return vmx_msr_high & ctl;
3748}
3749
Yang, Sheng002c7f72007-07-31 14:23:01 +03003750static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003751{
3752 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08003753 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003754 u32 _pin_based_exec_control = 0;
3755 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003756 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003757 u32 _vmexit_control = 0;
3758 u32 _vmentry_control = 0;
3759
Paolo Bonzini13893092018-02-26 13:40:09 +01003760 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05303761 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003762#ifdef CONFIG_X86_64
3763 CPU_BASED_CR8_LOAD_EXITING |
3764 CPU_BASED_CR8_STORE_EXITING |
3765#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08003766 CPU_BASED_CR3_LOAD_EXITING |
3767 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e2d2017-12-12 16:44:21 +08003768 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003769 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03003770 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003771 CPU_BASED_MWAIT_EXITING |
3772 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02003773 CPU_BASED_INVLPG_EXITING |
3774 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06003775
Sheng Yangf78e0e22007-10-29 09:40:42 +08003776 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08003777 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08003778 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003779 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3780 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003781 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003782#ifdef CONFIG_X86_64
3783 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3784 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3785 ~CPU_BASED_CR8_STORE_EXITING;
3786#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08003787 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08003788 min2 = 0;
3789 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08003790 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08003791 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08003792 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003793 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003794 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003795 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02003796 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00003797 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08003798 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003799 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03003800 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08003801 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08003802 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02003803 SECONDARY_EXEC_RDSEED_EXITING |
3804 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08003805 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04003806 SECONDARY_EXEC_TSC_SCALING |
3807 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08003808 if (adjust_vmx_controls(min2, opt2,
3809 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003810 &_cpu_based_2nd_exec_control) < 0)
3811 return -EIO;
3812 }
3813#ifndef CONFIG_X86_64
3814 if (!(_cpu_based_2nd_exec_control &
3815 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
3816 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
3817#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08003818
3819 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3820 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08003821 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003822 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3823 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08003824
Wanpeng Li61f1dd92017-10-18 16:02:19 -07003825 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
3826 &vmx_capability.ept, &vmx_capability.vpid);
3827
Sheng Yangd56f5462008-04-25 10:13:16 +08003828 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03003829 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
3830 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03003831 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
3832 CPU_BASED_CR3_STORE_EXITING |
3833 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07003834 } else if (vmx_capability.ept) {
3835 vmx_capability.ept = 0;
3836 pr_warn_once("EPT CAP should not exist if not support "
3837 "1-setting enable EPT VM-execution control\n");
3838 }
3839 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
3840 vmx_capability.vpid) {
3841 vmx_capability.vpid = 0;
3842 pr_warn_once("VPID CAP should not exist if not support "
3843 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08003844 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003845
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003846 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003847#ifdef CONFIG_X86_64
3848 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
3849#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08003850 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003851 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003852 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
3853 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003854 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003855
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01003856 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
3857 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
3858 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003859 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
3860 &_pin_based_exec_control) < 0)
3861 return -EIO;
3862
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02003863 if (cpu_has_broken_vmx_preemption_timer())
3864 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003865 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003866 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08003867 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
3868
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01003869 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00003870 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003871 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
3872 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003873 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003874
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003875 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003876
3877 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
3878 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003879 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003880
3881#ifdef CONFIG_X86_64
3882 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
3883 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03003884 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003885#endif
3886
3887 /* Require Write-Back (WB) memory type for VMCS accesses. */
3888 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003889 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003890
Yang, Sheng002c7f72007-07-31 14:23:01 +03003891 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02003892 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03003893 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003894 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003895
Yang, Sheng002c7f72007-07-31 14:23:01 +03003896 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
3897 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003898 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003899 vmcs_conf->vmexit_ctrl = _vmexit_control;
3900 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003901
Avi Kivity110312c2010-12-21 12:54:20 +02003902 cpu_has_load_ia32_efer =
3903 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3904 VM_ENTRY_LOAD_IA32_EFER)
3905 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3906 VM_EXIT_LOAD_IA32_EFER);
3907
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003908 cpu_has_load_perf_global_ctrl =
3909 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3910 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
3911 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3912 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
3913
3914 /*
3915 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02003916 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003917 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
3918 *
3919 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
3920 *
3921 * AAK155 (model 26)
3922 * AAP115 (model 30)
3923 * AAT100 (model 37)
3924 * BC86,AAY89,BD102 (model 44)
3925 * BA97 (model 46)
3926 *
3927 */
3928 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
3929 switch (boot_cpu_data.x86_model) {
3930 case 26:
3931 case 30:
3932 case 37:
3933 case 44:
3934 case 46:
3935 cpu_has_load_perf_global_ctrl = false;
3936 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
3937 "does not work properly. Using workaround\n");
3938 break;
3939 default:
3940 break;
3941 }
3942 }
3943
Borislav Petkov782511b2016-04-04 22:25:03 +02003944 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08003945 rdmsrl(MSR_IA32_XSS, host_xss);
3946
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003947 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003948}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003949
3950static struct vmcs *alloc_vmcs_cpu(int cpu)
3951{
3952 int node = cpu_to_node(cpu);
3953 struct page *pages;
3954 struct vmcs *vmcs;
3955
Vlastimil Babka96db8002015-09-08 15:03:50 -07003956 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003957 if (!pages)
3958 return NULL;
3959 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003960 memset(vmcs, 0, vmcs_config.size);
3961 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003962 return vmcs;
3963}
3964
Avi Kivity6aa8b732006-12-10 02:21:36 -08003965static void free_vmcs(struct vmcs *vmcs)
3966{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003967 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003968}
3969
Nadav Har'Eld462b812011-05-24 15:26:10 +03003970/*
3971 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
3972 */
3973static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3974{
3975 if (!loaded_vmcs->vmcs)
3976 return;
3977 loaded_vmcs_clear(loaded_vmcs);
3978 free_vmcs(loaded_vmcs->vmcs);
3979 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003980 if (loaded_vmcs->msr_bitmap)
3981 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07003982 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03003983}
3984
Paolo Bonzinif21f1652018-01-11 12:16:15 +01003985static struct vmcs *alloc_vmcs(void)
3986{
3987 return alloc_vmcs_cpu(raw_smp_processor_id());
3988}
3989
3990static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3991{
3992 loaded_vmcs->vmcs = alloc_vmcs();
3993 if (!loaded_vmcs->vmcs)
3994 return -ENOMEM;
3995
3996 loaded_vmcs->shadow_vmcs = NULL;
3997 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003998
3999 if (cpu_has_vmx_msr_bitmap()) {
4000 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4001 if (!loaded_vmcs->msr_bitmap)
4002 goto out_vmcs;
4003 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
4004 }
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004005 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004006
4007out_vmcs:
4008 free_loaded_vmcs(loaded_vmcs);
4009 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004010}
4011
Sam Ravnborg39959582007-06-01 00:47:13 -07004012static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004013{
4014 int cpu;
4015
Zachary Amsden3230bb42009-09-29 11:38:37 -10004016 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004017 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004018 per_cpu(vmxarea, cpu) = NULL;
4019 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004020}
4021
Jim Mattsond37f4262017-12-22 12:12:16 -08004022enum vmcs_field_width {
4023 VMCS_FIELD_WIDTH_U16 = 0,
4024 VMCS_FIELD_WIDTH_U64 = 1,
4025 VMCS_FIELD_WIDTH_U32 = 2,
4026 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004027};
4028
Jim Mattsond37f4262017-12-22 12:12:16 -08004029static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004030{
4031 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004032 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004033 return (field >> 13) & 0x3 ;
4034}
4035
4036static inline int vmcs_field_readonly(unsigned long field)
4037{
4038 return (((field >> 10) & 0x3) == 1);
4039}
4040
Bandan Dasfe2b2012014-04-21 15:20:14 -04004041static void init_vmcs_shadow_fields(void)
4042{
4043 int i, j;
4044
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004045 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4046 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004047 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004048 (i + 1 == max_shadow_read_only_fields ||
4049 shadow_read_only_fields[i + 1] != field + 1))
4050 pr_err("Missing field from shadow_read_only_field %x\n",
4051 field + 1);
4052
4053 clear_bit(field, vmx_vmread_bitmap);
4054#ifdef CONFIG_X86_64
4055 if (field & 1)
4056 continue;
4057#endif
4058 if (j < i)
4059 shadow_read_only_fields[j] = field;
4060 j++;
4061 }
4062 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004063
4064 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004065 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004066 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004067 (i + 1 == max_shadow_read_write_fields ||
4068 shadow_read_write_fields[i + 1] != field + 1))
4069 pr_err("Missing field from shadow_read_write_field %x\n",
4070 field + 1);
4071
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004072 /*
4073 * PML and the preemption timer can be emulated, but the
4074 * processor cannot vmwrite to fields that don't exist
4075 * on bare metal.
4076 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004077 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004078 case GUEST_PML_INDEX:
4079 if (!cpu_has_vmx_pml())
4080 continue;
4081 break;
4082 case VMX_PREEMPTION_TIMER_VALUE:
4083 if (!cpu_has_vmx_preemption_timer())
4084 continue;
4085 break;
4086 case GUEST_INTR_STATUS:
4087 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004088 continue;
4089 break;
4090 default:
4091 break;
4092 }
4093
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004094 clear_bit(field, vmx_vmwrite_bitmap);
4095 clear_bit(field, vmx_vmread_bitmap);
4096#ifdef CONFIG_X86_64
4097 if (field & 1)
4098 continue;
4099#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004100 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004101 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004102 j++;
4103 }
4104 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004105}
4106
Avi Kivity6aa8b732006-12-10 02:21:36 -08004107static __init int alloc_kvm_area(void)
4108{
4109 int cpu;
4110
Zachary Amsden3230bb42009-09-29 11:38:37 -10004111 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004112 struct vmcs *vmcs;
4113
4114 vmcs = alloc_vmcs_cpu(cpu);
4115 if (!vmcs) {
4116 free_kvm_area();
4117 return -ENOMEM;
4118 }
4119
4120 per_cpu(vmxarea, cpu) = vmcs;
4121 }
4122 return 0;
4123}
4124
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004125static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004126 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004127{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004128 if (!emulate_invalid_guest_state) {
4129 /*
4130 * CS and SS RPL should be equal during guest entry according
4131 * to VMX spec, but in reality it is not always so. Since vcpu
4132 * is in the middle of the transition from real mode to
4133 * protected mode it is safe to assume that RPL 0 is a good
4134 * default value.
4135 */
4136 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004137 save->selector &= ~SEGMENT_RPL_MASK;
4138 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004139 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004140 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004141 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004142}
4143
4144static void enter_pmode(struct kvm_vcpu *vcpu)
4145{
4146 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004147 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004148
Gleb Natapovd99e4152012-12-20 16:57:45 +02004149 /*
4150 * Update real mode segment cache. It may be not up-to-date if sement
4151 * register was written while vcpu was in a guest mode.
4152 */
4153 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4154 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4155 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4156 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4157 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4158 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4159
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004160 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004161
Avi Kivity2fb92db2011-04-27 19:42:18 +03004162 vmx_segment_cache_clear(vmx);
4163
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004164 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004165
4166 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004167 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4168 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004169 vmcs_writel(GUEST_RFLAGS, flags);
4170
Rusty Russell66aee912007-07-17 23:34:16 +10004171 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4172 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004173
4174 update_exception_bitmap(vcpu);
4175
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004176 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4177 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4178 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4179 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4180 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4181 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004182}
4183
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004184static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004185{
Mathias Krause772e0312012-08-30 01:30:19 +02004186 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004187 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004188
Gleb Natapovd99e4152012-12-20 16:57:45 +02004189 var.dpl = 0x3;
4190 if (seg == VCPU_SREG_CS)
4191 var.type = 0x3;
4192
4193 if (!emulate_invalid_guest_state) {
4194 var.selector = var.base >> 4;
4195 var.base = var.base & 0xffff0;
4196 var.limit = 0xffff;
4197 var.g = 0;
4198 var.db = 0;
4199 var.present = 1;
4200 var.s = 1;
4201 var.l = 0;
4202 var.unusable = 0;
4203 var.type = 0x3;
4204 var.avl = 0;
4205 if (save->base & 0xf)
4206 printk_once(KERN_WARNING "kvm: segment base is not "
4207 "paragraph aligned when entering "
4208 "protected mode (seg=%d)", seg);
4209 }
4210
4211 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004212 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004213 vmcs_write32(sf->limit, var.limit);
4214 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004215}
4216
4217static void enter_rmode(struct kvm_vcpu *vcpu)
4218{
4219 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004220 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004221
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004222 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4223 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4224 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4225 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4226 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004227 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4228 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004229
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004230 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004231
Gleb Natapov776e58e2011-03-13 12:34:27 +02004232 /*
4233 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004234 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004235 */
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004236 if (!vcpu->kvm->arch.tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004237 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4238 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004239
Avi Kivity2fb92db2011-04-27 19:42:18 +03004240 vmx_segment_cache_clear(vmx);
4241
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004242 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004243 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004244 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4245
4246 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004247 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004248
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004249 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004250
4251 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004252 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004253 update_exception_bitmap(vcpu);
4254
Gleb Natapovd99e4152012-12-20 16:57:45 +02004255 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4256 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4257 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4258 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4259 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4260 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004261
Eddie Dong8668a3c2007-10-10 14:26:45 +08004262 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004263}
4264
Amit Shah401d10d2009-02-20 22:53:37 +05304265static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4266{
4267 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004268 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4269
4270 if (!msr)
4271 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304272
Avi Kivity44ea2b12009-09-06 15:55:37 +03004273 /*
4274 * Force kernel_gs_base reloading before EFER changes, as control
4275 * of this msr depends on is_long_mode().
4276 */
4277 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004278 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304279 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004280 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304281 msr->data = efer;
4282 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004283 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304284
4285 msr->data = efer & ~EFER_LME;
4286 }
4287 setup_msrs(vmx);
4288}
4289
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004290#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004291
4292static void enter_lmode(struct kvm_vcpu *vcpu)
4293{
4294 u32 guest_tr_ar;
4295
Avi Kivity2fb92db2011-04-27 19:42:18 +03004296 vmx_segment_cache_clear(to_vmx(vcpu));
4297
Avi Kivity6aa8b732006-12-10 02:21:36 -08004298 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004299 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004300 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4301 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004302 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004303 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4304 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004305 }
Avi Kivityda38f432010-07-06 11:30:49 +03004306 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004307}
4308
4309static void exit_lmode(struct kvm_vcpu *vcpu)
4310{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004311 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004312 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004313}
4314
4315#endif
4316
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004317static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4318 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004319{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004320 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004321 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4322 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004323 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004324 } else {
4325 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004326 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004327}
4328
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004329static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004330{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004331 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004332}
4333
Jim Mattsonfb6c8192017-03-16 13:53:59 -07004334static void vmx_flush_tlb_ept_only(struct kvm_vcpu *vcpu)
4335{
4336 if (enable_ept)
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004337 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07004338}
4339
Avi Kivitye8467fd2009-12-29 18:43:06 +02004340static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4341{
4342 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4343
4344 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4345 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4346}
4347
Avi Kivityaff48ba2010-12-05 18:56:11 +02004348static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4349{
Sean Christophersonb4d18512018-03-05 12:04:40 -08004350 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02004351 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4352 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4353}
4354
Anthony Liguori25c4c272007-04-27 09:29:21 +03004355static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004356{
Avi Kivityfc78f512009-12-07 12:16:48 +02004357 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4358
4359 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4360 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004361}
4362
Sheng Yang14394422008-04-28 12:24:45 +08004363static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4364{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004365 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4366
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004367 if (!test_bit(VCPU_EXREG_PDPTR,
4368 (unsigned long *)&vcpu->arch.regs_dirty))
4369 return;
4370
Sheng Yang14394422008-04-28 12:24:45 +08004371 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004372 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4373 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4374 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4375 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004376 }
4377}
4378
Avi Kivity8f5d5492009-05-31 18:41:29 +03004379static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4380{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004381 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4382
Avi Kivity8f5d5492009-05-31 18:41:29 +03004383 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004384 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4385 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4386 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4387 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004388 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004389
4390 __set_bit(VCPU_EXREG_PDPTR,
4391 (unsigned long *)&vcpu->arch.regs_avail);
4392 __set_bit(VCPU_EXREG_PDPTR,
4393 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004394}
4395
David Matlack38991522016-11-29 18:14:08 -08004396static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4397{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004398 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4399 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004400 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4401
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004402 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08004403 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4404 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4405 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4406
4407 return fixed_bits_valid(val, fixed0, fixed1);
4408}
4409
4410static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4411{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004412 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4413 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004414
4415 return fixed_bits_valid(val, fixed0, fixed1);
4416}
4417
4418static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4419{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004420 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
4421 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004422
4423 return fixed_bits_valid(val, fixed0, fixed1);
4424}
4425
4426/* No difference in the restrictions on guest and host CR4 in VMX operation. */
4427#define nested_guest_cr4_valid nested_cr4_valid
4428#define nested_host_cr4_valid nested_cr4_valid
4429
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004430static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004431
4432static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4433 unsigned long cr0,
4434 struct kvm_vcpu *vcpu)
4435{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004436 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4437 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004438 if (!(cr0 & X86_CR0_PG)) {
4439 /* From paging/starting to nonpaging */
4440 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004441 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004442 (CPU_BASED_CR3_LOAD_EXITING |
4443 CPU_BASED_CR3_STORE_EXITING));
4444 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004445 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004446 } else if (!is_paging(vcpu)) {
4447 /* From nonpaging to paging */
4448 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004449 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004450 ~(CPU_BASED_CR3_LOAD_EXITING |
4451 CPU_BASED_CR3_STORE_EXITING));
4452 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004453 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004454 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004455
4456 if (!(cr0 & X86_CR0_WP))
4457 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004458}
4459
Avi Kivity6aa8b732006-12-10 02:21:36 -08004460static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4461{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004462 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004463 unsigned long hw_cr0;
4464
Gleb Natapov50378782013-02-04 16:00:28 +02004465 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004466 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004467 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004468 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004469 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004470
Gleb Natapov218e7632013-01-21 15:36:45 +02004471 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4472 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004473
Gleb Natapov218e7632013-01-21 15:36:45 +02004474 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4475 enter_rmode(vcpu);
4476 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004477
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004478#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004479 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10004480 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004481 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10004482 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004483 exit_lmode(vcpu);
4484 }
4485#endif
4486
Sean Christophersonb4d18512018-03-05 12:04:40 -08004487 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08004488 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4489
Avi Kivity6aa8b732006-12-10 02:21:36 -08004490 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004491 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004492 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004493
4494 /* depends on vcpu->arch.cr0 to be set to a new value */
4495 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004496}
4497
Yu Zhang855feb62017-08-24 20:27:55 +08004498static int get_ept_level(struct kvm_vcpu *vcpu)
4499{
4500 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
4501 return 5;
4502 return 4;
4503}
4504
Peter Feiner995f00a2017-06-30 17:26:32 -07004505static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08004506{
Yu Zhang855feb62017-08-24 20:27:55 +08004507 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08004508
Yu Zhang855feb62017-08-24 20:27:55 +08004509 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08004510
Peter Feiner995f00a2017-06-30 17:26:32 -07004511 if (enable_ept_ad_bits &&
4512 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02004513 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08004514 eptp |= (root_hpa & PAGE_MASK);
4515
4516 return eptp;
4517}
4518
Avi Kivity6aa8b732006-12-10 02:21:36 -08004519static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4520{
Sheng Yang14394422008-04-28 12:24:45 +08004521 unsigned long guest_cr3;
4522 u64 eptp;
4523
4524 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02004525 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07004526 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08004527 vmcs_write64(EPT_POINTER, eptp);
Sean Christophersone90008d2018-03-05 12:04:37 -08004528 if (enable_unrestricted_guest || is_paging(vcpu) ||
4529 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02004530 guest_cr3 = kvm_read_cr3(vcpu);
4531 else
4532 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02004533 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004534 }
4535
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004536 vmx_flush_tlb(vcpu, true);
Sheng Yang14394422008-04-28 12:24:45 +08004537 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004538}
4539
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004540static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004541{
Ben Serebrin085e68e2015-04-16 11:58:05 -07004542 /*
4543 * Pass through host's Machine Check Enable value to hw_cr4, which
4544 * is in force while we are in guest mode. Do not let guests control
4545 * this bit, even if host CR4.MCE == 0.
4546 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08004547 unsigned long hw_cr4;
4548
4549 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
4550 if (enable_unrestricted_guest)
4551 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
4552 else if (to_vmx(vcpu)->rmode.vm86_active)
4553 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
4554 else
4555 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004556
Paolo Bonzini0367f202016-07-12 10:44:55 +02004557 if ((cr4 & X86_CR4_UMIP) && !boot_cpu_has(X86_FEATURE_UMIP)) {
4558 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
4559 SECONDARY_EXEC_DESC);
4560 hw_cr4 &= ~X86_CR4_UMIP;
Radim Krčmář99158242018-01-31 18:12:50 +01004561 } else if (!is_guest_mode(vcpu) ||
4562 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
Paolo Bonzini0367f202016-07-12 10:44:55 +02004563 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
4564 SECONDARY_EXEC_DESC);
4565
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004566 if (cr4 & X86_CR4_VMXE) {
4567 /*
4568 * To use VMXON (and later other VMX instructions), a guest
4569 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4570 * So basically the check on whether to allow nested VMX
4571 * is here.
4572 */
4573 if (!nested_vmx_allowed(vcpu))
4574 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004575 }
David Matlack38991522016-11-29 18:14:08 -08004576
4577 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004578 return 1;
4579
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004580 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08004581
Sean Christopherson5dc1f042018-03-05 12:04:39 -08004582 if (!enable_unrestricted_guest) {
4583 if (enable_ept) {
4584 if (!is_paging(vcpu)) {
4585 hw_cr4 &= ~X86_CR4_PAE;
4586 hw_cr4 |= X86_CR4_PSE;
4587 } else if (!(cr4 & X86_CR4_PAE)) {
4588 hw_cr4 &= ~X86_CR4_PAE;
4589 }
4590 }
4591
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004592 /*
Huaitong Handdba2622016-03-22 16:51:15 +08004593 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4594 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4595 * to be manually disabled when guest switches to non-paging
4596 * mode.
4597 *
4598 * If !enable_unrestricted_guest, the CPU is always running
4599 * with CR0.PG=1 and CR4 needs to be modified.
4600 * If enable_unrestricted_guest, the CPU automatically
4601 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004602 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08004603 if (!is_paging(vcpu))
4604 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
4605 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004606
Sheng Yang14394422008-04-28 12:24:45 +08004607 vmcs_writel(CR4_READ_SHADOW, cr4);
4608 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004609 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004610}
4611
Avi Kivity6aa8b732006-12-10 02:21:36 -08004612static void vmx_get_segment(struct kvm_vcpu *vcpu,
4613 struct kvm_segment *var, int seg)
4614{
Avi Kivitya9179492011-01-03 14:28:52 +02004615 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004616 u32 ar;
4617
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004618 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004619 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02004620 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03004621 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004622 return;
Avi Kivity1390a282012-08-21 17:07:08 +03004623 var->base = vmx_read_guest_seg_base(vmx, seg);
4624 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4625 return;
Avi Kivitya9179492011-01-03 14:28:52 +02004626 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004627 var->base = vmx_read_guest_seg_base(vmx, seg);
4628 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4629 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4630 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03004631 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004632 var->type = ar & 15;
4633 var->s = (ar >> 4) & 1;
4634 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03004635 /*
4636 * Some userspaces do not preserve unusable property. Since usable
4637 * segment has to be present according to VMX spec we can use present
4638 * property to amend userspace bug by making unusable segment always
4639 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4640 * segment as unusable.
4641 */
4642 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004643 var->avl = (ar >> 12) & 1;
4644 var->l = (ar >> 13) & 1;
4645 var->db = (ar >> 14) & 1;
4646 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004647}
4648
Avi Kivitya9179492011-01-03 14:28:52 +02004649static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4650{
Avi Kivitya9179492011-01-03 14:28:52 +02004651 struct kvm_segment s;
4652
4653 if (to_vmx(vcpu)->rmode.vm86_active) {
4654 vmx_get_segment(vcpu, &s, seg);
4655 return s.base;
4656 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004657 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02004658}
4659
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004660static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02004661{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004662 struct vcpu_vmx *vmx = to_vmx(vcpu);
4663
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004664 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02004665 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004666 else {
4667 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004668 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02004669 }
Avi Kivity69c73022011-03-07 15:26:44 +02004670}
4671
Avi Kivity653e3102007-05-07 10:55:37 +03004672static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004673{
Avi Kivity6aa8b732006-12-10 02:21:36 -08004674 u32 ar;
4675
Avi Kivityf0495f92012-06-07 17:06:10 +03004676 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004677 ar = 1 << 16;
4678 else {
4679 ar = var->type & 15;
4680 ar |= (var->s & 1) << 4;
4681 ar |= (var->dpl & 3) << 5;
4682 ar |= (var->present & 1) << 7;
4683 ar |= (var->avl & 1) << 12;
4684 ar |= (var->l & 1) << 13;
4685 ar |= (var->db & 1) << 14;
4686 ar |= (var->g & 1) << 15;
4687 }
Avi Kivity653e3102007-05-07 10:55:37 +03004688
4689 return ar;
4690}
4691
4692static void vmx_set_segment(struct kvm_vcpu *vcpu,
4693 struct kvm_segment *var, int seg)
4694{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004695 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02004696 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03004697
Avi Kivity2fb92db2011-04-27 19:42:18 +03004698 vmx_segment_cache_clear(vmx);
4699
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004700 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4701 vmx->rmode.segs[seg] = *var;
4702 if (seg == VCPU_SREG_TR)
4703 vmcs_write16(sf->selector, var->selector);
4704 else if (var->s)
4705 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004706 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03004707 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004708
Avi Kivity653e3102007-05-07 10:55:37 +03004709 vmcs_writel(sf->base, var->base);
4710 vmcs_write32(sf->limit, var->limit);
4711 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004712
4713 /*
4714 * Fix the "Accessed" bit in AR field of segment registers for older
4715 * qemu binaries.
4716 * IA32 arch specifies that at the time of processor reset the
4717 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08004718 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004719 * state vmexit when "unrestricted guest" mode is turned on.
4720 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4721 * tree. Newer qemu binaries with that qemu fix would not need this
4722 * kvm hack.
4723 */
4724 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02004725 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004726
Gleb Natapovf924d662012-12-12 19:10:55 +02004727 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02004728
4729out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01004730 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004731}
4732
Avi Kivity6aa8b732006-12-10 02:21:36 -08004733static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4734{
Avi Kivity2fb92db2011-04-27 19:42:18 +03004735 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004736
4737 *db = (ar >> 14) & 1;
4738 *l = (ar >> 13) & 1;
4739}
4740
Gleb Natapov89a27f42010-02-16 10:51:48 +02004741static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004742{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004743 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4744 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004745}
4746
Gleb Natapov89a27f42010-02-16 10:51:48 +02004747static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004748{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004749 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4750 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004751}
4752
Gleb Natapov89a27f42010-02-16 10:51:48 +02004753static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004754{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004755 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4756 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004757}
4758
Gleb Natapov89a27f42010-02-16 10:51:48 +02004759static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004760{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004761 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4762 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004763}
4764
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004765static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4766{
4767 struct kvm_segment var;
4768 u32 ar;
4769
4770 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02004771 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02004772 if (seg == VCPU_SREG_CS)
4773 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004774 ar = vmx_segment_access_rights(&var);
4775
4776 if (var.base != (var.selector << 4))
4777 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02004778 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004779 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02004780 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004781 return false;
4782
4783 return true;
4784}
4785
4786static bool code_segment_valid(struct kvm_vcpu *vcpu)
4787{
4788 struct kvm_segment cs;
4789 unsigned int cs_rpl;
4790
4791 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004792 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004793
Avi Kivity1872a3f2009-01-04 23:26:52 +02004794 if (cs.unusable)
4795 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004796 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004797 return false;
4798 if (!cs.s)
4799 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004800 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004801 if (cs.dpl > cs_rpl)
4802 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004803 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004804 if (cs.dpl != cs_rpl)
4805 return false;
4806 }
4807 if (!cs.present)
4808 return false;
4809
4810 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
4811 return true;
4812}
4813
4814static bool stack_segment_valid(struct kvm_vcpu *vcpu)
4815{
4816 struct kvm_segment ss;
4817 unsigned int ss_rpl;
4818
4819 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004820 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004821
Avi Kivity1872a3f2009-01-04 23:26:52 +02004822 if (ss.unusable)
4823 return true;
4824 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004825 return false;
4826 if (!ss.s)
4827 return false;
4828 if (ss.dpl != ss_rpl) /* DPL != RPL */
4829 return false;
4830 if (!ss.present)
4831 return false;
4832
4833 return true;
4834}
4835
4836static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
4837{
4838 struct kvm_segment var;
4839 unsigned int rpl;
4840
4841 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03004842 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004843
Avi Kivity1872a3f2009-01-04 23:26:52 +02004844 if (var.unusable)
4845 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004846 if (!var.s)
4847 return false;
4848 if (!var.present)
4849 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004850 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004851 if (var.dpl < rpl) /* DPL < RPL */
4852 return false;
4853 }
4854
4855 /* TODO: Add other members to kvm_segment_field to allow checking for other access
4856 * rights flags
4857 */
4858 return true;
4859}
4860
4861static bool tr_valid(struct kvm_vcpu *vcpu)
4862{
4863 struct kvm_segment tr;
4864
4865 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
4866
Avi Kivity1872a3f2009-01-04 23:26:52 +02004867 if (tr.unusable)
4868 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03004869 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004870 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004871 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004872 return false;
4873 if (!tr.present)
4874 return false;
4875
4876 return true;
4877}
4878
4879static bool ldtr_valid(struct kvm_vcpu *vcpu)
4880{
4881 struct kvm_segment ldtr;
4882
4883 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
4884
Avi Kivity1872a3f2009-01-04 23:26:52 +02004885 if (ldtr.unusable)
4886 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03004887 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004888 return false;
4889 if (ldtr.type != 2)
4890 return false;
4891 if (!ldtr.present)
4892 return false;
4893
4894 return true;
4895}
4896
4897static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
4898{
4899 struct kvm_segment cs, ss;
4900
4901 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4902 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4903
Nadav Amitb32a9912015-03-29 16:33:04 +03004904 return ((cs.selector & SEGMENT_RPL_MASK) ==
4905 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004906}
4907
4908/*
4909 * Check if guest state is valid. Returns true if valid, false if
4910 * not.
4911 * We assume that registers are always usable
4912 */
4913static bool guest_state_valid(struct kvm_vcpu *vcpu)
4914{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02004915 if (enable_unrestricted_guest)
4916 return true;
4917
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004918 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03004919 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004920 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
4921 return false;
4922 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
4923 return false;
4924 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
4925 return false;
4926 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
4927 return false;
4928 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
4929 return false;
4930 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
4931 return false;
4932 } else {
4933 /* protected mode guest state checks */
4934 if (!cs_ss_rpl_check(vcpu))
4935 return false;
4936 if (!code_segment_valid(vcpu))
4937 return false;
4938 if (!stack_segment_valid(vcpu))
4939 return false;
4940 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
4941 return false;
4942 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
4943 return false;
4944 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
4945 return false;
4946 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
4947 return false;
4948 if (!tr_valid(vcpu))
4949 return false;
4950 if (!ldtr_valid(vcpu))
4951 return false;
4952 }
4953 /* TODO:
4954 * - Add checks on RIP
4955 * - Add checks on RFLAGS
4956 */
4957
4958 return true;
4959}
4960
Jim Mattson5fa99cb2017-07-06 16:33:07 -07004961static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
4962{
4963 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
4964}
4965
Mike Dayd77c26f2007-10-08 09:02:08 -04004966static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004967{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004968 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02004969 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004970 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004971
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004972 idx = srcu_read_lock(&kvm->srcu);
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004973 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02004974 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4975 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004976 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004977 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08004978 r = kvm_write_guest_page(kvm, fn++, &data,
4979 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02004980 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004981 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004982 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
4983 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004984 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004985 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4986 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004987 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004988 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004989 r = kvm_write_guest_page(kvm, fn, &data,
4990 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
4991 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004992out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004993 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004994 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004995}
4996
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004997static int init_rmode_identity_map(struct kvm *kvm)
4998{
Tang Chenf51770e2014-09-16 18:41:59 +08004999 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005000 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005001 u32 tmp;
5002
Tang Chena255d472014-09-16 18:41:58 +08005003 /* Protect kvm->arch.ept_identity_pagetable_done. */
5004 mutex_lock(&kvm->slots_lock);
5005
Tang Chenf51770e2014-09-16 18:41:59 +08005006 if (likely(kvm->arch.ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005007 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005008
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005009 if (!kvm->arch.ept_identity_map_addr)
5010 kvm->arch.ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Sheng Yangb927a3c2009-07-21 10:42:48 +08005011 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005012
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005013 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
5014 kvm->arch.ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005015 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005016 goto out2;
5017
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005018 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005019 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5020 if (r < 0)
5021 goto out;
5022 /* Set up identity-mapping pagetable for EPT in real mode */
5023 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5024 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5025 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5026 r = kvm_write_guest_page(kvm, identity_map_pfn,
5027 &tmp, i * sizeof(tmp), sizeof(tmp));
5028 if (r < 0)
5029 goto out;
5030 }
5031 kvm->arch.ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005032
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005033out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005034 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005035
5036out2:
5037 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005038 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005039}
5040
Avi Kivity6aa8b732006-12-10 02:21:36 -08005041static void seg_setup(int seg)
5042{
Mathias Krause772e0312012-08-30 01:30:19 +02005043 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005044 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005045
5046 vmcs_write16(sf->selector, 0);
5047 vmcs_writel(sf->base, 0);
5048 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005049 ar = 0x93;
5050 if (seg == VCPU_SREG_CS)
5051 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005052
5053 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005054}
5055
Sheng Yangf78e0e22007-10-29 09:40:42 +08005056static int alloc_apic_access_page(struct kvm *kvm)
5057{
Xiao Guangrong44841412012-09-07 14:14:20 +08005058 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005059 int r = 0;
5060
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005061 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005062 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005063 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005064 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5065 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005066 if (r)
5067 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005068
Tang Chen73a6d942014-09-11 13:38:00 +08005069 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005070 if (is_error_page(page)) {
5071 r = -EFAULT;
5072 goto out;
5073 }
5074
Tang Chenc24ae0d2014-09-24 15:57:58 +08005075 /*
5076 * Do not pin the page in memory, so that memory hot-unplug
5077 * is able to migrate it.
5078 */
5079 put_page(page);
5080 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005081out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005082 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005083 return r;
5084}
5085
Wanpeng Li991e7a02015-09-16 17:30:05 +08005086static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005087{
5088 int vpid;
5089
Avi Kivity919818a2009-03-23 18:01:29 +02005090 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005091 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005092 spin_lock(&vmx_vpid_lock);
5093 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005094 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005095 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005096 else
5097 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005098 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005099 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005100}
5101
Wanpeng Li991e7a02015-09-16 17:30:05 +08005102static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005103{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005104 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005105 return;
5106 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005107 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005108 spin_unlock(&vmx_vpid_lock);
5109}
5110
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005111static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5112 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005113{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005114 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005115
5116 if (!cpu_has_vmx_msr_bitmap())
5117 return;
5118
5119 /*
5120 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5121 * have the write-low and read-high bitmap offsets the wrong way round.
5122 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5123 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005124 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005125 if (type & MSR_TYPE_R)
5126 /* read-low */
5127 __clear_bit(msr, msr_bitmap + 0x000 / f);
5128
5129 if (type & MSR_TYPE_W)
5130 /* write-low */
5131 __clear_bit(msr, msr_bitmap + 0x800 / f);
5132
Sheng Yang25c5f222008-03-28 13:18:56 +08005133 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5134 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005135 if (type & MSR_TYPE_R)
5136 /* read-high */
5137 __clear_bit(msr, msr_bitmap + 0x400 / f);
5138
5139 if (type & MSR_TYPE_W)
5140 /* write-high */
5141 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5142
5143 }
5144}
5145
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005146static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5147 u32 msr, int type)
5148{
5149 int f = sizeof(unsigned long);
5150
5151 if (!cpu_has_vmx_msr_bitmap())
5152 return;
5153
5154 /*
5155 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5156 * have the write-low and read-high bitmap offsets the wrong way round.
5157 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5158 */
5159 if (msr <= 0x1fff) {
5160 if (type & MSR_TYPE_R)
5161 /* read-low */
5162 __set_bit(msr, msr_bitmap + 0x000 / f);
5163
5164 if (type & MSR_TYPE_W)
5165 /* write-low */
5166 __set_bit(msr, msr_bitmap + 0x800 / f);
5167
5168 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5169 msr &= 0x1fff;
5170 if (type & MSR_TYPE_R)
5171 /* read-high */
5172 __set_bit(msr, msr_bitmap + 0x400 / f);
5173
5174 if (type & MSR_TYPE_W)
5175 /* write-high */
5176 __set_bit(msr, msr_bitmap + 0xc00 / f);
5177
5178 }
5179}
5180
5181static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5182 u32 msr, int type, bool value)
5183{
5184 if (value)
5185 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5186 else
5187 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5188}
5189
Wincy Vanf2b93282015-02-03 23:56:03 +08005190/*
5191 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5192 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5193 */
5194static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5195 unsigned long *msr_bitmap_nested,
5196 u32 msr, int type)
5197{
5198 int f = sizeof(unsigned long);
5199
Wincy Vanf2b93282015-02-03 23:56:03 +08005200 /*
5201 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5202 * have the write-low and read-high bitmap offsets the wrong way round.
5203 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5204 */
5205 if (msr <= 0x1fff) {
5206 if (type & MSR_TYPE_R &&
5207 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5208 /* read-low */
5209 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5210
5211 if (type & MSR_TYPE_W &&
5212 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5213 /* write-low */
5214 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5215
5216 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5217 msr &= 0x1fff;
5218 if (type & MSR_TYPE_R &&
5219 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5220 /* read-high */
5221 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5222
5223 if (type & MSR_TYPE_W &&
5224 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5225 /* write-high */
5226 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5227
5228 }
5229}
5230
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005231static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02005232{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005233 u8 mode = 0;
5234
5235 if (cpu_has_secondary_exec_ctrls() &&
5236 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5237 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5238 mode |= MSR_BITMAP_MODE_X2APIC;
5239 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5240 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5241 }
5242
5243 if (is_long_mode(vcpu))
5244 mode |= MSR_BITMAP_MODE_LM;
5245
5246 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005247}
5248
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005249#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5250
5251static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5252 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005253{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005254 int msr;
5255
5256 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5257 unsigned word = msr / BITS_PER_LONG;
5258 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5259 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005260 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005261
5262 if (mode & MSR_BITMAP_MODE_X2APIC) {
5263 /*
5264 * TPR reads and writes can be virtualized even if virtual interrupt
5265 * delivery is not in use.
5266 */
5267 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5268 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5269 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5270 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5271 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5272 }
5273 }
5274}
5275
5276static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5277{
5278 struct vcpu_vmx *vmx = to_vmx(vcpu);
5279 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5280 u8 mode = vmx_msr_bitmap_mode(vcpu);
5281 u8 changed = mode ^ vmx->msr_bitmap_mode;
5282
5283 if (!changed)
5284 return;
5285
5286 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5287 !(mode & MSR_BITMAP_MODE_LM));
5288
5289 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5290 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5291
5292 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005293}
5294
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005295static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005296{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005297 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005298}
5299
David Matlackc9f04402017-08-01 14:00:40 -07005300static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5301{
5302 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5303 gfn_t gfn;
5304
5305 /*
5306 * Don't need to mark the APIC access page dirty; it is never
5307 * written to by the CPU during APIC virtualization.
5308 */
5309
5310 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5311 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5312 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5313 }
5314
5315 if (nested_cpu_has_posted_intr(vmcs12)) {
5316 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5317 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5318 }
5319}
5320
5321
David Hildenbrand6342c502017-01-25 11:58:58 +01005322static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005323{
5324 struct vcpu_vmx *vmx = to_vmx(vcpu);
5325 int max_irr;
5326 void *vapic_page;
5327 u16 status;
5328
David Matlackc9f04402017-08-01 14:00:40 -07005329 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5330 return;
Wincy Van705699a2015-02-03 23:58:17 +08005331
David Matlackc9f04402017-08-01 14:00:40 -07005332 vmx->nested.pi_pending = false;
5333 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5334 return;
Wincy Van705699a2015-02-03 23:58:17 +08005335
David Matlackc9f04402017-08-01 14:00:40 -07005336 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5337 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005338 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02005339 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5340 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08005341 kunmap(vmx->nested.virtual_apic_page);
5342
5343 status = vmcs_read16(GUEST_INTR_STATUS);
5344 if ((u8)max_irr > ((u8)status & 0xff)) {
5345 status &= ~0xff;
5346 status |= (u8)max_irr;
5347 vmcs_write16(GUEST_INTR_STATUS, status);
5348 }
5349 }
David Matlackc9f04402017-08-01 14:00:40 -07005350
5351 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005352}
5353
Wincy Van06a55242017-04-28 13:13:59 +08005354static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5355 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005356{
5357#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005358 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5359
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005360 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005361 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005362 * The vector of interrupt to be delivered to vcpu had
5363 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005364 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005365 * Following cases will be reached in this block, and
5366 * we always send a notification event in all cases as
5367 * explained below.
5368 *
5369 * Case 1: vcpu keeps in non-root mode. Sending a
5370 * notification event posts the interrupt to vcpu.
5371 *
5372 * Case 2: vcpu exits to root mode and is still
5373 * runnable. PIR will be synced to vIRR before the
5374 * next vcpu entry. Sending a notification event in
5375 * this case has no effect, as vcpu is not in root
5376 * mode.
5377 *
5378 * Case 3: vcpu exits to root mode and is blocked.
5379 * vcpu_block() has already synced PIR to vIRR and
5380 * never blocks vcpu if vIRR is not cleared. Therefore,
5381 * a blocked vcpu here does not wait for any requested
5382 * interrupts in PIR, and sending a notification event
5383 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005384 */
Feng Wu28b835d2015-09-18 22:29:54 +08005385
Wincy Van06a55242017-04-28 13:13:59 +08005386 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005387 return true;
5388 }
5389#endif
5390 return false;
5391}
5392
Wincy Van705699a2015-02-03 23:58:17 +08005393static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5394 int vector)
5395{
5396 struct vcpu_vmx *vmx = to_vmx(vcpu);
5397
5398 if (is_guest_mode(vcpu) &&
5399 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005400 /*
5401 * If a posted intr is not recognized by hardware,
5402 * we will accomplish it in the next vmentry.
5403 */
5404 vmx->nested.pi_pending = true;
5405 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02005406 /* the PIR and ON have been set by L1. */
5407 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
5408 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005409 return 0;
5410 }
5411 return -1;
5412}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005413/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005414 * Send interrupt to vcpu via posted interrupt way.
5415 * 1. If target vcpu is running(non-root mode), send posted interrupt
5416 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5417 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5418 * interrupt from PIR in next vmentry.
5419 */
5420static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5421{
5422 struct vcpu_vmx *vmx = to_vmx(vcpu);
5423 int r;
5424
Wincy Van705699a2015-02-03 23:58:17 +08005425 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5426 if (!r)
5427 return;
5428
Yang Zhanga20ed542013-04-11 19:25:15 +08005429 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5430 return;
5431
Paolo Bonzinib95234c2016-12-19 13:57:33 +01005432 /* If a previous notification has sent the IPI, nothing to do. */
5433 if (pi_test_and_set_on(&vmx->pi_desc))
5434 return;
5435
Wincy Van06a55242017-04-28 13:13:59 +08005436 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08005437 kvm_vcpu_kick(vcpu);
5438}
5439
Avi Kivity6aa8b732006-12-10 02:21:36 -08005440/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005441 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5442 * will not change in the lifetime of the guest.
5443 * Note that host-state that does change is set elsewhere. E.g., host-state
5444 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5445 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005446static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005447{
5448 u32 low32, high32;
5449 unsigned long tmpl;
5450 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005451 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005452
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07005453 cr0 = read_cr0();
5454 WARN_ON(cr0 & X86_CR0_TS);
5455 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005456
5457 /*
5458 * Save the most likely value for this task's CR3 in the VMCS.
5459 * We can't use __get_current_cr3_fast() because we're not atomic.
5460 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07005461 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005462 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02005463 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005464
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005465 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005466 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005467 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02005468 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005469
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005470 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005471#ifdef CONFIG_X86_64
5472 /*
5473 * Load null selectors, so we can avoid reloading them in
5474 * __vmx_load_host_state(), in case userspace uses the null selectors
5475 * too (the expected case).
5476 */
5477 vmcs_write16(HOST_DS_SELECTOR, 0);
5478 vmcs_write16(HOST_ES_SELECTOR, 0);
5479#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005480 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5481 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005482#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005483 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5484 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5485
Juergen Gross87930012017-09-04 12:25:27 +02005486 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005487 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005488 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005489
Avi Kivity83287ea422012-09-16 15:10:57 +03005490 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005491
5492 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5493 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5494 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5495 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5496
5497 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5498 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5499 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5500 }
5501}
5502
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005503static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5504{
5505 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5506 if (enable_ept)
5507 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005508 if (is_guest_mode(&vmx->vcpu))
5509 vmx->vcpu.arch.cr4_guest_owned_bits &=
5510 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005511 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5512}
5513
Yang Zhang01e439b2013-04-11 19:25:12 +08005514static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5515{
5516 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5517
Andrey Smetanind62caab2015-11-10 15:36:33 +03005518 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005519 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005520
5521 if (!enable_vnmi)
5522 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
5523
Yunhong Jiang64672c92016-06-13 14:19:59 -07005524 /* Enable the preemption timer dynamically */
5525 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08005526 return pin_based_exec_ctrl;
5527}
5528
Andrey Smetanind62caab2015-11-10 15:36:33 +03005529static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5530{
5531 struct vcpu_vmx *vmx = to_vmx(vcpu);
5532
5533 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03005534 if (cpu_has_secondary_exec_ctrls()) {
5535 if (kvm_vcpu_apicv_active(vcpu))
5536 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5537 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5538 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5539 else
5540 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5541 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5542 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5543 }
5544
5545 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005546 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03005547}
5548
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005549static u32 vmx_exec_control(struct vcpu_vmx *vmx)
5550{
5551 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01005552
5553 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
5554 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5555
Paolo Bonzini35754c92015-07-29 12:05:37 +02005556 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005557 exec_control &= ~CPU_BASED_TPR_SHADOW;
5558#ifdef CONFIG_X86_64
5559 exec_control |= CPU_BASED_CR8_STORE_EXITING |
5560 CPU_BASED_CR8_LOAD_EXITING;
5561#endif
5562 }
5563 if (!enable_ept)
5564 exec_control |= CPU_BASED_CR3_STORE_EXITING |
5565 CPU_BASED_CR3_LOAD_EXITING |
5566 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07005567 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
5568 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
5569 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07005570 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
5571 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005572 return exec_control;
5573}
5574
Jim Mattson45ec3682017-08-23 16:32:04 -07005575static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005576{
Jim Mattson45ec3682017-08-23 16:32:04 -07005577 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02005578 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005579}
5580
Jim Mattson75f4fc82017-08-23 16:32:03 -07005581static bool vmx_rdseed_supported(void)
5582{
5583 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02005584 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005585}
5586
Paolo Bonzini80154d72017-08-24 13:55:35 +02005587static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005588{
Paolo Bonzini80154d72017-08-24 13:55:35 +02005589 struct kvm_vcpu *vcpu = &vmx->vcpu;
5590
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005591 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02005592
Paolo Bonzini80154d72017-08-24 13:55:35 +02005593 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005594 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5595 if (vmx->vpid == 0)
5596 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
5597 if (!enable_ept) {
5598 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
5599 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00005600 /* Enable INVPCID for non-ept guests may cause performance regression. */
5601 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005602 }
5603 if (!enable_unrestricted_guest)
5604 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07005605 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005606 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02005607 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08005608 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
5609 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08005610 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02005611
5612 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
5613 * in vmx_set_cr4. */
5614 exec_control &= ~SECONDARY_EXEC_DESC;
5615
Abel Gordonabc4fc52013-04-18 14:35:25 +03005616 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
5617 (handle_vmptrld).
5618 We can NOT enable shadow_vmcs here because we don't have yet
5619 a current VMCS12
5620 */
5621 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08005622
5623 if (!enable_pml)
5624 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08005625
Paolo Bonzini3db13482017-08-24 14:48:03 +02005626 if (vmx_xsaves_supported()) {
5627 /* Exposing XSAVES only when XSAVE is exposed */
5628 bool xsaves_enabled =
5629 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
5630 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
5631
5632 if (!xsaves_enabled)
5633 exec_control &= ~SECONDARY_EXEC_XSAVES;
5634
5635 if (nested) {
5636 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005637 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02005638 SECONDARY_EXEC_XSAVES;
5639 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005640 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02005641 ~SECONDARY_EXEC_XSAVES;
5642 }
5643 }
5644
Paolo Bonzini80154d72017-08-24 13:55:35 +02005645 if (vmx_rdtscp_supported()) {
5646 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
5647 if (!rdtscp_enabled)
5648 exec_control &= ~SECONDARY_EXEC_RDTSCP;
5649
5650 if (nested) {
5651 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005652 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02005653 SECONDARY_EXEC_RDTSCP;
5654 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005655 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02005656 ~SECONDARY_EXEC_RDTSCP;
5657 }
5658 }
5659
5660 if (vmx_invpcid_supported()) {
5661 /* Exposing INVPCID only when PCID is exposed */
5662 bool invpcid_enabled =
5663 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
5664 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
5665
5666 if (!invpcid_enabled) {
5667 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
5668 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
5669 }
5670
5671 if (nested) {
5672 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005673 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02005674 SECONDARY_EXEC_ENABLE_INVPCID;
5675 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005676 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02005677 ~SECONDARY_EXEC_ENABLE_INVPCID;
5678 }
5679 }
5680
Jim Mattson45ec3682017-08-23 16:32:04 -07005681 if (vmx_rdrand_supported()) {
5682 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
5683 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02005684 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005685
5686 if (nested) {
5687 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005688 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005689 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005690 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005691 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005692 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005693 }
5694 }
5695
Jim Mattson75f4fc82017-08-23 16:32:03 -07005696 if (vmx_rdseed_supported()) {
5697 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
5698 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02005699 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005700
5701 if (nested) {
5702 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005703 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005704 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005705 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005706 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005707 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005708 }
5709 }
5710
Paolo Bonzini80154d72017-08-24 13:55:35 +02005711 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005712}
5713
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005714static void ept_set_mmio_spte_mask(void)
5715{
5716 /*
5717 * EPT Misconfigurations can be generated if the value of bits 2:0
5718 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005719 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07005720 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
5721 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005722}
5723
Wanpeng Lif53cd632014-12-02 19:14:58 +08005724#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005725/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005726 * Sets up the vmcs for emulated real mode.
5727 */
David Hildenbrand12d79912017-08-24 20:51:26 +02005728static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005729{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005730#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005731 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005732#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08005733 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005734
Abel Gordon4607c2d2013-04-18 14:35:55 +03005735 if (enable_shadow_vmcs) {
5736 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5737 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
5738 }
Sheng Yang25c5f222008-03-28 13:18:56 +08005739 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005740 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08005741
Avi Kivity6aa8b732006-12-10 02:21:36 -08005742 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5743
Avi Kivity6aa8b732006-12-10 02:21:36 -08005744 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08005745 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07005746 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005747
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005748 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005749
Dan Williamsdfa169b2016-06-02 11:17:24 -07005750 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02005751 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005752 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02005753 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07005754 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08005755
Andrey Smetanind62caab2015-11-10 15:36:33 +03005756 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08005757 vmcs_write64(EOI_EXIT_BITMAP0, 0);
5758 vmcs_write64(EOI_EXIT_BITMAP1, 0);
5759 vmcs_write64(EOI_EXIT_BITMAP2, 0);
5760 vmcs_write64(EOI_EXIT_BITMAP3, 0);
5761
5762 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08005763
Li RongQing0bcf2612015-12-03 13:29:34 +08005764 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08005765 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08005766 }
5767
Wanpeng Lib31c1142018-03-12 04:53:04 -07005768 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005769 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02005770 vmx->ple_window = ple_window;
5771 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005772 }
5773
Xiao Guangrongc3707952011-07-12 03:28:04 +08005774 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5775 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005776 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5777
Avi Kivity9581d442010-10-19 16:46:55 +02005778 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
5779 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005780 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005781#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005782 rdmsrl(MSR_FS_BASE, a);
5783 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
5784 rdmsrl(MSR_GS_BASE, a);
5785 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
5786#else
5787 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
5788 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
5789#endif
5790
Bandan Das2a499e42017-08-03 15:54:41 -04005791 if (cpu_has_vmx_vmfunc())
5792 vmcs_write64(VM_FUNCTION_CONTROL, 0);
5793
Eddie Dong2cc51562007-05-21 07:28:09 +03005794 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
5795 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03005796 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03005797 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03005798 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005799
Radim Krčmář74545702015-04-27 15:11:25 +02005800 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
5801 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08005802
Paolo Bonzini03916db2014-07-24 14:21:57 +02005803 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08005804 u32 index = vmx_msr_index[i];
5805 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005806 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005807
5808 if (rdmsr_safe(index, &data_low, &data_high) < 0)
5809 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08005810 if (wrmsr_safe(index, data_low, data_high) < 0)
5811 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03005812 vmx->guest_msrs[j].index = i;
5813 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02005814 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005815 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005816 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005817
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01005818 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
5819 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02005820
5821 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005822
5823 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02005824 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03005825
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08005826 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
5827 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
5828
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005829 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005830
Wanpeng Lif53cd632014-12-02 19:14:58 +08005831 if (vmx_xsaves_supported())
5832 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
5833
Peter Feiner4e595162016-07-07 14:49:58 -07005834 if (enable_pml) {
5835 ASSERT(vmx->pml_pg);
5836 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
5837 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5838 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005839}
5840
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005841static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005842{
5843 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01005844 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005845 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005846
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005847 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01005848 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005849
Wanpeng Li518e7b92018-02-28 14:03:31 +08005850 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005851 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005852 kvm_set_cr8(vcpu, 0);
5853
5854 if (!init_event) {
5855 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
5856 MSR_IA32_APICBASE_ENABLE;
5857 if (kvm_vcpu_is_reset_bsp(vcpu))
5858 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
5859 apic_base_msr.host_initiated = true;
5860 kvm_set_apic_base(vcpu, &apic_base_msr);
5861 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005862
Avi Kivity2fb92db2011-04-27 19:42:18 +03005863 vmx_segment_cache_clear(vmx);
5864
Avi Kivity5706be02008-08-20 15:07:31 +03005865 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01005866 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005867 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005868
5869 seg_setup(VCPU_SREG_DS);
5870 seg_setup(VCPU_SREG_ES);
5871 seg_setup(VCPU_SREG_FS);
5872 seg_setup(VCPU_SREG_GS);
5873 seg_setup(VCPU_SREG_SS);
5874
5875 vmcs_write16(GUEST_TR_SELECTOR, 0);
5876 vmcs_writel(GUEST_TR_BASE, 0);
5877 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
5878 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5879
5880 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
5881 vmcs_writel(GUEST_LDTR_BASE, 0);
5882 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
5883 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
5884
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005885 if (!init_event) {
5886 vmcs_write32(GUEST_SYSENTER_CS, 0);
5887 vmcs_writel(GUEST_SYSENTER_ESP, 0);
5888 vmcs_writel(GUEST_SYSENTER_EIP, 0);
5889 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
5890 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005891
Wanpeng Lic37c2872017-11-20 14:52:21 -08005892 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01005893 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005894
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005895 vmcs_writel(GUEST_GDTR_BASE, 0);
5896 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
5897
5898 vmcs_writel(GUEST_IDTR_BASE, 0);
5899 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
5900
Anthony Liguori443381a2010-12-06 10:53:38 -06005901 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005902 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005903 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07005904 if (kvm_mpx_supported())
5905 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005906
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005907 setup_msrs(vmx);
5908
Avi Kivity6aa8b732006-12-10 02:21:36 -08005909 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
5910
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005911 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08005912 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005913 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08005914 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005915 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08005916 vmcs_write32(TPR_THRESHOLD, 0);
5917 }
5918
Paolo Bonzinia73896c2014-11-02 07:54:30 +01005919 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005920
Sheng Yang2384d2b2008-01-17 15:14:33 +08005921 if (vmx->vpid != 0)
5922 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
5923
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005924 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005925 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06005926 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005927 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02005928 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08005929
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005930 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005931
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005932 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07005933 if (init_event)
5934 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005935}
5936
Nadav Har'Elb6f12502011-05-25 23:13:06 +03005937/*
5938 * In nested virtualization, check if L1 asked to exit on external interrupts.
5939 * For most existing hypervisors, this will always return true.
5940 */
5941static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
5942{
5943 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5944 PIN_BASED_EXT_INTR_MASK;
5945}
5946
Bandan Das77b0f5d2014-04-19 18:17:45 -04005947/*
5948 * In nested virtualization, check if L1 has set
5949 * VM_EXIT_ACK_INTR_ON_EXIT
5950 */
5951static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
5952{
5953 return get_vmcs12(vcpu)->vm_exit_controls &
5954 VM_EXIT_ACK_INTR_ON_EXIT;
5955}
5956
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005957static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
5958{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05005959 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005960}
5961
Jan Kiszkac9a79532014-03-07 20:03:15 +01005962static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005963{
Paolo Bonzini47c01522016-12-19 11:44:07 +01005964 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
5965 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005966}
5967
Jan Kiszkac9a79532014-03-07 20:03:15 +01005968static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005969{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005970 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005971 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01005972 enable_irq_window(vcpu);
5973 return;
5974 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02005975
Paolo Bonzini47c01522016-12-19 11:44:07 +01005976 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
5977 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005978}
5979
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005980static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03005981{
Avi Kivity9c8cba32007-11-22 11:42:59 +02005982 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005983 uint32_t intr;
5984 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02005985
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005986 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005987
Avi Kivityfa89a812008-09-01 15:57:51 +03005988 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005989 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005990 int inc_eip = 0;
5991 if (vcpu->arch.interrupt.soft)
5992 inc_eip = vcpu->arch.event_exit_inst_len;
5993 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005994 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005995 return;
5996 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005997 intr = irq | INTR_INFO_VALID_MASK;
5998 if (vcpu->arch.interrupt.soft) {
5999 intr |= INTR_TYPE_SOFT_INTR;
6000 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6001 vmx->vcpu.arch.event_exit_inst_len);
6002 } else
6003 intr |= INTR_TYPE_EXT_INTR;
6004 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006005
6006 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006007}
6008
Sheng Yangf08864b2008-05-15 18:23:25 +08006009static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6010{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006011 struct vcpu_vmx *vmx = to_vmx(vcpu);
6012
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006013 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006014 /*
6015 * Tracking the NMI-blocked state in software is built upon
6016 * finding the next open IRQ window. This, in turn, depends on
6017 * well-behaving guests: They have to keep IRQs disabled at
6018 * least as long as the NMI handler runs. Otherwise we may
6019 * cause NMI nesting, maybe breaking the guest. But as this is
6020 * highly unlikely, we can live with the residual risk.
6021 */
6022 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6023 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6024 }
6025
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006026 ++vcpu->stat.nmi_injections;
6027 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006028
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006029 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006030 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006031 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006032 return;
6033 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006034
Sheng Yangf08864b2008-05-15 18:23:25 +08006035 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6036 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006037
6038 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006039}
6040
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006041static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6042{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006043 struct vcpu_vmx *vmx = to_vmx(vcpu);
6044 bool masked;
6045
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006046 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006047 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006048 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006049 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006050 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6051 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6052 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006053}
6054
6055static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6056{
6057 struct vcpu_vmx *vmx = to_vmx(vcpu);
6058
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006059 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006060 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6061 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6062 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6063 }
6064 } else {
6065 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6066 if (masked)
6067 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6068 GUEST_INTR_STATE_NMI);
6069 else
6070 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6071 GUEST_INTR_STATE_NMI);
6072 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006073}
6074
Jan Kiszka2505dc92013-04-14 12:12:47 +02006075static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6076{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006077 if (to_vmx(vcpu)->nested.nested_run_pending)
6078 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006079
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006080 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006081 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6082 return 0;
6083
Jan Kiszka2505dc92013-04-14 12:12:47 +02006084 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6085 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6086 | GUEST_INTR_STATE_NMI));
6087}
6088
Gleb Natapov78646122009-03-23 12:12:11 +02006089static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6090{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006091 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6092 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006093 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6094 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006095}
6096
Izik Eiduscbc94022007-10-25 00:29:55 +02006097static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6098{
6099 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006100
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006101 if (enable_unrestricted_guest)
6102 return 0;
6103
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006104 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6105 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006106 if (ret)
6107 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08006108 kvm->arch.tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006109 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006110}
6111
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006112static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006113{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006114 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006115 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01006116 /*
6117 * Update instruction length as we may reinject the exception
6118 * from user space while in guest debugging mode.
6119 */
6120 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6121 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006122 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006123 return false;
6124 /* fall through */
6125 case DB_VECTOR:
6126 if (vcpu->guest_debug &
6127 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6128 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006129 /* fall through */
6130 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006131 case OF_VECTOR:
6132 case BR_VECTOR:
6133 case UD_VECTOR:
6134 case DF_VECTOR:
6135 case SS_VECTOR:
6136 case GP_VECTOR:
6137 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006138 return true;
6139 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006140 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006141 return false;
6142}
6143
6144static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6145 int vec, u32 err_code)
6146{
6147 /*
6148 * Instruction with address size override prefix opcode 0x67
6149 * Cause the #SS fault with 0 error code in VM86 mode.
6150 */
6151 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6152 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6153 if (vcpu->arch.halt_request) {
6154 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006155 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006156 }
6157 return 1;
6158 }
6159 return 0;
6160 }
6161
6162 /*
6163 * Forward all other exceptions that are valid in real mode.
6164 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6165 * the required debugging infrastructure rework.
6166 */
6167 kvm_queue_exception(vcpu, vec);
6168 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006169}
6170
Andi Kleena0861c02009-06-08 17:37:09 +08006171/*
6172 * Trigger machine check on the host. We assume all the MSRs are already set up
6173 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6174 * We pass a fake environment to the machine check handler because we want
6175 * the guest to be always treated like user space, no matter what context
6176 * it used internally.
6177 */
6178static void kvm_machine_check(void)
6179{
6180#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6181 struct pt_regs regs = {
6182 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6183 .flags = X86_EFLAGS_IF,
6184 };
6185
6186 do_machine_check(&regs, 0);
6187#endif
6188}
6189
Avi Kivity851ba692009-08-24 11:10:17 +03006190static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08006191{
6192 /* already handled by vcpu_run */
6193 return 1;
6194}
6195
Avi Kivity851ba692009-08-24 11:10:17 +03006196static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006197{
Avi Kivity1155f762007-11-22 11:30:47 +02006198 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006199 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006200 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006201 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006202 u32 vect_info;
6203 enum emulation_result er;
6204
Avi Kivity1155f762007-11-22 11:30:47 +02006205 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02006206 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006207
Andi Kleena0861c02009-06-08 17:37:09 +08006208 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03006209 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006210
Jim Mattsonef85b672016-12-12 11:01:37 -08006211 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02006212 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03006213
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006214 if (is_invalid_opcode(intr_info)) {
Andre Przywara51d8b662010-12-21 11:12:02 +01006215 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Liran Alon61cb57c2017-11-05 16:56:32 +02006216 if (er == EMULATE_USER_EXIT)
6217 return 0;
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006218 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02006219 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006220 return 1;
6221 }
6222
Avi Kivity6aa8b732006-12-10 02:21:36 -08006223 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06006224 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006225 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006226
Liran Alon9e869482018-03-12 13:12:51 +02006227 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
6228 WARN_ON_ONCE(!enable_vmware_backdoor);
6229 er = emulate_instruction(vcpu,
6230 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
6231 if (er == EMULATE_USER_EXIT)
6232 return 0;
6233 else if (er != EMULATE_DONE)
6234 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
6235 return 1;
6236 }
6237
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006238 /*
6239 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6240 * MMIO, it is better to report an internal error.
6241 * See the comments in vmx_handle_exit.
6242 */
6243 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6244 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6245 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6246 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006247 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006248 vcpu->run->internal.data[0] = vect_info;
6249 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006250 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006251 return 0;
6252 }
6253
Avi Kivity6aa8b732006-12-10 02:21:36 -08006254 if (is_page_fault(intr_info)) {
6255 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006256 /* EPT won't cause page fault directly */
6257 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02006258 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006259 }
6260
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006261 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006262
6263 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6264 return handle_rmode_exception(vcpu, ex_no, error_code);
6265
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006266 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01006267 case AC_VECTOR:
6268 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6269 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006270 case DB_VECTOR:
6271 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6272 if (!(vcpu->guest_debug &
6273 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01006274 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006275 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Huw Daviesfd2a4452014-04-16 10:02:51 +01006276 if (!(dr6 & ~DR6_RESERVED)) /* icebp */
6277 skip_emulated_instruction(vcpu);
6278
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006279 kvm_queue_exception(vcpu, DB_VECTOR);
6280 return 1;
6281 }
6282 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6283 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6284 /* fall through */
6285 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01006286 /*
6287 * Update instruction length as we may reinject #BP from
6288 * user space while in guest debugging mode. Reading it for
6289 * #DB as well causes no harm, it is not used in that case.
6290 */
6291 vmx->vcpu.arch.event_exit_inst_len =
6292 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006293 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006294 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006295 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6296 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006297 break;
6298 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006299 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6300 kvm_run->ex.exception = ex_no;
6301 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006302 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006303 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006304 return 0;
6305}
6306
Avi Kivity851ba692009-08-24 11:10:17 +03006307static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006308{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006309 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006310 return 1;
6311}
6312
Avi Kivity851ba692009-08-24 11:10:17 +03006313static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006314{
Avi Kivity851ba692009-08-24 11:10:17 +03006315 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006316 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006317 return 0;
6318}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006319
Avi Kivity851ba692009-08-24 11:10:17 +03006320static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006321{
He, Qingbfdaab02007-09-12 14:18:28 +08006322 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08006323 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02006324 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006325
He, Qingbfdaab02007-09-12 14:18:28 +08006326 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006327 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006328
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006329 ++vcpu->stat.io_exits;
6330
Sean Christopherson432baf62018-03-08 08:57:26 -08006331 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01006332 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006333
6334 port = exit_qualification >> 16;
6335 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08006336 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006337
Sean Christophersondca7f122018-03-08 08:57:27 -08006338 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006339}
6340
Ingo Molnar102d8322007-02-19 14:37:47 +02006341static void
6342vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6343{
6344 /*
6345 * Patch in the VMCALL instruction:
6346 */
6347 hypercall[0] = 0x0f;
6348 hypercall[1] = 0x01;
6349 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006350}
6351
Guo Chao0fa06072012-06-28 15:16:19 +08006352/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006353static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6354{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006355 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006356 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6357 unsigned long orig_val = val;
6358
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006359 /*
6360 * We get here when L2 changed cr0 in a way that did not change
6361 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006362 * but did change L0 shadowed bits. So we first calculate the
6363 * effective cr0 value that L1 would like to write into the
6364 * hardware. It consists of the L2-owned bits from the new
6365 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006366 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006367 val = (val & ~vmcs12->cr0_guest_host_mask) |
6368 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6369
David Matlack38991522016-11-29 18:14:08 -08006370 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006371 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006372
6373 if (kvm_set_cr0(vcpu, val))
6374 return 1;
6375 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006376 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006377 } else {
6378 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08006379 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006380 return 1;
David Matlack38991522016-11-29 18:14:08 -08006381
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006382 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006383 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006384}
6385
6386static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6387{
6388 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006389 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6390 unsigned long orig_val = val;
6391
6392 /* analogously to handle_set_cr0 */
6393 val = (val & ~vmcs12->cr4_guest_host_mask) |
6394 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6395 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006396 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006397 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006398 return 0;
6399 } else
6400 return kvm_set_cr4(vcpu, val);
6401}
6402
Paolo Bonzini0367f202016-07-12 10:44:55 +02006403static int handle_desc(struct kvm_vcpu *vcpu)
6404{
6405 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
6406 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6407}
6408
Avi Kivity851ba692009-08-24 11:10:17 +03006409static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006410{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006411 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006412 int cr;
6413 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03006414 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006415 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006416
He, Qingbfdaab02007-09-12 14:18:28 +08006417 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006418 cr = exit_qualification & 15;
6419 reg = (exit_qualification >> 8) & 15;
6420 switch ((exit_qualification >> 4) & 3) {
6421 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03006422 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006423 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006424 switch (cr) {
6425 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006426 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006427 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006428 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006429 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03006430 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006431 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006432 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006433 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006434 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006435 case 8: {
6436 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03006437 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006438 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006439 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006440 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08006441 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006442 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006443 return ret;
6444 /*
6445 * TODO: we might be squashing a
6446 * KVM_GUESTDBG_SINGLESTEP-triggered
6447 * KVM_EXIT_DEBUG here.
6448 */
Avi Kivity851ba692009-08-24 11:10:17 +03006449 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006450 return 0;
6451 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02006452 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006453 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03006454 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006455 WARN_ONCE(1, "Guest should always own CR0.TS");
6456 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02006457 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08006458 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006459 case 1: /*mov from cr*/
6460 switch (cr) {
6461 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006462 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02006463 val = kvm_read_cr3(vcpu);
6464 kvm_register_write(vcpu, reg, val);
6465 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006466 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006467 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006468 val = kvm_get_cr8(vcpu);
6469 kvm_register_write(vcpu, reg, val);
6470 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006471 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006472 }
6473 break;
6474 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02006475 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02006476 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02006477 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006478
Kyle Huey6affcbe2016-11-29 12:40:40 -08006479 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006480 default:
6481 break;
6482 }
Avi Kivity851ba692009-08-24 11:10:17 +03006483 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03006484 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08006485 (int)(exit_qualification >> 4) & 3, cr);
6486 return 0;
6487}
6488
Avi Kivity851ba692009-08-24 11:10:17 +03006489static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006490{
He, Qingbfdaab02007-09-12 14:18:28 +08006491 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006492 int dr, dr7, reg;
6493
6494 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6495 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6496
6497 /* First, if DR does not exist, trigger UD */
6498 if (!kvm_require_dr(vcpu, dr))
6499 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006500
Jan Kiszkaf2483412010-01-20 18:20:20 +01006501 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03006502 if (!kvm_require_cpl(vcpu, 0))
6503 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006504 dr7 = vmcs_readl(GUEST_DR7);
6505 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006506 /*
6507 * As the vm-exit takes precedence over the debug trap, we
6508 * need to emulate the latter, either for the host or the
6509 * guest debugging itself.
6510 */
6511 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03006512 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006513 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02006514 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006515 vcpu->run->debug.arch.exception = DB_VECTOR;
6516 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006517 return 0;
6518 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02006519 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006520 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006521 kvm_queue_exception(vcpu, DB_VECTOR);
6522 return 1;
6523 }
6524 }
6525
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006526 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01006527 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6528 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006529
6530 /*
6531 * No more DR vmexits; force a reload of the debug registers
6532 * and reenter on this instruction. The next vmexit will
6533 * retrieve the full state of the debug registers.
6534 */
6535 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
6536 return 1;
6537 }
6538
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006539 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
6540 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03006541 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006542
6543 if (kvm_get_dr(vcpu, dr, &val))
6544 return 1;
6545 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03006546 } else
Nadav Amit57773922014-06-18 17:19:23 +03006547 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006548 return 1;
6549
Kyle Huey6affcbe2016-11-29 12:40:40 -08006550 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006551}
6552
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01006553static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
6554{
6555 return vcpu->arch.dr6;
6556}
6557
6558static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
6559{
6560}
6561
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006562static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
6563{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006564 get_debugreg(vcpu->arch.db[0], 0);
6565 get_debugreg(vcpu->arch.db[1], 1);
6566 get_debugreg(vcpu->arch.db[2], 2);
6567 get_debugreg(vcpu->arch.db[3], 3);
6568 get_debugreg(vcpu->arch.dr6, 6);
6569 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
6570
6571 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01006572 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006573}
6574
Gleb Natapov020df072010-04-13 10:05:23 +03006575static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
6576{
6577 vmcs_writel(GUEST_DR7, val);
6578}
6579
Avi Kivity851ba692009-08-24 11:10:17 +03006580static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006581{
Kyle Huey6a908b62016-11-29 12:40:37 -08006582 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006583}
6584
Avi Kivity851ba692009-08-24 11:10:17 +03006585static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006586{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006587 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006588 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006589
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006590 msr_info.index = ecx;
6591 msr_info.host_initiated = false;
6592 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02006593 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006594 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006595 return 1;
6596 }
6597
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006598 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006599
Avi Kivity6aa8b732006-12-10 02:21:36 -08006600 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006601 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
6602 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006603 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006604}
6605
Avi Kivity851ba692009-08-24 11:10:17 +03006606static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006607{
Will Auld8fe8ab42012-11-29 12:42:12 -08006608 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006609 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6610 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
6611 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006612
Will Auld8fe8ab42012-11-29 12:42:12 -08006613 msr.data = data;
6614 msr.index = ecx;
6615 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03006616 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02006617 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006618 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006619 return 1;
6620 }
6621
Avi Kivity59200272010-01-25 19:47:02 +02006622 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006623 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006624}
6625
Avi Kivity851ba692009-08-24 11:10:17 +03006626static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006627{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01006628 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006629 return 1;
6630}
6631
Avi Kivity851ba692009-08-24 11:10:17 +03006632static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006633{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006634 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6635 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006636
Avi Kivity3842d132010-07-27 12:30:24 +03006637 kvm_make_request(KVM_REQ_EVENT, vcpu);
6638
Jan Kiszkaa26bf122008-09-26 09:30:45 +02006639 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006640 return 1;
6641}
6642
Avi Kivity851ba692009-08-24 11:10:17 +03006643static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006644{
Avi Kivityd3bef152007-06-05 15:53:05 +03006645 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006646}
6647
Avi Kivity851ba692009-08-24 11:10:17 +03006648static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02006649{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03006650 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02006651}
6652
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006653static int handle_invd(struct kvm_vcpu *vcpu)
6654{
Andre Przywara51d8b662010-12-21 11:12:02 +01006655 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006656}
6657
Avi Kivity851ba692009-08-24 11:10:17 +03006658static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03006659{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006660 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006661
6662 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006663 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006664}
6665
Avi Kivityfee84b02011-11-10 14:57:25 +02006666static int handle_rdpmc(struct kvm_vcpu *vcpu)
6667{
6668 int err;
6669
6670 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006671 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02006672}
6673
Avi Kivity851ba692009-08-24 11:10:17 +03006674static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02006675{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006676 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02006677}
6678
Dexuan Cui2acf9232010-06-10 11:27:12 +08006679static int handle_xsetbv(struct kvm_vcpu *vcpu)
6680{
6681 u64 new_bv = kvm_read_edx_eax(vcpu);
6682 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
6683
6684 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006685 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08006686 return 1;
6687}
6688
Wanpeng Lif53cd632014-12-02 19:14:58 +08006689static int handle_xsaves(struct kvm_vcpu *vcpu)
6690{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006691 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08006692 WARN(1, "this should never happen\n");
6693 return 1;
6694}
6695
6696static int handle_xrstors(struct kvm_vcpu *vcpu)
6697{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006698 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08006699 WARN(1, "this should never happen\n");
6700 return 1;
6701}
6702
Avi Kivity851ba692009-08-24 11:10:17 +03006703static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08006704{
Kevin Tian58fbbf22011-08-30 13:56:17 +03006705 if (likely(fasteoi)) {
6706 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6707 int access_type, offset;
6708
6709 access_type = exit_qualification & APIC_ACCESS_TYPE;
6710 offset = exit_qualification & APIC_ACCESS_OFFSET;
6711 /*
6712 * Sane guest uses MOV to write EOI, with written value
6713 * not cared. So make a short-circuit here by avoiding
6714 * heavy instruction emulation.
6715 */
6716 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6717 (offset == APIC_EOI)) {
6718 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006719 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03006720 }
6721 }
Andre Przywara51d8b662010-12-21 11:12:02 +01006722 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08006723}
6724
Yang Zhangc7c9c562013-01-25 10:18:51 +08006725static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6726{
6727 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6728 int vector = exit_qualification & 0xff;
6729
6730 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6731 kvm_apic_set_eoi_accelerated(vcpu, vector);
6732 return 1;
6733}
6734
Yang Zhang83d4c282013-01-25 10:18:49 +08006735static int handle_apic_write(struct kvm_vcpu *vcpu)
6736{
6737 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6738 u32 offset = exit_qualification & 0xfff;
6739
6740 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6741 kvm_apic_write_nodecode(vcpu, offset);
6742 return 1;
6743}
6744
Avi Kivity851ba692009-08-24 11:10:17 +03006745static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02006746{
Jan Kiszka60637aa2008-09-26 09:30:47 +02006747 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02006748 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02006749 bool has_error_code = false;
6750 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02006751 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006752 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006753
6754 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006755 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006756 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02006757
6758 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6759
6760 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006761 if (reason == TASK_SWITCH_GATE && idt_v) {
6762 switch (type) {
6763 case INTR_TYPE_NMI_INTR:
6764 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02006765 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006766 break;
6767 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006768 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006769 kvm_clear_interrupt_queue(vcpu);
6770 break;
6771 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02006772 if (vmx->idt_vectoring_info &
6773 VECTORING_INFO_DELIVER_CODE_MASK) {
6774 has_error_code = true;
6775 error_code =
6776 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6777 }
6778 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006779 case INTR_TYPE_SOFT_EXCEPTION:
6780 kvm_clear_exception_queue(vcpu);
6781 break;
6782 default:
6783 break;
6784 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02006785 }
Izik Eidus37817f22008-03-24 23:14:53 +02006786 tss_selector = exit_qualification;
6787
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006788 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
6789 type != INTR_TYPE_EXT_INTR &&
6790 type != INTR_TYPE_NMI_INTR))
6791 skip_emulated_instruction(vcpu);
6792
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006793 if (kvm_task_switch(vcpu, tss_selector,
6794 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
6795 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03006796 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6797 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6798 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006799 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03006800 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006801
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006802 /*
6803 * TODO: What about debug traps on tss switch?
6804 * Are we supposed to inject them and update dr6?
6805 */
6806
6807 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02006808}
6809
Avi Kivity851ba692009-08-24 11:10:17 +03006810static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08006811{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006812 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08006813 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01006814 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08006815
Sheng Yangf9c617f2009-03-25 10:08:52 +08006816 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08006817
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006818 /*
6819 * EPT violation happened while executing iret from NMI,
6820 * "blocked by NMI" bit has to be set before next VM entry.
6821 * There are errata that may cause this bit to not be set:
6822 * AAK134, BY25.
6823 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006824 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006825 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006826 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006827 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
6828
Sheng Yang14394422008-04-28 12:24:45 +08006829 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006830 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006831
Junaid Shahid27959a42016-12-06 16:46:10 -08006832 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08006833 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08006834 ? PFERR_USER_MASK : 0;
6835 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08006836 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08006837 ? PFERR_WRITE_MASK : 0;
6838 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08006839 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08006840 ? PFERR_FETCH_MASK : 0;
6841 /* ept page table entry is present? */
6842 error_code |= (exit_qualification &
6843 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
6844 EPT_VIOLATION_EXECUTABLE))
6845 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006846
Paolo Bonzinieebed242016-11-28 14:39:58 +01006847 error_code |= (exit_qualification & 0x100) != 0 ?
6848 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03006849
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006850 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006851 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08006852}
6853
Avi Kivity851ba692009-08-24 11:10:17 +03006854static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006855{
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006856 int ret;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006857 gpa_t gpa;
6858
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02006859 /*
6860 * A nested guest cannot optimize MMIO vmexits, because we have an
6861 * nGPA here instead of the required GPA.
6862 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006863 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02006864 if (!is_guest_mode(vcpu) &&
6865 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08006866 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01006867 /*
6868 * Doing kvm_skip_emulated_instruction() depends on undefined
6869 * behavior: Intel's manual doesn't mandate
6870 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
6871 * occurs and while on real hardware it was observed to be set,
6872 * other hypervisors (namely Hyper-V) don't set it, we end up
6873 * advancing IP with some random value. Disable fast mmio when
6874 * running nested and keep it for real hardware in hope that
6875 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
6876 */
6877 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
6878 return kvm_skip_emulated_instruction(vcpu);
6879 else
6880 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
6881 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006882 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006883
Paolo Bonzinie08d26f2017-08-17 18:36:56 +02006884 ret = kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
6885 if (ret >= 0)
6886 return ret;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006887
6888 /* It is the real ept misconfig */
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006889 WARN_ON(1);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006890
Avi Kivity851ba692009-08-24 11:10:17 +03006891 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6892 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006893
6894 return 0;
6895}
6896
Avi Kivity851ba692009-08-24 11:10:17 +03006897static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08006898{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006899 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01006900 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6901 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08006902 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03006903 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006904
6905 return 1;
6906}
6907
Mohammed Gamal80ced182009-09-01 12:48:18 +02006908static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006909{
Avi Kivity8b3079a2009-01-05 12:10:54 +02006910 struct vcpu_vmx *vmx = to_vmx(vcpu);
6911 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006912 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02006913 u32 cpu_exec_ctrl;
6914 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03006915 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02006916
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07006917 /*
6918 * We should never reach the point where we are emulating L2
6919 * due to invalid guest state as that means we incorrectly
6920 * allowed a nested VMEntry with an invalid vmcs12.
6921 */
6922 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
6923
Avi Kivity49e9d552010-09-19 14:34:08 +02006924 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6925 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006926
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01006927 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03006928 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02006929 return handle_interrupt_window(&vmx->vcpu);
6930
Radim Krčmář72875d82017-04-26 22:32:19 +02006931 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03006932 return 1;
6933
Liran Alon9b8ae632017-11-05 16:56:34 +02006934 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006935
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02006936 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02006937 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006938 ret = 0;
6939 goto out;
6940 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01006941
Avi Kivityde5f70e2012-06-12 20:22:28 +03006942 if (err != EMULATE_DONE) {
6943 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6944 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6945 vcpu->run->internal.ndata = 0;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006946 return 0;
Avi Kivityde5f70e2012-06-12 20:22:28 +03006947 }
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006948
Gleb Natapov8d76c492013-05-08 18:38:44 +03006949 if (vcpu->arch.halt_request) {
6950 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006951 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03006952 goto out;
6953 }
6954
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006955 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02006956 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006957 if (need_resched())
6958 schedule();
6959 }
6960
Mohammed Gamal80ced182009-09-01 12:48:18 +02006961out:
6962 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006963}
6964
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006965static int __grow_ple_window(int val)
6966{
6967 if (ple_window_grow < 1)
6968 return ple_window;
6969
6970 val = min(val, ple_window_actual_max);
6971
6972 if (ple_window_grow < ple_window)
6973 val *= ple_window_grow;
6974 else
6975 val += ple_window_grow;
6976
6977 return val;
6978}
6979
6980static int __shrink_ple_window(int val, int modifier, int minimum)
6981{
6982 if (modifier < 1)
6983 return ple_window;
6984
6985 if (modifier < ple_window)
6986 val /= modifier;
6987 else
6988 val -= modifier;
6989
6990 return max(val, minimum);
6991}
6992
6993static void grow_ple_window(struct kvm_vcpu *vcpu)
6994{
6995 struct vcpu_vmx *vmx = to_vmx(vcpu);
6996 int old = vmx->ple_window;
6997
6998 vmx->ple_window = __grow_ple_window(old);
6999
7000 if (vmx->ple_window != old)
7001 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007002
7003 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007004}
7005
7006static void shrink_ple_window(struct kvm_vcpu *vcpu)
7007{
7008 struct vcpu_vmx *vmx = to_vmx(vcpu);
7009 int old = vmx->ple_window;
7010
7011 vmx->ple_window = __shrink_ple_window(old,
7012 ple_window_shrink, ple_window);
7013
7014 if (vmx->ple_window != old)
7015 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007016
7017 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007018}
7019
7020/*
7021 * ple_window_actual_max is computed to be one grow_ple_window() below
7022 * ple_window_max. (See __grow_ple_window for the reason.)
7023 * This prevents overflows, because ple_window_max is int.
7024 * ple_window_max effectively rounded down to a multiple of ple_window_grow in
7025 * this process.
7026 * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
7027 */
7028static void update_ple_window_actual_max(void)
7029{
7030 ple_window_actual_max =
7031 __shrink_ple_window(max(ple_window_max, ple_window),
7032 ple_window_grow, INT_MIN);
7033}
7034
Feng Wubf9f6ac2015-09-18 22:29:55 +08007035/*
7036 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7037 */
7038static void wakeup_handler(void)
7039{
7040 struct kvm_vcpu *vcpu;
7041 int cpu = smp_processor_id();
7042
7043 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7044 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7045 blocked_vcpu_list) {
7046 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7047
7048 if (pi_test_on(pi_desc) == 1)
7049 kvm_vcpu_kick(vcpu);
7050 }
7051 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7052}
7053
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007054void vmx_enable_tdp(void)
7055{
7056 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7057 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7058 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7059 0ull, VMX_EPT_EXECUTABLE_MASK,
7060 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007061 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007062
7063 ept_set_mmio_spte_mask();
7064 kvm_enable_tdp();
7065}
7066
Tiejun Chenf2c76482014-10-28 10:14:47 +08007067static __init int hardware_setup(void)
7068{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007069 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007070
7071 rdmsrl_safe(MSR_EFER, &host_efer);
7072
7073 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7074 kvm_define_shared_msr(i, vmx_msr_index[i]);
7075
Radim Krčmář23611332016-09-29 22:41:33 +02007076 for (i = 0; i < VMX_BITMAP_NR; i++) {
7077 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7078 if (!vmx_bitmap[i])
7079 goto out;
7080 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007081
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007082 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7083 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7084
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007085 if (setup_vmcs_config(&vmcs_config) < 0) {
7086 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007087 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007088 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007089
7090 if (boot_cpu_has(X86_FEATURE_NX))
7091 kvm_enable_efer_bits(EFER_NX);
7092
Wanpeng Li08d839c2017-03-23 05:30:08 -07007093 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7094 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007095 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007096
Tiejun Chenf2c76482014-10-28 10:14:47 +08007097 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007098 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007099 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007100 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007101 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007102
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007103 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007104 enable_ept_ad_bits = 0;
7105
Wanpeng Li8ad81822017-10-09 15:51:53 -07007106 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007107 enable_unrestricted_guest = 0;
7108
Paolo Bonziniad15a292015-01-30 16:18:49 +01007109 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007110 flexpriority_enabled = 0;
7111
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007112 if (!cpu_has_virtual_nmis())
7113 enable_vnmi = 0;
7114
Paolo Bonziniad15a292015-01-30 16:18:49 +01007115 /*
7116 * set_apic_access_page_addr() is used to reload apic access
7117 * page upon invalidation. No need to do anything if not
7118 * using the APIC_ACCESS_ADDR VMCS field.
7119 */
7120 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007121 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007122
7123 if (!cpu_has_vmx_tpr_shadow())
7124 kvm_x86_ops->update_cr8_intercept = NULL;
7125
7126 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7127 kvm_disable_largepages();
7128
Wanpeng Li0f107682017-09-28 18:06:24 -07007129 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007130 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007131 ple_window = 0;
7132 ple_window_grow = 0;
7133 ple_window_max = 0;
7134 ple_window_shrink = 0;
7135 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007136
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007137 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007138 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007139 kvm_x86_ops->sync_pir_to_irr = NULL;
7140 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007141
Haozhong Zhang64903d62015-10-20 15:39:09 +08007142 if (cpu_has_vmx_tsc_scaling()) {
7143 kvm_has_tsc_control = true;
7144 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7145 kvm_tsc_scaling_ratio_frac_bits = 48;
7146 }
7147
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007148 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7149
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007150 if (enable_ept)
7151 vmx_enable_tdp();
7152 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007153 kvm_disable_tdp();
7154
7155 update_ple_window_actual_max();
7156
Kai Huang843e4332015-01-28 10:54:28 +08007157 /*
7158 * Only enable PML when hardware supports PML feature, and both EPT
7159 * and EPT A/D bit features are enabled -- PML depends on them to work.
7160 */
7161 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7162 enable_pml = 0;
7163
7164 if (!enable_pml) {
7165 kvm_x86_ops->slot_enable_log_dirty = NULL;
7166 kvm_x86_ops->slot_disable_log_dirty = NULL;
7167 kvm_x86_ops->flush_log_dirty = NULL;
7168 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7169 }
7170
Yunhong Jiang64672c92016-06-13 14:19:59 -07007171 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7172 u64 vmx_msr;
7173
7174 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7175 cpu_preemption_timer_multi =
7176 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7177 } else {
7178 kvm_x86_ops->set_hv_timer = NULL;
7179 kvm_x86_ops->cancel_hv_timer = NULL;
7180 }
7181
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007182 if (!cpu_has_vmx_shadow_vmcs())
7183 enable_shadow_vmcs = 0;
7184 if (enable_shadow_vmcs)
7185 init_vmcs_shadow_fields();
7186
Feng Wubf9f6ac2015-09-18 22:29:55 +08007187 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007188 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007189
Ashok Rajc45dcc72016-06-22 14:59:56 +08007190 kvm_mce_cap_supported |= MCG_LMCE_P;
7191
Tiejun Chenf2c76482014-10-28 10:14:47 +08007192 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007193
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007194out:
Radim Krčmář23611332016-09-29 22:41:33 +02007195 for (i = 0; i < VMX_BITMAP_NR; i++)
7196 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007197
7198 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007199}
7200
7201static __exit void hardware_unsetup(void)
7202{
Radim Krčmář23611332016-09-29 22:41:33 +02007203 int i;
7204
7205 for (i = 0; i < VMX_BITMAP_NR; i++)
7206 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007207
Tiejun Chenf2c76482014-10-28 10:14:47 +08007208 free_kvm_area();
7209}
7210
Avi Kivity6aa8b732006-12-10 02:21:36 -08007211/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007212 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7213 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7214 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03007215static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007216{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007217 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007218 grow_ple_window(vcpu);
7219
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08007220 /*
7221 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7222 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7223 * never set PAUSE_EXITING and just set PLE if supported,
7224 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7225 */
7226 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007227 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007228}
7229
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007230static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08007231{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007232 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08007233}
7234
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007235static int handle_mwait(struct kvm_vcpu *vcpu)
7236{
7237 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7238 return handle_nop(vcpu);
7239}
7240
Jim Mattson45ec3682017-08-23 16:32:04 -07007241static int handle_invalid_op(struct kvm_vcpu *vcpu)
7242{
7243 kvm_queue_exception(vcpu, UD_VECTOR);
7244 return 1;
7245}
7246
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007247static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7248{
7249 return 1;
7250}
7251
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007252static int handle_monitor(struct kvm_vcpu *vcpu)
7253{
7254 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7255 return handle_nop(vcpu);
7256}
7257
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007258/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007259 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7260 * set the success or error code of an emulated VMX instruction, as specified
7261 * by Vol 2B, VMX Instruction Reference, "Conventions".
7262 */
7263static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7264{
7265 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7266 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7267 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7268}
7269
7270static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7271{
7272 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7273 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7274 X86_EFLAGS_SF | X86_EFLAGS_OF))
7275 | X86_EFLAGS_CF);
7276}
7277
Abel Gordon145c28d2013-04-18 14:36:55 +03007278static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007279 u32 vm_instruction_error)
7280{
7281 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7282 /*
7283 * failValid writes the error number to the current VMCS, which
7284 * can't be done there isn't a current VMCS.
7285 */
7286 nested_vmx_failInvalid(vcpu);
7287 return;
7288 }
7289 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7290 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7291 X86_EFLAGS_SF | X86_EFLAGS_OF))
7292 | X86_EFLAGS_ZF);
7293 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7294 /*
7295 * We don't need to force a shadow sync because
7296 * VM_INSTRUCTION_ERROR is not shadowed
7297 */
7298}
Abel Gordon145c28d2013-04-18 14:36:55 +03007299
Wincy Vanff651cb2014-12-11 08:52:58 +03007300static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7301{
7302 /* TODO: not to reset guest simply here. */
7303 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007304 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007305}
7306
Jan Kiszkaf41245002014-03-07 20:03:13 +01007307static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7308{
7309 struct vcpu_vmx *vmx =
7310 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7311
7312 vmx->nested.preemption_timer_expired = true;
7313 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7314 kvm_vcpu_kick(&vmx->vcpu);
7315
7316 return HRTIMER_NORESTART;
7317}
7318
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007319/*
Bandan Das19677e32014-05-06 02:19:15 -04007320 * Decode the memory-address operand of a vmx instruction, as recorded on an
7321 * exit caused by such an instruction (run by a guest hypervisor).
7322 * On success, returns 0. When the operand is invalid, returns 1 and throws
7323 * #UD or #GP.
7324 */
7325static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7326 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007327 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007328{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007329 gva_t off;
7330 bool exn;
7331 struct kvm_segment s;
7332
Bandan Das19677e32014-05-06 02:19:15 -04007333 /*
7334 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7335 * Execution", on an exit, vmx_instruction_info holds most of the
7336 * addressing components of the operand. Only the displacement part
7337 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7338 * For how an actual address is calculated from all these components,
7339 * refer to Vol. 1, "Operand Addressing".
7340 */
7341 int scaling = vmx_instruction_info & 3;
7342 int addr_size = (vmx_instruction_info >> 7) & 7;
7343 bool is_reg = vmx_instruction_info & (1u << 10);
7344 int seg_reg = (vmx_instruction_info >> 15) & 7;
7345 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7346 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7347 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7348 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7349
7350 if (is_reg) {
7351 kvm_queue_exception(vcpu, UD_VECTOR);
7352 return 1;
7353 }
7354
7355 /* Addr = segment_base + offset */
7356 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007357 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007358 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007359 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007360 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007361 off += kvm_register_read(vcpu, index_reg)<<scaling;
7362 vmx_get_segment(vcpu, &s, seg_reg);
7363 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007364
7365 if (addr_size == 1) /* 32 bit */
7366 *ret &= 0xffffffff;
7367
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007368 /* Checks for #GP/#SS exceptions. */
7369 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007370 if (is_long_mode(vcpu)) {
7371 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7372 * non-canonical form. This is the only check on the memory
7373 * destination for long mode!
7374 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007375 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007376 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007377 /* Protected mode: apply checks for segment validity in the
7378 * following order:
7379 * - segment type check (#GP(0) may be thrown)
7380 * - usability check (#GP(0)/#SS(0))
7381 * - limit check (#GP(0)/#SS(0))
7382 */
7383 if (wr)
7384 /* #GP(0) if the destination operand is located in a
7385 * read-only data segment or any code segment.
7386 */
7387 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7388 else
7389 /* #GP(0) if the source operand is located in an
7390 * execute-only code segment
7391 */
7392 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007393 if (exn) {
7394 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7395 return 1;
7396 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007397 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7398 */
7399 exn = (s.unusable != 0);
7400 /* Protected mode: #GP(0)/#SS(0) if the memory
7401 * operand is outside the segment limit.
7402 */
7403 exn = exn || (off + sizeof(u64) > s.limit);
7404 }
7405 if (exn) {
7406 kvm_queue_exception_e(vcpu,
7407 seg_reg == VCPU_SREG_SS ?
7408 SS_VECTOR : GP_VECTOR,
7409 0);
7410 return 1;
7411 }
7412
Bandan Das19677e32014-05-06 02:19:15 -04007413 return 0;
7414}
7415
Radim Krčmářcbf71272017-05-19 15:48:51 +02007416static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007417{
7418 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04007419 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04007420
7421 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007422 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007423 return 1;
7424
Radim Krčmářcbf71272017-05-19 15:48:51 +02007425 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, vmpointer,
7426 sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007427 kvm_inject_page_fault(vcpu, &e);
7428 return 1;
7429 }
7430
Bandan Das3573e222014-05-06 02:19:16 -04007431 return 0;
7432}
7433
Jim Mattsone29acc52016-11-30 12:03:43 -08007434static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7435{
7436 struct vcpu_vmx *vmx = to_vmx(vcpu);
7437 struct vmcs *shadow_vmcs;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007438 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08007439
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007440 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7441 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06007442 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08007443
7444 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7445 if (!vmx->nested.cached_vmcs12)
7446 goto out_cached_vmcs12;
7447
7448 if (enable_shadow_vmcs) {
7449 shadow_vmcs = alloc_vmcs();
7450 if (!shadow_vmcs)
7451 goto out_shadow_vmcs;
7452 /* mark vmcs as shadow */
7453 shadow_vmcs->revision_id |= (1u << 31);
7454 /* init shadow vmcs */
7455 vmcs_clear(shadow_vmcs);
7456 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7457 }
7458
Jim Mattsone29acc52016-11-30 12:03:43 -08007459 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7460 HRTIMER_MODE_REL_PINNED);
7461 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7462
7463 vmx->nested.vmxon = true;
7464 return 0;
7465
7466out_shadow_vmcs:
7467 kfree(vmx->nested.cached_vmcs12);
7468
7469out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06007470 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08007471
Jim Mattsonde3a0022017-11-27 17:22:25 -06007472out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08007473 return -ENOMEM;
7474}
7475
Bandan Das3573e222014-05-06 02:19:16 -04007476/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007477 * Emulate the VMXON instruction.
7478 * Currently, we just remember that VMX is active, and do not save or even
7479 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7480 * do not currently need to store anything in that guest-allocated memory
7481 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7482 * argument is different from the VMXON pointer (which the spec says they do).
7483 */
7484static int handle_vmon(struct kvm_vcpu *vcpu)
7485{
Jim Mattsone29acc52016-11-30 12:03:43 -08007486 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007487 gpa_t vmptr;
7488 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007489 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007490 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7491 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007492
Jim Mattson70f3aac2017-04-26 08:53:46 -07007493 /*
7494 * The Intel VMX Instruction Reference lists a bunch of bits that are
7495 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
7496 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
7497 * Otherwise, we should fail with #UD. But most faulting conditions
7498 * have already been checked by hardware, prior to the VM-exit for
7499 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
7500 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007501 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07007502 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007503 kvm_queue_exception(vcpu, UD_VECTOR);
7504 return 1;
7505 }
7506
Abel Gordon145c28d2013-04-18 14:36:55 +03007507 if (vmx->nested.vmxon) {
7508 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007509 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03007510 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007511
Haozhong Zhang3b840802016-06-22 14:59:54 +08007512 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007513 != VMXON_NEEDED_FEATURES) {
7514 kvm_inject_gp(vcpu, 0);
7515 return 1;
7516 }
7517
Radim Krčmářcbf71272017-05-19 15:48:51 +02007518 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08007519 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007520
7521 /*
7522 * SDM 3: 24.11.5
7523 * The first 4 bytes of VMXON region contain the supported
7524 * VMCS revision identifier
7525 *
7526 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
7527 * which replaces physical address width with 32
7528 */
7529 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7530 nested_vmx_failInvalid(vcpu);
7531 return kvm_skip_emulated_instruction(vcpu);
7532 }
7533
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02007534 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7535 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02007536 nested_vmx_failInvalid(vcpu);
7537 return kvm_skip_emulated_instruction(vcpu);
7538 }
7539 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
7540 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007541 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007542 nested_vmx_failInvalid(vcpu);
7543 return kvm_skip_emulated_instruction(vcpu);
7544 }
7545 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007546 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007547
7548 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08007549 ret = enter_vmx_operation(vcpu);
7550 if (ret)
7551 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007552
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007553 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007554 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007555}
7556
7557/*
7558 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7559 * for running VMX instructions (except VMXON, whose prerequisites are
7560 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07007561 * Note that many of these exceptions have priority over VM exits, so they
7562 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007563 */
7564static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7565{
Jim Mattson70f3aac2017-04-26 08:53:46 -07007566 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007567 kvm_queue_exception(vcpu, UD_VECTOR);
7568 return 0;
7569 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007570 return 1;
7571}
7572
David Matlack8ca44e82017-08-01 14:00:39 -07007573static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
7574{
7575 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
7576 vmcs_write64(VMCS_LINK_POINTER, -1ull);
7577}
7578
Abel Gordone7953d72013-04-18 14:37:55 +03007579static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7580{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007581 if (vmx->nested.current_vmptr == -1ull)
7582 return;
7583
Abel Gordon012f83c2013-04-18 14:39:25 +03007584 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007585 /* copy to memory all shadowed fields in case
7586 they were modified */
7587 copy_shadow_to_vmcs12(vmx);
7588 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07007589 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03007590 }
Wincy Van705699a2015-02-03 23:58:17 +08007591 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07007592
7593 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02007594 kvm_vcpu_write_guest_page(&vmx->vcpu,
7595 vmx->nested.current_vmptr >> PAGE_SHIFT,
7596 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07007597
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007598 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03007599}
7600
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007601/*
7602 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7603 * just stops using VMX.
7604 */
7605static void free_nested(struct vcpu_vmx *vmx)
7606{
Wanpeng Lib7455822017-11-22 14:04:00 -08007607 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007608 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007609
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007610 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08007611 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07007612 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07007613 vmx->nested.posted_intr_nv = -1;
7614 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007615 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07007616 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007617 vmcs_clear(vmx->vmcs01.shadow_vmcs);
7618 free_vmcs(vmx->vmcs01.shadow_vmcs);
7619 vmx->vmcs01.shadow_vmcs = NULL;
7620 }
David Matlack4f2777b2016-07-13 17:16:37 -07007621 kfree(vmx->nested.cached_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06007622 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007623 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02007624 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007625 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007626 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007627 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02007628 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007629 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007630 }
Wincy Van705699a2015-02-03 23:58:17 +08007631 if (vmx->nested.pi_desc_page) {
7632 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007633 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08007634 vmx->nested.pi_desc_page = NULL;
7635 vmx->nested.pi_desc = NULL;
7636 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007637
Jim Mattsonde3a0022017-11-27 17:22:25 -06007638 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007639}
7640
7641/* Emulate the VMXOFF instruction */
7642static int handle_vmoff(struct kvm_vcpu *vcpu)
7643{
7644 if (!nested_vmx_check_permission(vcpu))
7645 return 1;
7646 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007647 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007648 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007649}
7650
Nadav Har'El27d6c862011-05-25 23:06:59 +03007651/* Emulate the VMCLEAR instruction */
7652static int handle_vmclear(struct kvm_vcpu *vcpu)
7653{
7654 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08007655 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007656 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007657
7658 if (!nested_vmx_check_permission(vcpu))
7659 return 1;
7660
Radim Krčmářcbf71272017-05-19 15:48:51 +02007661 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03007662 return 1;
7663
Radim Krčmářcbf71272017-05-19 15:48:51 +02007664 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7665 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
7666 return kvm_skip_emulated_instruction(vcpu);
7667 }
7668
7669 if (vmptr == vmx->nested.vmxon_ptr) {
7670 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
7671 return kvm_skip_emulated_instruction(vcpu);
7672 }
7673
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007674 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03007675 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007676
Jim Mattson587d7e722017-03-02 12:41:48 -08007677 kvm_vcpu_write_guest(vcpu,
7678 vmptr + offsetof(struct vmcs12, launch_state),
7679 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03007680
Nadav Har'El27d6c862011-05-25 23:06:59 +03007681 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007682 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007683}
7684
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007685static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7686
7687/* Emulate the VMLAUNCH instruction */
7688static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7689{
7690 return nested_vmx_run(vcpu, true);
7691}
7692
7693/* Emulate the VMRESUME instruction */
7694static int handle_vmresume(struct kvm_vcpu *vcpu)
7695{
7696
7697 return nested_vmx_run(vcpu, false);
7698}
7699
Nadav Har'El49f705c2011-05-25 23:08:30 +03007700/*
7701 * Read a vmcs12 field. Since these can have varying lengths and we return
7702 * one type, we chose the biggest type (u64) and zero-extend the return value
7703 * to that size. Note that the caller, handle_vmread, might need to use only
7704 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7705 * 64-bit fields are to be returned).
7706 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007707static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7708 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03007709{
7710 short offset = vmcs_field_to_offset(field);
7711 char *p;
7712
7713 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007714 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007715
7716 p = ((char *)(get_vmcs12(vcpu))) + offset;
7717
Jim Mattsond37f4262017-12-22 12:12:16 -08007718 switch (vmcs_field_width(field)) {
7719 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007720 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007721 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007722 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007723 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007724 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007725 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007726 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007727 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007728 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007729 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007730 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007731 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007732 WARN_ON(1);
7733 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007734 }
7735}
7736
Abel Gordon20b97fe2013-04-18 14:36:25 +03007737
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007738static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7739 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03007740 short offset = vmcs_field_to_offset(field);
7741 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7742 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007743 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007744
Jim Mattsond37f4262017-12-22 12:12:16 -08007745 switch (vmcs_field_width(field)) {
7746 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007747 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007748 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007749 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007750 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007751 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007752 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007753 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007754 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007755 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007756 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007757 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007758 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007759 WARN_ON(1);
7760 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007761 }
7762
7763}
7764
Abel Gordon16f5b902013-04-18 14:38:25 +03007765static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7766{
7767 int i;
7768 unsigned long field;
7769 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007770 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007771 const u16 *fields = shadow_read_write_fields;
Mathias Krausec2bae892013-06-26 20:36:21 +02007772 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03007773
Jan Kiszka282da872014-10-08 18:05:39 +02007774 preempt_disable();
7775
Abel Gordon16f5b902013-04-18 14:38:25 +03007776 vmcs_load(shadow_vmcs);
7777
7778 for (i = 0; i < num_fields; i++) {
7779 field = fields[i];
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007780 field_value = __vmcs_readl(field);
Abel Gordon16f5b902013-04-18 14:38:25 +03007781 vmcs12_write_any(&vmx->vcpu, field, field_value);
7782 }
7783
7784 vmcs_clear(shadow_vmcs);
7785 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02007786
7787 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03007788}
7789
Abel Gordonc3114422013-04-18 14:38:55 +03007790static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7791{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007792 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02007793 shadow_read_write_fields,
7794 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03007795 };
Mathias Krausec2bae892013-06-26 20:36:21 +02007796 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03007797 max_shadow_read_write_fields,
7798 max_shadow_read_only_fields
7799 };
7800 int i, q;
7801 unsigned long field;
7802 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007803 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03007804
7805 vmcs_load(shadow_vmcs);
7806
Mathias Krausec2bae892013-06-26 20:36:21 +02007807 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03007808 for (i = 0; i < max_fields[q]; i++) {
7809 field = fields[q][i];
7810 vmcs12_read_any(&vmx->vcpu, field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007811 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03007812 }
7813 }
7814
7815 vmcs_clear(shadow_vmcs);
7816 vmcs_load(vmx->loaded_vmcs->vmcs);
7817}
7818
Nadav Har'El49f705c2011-05-25 23:08:30 +03007819/*
7820 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7821 * used before) all generate the same failure when it is missing.
7822 */
7823static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
7824{
7825 struct vcpu_vmx *vmx = to_vmx(vcpu);
7826 if (vmx->nested.current_vmptr == -1ull) {
7827 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007828 return 0;
7829 }
7830 return 1;
7831}
7832
7833static int handle_vmread(struct kvm_vcpu *vcpu)
7834{
7835 unsigned long field;
7836 u64 field_value;
7837 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7838 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7839 gva_t gva = 0;
7840
Kyle Hueyeb277562016-11-29 12:40:39 -08007841 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007842 return 1;
7843
Kyle Huey6affcbe2016-11-29 12:40:40 -08007844 if (!nested_vmx_check_vmcs12(vcpu))
7845 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08007846
Nadav Har'El49f705c2011-05-25 23:08:30 +03007847 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03007848 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007849 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007850 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007851 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007852 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007853 }
7854 /*
7855 * Now copy part of this value to register or memory, as requested.
7856 * Note that the number of bits actually copied is 32 or 64 depending
7857 * on the guest's mode (32 or 64 bit), not on the given field's length.
7858 */
7859 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03007860 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03007861 field_value);
7862 } else {
7863 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007864 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007865 return 1;
Jim Mattson70f3aac2017-04-26 08:53:46 -07007866 /* _system ok, as hardware has verified cpl=0 */
Nadav Har'El49f705c2011-05-25 23:08:30 +03007867 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
7868 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
7869 }
7870
7871 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007872 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007873}
7874
7875
7876static int handle_vmwrite(struct kvm_vcpu *vcpu)
7877{
7878 unsigned long field;
7879 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01007880 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007881 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7882 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01007883
Nadav Har'El49f705c2011-05-25 23:08:30 +03007884 /* The value to write might be 32 or 64 bits, depending on L1's long
7885 * mode, and eventually we need to write that into a field of several
7886 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08007887 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03007888 * bits into the vmcs12 field.
7889 */
7890 u64 field_value = 0;
7891 struct x86_exception e;
7892
Kyle Hueyeb277562016-11-29 12:40:39 -08007893 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007894 return 1;
7895
Kyle Huey6affcbe2016-11-29 12:40:40 -08007896 if (!nested_vmx_check_vmcs12(vcpu))
7897 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08007898
Nadav Har'El49f705c2011-05-25 23:08:30 +03007899 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03007900 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007901 (((vmx_instruction_info) >> 3) & 0xf));
7902 else {
7903 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007904 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007905 return 1;
7906 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
Nadav Amit27e6fb52014-06-18 17:19:26 +03007907 &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007908 kvm_inject_page_fault(vcpu, &e);
7909 return 1;
7910 }
7911 }
7912
7913
Nadav Amit27e6fb52014-06-18 17:19:26 +03007914 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007915 if (vmcs_field_readonly(field)) {
7916 nested_vmx_failValid(vcpu,
7917 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007918 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007919 }
7920
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007921 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007922 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007923 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007924 }
7925
Paolo Bonzini74a497f2017-12-20 13:55:39 +01007926 switch (field) {
7927#define SHADOW_FIELD_RW(x) case x:
7928#include "vmx_shadow_fields.h"
7929 /*
7930 * The fields that can be updated by L1 without a vmexit are
7931 * always updated in the vmcs02, the others go down the slow
7932 * path of prepare_vmcs02.
7933 */
7934 break;
7935 default:
7936 vmx->nested.dirty_vmcs12 = true;
7937 break;
7938 }
7939
Nadav Har'El49f705c2011-05-25 23:08:30 +03007940 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007941 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007942}
7943
Jim Mattsona8bc2842016-11-30 12:03:44 -08007944static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
7945{
7946 vmx->nested.current_vmptr = vmptr;
7947 if (enable_shadow_vmcs) {
7948 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
7949 SECONDARY_EXEC_SHADOW_VMCS);
7950 vmcs_write64(VMCS_LINK_POINTER,
7951 __pa(vmx->vmcs01.shadow_vmcs));
7952 vmx->nested.sync_shadow_vmcs = true;
7953 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01007954 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08007955}
7956
Nadav Har'El63846662011-05-25 23:07:29 +03007957/* Emulate the VMPTRLD instruction */
7958static int handle_vmptrld(struct kvm_vcpu *vcpu)
7959{
7960 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007961 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03007962
7963 if (!nested_vmx_check_permission(vcpu))
7964 return 1;
7965
Radim Krčmářcbf71272017-05-19 15:48:51 +02007966 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03007967 return 1;
7968
Radim Krčmářcbf71272017-05-19 15:48:51 +02007969 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7970 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
7971 return kvm_skip_emulated_instruction(vcpu);
7972 }
7973
7974 if (vmptr == vmx->nested.vmxon_ptr) {
7975 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
7976 return kvm_skip_emulated_instruction(vcpu);
7977 }
7978
Nadav Har'El63846662011-05-25 23:07:29 +03007979 if (vmx->nested.current_vmptr != vmptr) {
7980 struct vmcs12 *new_vmcs12;
7981 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02007982 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7983 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03007984 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007985 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007986 }
7987 new_vmcs12 = kmap(page);
7988 if (new_vmcs12->revision_id != VMCS12_REVISION) {
7989 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007990 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03007991 nested_vmx_failValid(vcpu,
7992 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007993 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007994 }
Nadav Har'El63846662011-05-25 23:07:29 +03007995
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007996 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07007997 /*
7998 * Load VMCS12 from guest memory since it is not already
7999 * cached.
8000 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008001 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8002 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008003 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008004
Jim Mattsona8bc2842016-11-30 12:03:44 -08008005 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008006 }
8007
8008 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008009 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008010}
8011
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008012/* Emulate the VMPTRST instruction */
8013static int handle_vmptrst(struct kvm_vcpu *vcpu)
8014{
8015 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8016 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8017 gva_t vmcs_gva;
8018 struct x86_exception e;
8019
8020 if (!nested_vmx_check_permission(vcpu))
8021 return 1;
8022
8023 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008024 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008025 return 1;
Jim Mattson70f3aac2017-04-26 08:53:46 -07008026 /* ok to use *_system, as hardware has verified cpl=0 */
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008027 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
8028 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8029 sizeof(u64), &e)) {
8030 kvm_inject_page_fault(vcpu, &e);
8031 return 1;
8032 }
8033 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008034 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008035}
8036
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008037/* Emulate the INVEPT instruction */
8038static int handle_invept(struct kvm_vcpu *vcpu)
8039{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008040 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008041 u32 vmx_instruction_info, types;
8042 unsigned long type;
8043 gva_t gva;
8044 struct x86_exception e;
8045 struct {
8046 u64 eptp, gpa;
8047 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008048
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008049 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008050 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008051 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008052 kvm_queue_exception(vcpu, UD_VECTOR);
8053 return 1;
8054 }
8055
8056 if (!nested_vmx_check_permission(vcpu))
8057 return 1;
8058
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008059 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008060 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008061
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008062 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008063
Jim Mattson85c856b2016-10-26 08:38:38 -07008064 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008065 nested_vmx_failValid(vcpu,
8066 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008067 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008068 }
8069
8070 /* According to the Intel VMX instruction reference, the memory
8071 * operand is read even if it isn't needed (e.g., for type==global)
8072 */
8073 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008074 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008075 return 1;
8076 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
8077 sizeof(operand), &e)) {
8078 kvm_inject_page_fault(vcpu, &e);
8079 return 1;
8080 }
8081
8082 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008083 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008084 /*
8085 * TODO: track mappings and invalidate
8086 * single context requests appropriately
8087 */
8088 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008089 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008090 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008091 nested_vmx_succeed(vcpu);
8092 break;
8093 default:
8094 BUG_ON(1);
8095 break;
8096 }
8097
Kyle Huey6affcbe2016-11-29 12:40:40 -08008098 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008099}
8100
Petr Matouseka642fc32014-09-23 20:22:30 +02008101static int handle_invvpid(struct kvm_vcpu *vcpu)
8102{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008103 struct vcpu_vmx *vmx = to_vmx(vcpu);
8104 u32 vmx_instruction_info;
8105 unsigned long type, types;
8106 gva_t gva;
8107 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008108 struct {
8109 u64 vpid;
8110 u64 gla;
8111 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008112
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008113 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008114 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008115 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008116 kvm_queue_exception(vcpu, UD_VECTOR);
8117 return 1;
8118 }
8119
8120 if (!nested_vmx_check_permission(vcpu))
8121 return 1;
8122
8123 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8124 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8125
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008126 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008127 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008128
Jim Mattson85c856b2016-10-26 08:38:38 -07008129 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008130 nested_vmx_failValid(vcpu,
8131 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008132 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008133 }
8134
8135 /* according to the intel vmx instruction reference, the memory
8136 * operand is read even if it isn't needed (e.g., for type==global)
8137 */
8138 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8139 vmx_instruction_info, false, &gva))
8140 return 1;
Jim Mattson40352602017-06-28 09:37:37 -07008141 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
8142 sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008143 kvm_inject_page_fault(vcpu, &e);
8144 return 1;
8145 }
Jim Mattson40352602017-06-28 09:37:37 -07008146 if (operand.vpid >> 16) {
8147 nested_vmx_failValid(vcpu,
8148 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8149 return kvm_skip_emulated_instruction(vcpu);
8150 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008151
8152 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008153 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Yu Zhangfd8cb432017-08-24 20:27:56 +08008154 if (is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07008155 nested_vmx_failValid(vcpu,
8156 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8157 return kvm_skip_emulated_instruction(vcpu);
8158 }
8159 /* fall through */
Paolo Bonzinief697a72016-03-18 16:58:38 +01008160 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008161 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07008162 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008163 nested_vmx_failValid(vcpu,
8164 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008165 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008166 }
8167 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008168 case VMX_VPID_EXTENT_ALL_CONTEXT:
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008169 break;
8170 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008171 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008172 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008173 }
8174
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08008175 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008176 nested_vmx_succeed(vcpu);
8177
Kyle Huey6affcbe2016-11-29 12:40:40 -08008178 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02008179}
8180
Kai Huang843e4332015-01-28 10:54:28 +08008181static int handle_pml_full(struct kvm_vcpu *vcpu)
8182{
8183 unsigned long exit_qualification;
8184
8185 trace_kvm_pml_full(vcpu->vcpu_id);
8186
8187 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8188
8189 /*
8190 * PML buffer FULL happened while executing iret from NMI,
8191 * "blocked by NMI" bit has to be set before next VM entry.
8192 */
8193 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008194 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08008195 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8196 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8197 GUEST_INTR_STATE_NMI);
8198
8199 /*
8200 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8201 * here.., and there's no userspace involvement needed for PML.
8202 */
8203 return 1;
8204}
8205
Yunhong Jiang64672c92016-06-13 14:19:59 -07008206static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8207{
8208 kvm_lapic_expired_hv_timer(vcpu);
8209 return 1;
8210}
8211
Bandan Das41ab9372017-08-03 15:54:43 -04008212static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8213{
8214 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04008215 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8216
8217 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008218 switch (address & VMX_EPTP_MT_MASK) {
8219 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008220 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008221 return false;
8222 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02008223 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008224 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008225 return false;
8226 break;
8227 default:
8228 return false;
8229 }
8230
David Hildenbrandbb97a012017-08-10 23:15:28 +02008231 /* only 4 levels page-walk length are valid */
8232 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04008233 return false;
8234
8235 /* Reserved bits should not be set */
8236 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8237 return false;
8238
8239 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008240 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008241 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008242 return false;
8243 }
8244
8245 return true;
8246}
8247
8248static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8249 struct vmcs12 *vmcs12)
8250{
8251 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8252 u64 address;
8253 bool accessed_dirty;
8254 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8255
8256 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8257 !nested_cpu_has_ept(vmcs12))
8258 return 1;
8259
8260 if (index >= VMFUNC_EPTP_ENTRIES)
8261 return 1;
8262
8263
8264 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8265 &address, index * 8, 8))
8266 return 1;
8267
David Hildenbrandbb97a012017-08-10 23:15:28 +02008268 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04008269
8270 /*
8271 * If the (L2) guest does a vmfunc to the currently
8272 * active ept pointer, we don't have to do anything else
8273 */
8274 if (vmcs12->ept_pointer != address) {
8275 if (!valid_ept_address(vcpu, address))
8276 return 1;
8277
8278 kvm_mmu_unload(vcpu);
8279 mmu->ept_ad = accessed_dirty;
8280 mmu->base_role.ad_disabled = !accessed_dirty;
8281 vmcs12->ept_pointer = address;
8282 /*
8283 * TODO: Check what's the correct approach in case
8284 * mmu reload fails. Currently, we just let the next
8285 * reload potentially fail
8286 */
8287 kvm_mmu_reload(vcpu);
8288 }
8289
8290 return 0;
8291}
8292
Bandan Das2a499e42017-08-03 15:54:41 -04008293static int handle_vmfunc(struct kvm_vcpu *vcpu)
8294{
Bandan Das27c42a12017-08-03 15:54:42 -04008295 struct vcpu_vmx *vmx = to_vmx(vcpu);
8296 struct vmcs12 *vmcs12;
8297 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8298
8299 /*
8300 * VMFUNC is only supported for nested guests, but we always enable the
8301 * secondary control for simplicity; for non-nested mode, fake that we
8302 * didn't by injecting #UD.
8303 */
8304 if (!is_guest_mode(vcpu)) {
8305 kvm_queue_exception(vcpu, UD_VECTOR);
8306 return 1;
8307 }
8308
8309 vmcs12 = get_vmcs12(vcpu);
8310 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8311 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04008312
8313 switch (function) {
8314 case 0:
8315 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8316 goto fail;
8317 break;
8318 default:
8319 goto fail;
8320 }
8321 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04008322
8323fail:
8324 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8325 vmcs_read32(VM_EXIT_INTR_INFO),
8326 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04008327 return 1;
8328}
8329
Nadav Har'El0140cae2011-05-25 23:06:28 +03008330/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08008331 * The exit handlers return 1 if the exit was handled fully and guest execution
8332 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8333 * to be done to userspace and return 0.
8334 */
Mathias Krause772e0312012-08-30 01:30:19 +02008335static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08008336 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8337 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08008338 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08008339 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008340 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008341 [EXIT_REASON_CR_ACCESS] = handle_cr,
8342 [EXIT_REASON_DR_ACCESS] = handle_dr,
8343 [EXIT_REASON_CPUID] = handle_cpuid,
8344 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8345 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8346 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8347 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02008348 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03008349 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02008350 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02008351 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03008352 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008353 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03008354 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008355 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008356 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008357 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008358 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008359 [EXIT_REASON_VMOFF] = handle_vmoff,
8360 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08008361 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8362 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08008363 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008364 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02008365 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08008366 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02008367 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08008368 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02008369 [EXIT_REASON_GDTR_IDTR] = handle_desc,
8370 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03008371 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8372 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008373 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008374 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008375 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008376 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008377 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02008378 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07008379 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07008380 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08008381 [EXIT_REASON_XSAVES] = handle_xsaves,
8382 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08008383 [EXIT_REASON_PML_FULL] = handle_pml_full,
Bandan Das2a499e42017-08-03 15:54:41 -04008384 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07008385 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008386};
8387
8388static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04008389 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008390
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008391static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8392 struct vmcs12 *vmcs12)
8393{
8394 unsigned long exit_qualification;
8395 gpa_t bitmap, last_bitmap;
8396 unsigned int port;
8397 int size;
8398 u8 b;
8399
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008400 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05008401 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008402
8403 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8404
8405 port = exit_qualification >> 16;
8406 size = (exit_qualification & 7) + 1;
8407
8408 last_bitmap = (gpa_t)-1;
8409 b = -1;
8410
8411 while (size > 0) {
8412 if (port < 0x8000)
8413 bitmap = vmcs12->io_bitmap_a;
8414 else if (port < 0x10000)
8415 bitmap = vmcs12->io_bitmap_b;
8416 else
Joe Perches1d804d02015-03-30 16:46:09 -07008417 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008418 bitmap += (port & 0x7fff) / 8;
8419
8420 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008421 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008422 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008423 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07008424 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008425
8426 port++;
8427 size--;
8428 last_bitmap = bitmap;
8429 }
8430
Joe Perches1d804d02015-03-30 16:46:09 -07008431 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008432}
8433
Nadav Har'El644d7112011-05-25 23:12:35 +03008434/*
8435 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8436 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8437 * disinterest in the current event (read or write a specific MSR) by using an
8438 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8439 */
8440static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8441 struct vmcs12 *vmcs12, u32 exit_reason)
8442{
8443 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8444 gpa_t bitmap;
8445
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01008446 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07008447 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008448
8449 /*
8450 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8451 * for the four combinations of read/write and low/high MSR numbers.
8452 * First we need to figure out which of the four to use:
8453 */
8454 bitmap = vmcs12->msr_bitmap;
8455 if (exit_reason == EXIT_REASON_MSR_WRITE)
8456 bitmap += 2048;
8457 if (msr_index >= 0xc0000000) {
8458 msr_index -= 0xc0000000;
8459 bitmap += 1024;
8460 }
8461
8462 /* Then read the msr_index'th bit from this bitmap: */
8463 if (msr_index < 1024*8) {
8464 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008465 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008466 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008467 return 1 & (b >> (msr_index & 7));
8468 } else
Joe Perches1d804d02015-03-30 16:46:09 -07008469 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03008470}
8471
8472/*
8473 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8474 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8475 * intercept (via guest_host_mask etc.) the current event.
8476 */
8477static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8478 struct vmcs12 *vmcs12)
8479{
8480 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8481 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008482 int reg;
8483 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03008484
8485 switch ((exit_qualification >> 4) & 3) {
8486 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008487 reg = (exit_qualification >> 8) & 15;
8488 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03008489 switch (cr) {
8490 case 0:
8491 if (vmcs12->cr0_guest_host_mask &
8492 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008493 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008494 break;
8495 case 3:
8496 if ((vmcs12->cr3_target_count >= 1 &&
8497 vmcs12->cr3_target_value0 == val) ||
8498 (vmcs12->cr3_target_count >= 2 &&
8499 vmcs12->cr3_target_value1 == val) ||
8500 (vmcs12->cr3_target_count >= 3 &&
8501 vmcs12->cr3_target_value2 == val) ||
8502 (vmcs12->cr3_target_count >= 4 &&
8503 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07008504 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008505 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008506 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008507 break;
8508 case 4:
8509 if (vmcs12->cr4_guest_host_mask &
8510 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07008511 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008512 break;
8513 case 8:
8514 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008515 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008516 break;
8517 }
8518 break;
8519 case 2: /* clts */
8520 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
8521 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008522 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008523 break;
8524 case 1: /* mov from cr */
8525 switch (cr) {
8526 case 3:
8527 if (vmcs12->cpu_based_vm_exec_control &
8528 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008529 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008530 break;
8531 case 8:
8532 if (vmcs12->cpu_based_vm_exec_control &
8533 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008534 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008535 break;
8536 }
8537 break;
8538 case 3: /* lmsw */
8539 /*
8540 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
8541 * cr0. Other attempted changes are ignored, with no exit.
8542 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008543 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03008544 if (vmcs12->cr0_guest_host_mask & 0xe &
8545 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008546 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008547 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
8548 !(vmcs12->cr0_read_shadow & 0x1) &&
8549 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07008550 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008551 break;
8552 }
Joe Perches1d804d02015-03-30 16:46:09 -07008553 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008554}
8555
8556/*
8557 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8558 * should handle it ourselves in L0 (and then continue L2). Only call this
8559 * when in is_guest_mode (L2).
8560 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02008561static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03008562{
Nadav Har'El644d7112011-05-25 23:12:35 +03008563 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8564 struct vcpu_vmx *vmx = to_vmx(vcpu);
8565 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8566
Jim Mattson4f350c62017-09-14 16:31:44 -07008567 if (vmx->nested.nested_run_pending)
8568 return false;
8569
8570 if (unlikely(vmx->fail)) {
8571 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8572 vmcs_read32(VM_INSTRUCTION_ERROR));
8573 return true;
8574 }
Jan Kiszka542060e2014-01-04 18:47:21 +01008575
David Matlackc9f04402017-08-01 14:00:40 -07008576 /*
8577 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06008578 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
8579 * Page). The CPU may write to these pages via their host
8580 * physical address while L2 is running, bypassing any
8581 * address-translation-based dirty tracking (e.g. EPT write
8582 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07008583 *
8584 * Mark them dirty on every exit from L2 to prevent them from
8585 * getting out of sync with dirty tracking.
8586 */
8587 nested_mark_vmcs12_pages_dirty(vcpu);
8588
Jim Mattson4f350c62017-09-14 16:31:44 -07008589 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
8590 vmcs_readl(EXIT_QUALIFICATION),
8591 vmx->idt_vectoring_info,
8592 intr_info,
8593 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8594 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03008595
8596 switch (exit_reason) {
8597 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08008598 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07008599 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008600 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07008601 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01008602 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01008603 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008604 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01008605 else if (is_debug(intr_info) &&
8606 vcpu->guest_debug &
8607 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8608 return false;
8609 else if (is_breakpoint(intr_info) &&
8610 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8611 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008612 return vmcs12->exception_bitmap &
8613 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8614 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07008615 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008616 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07008617 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008618 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008619 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008620 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008621 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008622 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07008623 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008624 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07008625 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008626 case EXIT_REASON_HLT:
8627 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8628 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07008629 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008630 case EXIT_REASON_INVLPG:
8631 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8632 case EXIT_REASON_RDPMC:
8633 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02008634 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02008635 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02008636 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02008637 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01008638 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03008639 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8640 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8641 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8642 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8643 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8644 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02008645 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03008646 /*
8647 * VMX instructions trap unconditionally. This allows L1 to
8648 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8649 */
Joe Perches1d804d02015-03-30 16:46:09 -07008650 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008651 case EXIT_REASON_CR_ACCESS:
8652 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8653 case EXIT_REASON_DR_ACCESS:
8654 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8655 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008656 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02008657 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
8658 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03008659 case EXIT_REASON_MSR_READ:
8660 case EXIT_REASON_MSR_WRITE:
8661 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8662 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07008663 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008664 case EXIT_REASON_MWAIT_INSTRUCTION:
8665 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008666 case EXIT_REASON_MONITOR_TRAP_FLAG:
8667 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03008668 case EXIT_REASON_MONITOR_INSTRUCTION:
8669 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8670 case EXIT_REASON_PAUSE_INSTRUCTION:
8671 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8672 nested_cpu_has2(vmcs12,
8673 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8674 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07008675 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008676 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008677 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03008678 case EXIT_REASON_APIC_ACCESS:
8679 return nested_cpu_has2(vmcs12,
8680 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Wincy Van82f0dd42015-02-03 23:57:18 +08008681 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08008682 case EXIT_REASON_EOI_INDUCED:
8683 /* apic_write and eoi_induced should exit unconditionally. */
Joe Perches1d804d02015-03-30 16:46:09 -07008684 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008685 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008686 /*
8687 * L0 always deals with the EPT violation. If nested EPT is
8688 * used, and the nested mmu code discovers that the address is
8689 * missing in the guest EPT table (EPT12), the EPT violation
8690 * will be injected with nested_ept_inject_page_fault()
8691 */
Joe Perches1d804d02015-03-30 16:46:09 -07008692 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008693 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008694 /*
8695 * L2 never uses directly L1's EPT, but rather L0's own EPT
8696 * table (shadow on EPT) or a merged EPT table that L0 built
8697 * (EPT on EPT). So any problems with the structure of the
8698 * table is L0's fault.
8699 */
Joe Perches1d804d02015-03-30 16:46:09 -07008700 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02008701 case EXIT_REASON_INVPCID:
8702 return
8703 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
8704 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008705 case EXIT_REASON_WBINVD:
8706 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8707 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07008708 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08008709 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8710 /*
8711 * This should never happen, since it is not possible to
8712 * set XSS to a non-zero value---neither in L1 nor in L2.
8713 * If if it were, XSS would have to be checked against
8714 * the XSS exit bitmap in vmcs12.
8715 */
8716 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08008717 case EXIT_REASON_PREEMPTION_TIMER:
8718 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02008719 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04008720 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02008721 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04008722 case EXIT_REASON_VMFUNC:
8723 /* VM functions are emulated through L2->L0 vmexits. */
8724 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008725 default:
Joe Perches1d804d02015-03-30 16:46:09 -07008726 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008727 }
8728}
8729
Paolo Bonzini7313c692017-07-27 10:31:25 +02008730static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
8731{
8732 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8733
8734 /*
8735 * At this point, the exit interruption info in exit_intr_info
8736 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
8737 * we need to query the in-kernel LAPIC.
8738 */
8739 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
8740 if ((exit_intr_info &
8741 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
8742 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
8743 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8744 vmcs12->vm_exit_intr_error_code =
8745 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
8746 }
8747
8748 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
8749 vmcs_readl(EXIT_QUALIFICATION));
8750 return 1;
8751}
8752
Avi Kivity586f9602010-11-18 13:09:54 +02008753static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8754{
8755 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8756 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8757}
8758
Kai Huanga3eaa862015-11-04 13:46:05 +08008759static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08008760{
Kai Huanga3eaa862015-11-04 13:46:05 +08008761 if (vmx->pml_pg) {
8762 __free_page(vmx->pml_pg);
8763 vmx->pml_pg = NULL;
8764 }
Kai Huang843e4332015-01-28 10:54:28 +08008765}
8766
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008767static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08008768{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008769 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008770 u64 *pml_buf;
8771 u16 pml_idx;
8772
8773 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8774
8775 /* Do nothing if PML buffer is empty */
8776 if (pml_idx == (PML_ENTITY_NUM - 1))
8777 return;
8778
8779 /* PML index always points to next available PML buffer entity */
8780 if (pml_idx >= PML_ENTITY_NUM)
8781 pml_idx = 0;
8782 else
8783 pml_idx++;
8784
8785 pml_buf = page_address(vmx->pml_pg);
8786 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8787 u64 gpa;
8788
8789 gpa = pml_buf[pml_idx];
8790 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008791 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08008792 }
8793
8794 /* reset PML index */
8795 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8796}
8797
8798/*
8799 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8800 * Called before reporting dirty_bitmap to userspace.
8801 */
8802static void kvm_flush_pml_buffers(struct kvm *kvm)
8803{
8804 int i;
8805 struct kvm_vcpu *vcpu;
8806 /*
8807 * We only need to kick vcpu out of guest mode here, as PML buffer
8808 * is flushed at beginning of all VMEXITs, and it's obvious that only
8809 * vcpus running in guest are possible to have unflushed GPAs in PML
8810 * buffer.
8811 */
8812 kvm_for_each_vcpu(i, vcpu, kvm)
8813 kvm_vcpu_kick(vcpu);
8814}
8815
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008816static void vmx_dump_sel(char *name, uint32_t sel)
8817{
8818 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05008819 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008820 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8821 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
8822 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
8823}
8824
8825static void vmx_dump_dtsel(char *name, uint32_t limit)
8826{
8827 pr_err("%s limit=0x%08x, base=0x%016lx\n",
8828 name, vmcs_read32(limit),
8829 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
8830}
8831
8832static void dump_vmcs(void)
8833{
8834 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
8835 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
8836 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
8837 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
8838 u32 secondary_exec_control = 0;
8839 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01008840 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008841 int i, n;
8842
8843 if (cpu_has_secondary_exec_ctrls())
8844 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8845
8846 pr_err("*** Guest State ***\n");
8847 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8848 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
8849 vmcs_readl(CR0_GUEST_HOST_MASK));
8850 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8851 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
8852 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
8853 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
8854 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
8855 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008856 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
8857 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
8858 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
8859 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008860 }
8861 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
8862 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
8863 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
8864 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
8865 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8866 vmcs_readl(GUEST_SYSENTER_ESP),
8867 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
8868 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
8869 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
8870 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
8871 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
8872 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
8873 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
8874 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
8875 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
8876 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
8877 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
8878 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
8879 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008880 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8881 efer, vmcs_read64(GUEST_IA32_PAT));
8882 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
8883 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008884 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
8885 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008886 pr_err("PerfGlobCtl = 0x%016llx\n",
8887 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008888 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008889 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008890 pr_err("Interruptibility = %08x ActivityState = %08x\n",
8891 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
8892 vmcs_read32(GUEST_ACTIVITY_STATE));
8893 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
8894 pr_err("InterruptStatus = %04x\n",
8895 vmcs_read16(GUEST_INTR_STATUS));
8896
8897 pr_err("*** Host State ***\n");
8898 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
8899 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
8900 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
8901 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
8902 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
8903 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
8904 vmcs_read16(HOST_TR_SELECTOR));
8905 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
8906 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
8907 vmcs_readl(HOST_TR_BASE));
8908 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
8909 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
8910 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
8911 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
8912 vmcs_readl(HOST_CR4));
8913 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8914 vmcs_readl(HOST_IA32_SYSENTER_ESP),
8915 vmcs_read32(HOST_IA32_SYSENTER_CS),
8916 vmcs_readl(HOST_IA32_SYSENTER_EIP));
8917 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008918 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8919 vmcs_read64(HOST_IA32_EFER),
8920 vmcs_read64(HOST_IA32_PAT));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008921 if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008922 pr_err("PerfGlobCtl = 0x%016llx\n",
8923 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008924
8925 pr_err("*** Control State ***\n");
8926 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
8927 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
8928 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
8929 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
8930 vmcs_read32(EXCEPTION_BITMAP),
8931 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
8932 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
8933 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
8934 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8935 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
8936 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
8937 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
8938 vmcs_read32(VM_EXIT_INTR_INFO),
8939 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8940 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
8941 pr_err(" reason=%08x qualification=%016lx\n",
8942 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
8943 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
8944 vmcs_read32(IDT_VECTORING_INFO_FIELD),
8945 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008946 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08008947 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008948 pr_err("TSC Multiplier = 0x%016llx\n",
8949 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008950 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
8951 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
8952 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
8953 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
8954 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008955 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008956 n = vmcs_read32(CR3_TARGET_COUNT);
8957 for (i = 0; i + 1 < n; i += 4)
8958 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
8959 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
8960 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
8961 if (i < n)
8962 pr_err("CR3 target%u=%016lx\n",
8963 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
8964 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
8965 pr_err("PLE Gap=%08x Window=%08x\n",
8966 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
8967 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
8968 pr_err("Virtual processor ID = 0x%04x\n",
8969 vmcs_read16(VIRTUAL_PROCESSOR_ID));
8970}
8971
Avi Kivity6aa8b732006-12-10 02:21:36 -08008972/*
8973 * The guest has exited. See if we can fix it or if we need userspace
8974 * assistance.
8975 */
Avi Kivity851ba692009-08-24 11:10:17 +03008976static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008977{
Avi Kivity29bd8a72007-09-10 17:27:03 +03008978 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08008979 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02008980 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03008981
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008982 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
8983
Kai Huang843e4332015-01-28 10:54:28 +08008984 /*
8985 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
8986 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
8987 * querying dirty_bitmap, we only need to kick all vcpus out of guest
8988 * mode as if vcpus is in root mode, the PML buffer must has been
8989 * flushed already.
8990 */
8991 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008992 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008993
Mohammed Gamal80ced182009-09-01 12:48:18 +02008994 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02008995 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02008996 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01008997
Paolo Bonzini7313c692017-07-27 10:31:25 +02008998 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
8999 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03009000
Mohammed Gamal51207022010-05-31 22:40:54 +03009001 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009002 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03009003 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9004 vcpu->run->fail_entry.hardware_entry_failure_reason
9005 = exit_reason;
9006 return 0;
9007 }
9008
Avi Kivity29bd8a72007-09-10 17:27:03 +03009009 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009010 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9011 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03009012 = vmcs_read32(VM_INSTRUCTION_ERROR);
9013 return 0;
9014 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009015
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009016 /*
9017 * Note:
9018 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9019 * delivery event since it indicates guest is accessing MMIO.
9020 * The vm-exit can be triggered again after return to guest that
9021 * will cause infinite loop.
9022 */
Mike Dayd77c26f2007-10-08 09:02:08 -04009023 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08009024 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02009025 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00009026 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009027 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9028 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9029 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009030 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009031 vcpu->run->internal.data[0] = vectoring_info;
9032 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009033 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9034 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9035 vcpu->run->internal.ndata++;
9036 vcpu->run->internal.data[3] =
9037 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9038 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009039 return 0;
9040 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009041
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009042 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009043 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9044 if (vmx_interrupt_allowed(vcpu)) {
9045 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9046 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9047 vcpu->arch.nmi_pending) {
9048 /*
9049 * This CPU don't support us in finding the end of an
9050 * NMI-blocked window if the guest runs with IRQs
9051 * disabled. So we pull the trigger after 1 s of
9052 * futile waiting, but inform the user about this.
9053 */
9054 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9055 "state on VCPU %d after 1 s timeout\n",
9056 __func__, vcpu->vcpu_id);
9057 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9058 }
9059 }
9060
Avi Kivity6aa8b732006-12-10 02:21:36 -08009061 if (exit_reason < kvm_vmx_max_exit_handlers
9062 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03009063 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009064 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01009065 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9066 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03009067 kvm_queue_exception(vcpu, UD_VECTOR);
9068 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009069 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009070}
9071
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009072static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009073{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009074 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9075
9076 if (is_guest_mode(vcpu) &&
9077 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9078 return;
9079
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009080 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009081 vmcs_write32(TPR_THRESHOLD, 0);
9082 return;
9083 }
9084
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009085 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009086}
9087
Yang Zhang8d146952013-01-25 10:18:50 +08009088static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
9089{
9090 u32 sec_exec_control;
9091
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009092 /* Postpone execution until vmcs01 is the current VMCS. */
9093 if (is_guest_mode(vcpu)) {
9094 to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
9095 return;
9096 }
9097
Wanpeng Lif6e90f92016-09-22 07:43:25 +08009098 if (!cpu_has_vmx_virtualize_x2apic_mode())
Yang Zhang8d146952013-01-25 10:18:50 +08009099 return;
9100
Paolo Bonzini35754c92015-07-29 12:05:37 +02009101 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08009102 return;
9103
9104 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9105
9106 if (set) {
9107 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9108 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9109 } else {
9110 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9111 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009112 vmx_flush_tlb_ept_only(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009113 }
9114 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9115
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009116 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009117}
9118
Tang Chen38b99172014-09-24 15:57:54 +08009119static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9120{
9121 struct vcpu_vmx *vmx = to_vmx(vcpu);
9122
9123 /*
9124 * Currently we do not handle the nested case where L2 has an
9125 * APIC access page of its own; that page is still pinned.
9126 * Hence, we skip the case where the VCPU is in guest mode _and_
9127 * L1 prepared an APIC access page for L2.
9128 *
9129 * For the case where L1 and L2 share the same APIC access page
9130 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
9131 * in the vmcs12), this function will only update either the vmcs01
9132 * or the vmcs02. If the former, the vmcs02 will be updated by
9133 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
9134 * the next L2->L1 exit.
9135 */
9136 if (!is_guest_mode(vcpu) ||
David Matlack4f2777b2016-07-13 17:16:37 -07009137 !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009138 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Tang Chen38b99172014-09-24 15:57:54 +08009139 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009140 vmx_flush_tlb_ept_only(vcpu);
9141 }
Tang Chen38b99172014-09-24 15:57:54 +08009142}
9143
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009144static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009145{
9146 u16 status;
9147 u8 old;
9148
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009149 if (max_isr == -1)
9150 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009151
9152 status = vmcs_read16(GUEST_INTR_STATUS);
9153 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009154 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08009155 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009156 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009157 vmcs_write16(GUEST_INTR_STATUS, status);
9158 }
9159}
9160
9161static void vmx_set_rvi(int vector)
9162{
9163 u16 status;
9164 u8 old;
9165
Wei Wang4114c272014-11-05 10:53:43 +08009166 if (vector == -1)
9167 vector = 0;
9168
Yang Zhangc7c9c562013-01-25 10:18:51 +08009169 status = vmcs_read16(GUEST_INTR_STATUS);
9170 old = (u8)status & 0xff;
9171 if ((u8)vector != old) {
9172 status &= ~0xff;
9173 status |= (u8)vector;
9174 vmcs_write16(GUEST_INTR_STATUS, status);
9175 }
9176}
9177
9178static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9179{
Liran Alon851c1a182017-12-24 18:12:56 +02009180 /*
9181 * When running L2, updating RVI is only relevant when
9182 * vmcs12 virtual-interrupt-delivery enabled.
9183 * However, it can be enabled only when L1 also
9184 * intercepts external-interrupts and in that case
9185 * we should not update vmcs02 RVI but instead intercept
9186 * interrupt. Therefore, do nothing when running L2.
9187 */
9188 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08009189 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009190}
9191
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009192static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009193{
9194 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009195 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02009196 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009197
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009198 WARN_ON(!vcpu->arch.apicv_active);
9199 if (pi_test_on(&vmx->pi_desc)) {
9200 pi_clear_on(&vmx->pi_desc);
9201 /*
9202 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9203 * But on x86 this is just a compiler barrier anyway.
9204 */
9205 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02009206 max_irr_updated =
9207 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
9208
9209 /*
9210 * If we are running L2 and L1 has a new pending interrupt
9211 * which can be injected, we should re-evaluate
9212 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02009213 * If L1 intercepts external-interrupts, we should
9214 * exit from L2 to L1. Otherwise, interrupt should be
9215 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02009216 */
Liran Alon851c1a182017-12-24 18:12:56 +02009217 if (is_guest_mode(vcpu) && max_irr_updated) {
9218 if (nested_exit_on_intr(vcpu))
9219 kvm_vcpu_exiting_guest_mode(vcpu);
9220 else
9221 kvm_make_request(KVM_REQ_EVENT, vcpu);
9222 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009223 } else {
9224 max_irr = kvm_lapic_find_highest_irr(vcpu);
9225 }
9226 vmx_hwapic_irr_update(vcpu, max_irr);
9227 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009228}
9229
Andrey Smetanin63086302015-11-10 15:36:32 +03009230static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009231{
Andrey Smetanind62caab2015-11-10 15:36:33 +03009232 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08009233 return;
9234
Yang Zhangc7c9c562013-01-25 10:18:51 +08009235 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9236 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9237 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9238 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9239}
9240
Paolo Bonzini967235d2016-12-19 14:03:45 +01009241static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9242{
9243 struct vcpu_vmx *vmx = to_vmx(vcpu);
9244
9245 pi_clear_on(&vmx->pi_desc);
9246 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9247}
9248
Avi Kivity51aa01d2010-07-20 14:31:20 +03009249static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03009250{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009251 u32 exit_intr_info = 0;
9252 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02009253
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009254 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9255 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02009256 return;
9257
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009258 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9259 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9260 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08009261
Wanpeng Li1261bfa2017-07-13 18:30:40 -07009262 /* if exit due to PF check for async PF */
9263 if (is_page_fault(exit_intr_info))
9264 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9265
Andi Kleena0861c02009-06-08 17:37:09 +08009266 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009267 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9268 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08009269 kvm_machine_check();
9270
Gleb Natapov20f65982009-05-11 13:35:55 +03009271 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08009272 if (is_nmi(exit_intr_info)) {
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009273 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03009274 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009275 kvm_after_handle_nmi(&vmx->vcpu);
9276 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03009277}
Gleb Natapov20f65982009-05-11 13:35:55 +03009278
Yang Zhanga547c6d2013-04-11 19:25:10 +08009279static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9280{
9281 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9282
Yang Zhanga547c6d2013-04-11 19:25:10 +08009283 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9284 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9285 unsigned int vector;
9286 unsigned long entry;
9287 gate_desc *desc;
9288 struct vcpu_vmx *vmx = to_vmx(vcpu);
9289#ifdef CONFIG_X86_64
9290 unsigned long tmp;
9291#endif
9292
9293 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9294 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02009295 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009296 asm volatile(
9297#ifdef CONFIG_X86_64
9298 "mov %%" _ASM_SP ", %[sp]\n\t"
9299 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
9300 "push $%c[ss]\n\t"
9301 "push %[sp]\n\t"
9302#endif
9303 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08009304 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009305 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08009306 :
9307#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06009308 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009309#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -05009310 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +08009311 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009312 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009313 [ss]"i"(__KERNEL_DS),
9314 [cs]"i"(__KERNEL_CS)
9315 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02009316 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08009317}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009318STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009319
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009320static bool vmx_has_high_real_mode_segbase(void)
9321{
9322 return enable_unrestricted_guest || emulate_invalid_guest_state;
9323}
9324
Liu, Jinsongda8999d2014-02-24 10:55:46 +00009325static bool vmx_mpx_supported(void)
9326{
9327 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
9328 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
9329}
9330
Wanpeng Li55412b22014-12-02 19:21:30 +08009331static bool vmx_xsaves_supported(void)
9332{
9333 return vmcs_config.cpu_based_2nd_exec_ctrl &
9334 SECONDARY_EXEC_XSAVES;
9335}
9336
Paolo Bonzini66336ca2016-07-12 10:36:41 +02009337static bool vmx_umip_emulated(void)
9338{
Paolo Bonzini0367f202016-07-12 10:44:55 +02009339 return vmcs_config.cpu_based_2nd_exec_ctrl &
9340 SECONDARY_EXEC_DESC;
Paolo Bonzini66336ca2016-07-12 10:36:41 +02009341}
9342
Avi Kivity51aa01d2010-07-20 14:31:20 +03009343static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9344{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02009345 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03009346 bool unblock_nmi;
9347 u8 vector;
9348 bool idtv_info_valid;
9349
9350 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03009351
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009352 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009353 if (vmx->loaded_vmcs->nmi_known_unmasked)
9354 return;
9355 /*
9356 * Can't use vmx->exit_intr_info since we're not sure what
9357 * the exit reason is.
9358 */
9359 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9360 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9361 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9362 /*
9363 * SDM 3: 27.7.1.2 (September 2008)
9364 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9365 * a guest IRET fault.
9366 * SDM 3: 23.2.2 (September 2008)
9367 * Bit 12 is undefined in any of the following cases:
9368 * If the VM exit sets the valid bit in the IDT-vectoring
9369 * information field.
9370 * If the VM exit is due to a double fault.
9371 */
9372 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9373 vector != DF_VECTOR && !idtv_info_valid)
9374 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9375 GUEST_INTR_STATE_NMI);
9376 else
9377 vmx->loaded_vmcs->nmi_known_unmasked =
9378 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9379 & GUEST_INTR_STATE_NMI);
9380 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
9381 vmx->loaded_vmcs->vnmi_blocked_time +=
9382 ktime_to_ns(ktime_sub(ktime_get(),
9383 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03009384}
9385
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009386static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03009387 u32 idt_vectoring_info,
9388 int instr_len_field,
9389 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03009390{
Avi Kivity51aa01d2010-07-20 14:31:20 +03009391 u8 vector;
9392 int type;
9393 bool idtv_info_valid;
9394
9395 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03009396
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009397 vcpu->arch.nmi_injected = false;
9398 kvm_clear_exception_queue(vcpu);
9399 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009400
9401 if (!idtv_info_valid)
9402 return;
9403
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009404 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03009405
Avi Kivity668f6122008-07-02 09:28:55 +03009406 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
9407 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009408
Gleb Natapov64a7ec02009-03-30 16:03:29 +03009409 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03009410 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009411 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03009412 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009413 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03009414 * Clear bit "block by NMI" before VM entry if a NMI
9415 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03009416 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009417 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009418 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009419 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009420 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009421 /* fall through */
9422 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03009423 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03009424 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03009425 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03009426 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03009427 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009428 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009429 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009430 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009431 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03009432 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009433 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009434 break;
9435 default:
9436 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03009437 }
Avi Kivitycf393f72008-07-01 16:20:21 +03009438}
9439
Avi Kivity83422e12010-07-20 14:43:23 +03009440static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
9441{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009442 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03009443 VM_EXIT_INSTRUCTION_LEN,
9444 IDT_VECTORING_ERROR_CODE);
9445}
9446
Avi Kivityb463a6f2010-07-20 15:06:17 +03009447static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
9448{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009449 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03009450 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9451 VM_ENTRY_INSTRUCTION_LEN,
9452 VM_ENTRY_EXCEPTION_ERROR_CODE);
9453
9454 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
9455}
9456
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009457static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
9458{
9459 int i, nr_msrs;
9460 struct perf_guest_switch_msr *msrs;
9461
9462 msrs = perf_guest_get_msrs(&nr_msrs);
9463
9464 if (!msrs)
9465 return;
9466
9467 for (i = 0; i < nr_msrs; i++)
9468 if (msrs[i].host == msrs[i].guest)
9469 clear_atomic_switch_msr(vmx, msrs[i].msr);
9470 else
9471 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
9472 msrs[i].host);
9473}
9474
Jiang Biao33365e72016-11-03 15:03:37 +08009475static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07009476{
9477 struct vcpu_vmx *vmx = to_vmx(vcpu);
9478 u64 tscl;
9479 u32 delta_tsc;
9480
9481 if (vmx->hv_deadline_tsc == -1)
9482 return;
9483
9484 tscl = rdtsc();
9485 if (vmx->hv_deadline_tsc > tscl)
9486 /* sure to be 32 bit only because checked on set_hv_timer */
9487 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
9488 cpu_preemption_timer_multi);
9489 else
9490 delta_tsc = 0;
9491
9492 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
9493}
9494
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08009495static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009496{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009497 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Li74c55932017-11-29 01:31:20 -08009498 unsigned long cr3, cr4;
Avi Kivity104f2262010-11-18 13:12:52 +02009499
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009500 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009501 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009502 vmx->loaded_vmcs->soft_vnmi_blocked))
9503 vmx->loaded_vmcs->entry_time = ktime_get();
9504
Avi Kivity104f2262010-11-18 13:12:52 +02009505 /* Don't enter VMX if guest state is invalid, let the exit handler
9506 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02009507 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02009508 return;
9509
Radim Krčmářa7653ec2014-08-21 18:08:07 +02009510 if (vmx->ple_window_dirty) {
9511 vmx->ple_window_dirty = false;
9512 vmcs_write32(PLE_WINDOW, vmx->ple_window);
9513 }
9514
Abel Gordon012f83c2013-04-18 14:39:25 +03009515 if (vmx->nested.sync_shadow_vmcs) {
9516 copy_vmcs12_to_shadow(vmx);
9517 vmx->nested.sync_shadow_vmcs = false;
9518 }
9519
Avi Kivity104f2262010-11-18 13:12:52 +02009520 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
9521 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
9522 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
9523 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
9524
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009525 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +02009526 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009527 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +02009528 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009529 }
9530
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07009531 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +02009532 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009533 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +02009534 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009535 }
9536
Avi Kivity104f2262010-11-18 13:12:52 +02009537 /* When single-stepping over STI and MOV SS, we must clear the
9538 * corresponding interruptibility bits in the guest state. Otherwise
9539 * vmentry fails as it then expects bit 14 (BS) in pending debug
9540 * exceptions being set, but that's not correct for the guest debugging
9541 * case. */
9542 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9543 vmx_set_interrupt_shadow(vcpu, 0);
9544
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02009545 if (static_cpu_has(X86_FEATURE_PKU) &&
9546 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
9547 vcpu->arch.pkru != vmx->host_pkru)
9548 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009549
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009550 atomic_switch_perf_msrs(vmx);
9551
Yunhong Jiang64672c92016-06-13 14:19:59 -07009552 vmx_arm_hv_timer(vcpu);
9553
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009554 /*
9555 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
9556 * it's non-zero. Since vmentry is serialising on affected CPUs, there
9557 * is no need to worry about the conditional branch over the wrmsr
9558 * being speculatively taken.
9559 */
9560 if (vmx->spec_ctrl)
Paolo Bonziniecb586b2018-02-22 16:43:17 +01009561 native_wrmsrl(MSR_IA32_SPEC_CTRL, vmx->spec_ctrl);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009562
Nadav Har'Eld462b812011-05-24 15:26:10 +03009563 vmx->__launched = vmx->loaded_vmcs->launched;
Avi Kivity104f2262010-11-18 13:12:52 +02009564 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08009565 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009566 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
9567 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
9568 "push %%" _ASM_CX " \n\t"
9569 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03009570 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009571 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009572 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03009573 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009574 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009575 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
9576 "mov %%cr2, %%" _ASM_DX " \n\t"
9577 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009578 "je 2f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009579 "mov %%" _ASM_AX", %%cr2 \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009580 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009581 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02009582 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009583 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009584 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
9585 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
9586 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
9587 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
9588 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
9589 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009590#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009591 "mov %c[r8](%0), %%r8 \n\t"
9592 "mov %c[r9](%0), %%r9 \n\t"
9593 "mov %c[r10](%0), %%r10 \n\t"
9594 "mov %c[r11](%0), %%r11 \n\t"
9595 "mov %c[r12](%0), %%r12 \n\t"
9596 "mov %c[r13](%0), %%r13 \n\t"
9597 "mov %c[r14](%0), %%r14 \n\t"
9598 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009599#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009600 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03009601
Avi Kivity6aa8b732006-12-10 02:21:36 -08009602 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03009603 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009604 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009605 "jmp 2f \n\t"
9606 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
9607 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08009608 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009609 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02009610 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -08009611 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009612 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
9613 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
9614 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
9615 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
9616 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
9617 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
9618 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009619#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009620 "mov %%r8, %c[r8](%0) \n\t"
9621 "mov %%r9, %c[r9](%0) \n\t"
9622 "mov %%r10, %c[r10](%0) \n\t"
9623 "mov %%r11, %c[r11](%0) \n\t"
9624 "mov %%r12, %c[r12](%0) \n\t"
9625 "mov %%r13, %c[r13](%0) \n\t"
9626 "mov %%r14, %c[r14](%0) \n\t"
9627 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -08009628 "xor %%r8d, %%r8d \n\t"
9629 "xor %%r9d, %%r9d \n\t"
9630 "xor %%r10d, %%r10d \n\t"
9631 "xor %%r11d, %%r11d \n\t"
9632 "xor %%r12d, %%r12d \n\t"
9633 "xor %%r13d, %%r13d \n\t"
9634 "xor %%r14d, %%r14d \n\t"
9635 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009636#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009637 "mov %%cr2, %%" _ASM_AX " \n\t"
9638 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03009639
Jim Mattson0cb5b302018-01-03 14:31:38 -08009640 "xor %%eax, %%eax \n\t"
9641 "xor %%ebx, %%ebx \n\t"
9642 "xor %%esi, %%esi \n\t"
9643 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009644 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009645 ".pushsection .rodata \n\t"
9646 ".global vmx_return \n\t"
9647 "vmx_return: " _ASM_PTR " 2b \n\t"
9648 ".popsection"
Avi Kivitye08aa782007-11-15 18:06:18 +02009649 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03009650 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02009651 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03009652 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009653 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
9654 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
9655 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
9656 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
9657 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
9658 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
9659 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009660#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009661 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
9662 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
9663 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
9664 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
9665 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
9666 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
9667 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
9668 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08009669#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02009670 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
9671 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02009672 : "cc", "memory"
9673#ifdef CONFIG_X86_64
Avi Kivityb188c81f2012-09-16 15:10:58 +03009674 , "rax", "rbx", "rdi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009675 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009676#else
9677 , "eax", "ebx", "edi", "esi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009678#endif
9679 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08009680
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009681 /*
9682 * We do not use IBRS in the kernel. If this vCPU has used the
9683 * SPEC_CTRL MSR it may have left it on; save the value and
9684 * turn it off. This is much more efficient than blindly adding
9685 * it to the atomic save/restore list. Especially as the former
9686 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
9687 *
9688 * For non-nested case:
9689 * If the L01 MSR bitmap does not intercept the MSR, then we need to
9690 * save it.
9691 *
9692 * For nested case:
9693 * If the L02 MSR bitmap does not intercept the MSR, then we need to
9694 * save it.
9695 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +01009696 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +01009697 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009698
9699 if (vmx->spec_ctrl)
Paolo Bonziniecb586b2018-02-22 16:43:17 +01009700 native_wrmsrl(MSR_IA32_SPEC_CTRL, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009701
David Woodhouse117cc7a2018-01-12 11:11:27 +00009702 /* Eliminate branch target predictions from guest mode */
9703 vmexit_fill_RSB();
9704
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009705 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -08009706 if (vmx->host_debugctlmsr)
9707 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009708
Avi Kivityaa67f602012-08-01 16:48:03 +03009709#ifndef CONFIG_X86_64
9710 /*
9711 * The sysexit path does not restore ds/es, so we must set them to
9712 * a reasonable value ourselves.
9713 *
9714 * We can't defer this to vmx_load_host_state() since that function
9715 * may be executed in interrupt context, which saves and restore segments
9716 * around it, nullifying its effect.
9717 */
9718 loadsegment(ds, __USER_DS);
9719 loadsegment(es, __USER_DS);
9720#endif
9721
Avi Kivity6de4f3a2009-05-31 22:58:47 +03009722 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02009723 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009724 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03009725 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009726 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009727 vcpu->arch.regs_dirty = 0;
9728
Gleb Natapove0b890d2013-09-25 12:51:33 +03009729 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009730 * eager fpu is enabled if PKEY is supported and CR4 is switched
9731 * back on host, so it is safe to read guest PKRU from current
9732 * XSAVE.
9733 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02009734 if (static_cpu_has(X86_FEATURE_PKU) &&
9735 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
9736 vcpu->arch.pkru = __read_pkru();
9737 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009738 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009739 }
9740
9741 /*
Gleb Natapove0b890d2013-09-25 12:51:33 +03009742 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
9743 * we did not inject a still-pending event to L1 now because of
9744 * nested_run_pending, we need to re-enable this bit.
9745 */
9746 if (vmx->nested.nested_run_pending)
9747 kvm_make_request(KVM_REQ_EVENT, vcpu);
9748
9749 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -07009750 vmx->idt_vectoring_info = 0;
9751
9752 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
9753 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9754 return;
9755
9756 vmx->loaded_vmcs->launched = 1;
9757 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +03009758
Avi Kivity51aa01d2010-07-20 14:31:20 +03009759 vmx_complete_atomic_exit(vmx);
9760 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03009761 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009762}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009763STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009764
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009765static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009766{
9767 struct vcpu_vmx *vmx = to_vmx(vcpu);
9768 int cpu;
9769
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009770 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009771 return;
9772
9773 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009774 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009775 vmx_vcpu_put(vcpu);
9776 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009777 put_cpu();
9778}
9779
Jim Mattson2f1fe812016-07-08 15:36:06 -07009780/*
9781 * Ensure that the current vmcs of the logical processor is the
9782 * vmcs01 of the vcpu before calling free_nested().
9783 */
9784static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9785{
9786 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009787
Christoffer Dallec7660c2017-12-04 21:35:23 +01009788 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009789 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009790 free_nested(vmx);
9791 vcpu_put(vcpu);
9792}
9793
Avi Kivity6aa8b732006-12-10 02:21:36 -08009794static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9795{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009796 struct vcpu_vmx *vmx = to_vmx(vcpu);
9797
Kai Huang843e4332015-01-28 10:54:28 +08009798 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08009799 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08009800 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009801 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009802 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009803 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009804 kfree(vmx->guest_msrs);
9805 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10009806 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009807}
9808
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009809static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009810{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009811 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10009812 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009813 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +03009814 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009815
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009816 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009817 return ERR_PTR(-ENOMEM);
9818
Wanpeng Li991e7a02015-09-16 17:30:05 +08009819 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +08009820
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009821 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
9822 if (err)
9823 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009824
Peter Feiner4e595162016-07-07 14:49:58 -07009825 err = -ENOMEM;
9826
9827 /*
9828 * If PML is turned on, failure on enabling PML just results in failure
9829 * of creating the vcpu, therefore we can simplify PML logic (by
9830 * avoiding dealing with cases, such as enabling PML partially on vcpus
9831 * for the guest, etc.
9832 */
9833 if (enable_pml) {
9834 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
9835 if (!vmx->pml_pg)
9836 goto uninit_vcpu;
9837 }
9838
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009839 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +02009840 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
9841 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +03009842
Peter Feiner4e595162016-07-07 14:49:58 -07009843 if (!vmx->guest_msrs)
9844 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009845
Paolo Bonzinif21f1652018-01-11 12:16:15 +01009846 err = alloc_loaded_vmcs(&vmx->vmcs01);
9847 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009848 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009849
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009850 msr_bitmap = vmx->vmcs01.msr_bitmap;
9851 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
9852 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
9853 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
9854 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
9855 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
9856 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
9857 vmx->msr_bitmap_mode = 0;
9858
Paolo Bonzinif21f1652018-01-11 12:16:15 +01009859 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +03009860 cpu = get_cpu();
9861 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10009862 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +02009863 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009864 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03009865 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +02009866 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02009867 err = alloc_apic_access_page(kvm);
9868 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02009869 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02009870 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08009871
Sean Christophersone90008d2018-03-05 12:04:37 -08009872 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +08009873 err = init_rmode_identity_map(kvm);
9874 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02009875 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08009876 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08009877
Wanpeng Li5c614b32015-10-13 09:18:36 -07009878 if (nested) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009879 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
9880 kvm_vcpu_apicv_active(&vmx->vcpu));
Wanpeng Li5c614b32015-10-13 09:18:36 -07009881 vmx->nested.vpid02 = allocate_vpid();
9882 }
Wincy Vanb9c237b2015-02-03 23:56:30 +08009883
Wincy Van705699a2015-02-03 23:58:17 +08009884 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009885 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009886
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009887 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
9888
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02009889 /*
9890 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
9891 * or POSTED_INTR_WAKEUP_VECTOR.
9892 */
9893 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
9894 vmx->pi_desc.sn = 1;
9895
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009896 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009897
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009898free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -07009899 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08009900 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009901free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009902 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -07009903free_pml:
9904 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009905uninit_vcpu:
9906 kvm_vcpu_uninit(&vmx->vcpu);
9907free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +08009908 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +10009909 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009910 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009911}
9912
Wanpeng Lib31c1142018-03-12 04:53:04 -07009913static int vmx_vm_init(struct kvm *kvm)
9914{
9915 if (!ple_gap)
9916 kvm->arch.pause_in_guest = true;
9917 return 0;
9918}
9919
Yang, Sheng002c7f72007-07-31 14:23:01 +03009920static void __init vmx_check_processor_compat(void *rtn)
9921{
9922 struct vmcs_config vmcs_conf;
9923
9924 *(int *)rtn = 0;
9925 if (setup_vmcs_config(&vmcs_conf) < 0)
9926 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +01009927 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +03009928 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
9929 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
9930 smp_processor_id());
9931 *(int *)rtn = -EIO;
9932 }
9933}
9934
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009935static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08009936{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009937 u8 cache;
9938 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009939
Sheng Yang522c68c2009-04-27 20:35:43 +08009940 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +02009941 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +08009942 * 2. EPT with VT-d:
9943 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +02009944 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +08009945 * b. VT-d with snooping control feature: snooping control feature of
9946 * VT-d engine can guarantee the cache correctness. Just set it
9947 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08009948 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08009949 * consistent with host MTRR
9950 */
Paolo Bonzini606decd2015-10-01 13:12:47 +02009951 if (is_mmio) {
9952 cache = MTRR_TYPE_UNCACHABLE;
9953 goto exit;
9954 }
9955
9956 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009957 ipat = VMX_EPT_IPAT_BIT;
9958 cache = MTRR_TYPE_WRBACK;
9959 goto exit;
9960 }
9961
9962 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
9963 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02009964 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08009965 cache = MTRR_TYPE_WRBACK;
9966 else
9967 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009968 goto exit;
9969 }
9970
Xiao Guangrongff536042015-06-15 16:55:22 +08009971 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009972
9973exit:
9974 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08009975}
9976
Sheng Yang17cc3932010-01-05 19:02:27 +08009977static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02009978{
Sheng Yang878403b2010-01-05 19:02:29 +08009979 if (enable_ept && !cpu_has_vmx_ept_1g_page())
9980 return PT_DIRECTORY_LEVEL;
9981 else
9982 /* For shadow and EPT supported 1GB page */
9983 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02009984}
9985
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009986static void vmcs_set_secondary_exec_control(u32 new_ctl)
9987{
9988 /*
9989 * These bits in the secondary execution controls field
9990 * are dynamic, the others are mostly based on the hypervisor
9991 * architecture and the guest's CPUID. Do not touch the
9992 * dynamic bits.
9993 */
9994 u32 mask =
9995 SECONDARY_EXEC_SHADOW_VMCS |
9996 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +02009997 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9998 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009999
10000 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10001
10002 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10003 (new_ctl & ~mask) | (cur_ctl & mask));
10004}
10005
David Matlack8322ebb2016-11-29 18:14:09 -080010006/*
10007 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10008 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10009 */
10010static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10011{
10012 struct vcpu_vmx *vmx = to_vmx(vcpu);
10013 struct kvm_cpuid_entry2 *entry;
10014
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010015 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
10016 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080010017
10018#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10019 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010020 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080010021} while (0)
10022
10023 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10024 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10025 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10026 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10027 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10028 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10029 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10030 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10031 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10032 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10033 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10034 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10035 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10036 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10037 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10038
10039 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10040 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10041 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10042 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10043 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010010044 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080010045
10046#undef cr4_fixed1_update
10047}
10048
Sheng Yang0e851882009-12-18 16:48:46 +080010049static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10050{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010051 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010052
Paolo Bonzini80154d72017-08-24 13:55:35 +020010053 if (cpu_has_secondary_exec_ctrls()) {
10054 vmx_compute_secondary_exec_control(vmx);
10055 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010056 }
Mao, Junjiead756a12012-07-02 01:18:48 +000010057
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010058 if (nested_vmx_allowed(vcpu))
10059 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10060 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10061 else
10062 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10063 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080010064
10065 if (nested_vmx_allowed(vcpu))
10066 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +080010067}
10068
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010069static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10070{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030010071 if (func == 1 && nested)
10072 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010073}
10074
Yang Zhang25d92082013-08-06 12:00:32 +030010075static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10076 struct x86_exception *fault)
10077{
Jan Kiszka533558b2014-01-04 18:47:20 +010010078 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040010079 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010080 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010081 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030010082
Bandan Dasc5f983f2017-05-05 15:25:14 -040010083 if (vmx->nested.pml_full) {
10084 exit_reason = EXIT_REASON_PML_FULL;
10085 vmx->nested.pml_full = false;
10086 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10087 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010010088 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030010089 else
Jan Kiszka533558b2014-01-04 18:47:20 +010010090 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010091
10092 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030010093 vmcs12->guest_physical_address = fault->address;
10094}
10095
Peter Feiner995f00a2017-06-30 17:26:32 -070010096static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10097{
David Hildenbrandbb97a012017-08-10 23:15:28 +020010098 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070010099}
10100
Nadav Har'El155a97a2013-08-05 11:07:16 +030010101/* Callbacks for nested_ept_init_mmu_context: */
10102
10103static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10104{
10105 /* return the page table to be shadowed - in our case, EPT12 */
10106 return get_vmcs12(vcpu)->ept_pointer;
10107}
10108
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010109static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030010110{
Paolo Bonziniad896af2013-10-02 16:56:14 +020010111 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020010112 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010113 return 1;
10114
10115 kvm_mmu_unload(vcpu);
Paolo Bonziniad896af2013-10-02 16:56:14 +020010116 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010117 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010118 VMX_EPT_EXECUTE_ONLY_BIT,
David Hildenbranda057e0e2017-08-10 23:36:54 +020010119 nested_ept_ad_enabled(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030010120 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10121 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10122 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10123
10124 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010125 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030010126}
10127
10128static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10129{
10130 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10131}
10132
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030010133static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10134 u16 error_code)
10135{
10136 bool inequality, bit;
10137
10138 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10139 inequality =
10140 (error_code & vmcs12->page_fault_error_code_mask) !=
10141 vmcs12->page_fault_error_code_match;
10142 return inequality ^ bit;
10143}
10144
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010145static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10146 struct x86_exception *fault)
10147{
10148 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10149
10150 WARN_ON(!is_guest_mode(vcpu));
10151
Wanpeng Li305d0ab2017-09-28 18:16:44 -070010152 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10153 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020010154 vmcs12->vm_exit_intr_error_code = fault->error_code;
10155 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10156 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10157 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10158 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010159 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010160 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010161 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010162}
10163
Paolo Bonzinic9923842017-12-13 14:16:30 +010010164static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10165 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010166
10167static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010168 struct vmcs12 *vmcs12)
10169{
10170 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010171 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010172 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010173
10174 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010175 /*
10176 * Translate L1 physical address to host physical
10177 * address for vmcs02. Keep the page pinned, so this
10178 * physical address remains valid. We keep a reference
10179 * to it so we can release it later.
10180 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010181 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010182 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010183 vmx->nested.apic_access_page = NULL;
10184 }
10185 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010186 /*
10187 * If translation failed, no matter: This feature asks
10188 * to exit when accessing the given address, and if it
10189 * can never be accessed, this feature won't do
10190 * anything anyway.
10191 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010192 if (!is_error_page(page)) {
10193 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010194 hpa = page_to_phys(vmx->nested.apic_access_page);
10195 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10196 } else {
10197 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
10198 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10199 }
10200 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
10201 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
10202 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
10203 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10204 kvm_vcpu_reload_apic_access_page(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010205 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010206
10207 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010208 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010209 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010210 vmx->nested.virtual_apic_page = NULL;
10211 }
10212 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010213
10214 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010215 * If translation failed, VM entry will fail because
10216 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10217 * Failing the vm entry is _not_ what the processor
10218 * does but it's basically the only possibility we
10219 * have. We could still enter the guest if CR8 load
10220 * exits are enabled, CR8 store exits are enabled, and
10221 * virtualize APIC access is disabled; in this case
10222 * the processor would never use the TPR shadow and we
10223 * could simply clear the bit from the execution
10224 * control. But such a configuration is useless, so
10225 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010226 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010227 if (!is_error_page(page)) {
10228 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010229 hpa = page_to_phys(vmx->nested.virtual_apic_page);
10230 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
10231 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010232 }
10233
Wincy Van705699a2015-02-03 23:58:17 +080010234 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010235 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
10236 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010237 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010238 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080010239 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010240 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
10241 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010242 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010243 vmx->nested.pi_desc_page = page;
10244 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080010245 vmx->nested.pi_desc =
10246 (struct pi_desc *)((void *)vmx->nested.pi_desc +
10247 (unsigned long)(vmcs12->posted_intr_desc_addr &
10248 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010249 vmcs_write64(POSTED_INTR_DESC_ADDR,
10250 page_to_phys(vmx->nested.pi_desc_page) +
10251 (unsigned long)(vmcs12->posted_intr_desc_addr &
10252 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080010253 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080010254 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000010255 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
10256 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010257 else
10258 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
10259 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010260}
10261
Jan Kiszkaf41245002014-03-07 20:03:13 +010010262static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
10263{
10264 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
10265 struct vcpu_vmx *vmx = to_vmx(vcpu);
10266
10267 if (vcpu->arch.virtual_tsc_khz == 0)
10268 return;
10269
10270 /* Make sure short timeouts reliably trigger an immediate vmexit.
10271 * hrtimer_start does not guarantee this. */
10272 if (preemption_timeout <= 1) {
10273 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
10274 return;
10275 }
10276
10277 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10278 preemption_timeout *= 1000000;
10279 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
10280 hrtimer_start(&vmx->nested.preemption_timer,
10281 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
10282}
10283
Jim Mattson56a20512017-07-06 16:33:06 -070010284static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
10285 struct vmcs12 *vmcs12)
10286{
10287 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
10288 return 0;
10289
10290 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
10291 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
10292 return -EINVAL;
10293
10294 return 0;
10295}
10296
Wincy Van3af18d92015-02-03 23:49:31 +080010297static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
10298 struct vmcs12 *vmcs12)
10299{
Wincy Van3af18d92015-02-03 23:49:31 +080010300 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10301 return 0;
10302
Jim Mattson5fa99cb2017-07-06 16:33:07 -070010303 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080010304 return -EINVAL;
10305
10306 return 0;
10307}
10308
Jim Mattson712b12d2017-08-24 13:24:47 -070010309static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
10310 struct vmcs12 *vmcs12)
10311{
10312 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10313 return 0;
10314
10315 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
10316 return -EINVAL;
10317
10318 return 0;
10319}
10320
Wincy Van3af18d92015-02-03 23:49:31 +080010321/*
10322 * Merge L0's and L1's MSR bitmap, return false to indicate that
10323 * we do not use the hardware.
10324 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010010325static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10326 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080010327{
Wincy Van82f0dd42015-02-03 23:57:18 +080010328 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080010329 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020010330 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010331 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010010332 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010333 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010010334 *
10335 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
10336 * ensures that we do not accidentally generate an L02 MSR bitmap
10337 * from the L12 MSR bitmap that is too permissive.
10338 * 2. That L1 or L2s have actually used the MSR. This avoids
10339 * unnecessarily merging of the bitmap if the MSR is unused. This
10340 * works properly because we only update the L01 MSR bitmap lazily.
10341 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
10342 * updated to reflect this when L1 (or its L2s) actually write to
10343 * the MSR.
10344 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000010345 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
10346 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080010347
Paolo Bonzinic9923842017-12-13 14:16:30 +010010348 /* Nothing to do if the MSR bitmap is not in use. */
10349 if (!cpu_has_vmx_msr_bitmap() ||
10350 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10351 return false;
10352
Ashok Raj15d45072018-02-01 22:59:43 +010010353 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010354 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080010355 return false;
10356
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010357 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
10358 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080010359 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010010360
Radim Krčmářd048c092016-08-08 20:16:22 +020010361 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010010362 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
10363 /*
10364 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
10365 * just lets the processor take the value from the virtual-APIC page;
10366 * take those 256 bits directly from the L1 bitmap.
10367 */
10368 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10369 unsigned word = msr / BITS_PER_LONG;
10370 msr_bitmap_l0[word] = msr_bitmap_l1[word];
10371 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080010372 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010010373 } else {
10374 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10375 unsigned word = msr / BITS_PER_LONG;
10376 msr_bitmap_l0[word] = ~0;
10377 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
10378 }
10379 }
10380
10381 nested_vmx_disable_intercept_for_msr(
10382 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010383 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010384 MSR_TYPE_W);
10385
10386 if (nested_cpu_has_vid(vmcs12)) {
10387 nested_vmx_disable_intercept_for_msr(
10388 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010389 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010390 MSR_TYPE_W);
10391 nested_vmx_disable_intercept_for_msr(
10392 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010393 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010394 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080010395 }
Ashok Raj15d45072018-02-01 22:59:43 +010010396
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010397 if (spec_ctrl)
10398 nested_vmx_disable_intercept_for_msr(
10399 msr_bitmap_l1, msr_bitmap_l0,
10400 MSR_IA32_SPEC_CTRL,
10401 MSR_TYPE_R | MSR_TYPE_W);
10402
Ashok Raj15d45072018-02-01 22:59:43 +010010403 if (pred_cmd)
10404 nested_vmx_disable_intercept_for_msr(
10405 msr_bitmap_l1, msr_bitmap_l0,
10406 MSR_IA32_PRED_CMD,
10407 MSR_TYPE_W);
10408
Wincy Vanf2b93282015-02-03 23:56:03 +080010409 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010410 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080010411
10412 return true;
10413}
10414
10415static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
10416 struct vmcs12 *vmcs12)
10417{
Wincy Van82f0dd42015-02-03 23:57:18 +080010418 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080010419 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080010420 !nested_cpu_has_vid(vmcs12) &&
10421 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080010422 return 0;
10423
10424 /*
10425 * If virtualize x2apic mode is enabled,
10426 * virtualize apic access must be disabled.
10427 */
Wincy Van82f0dd42015-02-03 23:57:18 +080010428 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10429 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080010430 return -EINVAL;
10431
Wincy Van608406e2015-02-03 23:57:51 +080010432 /*
10433 * If virtual interrupt delivery is enabled,
10434 * we must exit on external interrupts.
10435 */
10436 if (nested_cpu_has_vid(vmcs12) &&
10437 !nested_exit_on_intr(vcpu))
10438 return -EINVAL;
10439
Wincy Van705699a2015-02-03 23:58:17 +080010440 /*
10441 * bits 15:8 should be zero in posted_intr_nv,
10442 * the descriptor address has been already checked
10443 * in nested_get_vmcs12_pages.
10444 */
10445 if (nested_cpu_has_posted_intr(vmcs12) &&
10446 (!nested_cpu_has_vid(vmcs12) ||
10447 !nested_exit_intr_ack_set(vcpu) ||
10448 vmcs12->posted_intr_nv & 0xff00))
10449 return -EINVAL;
10450
Wincy Vanf2b93282015-02-03 23:56:03 +080010451 /* tpr shadow is needed by all apicv features. */
10452 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10453 return -EINVAL;
10454
10455 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080010456}
10457
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010458static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
10459 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010460 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030010461{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010462 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010463 u64 count, addr;
10464
10465 if (vmcs12_read_any(vcpu, count_field, &count) ||
10466 vmcs12_read_any(vcpu, addr_field, &addr)) {
10467 WARN_ON(1);
10468 return -EINVAL;
10469 }
10470 if (count == 0)
10471 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010472 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010473 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
10474 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010475 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010476 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
10477 addr_field, maxphyaddr, count, addr);
10478 return -EINVAL;
10479 }
10480 return 0;
10481}
10482
10483static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
10484 struct vmcs12 *vmcs12)
10485{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010486 if (vmcs12->vm_exit_msr_load_count == 0 &&
10487 vmcs12->vm_exit_msr_store_count == 0 &&
10488 vmcs12->vm_entry_msr_load_count == 0)
10489 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010490 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010491 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010492 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010493 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010494 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010495 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030010496 return -EINVAL;
10497 return 0;
10498}
10499
Bandan Dasc5f983f2017-05-05 15:25:14 -040010500static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
10501 struct vmcs12 *vmcs12)
10502{
10503 u64 address = vmcs12->pml_address;
10504 int maxphyaddr = cpuid_maxphyaddr(vcpu);
10505
10506 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
10507 if (!nested_cpu_has_ept(vmcs12) ||
10508 !IS_ALIGNED(address, 4096) ||
10509 address >> maxphyaddr)
10510 return -EINVAL;
10511 }
10512
10513 return 0;
10514}
10515
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010516static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
10517 struct vmx_msr_entry *e)
10518{
10519 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020010520 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010521 return -EINVAL;
10522 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
10523 e->index == MSR_IA32_UCODE_REV)
10524 return -EINVAL;
10525 if (e->reserved != 0)
10526 return -EINVAL;
10527 return 0;
10528}
10529
10530static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
10531 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030010532{
10533 if (e->index == MSR_FS_BASE ||
10534 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010535 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
10536 nested_vmx_msr_check_common(vcpu, e))
10537 return -EINVAL;
10538 return 0;
10539}
10540
10541static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
10542 struct vmx_msr_entry *e)
10543{
10544 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
10545 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030010546 return -EINVAL;
10547 return 0;
10548}
10549
10550/*
10551 * Load guest's/host's msr at nested entry/exit.
10552 * return 0 for success, entry index for failure.
10553 */
10554static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10555{
10556 u32 i;
10557 struct vmx_msr_entry e;
10558 struct msr_data msr;
10559
10560 msr.host_initiated = false;
10561 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010562 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
10563 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010564 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010565 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10566 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010567 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010568 }
10569 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010570 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010571 "%s check failed (%u, 0x%x, 0x%x)\n",
10572 __func__, i, e.index, e.reserved);
10573 goto fail;
10574 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010575 msr.index = e.index;
10576 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010577 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010578 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010579 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10580 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030010581 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010582 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010583 }
10584 return 0;
10585fail:
10586 return i + 1;
10587}
10588
10589static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10590{
10591 u32 i;
10592 struct vmx_msr_entry e;
10593
10594 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010595 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010596 if (kvm_vcpu_read_guest(vcpu,
10597 gpa + i * sizeof(e),
10598 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010599 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010600 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10601 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010602 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010603 }
10604 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010605 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010606 "%s check failed (%u, 0x%x, 0x%x)\n",
10607 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030010608 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010609 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010610 msr_info.host_initiated = false;
10611 msr_info.index = e.index;
10612 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010613 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010614 "%s cannot read MSR (%u, 0x%x)\n",
10615 __func__, i, e.index);
10616 return -EINVAL;
10617 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010618 if (kvm_vcpu_write_guest(vcpu,
10619 gpa + i * sizeof(e) +
10620 offsetof(struct vmx_msr_entry, value),
10621 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010622 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010623 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010624 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010625 return -EINVAL;
10626 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010627 }
10628 return 0;
10629}
10630
Ladi Prosek1dc35da2016-11-30 16:03:11 +010010631static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
10632{
10633 unsigned long invalid_mask;
10634
10635 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
10636 return (val & invalid_mask) == 0;
10637}
10638
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010639/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010640 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
10641 * emulating VM entry into a guest with EPT enabled.
10642 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10643 * is assigned to entry_failure_code on failure.
10644 */
10645static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080010646 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010647{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010648 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010010649 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010650 *entry_failure_code = ENTRY_FAIL_DEFAULT;
10651 return 1;
10652 }
10653
10654 /*
10655 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
10656 * must not be dereferenced.
10657 */
10658 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
10659 !nested_ept) {
10660 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
10661 *entry_failure_code = ENTRY_FAIL_PDPTE;
10662 return 1;
10663 }
10664 }
10665
10666 vcpu->arch.cr3 = cr3;
10667 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
10668 }
10669
10670 kvm_mmu_reset_context(vcpu);
10671 return 0;
10672}
10673
Paolo Bonzini74a497f2017-12-20 13:55:39 +010010674static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10675 bool from_vmentry)
10676{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010010677 struct vcpu_vmx *vmx = to_vmx(vcpu);
10678
10679 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
10680 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
10681 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
10682 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
10683 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
10684 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
10685 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
10686 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
10687 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
10688 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
10689 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
10690 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
10691 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
10692 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
10693 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
10694 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
10695 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
10696 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
10697 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
10698 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
10699 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
10700 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
10701 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
10702 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
10703 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
10704 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
10705 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
10706 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
10707 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
10708 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
10709 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010010710
10711 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
10712 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
10713 vmcs12->guest_pending_dbg_exceptions);
10714 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
10715 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
10716
10717 if (nested_cpu_has_xsaves(vmcs12))
10718 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
10719 vmcs_write64(VMCS_LINK_POINTER, -1ull);
10720
10721 if (cpu_has_vmx_posted_intr())
10722 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
10723
10724 /*
10725 * Whether page-faults are trapped is determined by a combination of
10726 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
10727 * If enable_ept, L0 doesn't care about page faults and we should
10728 * set all of these to L1's desires. However, if !enable_ept, L0 does
10729 * care about (at least some) page faults, and because it is not easy
10730 * (if at all possible?) to merge L0 and L1's desires, we simply ask
10731 * to exit on each and every L2 page fault. This is done by setting
10732 * MASK=MATCH=0 and (see below) EB.PF=1.
10733 * Note that below we don't need special code to set EB.PF beyond the
10734 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
10735 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
10736 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
10737 */
10738 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
10739 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
10740 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
10741 enable_ept ? vmcs12->page_fault_error_code_match : 0);
10742
10743 /* All VMFUNCs are currently emulated through L0 vmexits. */
10744 if (cpu_has_vmx_vmfunc())
10745 vmcs_write64(VM_FUNCTION_CONTROL, 0);
10746
10747 if (cpu_has_vmx_apicv()) {
10748 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
10749 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
10750 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
10751 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
10752 }
10753
10754 /*
10755 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
10756 * Some constant fields are set here by vmx_set_constant_host_state().
10757 * Other fields are different per CPU, and will be set later when
10758 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
10759 */
10760 vmx_set_constant_host_state(vmx);
10761
10762 /*
10763 * Set the MSR load/store lists to match L0's settings.
10764 */
10765 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
10766 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10767 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
10768 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10769 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
10770
10771 set_cr4_guest_host_mask(vmx);
10772
10773 if (vmx_mpx_supported())
10774 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
10775
10776 if (enable_vpid) {
10777 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
10778 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
10779 else
10780 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
10781 }
10782
10783 /*
10784 * L1 may access the L2's PDPTR, so save them to construct vmcs12
10785 */
10786 if (enable_ept) {
10787 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
10788 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
10789 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
10790 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
10791 }
Radim Krčmář80132f42018-02-02 18:26:58 +010010792
10793 if (cpu_has_vmx_msr_bitmap())
10794 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010010795}
10796
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010797/*
10798 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
10799 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080010800 * 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 +030010801 * guest in a way that will both be appropriate to L1's requests, and our
10802 * needs. In addition to modifying the active vmcs (which is vmcs02), this
10803 * function also has additional necessary side-effects, like setting various
10804 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010010805 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10806 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010807 */
Ladi Prosekee146c12016-11-30 16:03:09 +010010808static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattsonca0bde22016-11-30 12:03:46 -080010809 bool from_vmentry, u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010810{
10811 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040010812 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010813
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010010814 /*
10815 * First, the fields that are shadowed. This must be kept in sync
10816 * with vmx_shadow_fields.h.
10817 */
10818
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010819 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010820 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010821 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010822 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
10823 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010010824
10825 /*
10826 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
10827 * HOST_FS_BASE, HOST_GS_BASE.
10828 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010829
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010830 if (from_vmentry &&
10831 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020010832 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
10833 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
10834 } else {
10835 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
10836 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
10837 }
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010838 if (from_vmentry) {
10839 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
10840 vmcs12->vm_entry_intr_info_field);
10841 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
10842 vmcs12->vm_entry_exception_error_code);
10843 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
10844 vmcs12->vm_entry_instruction_len);
10845 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
10846 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070010847 vmx->loaded_vmcs->nmi_known_unmasked =
10848 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010849 } else {
10850 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10851 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030010852 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010853
Jan Kiszkaf41245002014-03-07 20:03:13 +010010854 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080010855
Paolo Bonzini9314006db2016-07-06 13:23:51 +020010856 /* Preemption timer setting is only taken from vmcs01. */
10857 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10858 exec_control |= vmcs_config.pin_based_exec_ctrl;
10859 if (vmx->hv_deadline_tsc == -1)
10860 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10861
10862 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080010863 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010864 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
10865 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010866 } else {
Wincy Van705699a2015-02-03 23:58:17 +080010867 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010868 }
Wincy Van705699a2015-02-03 23:58:17 +080010869
Jan Kiszkaf41245002014-03-07 20:03:13 +010010870 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010871
Jan Kiszkaf41245002014-03-07 20:03:13 +010010872 vmx->nested.preemption_timer_expired = false;
10873 if (nested_cpu_has_preemption_timer(vmcs12))
10874 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010010875
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010876 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020010877 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080010878
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010879 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010880 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020010881 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010010882 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020010883 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010884 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040010885 SECONDARY_EXEC_APIC_REGISTER_VIRT |
10886 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010887 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040010888 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
10889 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
10890 ~SECONDARY_EXEC_ENABLE_PML;
10891 exec_control |= vmcs12_exec_ctrl;
10892 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010893
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010010894 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080010895 vmcs_write16(GUEST_INTR_STATUS,
10896 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080010897
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010898 /*
10899 * Write an illegal value to APIC_ACCESS_ADDR. Later,
10900 * nested_get_vmcs12_pages will either fix it up or
10901 * remove the VM execution control.
10902 */
10903 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
10904 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
10905
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010906 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
10907 }
10908
Jim Mattson83bafef2016-10-04 10:48:38 -070010909 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010910 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
10911 * entry, but only if the current (host) sp changed from the value
10912 * we wrote last (vmx->host_rsp). This cache is no longer relevant
10913 * if we switch vmcs, and rather than hold a separate cache per vmcs,
10914 * here we just force the write to happen on entry.
10915 */
10916 vmx->host_rsp = 0;
10917
10918 exec_control = vmx_exec_control(vmx); /* L0's desires */
10919 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
10920 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
10921 exec_control &= ~CPU_BASED_TPR_SHADOW;
10922 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010923
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010924 /*
10925 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
10926 * nested_get_vmcs12_pages can't fix it up, the illegal value
10927 * will result in a VM entry failure.
10928 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010929 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010930 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010931 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070010932 } else {
10933#ifdef CONFIG_X86_64
10934 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
10935 CPU_BASED_CR8_STORE_EXITING;
10936#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010937 }
10938
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010939 /*
Quan Xu8eb73e2d2017-12-12 16:44:21 +080010940 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
10941 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010942 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010943 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
10944 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
10945
10946 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
10947
10948 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
10949 * bitwise-or of what L1 wants to trap for L2, and what we want to
10950 * trap. Note that CR0.TS also needs updating - we do this later.
10951 */
10952 update_exception_bitmap(vcpu);
10953 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
10954 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10955
Nadav Har'El8049d652013-08-05 11:07:06 +030010956 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
10957 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
10958 * bits are further modified by vmx_set_efer() below.
10959 */
Jan Kiszkaf41245002014-03-07 20:03:13 +010010960 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030010961
10962 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
10963 * emulated by vmx_set_efer(), below.
10964 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020010965 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030010966 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
10967 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010968 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
10969
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010970 if (from_vmentry &&
10971 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010972 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020010973 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010974 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010975 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010976 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010977
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010978 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
10979 vmcs_write64(TSC_OFFSET,
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010980 vcpu->arch.tsc_offset + vmcs12->tsc_offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010981 else
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010982 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Peter Feinerc95ba922016-08-17 09:36:47 -070010983 if (kvm_has_tsc_control)
10984 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010985
10986 if (enable_vpid) {
10987 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070010988 * There is no direct mapping between vpid02 and vpid12, the
10989 * vpid02 is per-vCPU for L0 and reused while the value of
10990 * vpid12 is changed w/ one invvpid during nested vmentry.
10991 * The vpid12 is allocated by L1 for L2, so it will not
10992 * influence global bitmap(for vpid01 and vpid02 allocation)
10993 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010994 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070010995 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070010996 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
10997 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080010998 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070010999 }
11000 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011001 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011002 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011003 }
11004
Ladi Prosek1fb883b2017-04-04 14:18:53 +020011005 if (enable_pml) {
11006 /*
11007 * Conceptually we want to copy the PML address and index from
11008 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11009 * since we always flush the log on each vmexit, this happens
11010 * to be equivalent to simply resetting the fields in vmcs02.
11011 */
11012 ASSERT(vmx->pml_pg);
11013 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11014 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11015 }
11016
Nadav Har'El155a97a2013-08-05 11:07:16 +030011017 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011018 if (nested_ept_init_mmu_context(vcpu)) {
11019 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11020 return 1;
11021 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011022 } else if (nested_cpu_has2(vmcs12,
11023 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
11024 vmx_flush_tlb_ept_only(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011025 }
11026
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011027 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011028 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11029 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011030 * The CR0_READ_SHADOW is what L2 should have expected to read given
11031 * the specifications by L1; It's not enough to take
11032 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11033 * have more bits than L1 expected.
11034 */
11035 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11036 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11037
11038 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11039 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11040
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011041 if (from_vmentry &&
11042 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080011043 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11044 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11045 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11046 else
11047 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11048 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11049 vmx_set_efer(vcpu, vcpu->arch.efer);
11050
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011051 if (vmx->nested.dirty_vmcs12) {
11052 prepare_vmcs02_full(vcpu, vmcs12, from_vmentry);
11053 vmx->nested.dirty_vmcs12 = false;
11054 }
11055
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011056 /*
11057 * Guest state is invalid and unrestricted guest is disabled,
11058 * which means L1 attempted VMEntry to L2 with invalid state.
11059 * Fail the VMEntry.
11060 */
11061 if (vmx->emulation_required)
11062 return 1;
11063
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011064 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010011065 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011066 entry_failure_code))
11067 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010011068
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011069 if (!enable_ept)
11070 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11071
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011072 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11073 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010011074 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011075}
11076
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011077static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11078{
11079 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11080 nested_cpu_has_virtual_nmis(vmcs12))
11081 return -EINVAL;
11082
11083 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11084 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
11085 return -EINVAL;
11086
11087 return 0;
11088}
11089
Jim Mattsonca0bde22016-11-30 12:03:46 -080011090static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11091{
11092 struct vcpu_vmx *vmx = to_vmx(vcpu);
11093
11094 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
11095 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11096 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11097
Jim Mattson56a20512017-07-06 16:33:06 -070011098 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11099 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11100
Jim Mattsonca0bde22016-11-30 12:03:46 -080011101 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11102 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11103
Jim Mattson712b12d2017-08-24 13:24:47 -070011104 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11105 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11106
Jim Mattsonca0bde22016-11-30 12:03:46 -080011107 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
11108 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11109
11110 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11111 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11112
Bandan Dasc5f983f2017-05-05 15:25:14 -040011113 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
11114 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11115
Jim Mattsonca0bde22016-11-30 12:03:46 -080011116 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011117 vmx->nested.msrs.procbased_ctls_low,
11118 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070011119 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
11120 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011121 vmx->nested.msrs.secondary_ctls_low,
11122 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011123 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011124 vmx->nested.msrs.pinbased_ctls_low,
11125 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011126 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011127 vmx->nested.msrs.exit_ctls_low,
11128 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011129 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011130 vmx->nested.msrs.entry_ctls_low,
11131 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011132 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11133
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011134 if (nested_vmx_check_nmi_controls(vmcs12))
11135 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11136
Bandan Das41ab9372017-08-03 15:54:43 -040011137 if (nested_cpu_has_vmfunc(vmcs12)) {
11138 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011139 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040011140 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11141
11142 if (nested_cpu_has_eptp_switching(vmcs12)) {
11143 if (!nested_cpu_has_ept(vmcs12) ||
11144 !page_address_valid(vcpu, vmcs12->eptp_list_address))
11145 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11146 }
11147 }
Bandan Das27c42a12017-08-03 15:54:42 -040011148
Jim Mattsonc7c2c7092017-05-05 11:28:09 -070011149 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
11150 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11151
Jim Mattsonca0bde22016-11-30 12:03:46 -080011152 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
11153 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
11154 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
11155 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11156
11157 return 0;
11158}
11159
11160static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11161 u32 *exit_qual)
11162{
11163 bool ia32e;
11164
11165 *exit_qual = ENTRY_FAIL_DEFAULT;
11166
11167 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
11168 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
11169 return 1;
11170
11171 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
11172 vmcs12->vmcs_link_pointer != -1ull) {
11173 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
11174 return 1;
11175 }
11176
11177 /*
11178 * If the load IA32_EFER VM-entry control is 1, the following checks
11179 * are performed on the field for the IA32_EFER MSR:
11180 * - Bits reserved in the IA32_EFER MSR must be 0.
11181 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
11182 * the IA-32e mode guest VM-exit control. It must also be identical
11183 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
11184 * CR0.PG) is 1.
11185 */
11186 if (to_vmx(vcpu)->nested.nested_run_pending &&
11187 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
11188 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
11189 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
11190 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
11191 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
11192 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
11193 return 1;
11194 }
11195
11196 /*
11197 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
11198 * IA32_EFER MSR must be 0 in the field for that register. In addition,
11199 * the values of the LMA and LME bits in the field must each be that of
11200 * the host address-space size VM-exit control.
11201 */
11202 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
11203 ia32e = (vmcs12->vm_exit_controls &
11204 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
11205 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
11206 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
11207 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
11208 return 1;
11209 }
11210
Wanpeng Lif1b026a2017-11-05 16:54:48 -080011211 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
11212 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
11213 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
11214 return 1;
11215
Jim Mattsonca0bde22016-11-30 12:03:46 -080011216 return 0;
11217}
11218
Jim Mattson858e25c2016-11-30 12:03:47 -080011219static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry)
11220{
11221 struct vcpu_vmx *vmx = to_vmx(vcpu);
11222 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080011223 u32 msr_entry_idx;
11224 u32 exit_qual;
11225
Jim Mattson858e25c2016-11-30 12:03:47 -080011226 enter_guest_mode(vcpu);
11227
11228 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
11229 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11230
Jim Mattsonde3a0022017-11-27 17:22:25 -060011231 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080011232 vmx_segment_cache_clear(vmx);
11233
11234 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &exit_qual)) {
11235 leave_guest_mode(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010011236 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson858e25c2016-11-30 12:03:47 -080011237 nested_vmx_entry_failure(vcpu, vmcs12,
11238 EXIT_REASON_INVALID_STATE, exit_qual);
11239 return 1;
11240 }
11241
11242 nested_get_vmcs12_pages(vcpu, vmcs12);
11243
11244 msr_entry_idx = nested_vmx_load_msr(vcpu,
11245 vmcs12->vm_entry_msr_load_addr,
11246 vmcs12->vm_entry_msr_load_count);
11247 if (msr_entry_idx) {
11248 leave_guest_mode(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010011249 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson858e25c2016-11-30 12:03:47 -080011250 nested_vmx_entry_failure(vcpu, vmcs12,
11251 EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
11252 return 1;
11253 }
11254
Jim Mattson858e25c2016-11-30 12:03:47 -080011255 /*
11256 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
11257 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
11258 * returned as far as L1 is concerned. It will only return (and set
11259 * the success flag) when L2 exits (see nested_vmx_vmexit()).
11260 */
11261 return 0;
11262}
11263
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011264/*
11265 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
11266 * for running an L2 nested guest.
11267 */
11268static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
11269{
11270 struct vmcs12 *vmcs12;
11271 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011272 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080011273 u32 exit_qual;
11274 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011275
Kyle Hueyeb277562016-11-29 12:40:39 -080011276 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011277 return 1;
11278
Kyle Hueyeb277562016-11-29 12:40:39 -080011279 if (!nested_vmx_check_vmcs12(vcpu))
11280 goto out;
11281
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011282 vmcs12 = get_vmcs12(vcpu);
11283
Abel Gordon012f83c2013-04-18 14:39:25 +030011284 if (enable_shadow_vmcs)
11285 copy_shadow_to_vmcs12(vmx);
11286
Nadav Har'El7c177932011-05-25 23:12:04 +030011287 /*
11288 * The nested entry process starts with enforcing various prerequisites
11289 * on vmcs12 as required by the Intel SDM, and act appropriately when
11290 * they fail: As the SDM explains, some conditions should cause the
11291 * instruction to fail, while others will cause the instruction to seem
11292 * to succeed, but return an EXIT_REASON_INVALID_STATE.
11293 * To speed up the normal (success) code path, we should avoid checking
11294 * for misconfigurations which will anyway be caught by the processor
11295 * when using the merged vmcs02.
11296 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011297 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
11298 nested_vmx_failValid(vcpu,
11299 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
11300 goto out;
11301 }
11302
Nadav Har'El7c177932011-05-25 23:12:04 +030011303 if (vmcs12->launch_state == launch) {
11304 nested_vmx_failValid(vcpu,
11305 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
11306 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080011307 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030011308 }
11309
Jim Mattsonca0bde22016-11-30 12:03:46 -080011310 ret = check_vmentry_prereqs(vcpu, vmcs12);
11311 if (ret) {
11312 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080011313 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020011314 }
11315
Nadav Har'El7c177932011-05-25 23:12:04 +030011316 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080011317 * After this point, the trap flag no longer triggers a singlestep trap
11318 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
11319 * This is not 100% correct; for performance reasons, we delegate most
11320 * of the checks on host state to the processor. If those fail,
11321 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020011322 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080011323 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020011324
Jim Mattsonca0bde22016-11-30 12:03:46 -080011325 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
11326 if (ret) {
11327 nested_vmx_entry_failure(vcpu, vmcs12,
11328 EXIT_REASON_INVALID_STATE, exit_qual);
11329 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020011330 }
11331
11332 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030011333 * We're finally done with prerequisite checking, and can start with
11334 * the nested entry.
11335 */
11336
Jim Mattson858e25c2016-11-30 12:03:47 -080011337 ret = enter_vmx_non_root_mode(vcpu, true);
11338 if (ret)
11339 return ret;
Wincy Vanff651cb2014-12-11 08:52:58 +030011340
Chao Gao135a06c2018-02-11 10:06:30 +080011341 /*
11342 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
11343 * by event injection, halt vcpu.
11344 */
11345 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
11346 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK))
Joel Schopp5cb56052015-03-02 13:43:31 -060011347 return kvm_vcpu_halt(vcpu);
Jan Kiszka6dfacad2013-12-04 08:58:54 +010011348
Jan Kiszka7af40ad32014-01-04 18:47:23 +010011349 vmx->nested.nested_run_pending = 1;
11350
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011351 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080011352
11353out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080011354 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011355}
11356
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011357/*
11358 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
11359 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
11360 * This function returns the new value we should put in vmcs12.guest_cr0.
11361 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
11362 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
11363 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
11364 * didn't trap the bit, because if L1 did, so would L0).
11365 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
11366 * been modified by L2, and L1 knows it. So just leave the old value of
11367 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
11368 * isn't relevant, because if L0 traps this bit it can set it to anything.
11369 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
11370 * changed these bits, and therefore they need to be updated, but L0
11371 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
11372 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
11373 */
11374static inline unsigned long
11375vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11376{
11377 return
11378 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
11379 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
11380 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
11381 vcpu->arch.cr0_guest_owned_bits));
11382}
11383
11384static inline unsigned long
11385vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11386{
11387 return
11388 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
11389 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
11390 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
11391 vcpu->arch.cr4_guest_owned_bits));
11392}
11393
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011394static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
11395 struct vmcs12 *vmcs12)
11396{
11397 u32 idt_vectoring;
11398 unsigned int nr;
11399
Wanpeng Li664f8e22017-08-24 03:35:09 -070011400 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011401 nr = vcpu->arch.exception.nr;
11402 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11403
11404 if (kvm_exception_is_soft(nr)) {
11405 vmcs12->vm_exit_instruction_len =
11406 vcpu->arch.event_exit_inst_len;
11407 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
11408 } else
11409 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
11410
11411 if (vcpu->arch.exception.has_error_code) {
11412 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
11413 vmcs12->idt_vectoring_error_code =
11414 vcpu->arch.exception.error_code;
11415 }
11416
11417 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010011418 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011419 vmcs12->idt_vectoring_info_field =
11420 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
11421 } else if (vcpu->arch.interrupt.pending) {
11422 nr = vcpu->arch.interrupt.nr;
11423 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11424
11425 if (vcpu->arch.interrupt.soft) {
11426 idt_vectoring |= INTR_TYPE_SOFT_INTR;
11427 vmcs12->vm_entry_instruction_len =
11428 vcpu->arch.event_exit_inst_len;
11429 } else
11430 idt_vectoring |= INTR_TYPE_EXT_INTR;
11431
11432 vmcs12->idt_vectoring_info_field = idt_vectoring;
11433 }
11434}
11435
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011436static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
11437{
11438 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011439 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020011440 bool block_nested_events =
11441 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080011442
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011443 if (vcpu->arch.exception.pending &&
11444 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020011445 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011446 return -EBUSY;
11447 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011448 return 0;
11449 }
11450
Jan Kiszkaf41245002014-03-07 20:03:13 +010011451 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
11452 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020011453 if (block_nested_events)
Jan Kiszkaf41245002014-03-07 20:03:13 +010011454 return -EBUSY;
11455 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
11456 return 0;
11457 }
11458
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011459 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020011460 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011461 return -EBUSY;
11462 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11463 NMI_VECTOR | INTR_TYPE_NMI_INTR |
11464 INTR_INFO_VALID_MASK, 0);
11465 /*
11466 * The NMI-triggered VM exit counts as injection:
11467 * clear this one and block further NMIs.
11468 */
11469 vcpu->arch.nmi_pending = 0;
11470 vmx_set_nmi_mask(vcpu, true);
11471 return 0;
11472 }
11473
11474 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
11475 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020011476 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011477 return -EBUSY;
11478 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080011479 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011480 }
11481
David Hildenbrand6342c502017-01-25 11:58:58 +010011482 vmx_complete_nested_posted_interrupt(vcpu);
11483 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011484}
11485
Jan Kiszkaf41245002014-03-07 20:03:13 +010011486static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
11487{
11488 ktime_t remaining =
11489 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
11490 u64 value;
11491
11492 if (ktime_to_ns(remaining) <= 0)
11493 return 0;
11494
11495 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
11496 do_div(value, 1000000);
11497 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11498}
11499
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011500/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011501 * Update the guest state fields of vmcs12 to reflect changes that
11502 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
11503 * VM-entry controls is also updated, since this is really a guest
11504 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011505 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011506static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011507{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011508 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
11509 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
11510
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011511 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
11512 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
11513 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
11514
11515 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
11516 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
11517 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
11518 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
11519 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
11520 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
11521 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
11522 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
11523 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
11524 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
11525 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
11526 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
11527 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
11528 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
11529 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
11530 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
11531 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
11532 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
11533 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
11534 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
11535 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
11536 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
11537 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
11538 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
11539 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
11540 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
11541 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
11542 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
11543 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
11544 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
11545 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
11546 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
11547 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
11548 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
11549 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
11550 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
11551
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011552 vmcs12->guest_interruptibility_info =
11553 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
11554 vmcs12->guest_pending_dbg_exceptions =
11555 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010011556 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
11557 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
11558 else
11559 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011560
Jan Kiszkaf41245002014-03-07 20:03:13 +010011561 if (nested_cpu_has_preemption_timer(vmcs12)) {
11562 if (vmcs12->vm_exit_controls &
11563 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
11564 vmcs12->vmx_preemption_timer_value =
11565 vmx_get_preemption_timer_value(vcpu);
11566 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
11567 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080011568
Nadav Har'El3633cfc2013-08-05 11:07:07 +030011569 /*
11570 * In some cases (usually, nested EPT), L2 is allowed to change its
11571 * own CR3 without exiting. If it has changed it, we must keep it.
11572 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
11573 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
11574 *
11575 * Additionally, restore L2's PDPTR to vmcs12.
11576 */
11577 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010011578 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030011579 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
11580 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
11581 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
11582 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
11583 }
11584
Jim Mattsond281e132017-06-01 12:44:46 -070011585 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030011586
Wincy Van608406e2015-02-03 23:57:51 +080011587 if (nested_cpu_has_vid(vmcs12))
11588 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
11589
Jan Kiszkac18911a2013-03-13 16:06:41 +010011590 vmcs12->vm_entry_controls =
11591 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020011592 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010011593
Jan Kiszka2996fca2014-06-16 13:59:43 +020011594 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
11595 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
11596 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11597 }
11598
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011599 /* TODO: These cannot have changed unless we have MSR bitmaps and
11600 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020011601 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011602 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020011603 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
11604 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011605 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
11606 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
11607 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010011608 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011609 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011610}
11611
11612/*
11613 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
11614 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
11615 * and this function updates it to reflect the changes to the guest state while
11616 * L2 was running (and perhaps made some exits which were handled directly by L0
11617 * without going back to L1), and to reflect the exit reason.
11618 * Note that we do not have to copy here all VMCS fields, just those that
11619 * could have changed by the L2 guest or the exit - i.e., the guest-state and
11620 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
11621 * which already writes to vmcs12 directly.
11622 */
11623static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11624 u32 exit_reason, u32 exit_intr_info,
11625 unsigned long exit_qualification)
11626{
11627 /* update guest state fields: */
11628 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011629
11630 /* update exit information fields: */
11631
Jan Kiszka533558b2014-01-04 18:47:20 +010011632 vmcs12->vm_exit_reason = exit_reason;
11633 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010011634 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020011635
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011636 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011637 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
11638 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
11639
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011640 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070011641 vmcs12->launch_state = 1;
11642
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011643 /* vm_entry_intr_info_field is cleared on exit. Emulate this
11644 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011645 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011646
11647 /*
11648 * Transfer the event that L0 or L1 may wanted to inject into
11649 * L2 to IDT_VECTORING_INFO_FIELD.
11650 */
11651 vmcs12_save_pending_event(vcpu, vmcs12);
11652 }
11653
11654 /*
11655 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
11656 * preserved above and would only end up incorrectly in L1.
11657 */
11658 vcpu->arch.nmi_injected = false;
11659 kvm_clear_exception_queue(vcpu);
11660 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011661}
11662
Wanpeng Li5af41572017-11-05 16:54:49 -080011663static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
11664 struct vmcs12 *vmcs12)
11665{
11666 u32 entry_failure_code;
11667
11668 nested_ept_uninit_mmu_context(vcpu);
11669
11670 /*
11671 * Only PDPTE load can fail as the value of cr3 was checked on entry and
11672 * couldn't have changed.
11673 */
11674 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
11675 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
11676
11677 if (!enable_ept)
11678 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
11679}
11680
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011681/*
11682 * A part of what we need to when the nested L2 guest exits and we want to
11683 * run its L1 parent, is to reset L1's guest state to the host state specified
11684 * in vmcs12.
11685 * This function is to be called not only on normal nested exit, but also on
11686 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
11687 * Failures During or After Loading Guest State").
11688 * This function should be called when the active VMCS is L1's (vmcs01).
11689 */
Jan Kiszka733568f2013-02-23 15:07:47 +010011690static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
11691 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011692{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011693 struct kvm_segment seg;
11694
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011695 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
11696 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020011697 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011698 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11699 else
11700 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11701 vmx_set_efer(vcpu, vcpu->arch.efer);
11702
11703 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
11704 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070011705 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011706 /*
11707 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011708 * actually changed, because vmx_set_cr0 refers to efer set above.
11709 *
11710 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
11711 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011712 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011713 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4dbf2013-09-03 21:11:45 +020011714 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011715
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011716 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011717 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080011718 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011719
Wanpeng Li5af41572017-11-05 16:54:49 -080011720 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011721
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011722 if (enable_vpid) {
11723 /*
11724 * Trivially support vpid by letting L2s share their parent
11725 * L1's vpid. TODO: move to a more elaborate solution, giving
11726 * each L2 its own vpid and exposing the vpid feature to L1.
11727 */
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011728 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011729 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011730
11731 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
11732 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
11733 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
11734 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
11735 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020011736 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
11737 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011738
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011739 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
11740 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
11741 vmcs_write64(GUEST_BNDCFGS, 0);
11742
Jan Kiszka44811c02013-08-04 17:17:27 +020011743 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011744 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011745 vcpu->arch.pat = vmcs12->host_ia32_pat;
11746 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011747 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
11748 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
11749 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011750
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011751 /* Set L1 segment info according to Intel SDM
11752 27.5.2 Loading Host Segment and Descriptor-Table Registers */
11753 seg = (struct kvm_segment) {
11754 .base = 0,
11755 .limit = 0xFFFFFFFF,
11756 .selector = vmcs12->host_cs_selector,
11757 .type = 11,
11758 .present = 1,
11759 .s = 1,
11760 .g = 1
11761 };
11762 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
11763 seg.l = 1;
11764 else
11765 seg.db = 1;
11766 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
11767 seg = (struct kvm_segment) {
11768 .base = 0,
11769 .limit = 0xFFFFFFFF,
11770 .type = 3,
11771 .present = 1,
11772 .s = 1,
11773 .db = 1,
11774 .g = 1
11775 };
11776 seg.selector = vmcs12->host_ds_selector;
11777 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
11778 seg.selector = vmcs12->host_es_selector;
11779 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
11780 seg.selector = vmcs12->host_ss_selector;
11781 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
11782 seg.selector = vmcs12->host_fs_selector;
11783 seg.base = vmcs12->host_fs_base;
11784 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
11785 seg.selector = vmcs12->host_gs_selector;
11786 seg.base = vmcs12->host_gs_base;
11787 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
11788 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030011789 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011790 .limit = 0x67,
11791 .selector = vmcs12->host_tr_selector,
11792 .type = 11,
11793 .present = 1
11794 };
11795 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
11796
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011797 kvm_set_dr(vcpu, 7, 0x400);
11798 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030011799
Wincy Van3af18d92015-02-03 23:49:31 +080011800 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011801 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080011802
Wincy Vanff651cb2014-12-11 08:52:58 +030011803 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
11804 vmcs12->vm_exit_msr_load_count))
11805 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011806}
11807
11808/*
11809 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
11810 * and modify vmcs12 to make it see what it would expect to see there if
11811 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
11812 */
Jan Kiszka533558b2014-01-04 18:47:20 +010011813static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
11814 u32 exit_intr_info,
11815 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011816{
11817 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011818 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11819
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011820 /* trying to cancel vmlaunch/vmresume is a bug */
11821 WARN_ON_ONCE(vmx->nested.nested_run_pending);
11822
Wanpeng Li6550c4d2017-07-31 19:25:27 -070011823 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070011824 * The only expected VM-instruction error is "VM entry with
11825 * invalid control field(s)." Anything else indicates a
11826 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070011827 */
Jim Mattson4f350c62017-09-14 16:31:44 -070011828 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
11829 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
11830
11831 leave_guest_mode(vcpu);
11832
11833 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020011834 if (exit_reason == -1)
11835 sync_vmcs12(vcpu, vmcs12);
11836 else
11837 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
11838 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070011839
11840 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
11841 vmcs12->vm_exit_msr_store_count))
11842 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040011843 }
11844
Jim Mattson4f350c62017-09-14 16:31:44 -070011845 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020011846 vm_entry_controls_reset_shadow(vmx);
11847 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010011848 vmx_segment_cache_clear(vmx);
11849
Paolo Bonzini9314006db2016-07-06 13:23:51 +020011850 /* Update any VMCS fields that might have changed while L2 ran */
Jim Mattson83bafef2016-10-04 10:48:38 -070011851 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11852 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010011853 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini9314006db2016-07-06 13:23:51 +020011854 if (vmx->hv_deadline_tsc == -1)
11855 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11856 PIN_BASED_VMX_PREEMPTION_TIMER);
11857 else
11858 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11859 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070011860 if (kvm_has_tsc_control)
11861 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011862
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020011863 if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
11864 vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
11865 vmx_set_virtual_x2apic_mode(vcpu,
11866 vcpu->arch.apic_base & X2APIC_ENABLE);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011867 } else if (!nested_cpu_has_ept(vmcs12) &&
11868 nested_cpu_has2(vmcs12,
11869 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
11870 vmx_flush_tlb_ept_only(vcpu);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020011871 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011872
11873 /* This is needed for same reason as it was needed in prepare_vmcs02 */
11874 vmx->host_rsp = 0;
11875
11876 /* Unpin physical memory we referred to in vmcs02 */
11877 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020011878 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011879 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011880 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011881 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020011882 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011883 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011884 }
Wincy Van705699a2015-02-03 23:58:17 +080011885 if (vmx->nested.pi_desc_page) {
11886 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011887 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080011888 vmx->nested.pi_desc_page = NULL;
11889 vmx->nested.pi_desc = NULL;
11890 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011891
11892 /*
Tang Chen38b99172014-09-24 15:57:54 +080011893 * We are now running in L2, mmu_notifier will force to reload the
11894 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
11895 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080011896 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080011897
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020011898 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030011899 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011900
11901 /* in case we halted in L2 */
11902 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070011903
11904 if (likely(!vmx->fail)) {
11905 /*
11906 * TODO: SDM says that with acknowledge interrupt on
11907 * exit, bit 31 of the VM-exit interrupt information
11908 * (valid interrupt) is always set to 1 on
11909 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
11910 * need kvm_cpu_has_interrupt(). See the commit
11911 * message for details.
11912 */
11913 if (nested_exit_intr_ack_set(vcpu) &&
11914 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
11915 kvm_cpu_has_interrupt(vcpu)) {
11916 int irq = kvm_cpu_get_interrupt(vcpu);
11917 WARN_ON(irq < 0);
11918 vmcs12->vm_exit_intr_info = irq |
11919 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
11920 }
11921
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020011922 if (exit_reason != -1)
11923 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
11924 vmcs12->exit_qualification,
11925 vmcs12->idt_vectoring_info_field,
11926 vmcs12->vm_exit_intr_info,
11927 vmcs12->vm_exit_intr_error_code,
11928 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070011929
11930 load_vmcs12_host_state(vcpu, vmcs12);
11931
11932 return;
11933 }
11934
11935 /*
11936 * After an early L2 VM-entry failure, we're now back
11937 * in L1 which thinks it just finished a VMLAUNCH or
11938 * VMRESUME instruction, so we need to set the failure
11939 * flag and the VM-instruction error field of the VMCS
11940 * accordingly.
11941 */
11942 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080011943
11944 load_vmcs12_mmu_host_state(vcpu, vmcs12);
11945
Jim Mattson4f350c62017-09-14 16:31:44 -070011946 /*
11947 * The emulated instruction was already skipped in
11948 * nested_vmx_run, but the updated RIP was never
11949 * written back to the vmcs01.
11950 */
11951 skip_emulated_instruction(vcpu);
11952 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011953}
11954
Nadav Har'El7c177932011-05-25 23:12:04 +030011955/*
Jan Kiszka42124922014-01-04 18:47:19 +010011956 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
11957 */
11958static void vmx_leave_nested(struct kvm_vcpu *vcpu)
11959{
Wanpeng Li2f707d92017-03-06 04:03:28 -080011960 if (is_guest_mode(vcpu)) {
11961 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010011962 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080011963 }
Jan Kiszka42124922014-01-04 18:47:19 +010011964 free_nested(to_vmx(vcpu));
11965}
11966
11967/*
Nadav Har'El7c177932011-05-25 23:12:04 +030011968 * L1's failure to enter L2 is a subset of a normal exit, as explained in
11969 * 23.7 "VM-entry failures during or after loading guest state" (this also
11970 * lists the acceptable exit-reason and exit-qualification parameters).
11971 * It should only be called before L2 actually succeeded to run, and when
11972 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
11973 */
11974static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
11975 struct vmcs12 *vmcs12,
11976 u32 reason, unsigned long qualification)
11977{
11978 load_vmcs12_host_state(vcpu, vmcs12);
11979 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
11980 vmcs12->exit_qualification = qualification;
11981 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030011982 if (enable_shadow_vmcs)
11983 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030011984}
11985
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011986static int vmx_check_intercept(struct kvm_vcpu *vcpu,
11987 struct x86_instruction_info *info,
11988 enum x86_intercept_stage stage)
11989{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020011990 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11991 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
11992
11993 /*
11994 * RDPID causes #UD if disabled through secondary execution controls.
11995 * Because it is marked as EmulateOnUD, we need to intercept it here.
11996 */
11997 if (info->intercept == x86_intercept_rdtscp &&
11998 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
11999 ctxt->exception.vector = UD_VECTOR;
12000 ctxt->exception.error_code_valid = false;
12001 return X86EMUL_PROPAGATE_FAULT;
12002 }
12003
12004 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012005 return X86EMUL_CONTINUE;
12006}
12007
Yunhong Jiang64672c92016-06-13 14:19:59 -070012008#ifdef CONFIG_X86_64
12009/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
12010static inline int u64_shl_div_u64(u64 a, unsigned int shift,
12011 u64 divisor, u64 *result)
12012{
12013 u64 low = a << shift, high = a >> (64 - shift);
12014
12015 /* To avoid the overflow on divq */
12016 if (high >= divisor)
12017 return 1;
12018
12019 /* Low hold the result, high hold rem which is discarded */
12020 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
12021 "rm" (divisor), "0" (low), "1" (high));
12022 *result = low;
12023
12024 return 0;
12025}
12026
12027static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
12028{
12029 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini9175d2e2016-06-27 15:08:01 +020012030 u64 tscl = rdtsc();
12031 u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
12032 u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012033
12034 /* Convert to host delta tsc if tsc scaling is enabled */
12035 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
12036 u64_shl_div_u64(delta_tsc,
12037 kvm_tsc_scaling_ratio_frac_bits,
12038 vcpu->arch.tsc_scaling_ratio,
12039 &delta_tsc))
12040 return -ERANGE;
12041
12042 /*
12043 * If the delta tsc can't fit in the 32 bit after the multi shift,
12044 * we can't use the preemption timer.
12045 * It's possible that it fits on later vmentries, but checking
12046 * on every vmentry is costly so we just use an hrtimer.
12047 */
12048 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
12049 return -ERANGE;
12050
12051 vmx->hv_deadline_tsc = tscl + delta_tsc;
12052 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12053 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070012054
12055 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012056}
12057
12058static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
12059{
12060 struct vcpu_vmx *vmx = to_vmx(vcpu);
12061 vmx->hv_deadline_tsc = -1;
12062 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12063 PIN_BASED_VMX_PREEMPTION_TIMER);
12064}
12065#endif
12066
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012067static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012068{
Wanpeng Lib31c1142018-03-12 04:53:04 -070012069 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020012070 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012071}
12072
Kai Huang843e4332015-01-28 10:54:28 +080012073static void vmx_slot_enable_log_dirty(struct kvm *kvm,
12074 struct kvm_memory_slot *slot)
12075{
12076 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
12077 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
12078}
12079
12080static void vmx_slot_disable_log_dirty(struct kvm *kvm,
12081 struct kvm_memory_slot *slot)
12082{
12083 kvm_mmu_slot_set_dirty(kvm, slot);
12084}
12085
12086static void vmx_flush_log_dirty(struct kvm *kvm)
12087{
12088 kvm_flush_pml_buffers(kvm);
12089}
12090
Bandan Dasc5f983f2017-05-05 15:25:14 -040012091static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
12092{
12093 struct vmcs12 *vmcs12;
12094 struct vcpu_vmx *vmx = to_vmx(vcpu);
12095 gpa_t gpa;
12096 struct page *page = NULL;
12097 u64 *pml_address;
12098
12099 if (is_guest_mode(vcpu)) {
12100 WARN_ON_ONCE(vmx->nested.pml_full);
12101
12102 /*
12103 * Check if PML is enabled for the nested guest.
12104 * Whether eptp bit 6 is set is already checked
12105 * as part of A/D emulation.
12106 */
12107 vmcs12 = get_vmcs12(vcpu);
12108 if (!nested_cpu_has_pml(vmcs12))
12109 return 0;
12110
Dan Carpenter47698862017-05-10 22:43:17 +030012111 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040012112 vmx->nested.pml_full = true;
12113 return 1;
12114 }
12115
12116 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
12117
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020012118 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
12119 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040012120 return 0;
12121
12122 pml_address = kmap(page);
12123 pml_address[vmcs12->guest_pml_index--] = gpa;
12124 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012125 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040012126 }
12127
12128 return 0;
12129}
12130
Kai Huang843e4332015-01-28 10:54:28 +080012131static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
12132 struct kvm_memory_slot *memslot,
12133 gfn_t offset, unsigned long mask)
12134{
12135 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
12136}
12137
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012138static void __pi_post_block(struct kvm_vcpu *vcpu)
12139{
12140 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12141 struct pi_desc old, new;
12142 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012143
12144 do {
12145 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012146 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
12147 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012148
12149 dest = cpu_physical_id(vcpu->cpu);
12150
12151 if (x2apic_enabled())
12152 new.ndst = dest;
12153 else
12154 new.ndst = (dest << 8) & 0xFF00;
12155
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012156 /* set 'NV' to 'notification vector' */
12157 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012158 } while (cmpxchg64(&pi_desc->control, old.control,
12159 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012160
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012161 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
12162 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012163 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012164 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012165 vcpu->pre_pcpu = -1;
12166 }
12167}
12168
Feng Wuefc64402015-09-18 22:29:51 +080012169/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080012170 * This routine does the following things for vCPU which is going
12171 * to be blocked if VT-d PI is enabled.
12172 * - Store the vCPU to the wakeup list, so when interrupts happen
12173 * we can find the right vCPU to wake up.
12174 * - Change the Posted-interrupt descriptor as below:
12175 * 'NDST' <-- vcpu->pre_pcpu
12176 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
12177 * - If 'ON' is set during this process, which means at least one
12178 * interrupt is posted for this vCPU, we cannot block it, in
12179 * this case, return 1, otherwise, return 0.
12180 *
12181 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070012182static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012183{
Feng Wubf9f6ac2015-09-18 22:29:55 +080012184 unsigned int dest;
12185 struct pi_desc old, new;
12186 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12187
12188 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012189 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12190 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080012191 return 0;
12192
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012193 WARN_ON(irqs_disabled());
12194 local_irq_disable();
12195 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
12196 vcpu->pre_pcpu = vcpu->cpu;
12197 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12198 list_add_tail(&vcpu->blocked_vcpu_list,
12199 &per_cpu(blocked_vcpu_on_cpu,
12200 vcpu->pre_pcpu));
12201 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12202 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080012203
12204 do {
12205 old.control = new.control = pi_desc->control;
12206
Feng Wubf9f6ac2015-09-18 22:29:55 +080012207 WARN((pi_desc->sn == 1),
12208 "Warning: SN field of posted-interrupts "
12209 "is set before blocking\n");
12210
12211 /*
12212 * Since vCPU can be preempted during this process,
12213 * vcpu->cpu could be different with pre_pcpu, we
12214 * need to set pre_pcpu as the destination of wakeup
12215 * notification event, then we can find the right vCPU
12216 * to wakeup in wakeup handler if interrupts happen
12217 * when the vCPU is in blocked state.
12218 */
12219 dest = cpu_physical_id(vcpu->pre_pcpu);
12220
12221 if (x2apic_enabled())
12222 new.ndst = dest;
12223 else
12224 new.ndst = (dest << 8) & 0xFF00;
12225
12226 /* set 'NV' to 'wakeup vector' */
12227 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012228 } while (cmpxchg64(&pi_desc->control, old.control,
12229 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012230
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012231 /* We should not block the vCPU if an interrupt is posted for it. */
12232 if (pi_test_on(pi_desc) == 1)
12233 __pi_post_block(vcpu);
12234
12235 local_irq_enable();
12236 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012237}
12238
Yunhong Jiangbc225122016-06-13 14:19:58 -070012239static int vmx_pre_block(struct kvm_vcpu *vcpu)
12240{
12241 if (pi_pre_block(vcpu))
12242 return 1;
12243
Yunhong Jiang64672c92016-06-13 14:19:59 -070012244 if (kvm_lapic_hv_timer_in_use(vcpu))
12245 kvm_lapic_switch_to_sw_timer(vcpu);
12246
Yunhong Jiangbc225122016-06-13 14:19:58 -070012247 return 0;
12248}
12249
12250static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012251{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012252 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012253 return;
12254
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012255 WARN_ON(irqs_disabled());
12256 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012257 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012258 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080012259}
12260
Yunhong Jiangbc225122016-06-13 14:19:58 -070012261static void vmx_post_block(struct kvm_vcpu *vcpu)
12262{
Yunhong Jiang64672c92016-06-13 14:19:59 -070012263 if (kvm_x86_ops->set_hv_timer)
12264 kvm_lapic_switch_to_hv_timer(vcpu);
12265
Yunhong Jiangbc225122016-06-13 14:19:58 -070012266 pi_post_block(vcpu);
12267}
12268
Feng Wubf9f6ac2015-09-18 22:29:55 +080012269/*
Feng Wuefc64402015-09-18 22:29:51 +080012270 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
12271 *
12272 * @kvm: kvm
12273 * @host_irq: host irq of the interrupt
12274 * @guest_irq: gsi of the interrupt
12275 * @set: set or unset PI
12276 * returns 0 on success, < 0 on failure
12277 */
12278static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
12279 uint32_t guest_irq, bool set)
12280{
12281 struct kvm_kernel_irq_routing_entry *e;
12282 struct kvm_irq_routing_table *irq_rt;
12283 struct kvm_lapic_irq irq;
12284 struct kvm_vcpu *vcpu;
12285 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012286 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080012287
12288 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012289 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12290 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080012291 return 0;
12292
12293 idx = srcu_read_lock(&kvm->irq_srcu);
12294 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012295 if (guest_irq >= irq_rt->nr_rt_entries ||
12296 hlist_empty(&irq_rt->map[guest_irq])) {
12297 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
12298 guest_irq, irq_rt->nr_rt_entries);
12299 goto out;
12300 }
Feng Wuefc64402015-09-18 22:29:51 +080012301
12302 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
12303 if (e->type != KVM_IRQ_ROUTING_MSI)
12304 continue;
12305 /*
12306 * VT-d PI cannot support posting multicast/broadcast
12307 * interrupts to a vCPU, we still use interrupt remapping
12308 * for these kind of interrupts.
12309 *
12310 * For lowest-priority interrupts, we only support
12311 * those with single CPU as the destination, e.g. user
12312 * configures the interrupts via /proc/irq or uses
12313 * irqbalance to make the interrupts single-CPU.
12314 *
12315 * We will support full lowest-priority interrupt later.
12316 */
12317
Radim Krčmář371313132016-07-12 22:09:27 +020012318 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080012319 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
12320 /*
12321 * Make sure the IRTE is in remapped mode if
12322 * we don't handle it in posted mode.
12323 */
12324 ret = irq_set_vcpu_affinity(host_irq, NULL);
12325 if (ret < 0) {
12326 printk(KERN_INFO
12327 "failed to back to remapped mode, irq: %u\n",
12328 host_irq);
12329 goto out;
12330 }
12331
Feng Wuefc64402015-09-18 22:29:51 +080012332 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080012333 }
Feng Wuefc64402015-09-18 22:29:51 +080012334
12335 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
12336 vcpu_info.vector = irq.vector;
12337
Feng Wub6ce9782016-01-25 16:53:35 +080012338 trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080012339 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
12340
12341 if (set)
12342 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080012343 else
Feng Wuefc64402015-09-18 22:29:51 +080012344 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080012345
12346 if (ret < 0) {
12347 printk(KERN_INFO "%s: failed to update PI IRTE\n",
12348 __func__);
12349 goto out;
12350 }
12351 }
12352
12353 ret = 0;
12354out:
12355 srcu_read_unlock(&kvm->irq_srcu, idx);
12356 return ret;
12357}
12358
Ashok Rajc45dcc72016-06-22 14:59:56 +080012359static void vmx_setup_mce(struct kvm_vcpu *vcpu)
12360{
12361 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
12362 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
12363 FEATURE_CONTROL_LMCE;
12364 else
12365 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
12366 ~FEATURE_CONTROL_LMCE;
12367}
12368
Ladi Prosek72d7b372017-10-11 16:54:41 +020012369static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
12370{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012371 /* we need a nested vmexit to enter SMM, postpone if run is pending */
12372 if (to_vmx(vcpu)->nested.nested_run_pending)
12373 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020012374 return 1;
12375}
12376
Ladi Prosek0234bf82017-10-11 16:54:40 +020012377static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
12378{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012379 struct vcpu_vmx *vmx = to_vmx(vcpu);
12380
12381 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
12382 if (vmx->nested.smm.guest_mode)
12383 nested_vmx_vmexit(vcpu, -1, 0, 0);
12384
12385 vmx->nested.smm.vmxon = vmx->nested.vmxon;
12386 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070012387 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020012388 return 0;
12389}
12390
12391static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
12392{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012393 struct vcpu_vmx *vmx = to_vmx(vcpu);
12394 int ret;
12395
12396 if (vmx->nested.smm.vmxon) {
12397 vmx->nested.vmxon = true;
12398 vmx->nested.smm.vmxon = false;
12399 }
12400
12401 if (vmx->nested.smm.guest_mode) {
12402 vcpu->arch.hflags &= ~HF_SMM_MASK;
12403 ret = enter_vmx_non_root_mode(vcpu, false);
12404 vcpu->arch.hflags |= HF_SMM_MASK;
12405 if (ret)
12406 return ret;
12407
12408 vmx->nested.smm.guest_mode = false;
12409 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020012410 return 0;
12411}
12412
Ladi Prosekcc3d9672017-10-17 16:02:39 +020012413static int enable_smi_window(struct kvm_vcpu *vcpu)
12414{
12415 return 0;
12416}
12417
Kees Cook404f6aa2016-08-08 16:29:06 -070012418static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080012419 .cpu_has_kvm_support = cpu_has_kvm_support,
12420 .disabled_by_bios = vmx_disabled_by_bios,
12421 .hardware_setup = hardware_setup,
12422 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030012423 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012424 .hardware_enable = hardware_enable,
12425 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080012426 .cpu_has_accelerated_tpr = report_flexpriority,
Paolo Bonzini6d396b52015-04-01 14:25:33 +020012427 .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012428
Wanpeng Lib31c1142018-03-12 04:53:04 -070012429 .vm_init = vmx_vm_init,
12430
Avi Kivity6aa8b732006-12-10 02:21:36 -080012431 .vcpu_create = vmx_create_vcpu,
12432 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030012433 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012434
Avi Kivity04d2cc72007-09-10 18:10:54 +030012435 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012436 .vcpu_load = vmx_vcpu_load,
12437 .vcpu_put = vmx_vcpu_put,
12438
Paolo Bonzinia96036b2015-11-10 11:55:36 +010012439 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060012440 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012441 .get_msr = vmx_get_msr,
12442 .set_msr = vmx_set_msr,
12443 .get_segment_base = vmx_get_segment_base,
12444 .get_segment = vmx_get_segment,
12445 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020012446 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012447 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020012448 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020012449 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030012450 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012451 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012452 .set_cr3 = vmx_set_cr3,
12453 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012454 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012455 .get_idt = vmx_get_idt,
12456 .set_idt = vmx_set_idt,
12457 .get_gdt = vmx_get_gdt,
12458 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010012459 .get_dr6 = vmx_get_dr6,
12460 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030012461 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010012462 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030012463 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012464 .get_rflags = vmx_get_rflags,
12465 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080012466
Avi Kivity6aa8b732006-12-10 02:21:36 -080012467 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012468
Avi Kivity6aa8b732006-12-10 02:21:36 -080012469 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020012470 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012471 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040012472 .set_interrupt_shadow = vmx_set_interrupt_shadow,
12473 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020012474 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030012475 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012476 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020012477 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030012478 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020012479 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012480 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010012481 .get_nmi_mask = vmx_get_nmi_mask,
12482 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012483 .enable_nmi_window = enable_nmi_window,
12484 .enable_irq_window = enable_irq_window,
12485 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +080012486 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080012487 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030012488 .get_enable_apicv = vmx_get_enable_apicv,
12489 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080012490 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010012491 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080012492 .hwapic_irr_update = vmx_hwapic_irr_update,
12493 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080012494 .sync_pir_to_irr = vmx_sync_pir_to_irr,
12495 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012496
Izik Eiduscbc94022007-10-25 00:29:55 +020012497 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080012498 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080012499 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030012500
Avi Kivity586f9602010-11-18 13:09:54 +020012501 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020012502
Sheng Yang17cc3932010-01-05 19:02:27 +080012503 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080012504
12505 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080012506
12507 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000012508 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020012509
12510 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080012511
12512 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100012513
12514 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020012515
12516 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012517
12518 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080012519 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000012520 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080012521 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020012522 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012523
12524 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012525
12526 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080012527
12528 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
12529 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
12530 .flush_log_dirty = vmx_flush_log_dirty,
12531 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040012532 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020012533
Feng Wubf9f6ac2015-09-18 22:29:55 +080012534 .pre_block = vmx_pre_block,
12535 .post_block = vmx_post_block,
12536
Wei Huang25462f72015-06-19 15:45:05 +020012537 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080012538
12539 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070012540
12541#ifdef CONFIG_X86_64
12542 .set_hv_timer = vmx_set_hv_timer,
12543 .cancel_hv_timer = vmx_cancel_hv_timer,
12544#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080012545
12546 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020012547
Ladi Prosek72d7b372017-10-11 16:54:41 +020012548 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020012549 .pre_enter_smm = vmx_pre_enter_smm,
12550 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020012551 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012552};
12553
12554static int __init vmx_init(void)
12555{
Tiejun Chen34a1cd62014-10-28 10:14:48 +080012556 int r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
12557 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030012558 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080012559 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080012560
Dave Young2965faa2015-09-09 15:38:55 -070012561#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080012562 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
12563 crash_vmclear_local_loaded_vmcss);
12564#endif
12565
He, Qingfdef3ad2007-04-30 09:45:24 +030012566 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080012567}
12568
12569static void __exit vmx_exit(void)
12570{
Dave Young2965faa2015-09-09 15:38:55 -070012571#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053012572 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080012573 synchronize_rcu();
12574#endif
12575
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080012576 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -080012577}
12578
12579module_init(vmx_init)
12580module_exit(vmx_exit)