blob: 95c05581a06fb4ff90e82df508b752953cf97953 [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 Kiszkaf4124502014-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>
David Woodhouse117cc7a2018-01-12 11:11:27 +000054#include <asm/nospec-branch.h>
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010055#include <asm/mshyperv.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"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010059#include "vmx_evmcs.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030060
Avi Kivity4ecac3f2008-05-13 13:23:38 +030061#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040062#define __ex_clear(x, reg) \
63 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030064
Avi Kivity6aa8b732006-12-10 02:21:36 -080065MODULE_AUTHOR("Qumranet");
66MODULE_LICENSE("GPL");
67
Josh Triplette9bda3b2012-03-20 23:33:51 -070068static const struct x86_cpu_id vmx_cpu_id[] = {
69 X86_FEATURE_MATCH(X86_FEATURE_VMX),
70 {}
71};
72MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
73
Rusty Russell476bc002012-01-13 09:32:18 +103074static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020075module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080076
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010077static bool __read_mostly enable_vnmi = 1;
78module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
79
Rusty Russell476bc002012-01-13 09:32:18 +103080static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020081module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020082
Rusty Russell476bc002012-01-13 09:32:18 +103083static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020084module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080085
Rusty Russell476bc002012-01-13 09:32:18 +103086static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070087module_param_named(unrestricted_guest,
88 enable_unrestricted_guest, bool, S_IRUGO);
89
Xudong Hao83c3a332012-05-28 19:33:35 +080090static bool __read_mostly enable_ept_ad_bits = 1;
91module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
92
Avi Kivitya27685c2012-06-12 20:30:18 +030093static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020094module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030095
Rusty Russell476bc002012-01-13 09:32:18 +103096static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030097module_param(fasteoi, bool, S_IRUGO);
98
Yang Zhang5a717852013-04-11 19:25:16 +080099static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +0800100module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800101
Abel Gordonabc4fc52013-04-18 14:35:25 +0300102static bool __read_mostly enable_shadow_vmcs = 1;
103module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300104/*
105 * If nested=1, nested virtualization is supported, i.e., guests may use
106 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
107 * use VMX instructions.
108 */
Rusty Russell476bc002012-01-13 09:32:18 +1030109static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300110module_param(nested, bool, S_IRUGO);
111
Wanpeng Li20300092014-12-02 19:14:59 +0800112static u64 __read_mostly host_xss;
113
Kai Huang843e4332015-01-28 10:54:28 +0800114static bool __read_mostly enable_pml = 1;
115module_param_named(pml, enable_pml, bool, S_IRUGO);
116
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100117#define MSR_TYPE_R 1
118#define MSR_TYPE_W 2
119#define MSR_TYPE_RW 3
120
121#define MSR_BITMAP_MODE_X2APIC 1
122#define MSR_BITMAP_MODE_X2APIC_APICV 2
123#define MSR_BITMAP_MODE_LM 4
124
Haozhong Zhang64903d62015-10-20 15:39:09 +0800125#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
126
Yunhong Jiang64672c92016-06-13 14:19:59 -0700127/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
128static int __read_mostly cpu_preemption_timer_multi;
129static bool __read_mostly enable_preemption_timer = 1;
130#ifdef CONFIG_X86_64
131module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
132#endif
133
Gleb Natapov50378782013-02-04 16:00:28 +0200134#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
Sean Christopherson1706bd02018-03-05 12:04:38 -0800135#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
136#define KVM_VM_CR0_ALWAYS_ON \
137 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
138 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200139#define KVM_CR4_GUEST_OWNED_BITS \
140 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Yu Zhangfd8cb432017-08-24 20:27:56 +0800141 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200142
Sean Christopherson5dc1f042018-03-05 12:04:39 -0800143#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
Avi Kivitycdc0e242009-12-06 17:21:14 +0200144#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
145#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
146
Avi Kivity78ac8b42010-04-08 18:19:35 +0300147#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
148
Jan Kiszkaf4124502014-03-07 20:03:13 +0100149#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
150
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800151/*
Jan Dakinevich16c2aec2016-10-28 07:00:30 +0300152 * Hyper-V requires all of these, so mark them as supported even though
153 * they are just treated the same as all-context.
154 */
155#define VMX_VPID_EXTENT_SUPPORTED_MASK \
156 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
157 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
158 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
159 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
160
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800161/*
162 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
163 * ple_gap: upper bound on the amount of time between two successive
164 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500165 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800166 * ple_window: upper bound on the amount of time a guest is allowed to execute
167 * in a PAUSE loop. Tests indicate that most spinlocks are held for
168 * less than 2^12 cycles
169 * Time is measured based on a counter that runs at the same rate as the TSC,
170 * refer SDM volume 3b section 21.6.13 & 22.1.3.
171 */
Babu Mogerc8e88712018-03-16 16:37:24 -0400172static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200173
Babu Moger7fbc85a2018-03-16 16:37:22 -0400174static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
175module_param(ple_window, uint, 0444);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800176
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200177/* Default doubles per-vcpu window every exit. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400178static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400179module_param(ple_window_grow, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200180
181/* Default resets per-vcpu window every exit to ple_window. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400182static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400183module_param(ple_window_shrink, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200184
185/* Default is to compute the maximum so we can never overflow. */
Babu Moger7fbc85a2018-03-16 16:37:22 -0400186static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
187module_param(ple_window_max, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200188
Avi Kivity83287ea422012-09-16 15:10:57 +0300189extern const ulong vmx_return;
190
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700191struct kvm_vmx {
192 struct kvm kvm;
193
194 unsigned int tss_addr;
195 bool ept_identity_pagetable_done;
196 gpa_t ept_identity_map_addr;
197};
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
Jim Mattson8d860bb2018-05-09 16:56:05 -0400484 bool change_vmcs01_virtual_apic_mode;
485
Nadav Har'El644d7112011-05-25 23:12:35 +0300486 /* L2 must run next, and mustn't decide to exit to L1. */
487 bool nested_run_pending;
Jim Mattsonde3a0022017-11-27 17:22:25 -0600488
489 struct loaded_vmcs vmcs02;
490
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300491 /*
Jim Mattsonde3a0022017-11-27 17:22:25 -0600492 * Guest pages referred to in the vmcs02 with host-physical
493 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300494 */
495 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800496 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800497 struct page *pi_desc_page;
498 struct pi_desc *pi_desc;
499 bool pi_pending;
500 u16 posted_intr_nv;
Jan Kiszkaf4124502014-03-07 20:03:13 +0100501
502 struct hrtimer preemption_timer;
503 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200504
505 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
506 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800507
Wanpeng Li5c614b32015-10-13 09:18:36 -0700508 u16 vpid02;
509 u16 last_vpid;
510
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100511 struct nested_vmx_msrs msrs;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200512
513 /* SMM related state */
514 struct {
515 /* in VMX operation on SMM entry? */
516 bool vmxon;
517 /* in guest mode on SMM entry? */
518 bool guest_mode;
519 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300520};
521
Yang Zhang01e439b2013-04-11 19:25:12 +0800522#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800523#define POSTED_INTR_SN 1
524
Yang Zhang01e439b2013-04-11 19:25:12 +0800525/* Posted-Interrupt Descriptor */
526struct pi_desc {
527 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800528 union {
529 struct {
530 /* bit 256 - Outstanding Notification */
531 u16 on : 1,
532 /* bit 257 - Suppress Notification */
533 sn : 1,
534 /* bit 271:258 - Reserved */
535 rsvd_1 : 14;
536 /* bit 279:272 - Notification Vector */
537 u8 nv;
538 /* bit 287:280 - Reserved */
539 u8 rsvd_2;
540 /* bit 319:288 - Notification Destination */
541 u32 ndst;
542 };
543 u64 control;
544 };
545 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800546} __aligned(64);
547
Yang Zhanga20ed542013-04-11 19:25:15 +0800548static bool pi_test_and_set_on(struct pi_desc *pi_desc)
549{
550 return test_and_set_bit(POSTED_INTR_ON,
551 (unsigned long *)&pi_desc->control);
552}
553
554static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
555{
556 return test_and_clear_bit(POSTED_INTR_ON,
557 (unsigned long *)&pi_desc->control);
558}
559
560static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
561{
562 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
563}
564
Feng Wuebbfc762015-09-18 22:29:46 +0800565static inline void pi_clear_sn(struct pi_desc *pi_desc)
566{
567 return clear_bit(POSTED_INTR_SN,
568 (unsigned long *)&pi_desc->control);
569}
570
571static inline void pi_set_sn(struct pi_desc *pi_desc)
572{
573 return set_bit(POSTED_INTR_SN,
574 (unsigned long *)&pi_desc->control);
575}
576
Paolo Bonziniad361092016-09-20 16:15:05 +0200577static inline void pi_clear_on(struct pi_desc *pi_desc)
578{
579 clear_bit(POSTED_INTR_ON,
580 (unsigned long *)&pi_desc->control);
581}
582
Feng Wuebbfc762015-09-18 22:29:46 +0800583static inline int pi_test_on(struct pi_desc *pi_desc)
584{
585 return test_bit(POSTED_INTR_ON,
586 (unsigned long *)&pi_desc->control);
587}
588
589static inline int pi_test_sn(struct pi_desc *pi_desc)
590{
591 return test_bit(POSTED_INTR_SN,
592 (unsigned long *)&pi_desc->control);
593}
594
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400595struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000596 struct kvm_vcpu vcpu;
Avi Kivity313dbd492008-07-17 18:04:30 +0300597 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300598 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100599 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300600 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200601 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200602 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300603 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400604 int nmsrs;
605 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800606 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400607#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300608 u64 msr_host_kernel_gs_base;
609 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400610#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100611
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100612 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100613 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100614
Gleb Natapov2961e8762013-11-25 15:37:13 +0200615 u32 vm_entry_controls_shadow;
616 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200617 u32 secondary_exec_control;
618
Nadav Har'Eld462b812011-05-24 15:26:10 +0300619 /*
620 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
621 * non-nested (L1) guest, it always points to vmcs01. For a nested
622 * guest (L2), it points to a different VMCS.
623 */
624 struct loaded_vmcs vmcs01;
625 struct loaded_vmcs *loaded_vmcs;
626 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300627 struct msr_autoload {
628 unsigned nr;
629 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
630 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
631 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400632 struct {
633 int loaded;
634 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300635#ifdef CONFIG_X86_64
636 u16 ds_sel, es_sel;
637#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200638 int gs_ldt_reload_needed;
639 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000640 u64 msr_host_bndcfgs;
Mike Dayd77c26f2007-10-08 09:02:08 -0400641 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200642 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300643 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300644 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300645 struct kvm_segment segs[8];
646 } rmode;
647 struct {
648 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300649 struct kvm_save_segment {
650 u16 selector;
651 unsigned long base;
652 u32 limit;
653 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300654 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300655 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800656 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300657 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200658
Andi Kleena0861c02009-06-08 17:37:09 +0800659 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800660
Yang Zhang01e439b2013-04-11 19:25:12 +0800661 /* Posted interrupt descriptor */
662 struct pi_desc pi_desc;
663
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300664 /* Support for a guest hypervisor (nested VMX) */
665 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200666
667 /* Dynamic PLE window. */
668 int ple_window;
669 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800670
671 /* Support for PML */
672#define PML_ENTITY_NUM 512
673 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800674
Yunhong Jiang64672c92016-06-13 14:19:59 -0700675 /* apic deadline value in host tsc */
676 u64 hv_deadline_tsc;
677
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800678 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800679
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800680 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800681
Wanpeng Li74c55932017-11-29 01:31:20 -0800682 unsigned long host_debugctlmsr;
683
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800684 /*
685 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
686 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
687 * in msr_ia32_feature_control_valid_bits.
688 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800689 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800690 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400691};
692
Avi Kivity2fb92db2011-04-27 19:42:18 +0300693enum segment_cache_field {
694 SEG_FIELD_SEL = 0,
695 SEG_FIELD_BASE = 1,
696 SEG_FIELD_LIMIT = 2,
697 SEG_FIELD_AR = 3,
698
699 SEG_FIELD_NR = 4
700};
701
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700702static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
703{
704 return container_of(kvm, struct kvm_vmx, kvm);
705}
706
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400707static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
708{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000709 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400710}
711
Feng Wuefc64402015-09-18 22:29:51 +0800712static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
713{
714 return &(to_vmx(vcpu)->pi_desc);
715}
716
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800717#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +0300718#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800719#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
720#define FIELD64(number, name) \
721 FIELD(number, name), \
722 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +0300723
Abel Gordon4607c2d2013-04-18 14:35:55 +0300724
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100725static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100726#define SHADOW_FIELD_RO(x) x,
727#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300728};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400729static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300730 ARRAY_SIZE(shadow_read_only_fields);
731
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100732static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100733#define SHADOW_FIELD_RW(x) x,
734#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300735};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400736static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300737 ARRAY_SIZE(shadow_read_write_fields);
738
Mathias Krause772e0312012-08-30 01:30:19 +0200739static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300740 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800741 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300742 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
743 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
744 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
745 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
746 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
747 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
748 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
749 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800750 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -0400751 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300752 FIELD(HOST_ES_SELECTOR, host_es_selector),
753 FIELD(HOST_CS_SELECTOR, host_cs_selector),
754 FIELD(HOST_SS_SELECTOR, host_ss_selector),
755 FIELD(HOST_DS_SELECTOR, host_ds_selector),
756 FIELD(HOST_FS_SELECTOR, host_fs_selector),
757 FIELD(HOST_GS_SELECTOR, host_gs_selector),
758 FIELD(HOST_TR_SELECTOR, host_tr_selector),
759 FIELD64(IO_BITMAP_A, io_bitmap_a),
760 FIELD64(IO_BITMAP_B, io_bitmap_b),
761 FIELD64(MSR_BITMAP, msr_bitmap),
762 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
763 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
764 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
765 FIELD64(TSC_OFFSET, tsc_offset),
766 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
767 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800768 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -0400769 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300770 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800771 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
772 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
773 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
774 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -0400775 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800776 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300777 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
778 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
Bandan Dasc5f983f2017-05-05 15:25:14 -0400779 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300780 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
781 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
782 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
783 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
784 FIELD64(GUEST_PDPTR0, guest_pdptr0),
785 FIELD64(GUEST_PDPTR1, guest_pdptr1),
786 FIELD64(GUEST_PDPTR2, guest_pdptr2),
787 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100788 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300789 FIELD64(HOST_IA32_PAT, host_ia32_pat),
790 FIELD64(HOST_IA32_EFER, host_ia32_efer),
791 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
792 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
793 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
794 FIELD(EXCEPTION_BITMAP, exception_bitmap),
795 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
796 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
797 FIELD(CR3_TARGET_COUNT, cr3_target_count),
798 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
799 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
800 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
801 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
802 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
803 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
804 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
805 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
806 FIELD(TPR_THRESHOLD, tpr_threshold),
807 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
808 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
809 FIELD(VM_EXIT_REASON, vm_exit_reason),
810 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
811 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
812 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
813 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
814 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
815 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
816 FIELD(GUEST_ES_LIMIT, guest_es_limit),
817 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
818 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
819 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
820 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
821 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
822 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
823 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
824 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
825 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
826 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
827 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
828 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
829 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
830 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
831 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
832 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
833 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
834 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
835 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
836 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
837 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +0100838 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300839 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
840 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
841 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
842 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
843 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
844 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
845 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
846 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
847 FIELD(EXIT_QUALIFICATION, exit_qualification),
848 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
849 FIELD(GUEST_CR0, guest_cr0),
850 FIELD(GUEST_CR3, guest_cr3),
851 FIELD(GUEST_CR4, guest_cr4),
852 FIELD(GUEST_ES_BASE, guest_es_base),
853 FIELD(GUEST_CS_BASE, guest_cs_base),
854 FIELD(GUEST_SS_BASE, guest_ss_base),
855 FIELD(GUEST_DS_BASE, guest_ds_base),
856 FIELD(GUEST_FS_BASE, guest_fs_base),
857 FIELD(GUEST_GS_BASE, guest_gs_base),
858 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
859 FIELD(GUEST_TR_BASE, guest_tr_base),
860 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
861 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
862 FIELD(GUEST_DR7, guest_dr7),
863 FIELD(GUEST_RSP, guest_rsp),
864 FIELD(GUEST_RIP, guest_rip),
865 FIELD(GUEST_RFLAGS, guest_rflags),
866 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
867 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
868 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
869 FIELD(HOST_CR0, host_cr0),
870 FIELD(HOST_CR3, host_cr3),
871 FIELD(HOST_CR4, host_cr4),
872 FIELD(HOST_FS_BASE, host_fs_base),
873 FIELD(HOST_GS_BASE, host_gs_base),
874 FIELD(HOST_TR_BASE, host_tr_base),
875 FIELD(HOST_GDTR_BASE, host_gdtr_base),
876 FIELD(HOST_IDTR_BASE, host_idtr_base),
877 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
878 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
879 FIELD(HOST_RSP, host_rsp),
880 FIELD(HOST_RIP, host_rip),
881};
Nadav Har'El22bd0352011-05-25 23:05:57 +0300882
883static inline short vmcs_field_to_offset(unsigned long field)
884{
Dan Williams085331d2018-01-31 17:47:03 -0800885 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
886 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800887 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +0100888
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800889 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -0800890 return -ENOENT;
891
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800892 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -0800893 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -0800894 return -ENOENT;
895
Linus Torvalds15303ba2018-02-10 13:16:35 -0800896 index = array_index_nospec(index, size);
897 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -0800898 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +0100899 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -0800900 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300901}
902
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300903static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
904{
David Matlack4f2777b2016-07-13 17:16:37 -0700905 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300906}
907
Peter Feiner995f00a2017-06-30 17:26:32 -0700908static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +0300909static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -0700910static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +0800911static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +0300912static void vmx_set_segment(struct kvm_vcpu *vcpu,
913 struct kvm_segment *var, int seg);
914static void vmx_get_segment(struct kvm_vcpu *vcpu,
915 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +0200916static bool guest_state_valid(struct kvm_vcpu *vcpu);
917static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +0300918static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +0200919static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
920static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
921static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
922 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100923static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +0100924static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
925 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +0300926
Avi Kivity6aa8b732006-12-10 02:21:36 -0800927static DEFINE_PER_CPU(struct vmcs *, vmxarea);
928static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300929/*
930 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
931 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
932 */
933static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934
Feng Wubf9f6ac2015-09-18 22:29:55 +0800935/*
936 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
937 * can find which vCPU should be waken up.
938 */
939static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
940static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
941
Radim Krčmář23611332016-09-29 22:41:33 +0200942enum {
Radim Krčmář23611332016-09-29 22:41:33 +0200943 VMX_VMREAD_BITMAP,
944 VMX_VMWRITE_BITMAP,
945 VMX_BITMAP_NR
946};
947
948static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
949
Radim Krčmář23611332016-09-29 22:41:33 +0200950#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
951#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +0300952
Avi Kivity110312c2010-12-21 12:54:20 +0200953static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200954static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +0200955
Sheng Yang2384d2b2008-01-17 15:14:33 +0800956static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
957static DEFINE_SPINLOCK(vmx_vpid_lock);
958
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300959static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800960 int size;
961 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +0300962 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800963 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300964 u32 pin_based_exec_ctrl;
965 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800966 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300967 u32 vmexit_ctrl;
968 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +0100969 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300970} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800971
Hannes Ederefff9e52008-11-28 17:02:06 +0100972static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800973 u32 ept;
974 u32 vpid;
975} vmx_capability;
976
Avi Kivity6aa8b732006-12-10 02:21:36 -0800977#define VMX_SEGMENT_FIELD(seg) \
978 [VCPU_SREG_##seg] = { \
979 .selector = GUEST_##seg##_SELECTOR, \
980 .base = GUEST_##seg##_BASE, \
981 .limit = GUEST_##seg##_LIMIT, \
982 .ar_bytes = GUEST_##seg##_AR_BYTES, \
983 }
984
Mathias Krause772e0312012-08-30 01:30:19 +0200985static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800986 unsigned selector;
987 unsigned base;
988 unsigned limit;
989 unsigned ar_bytes;
990} kvm_vmx_segment_fields[] = {
991 VMX_SEGMENT_FIELD(CS),
992 VMX_SEGMENT_FIELD(DS),
993 VMX_SEGMENT_FIELD(ES),
994 VMX_SEGMENT_FIELD(FS),
995 VMX_SEGMENT_FIELD(GS),
996 VMX_SEGMENT_FIELD(SS),
997 VMX_SEGMENT_FIELD(TR),
998 VMX_SEGMENT_FIELD(LDTR),
999};
1000
Avi Kivity26bb0982009-09-07 11:14:12 +03001001static u64 host_efer;
1002
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001003static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1004
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001005/*
Brian Gerst8c065852010-07-17 09:03:26 -04001006 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001007 * away by decrementing the array size.
1008 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001009static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001010#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001011 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001012#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001013 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001014};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001015
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001016DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1017
1018#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1019
1020#define KVM_EVMCS_VERSION 1
1021
1022#if IS_ENABLED(CONFIG_HYPERV)
1023static bool __read_mostly enlightened_vmcs = true;
1024module_param(enlightened_vmcs, bool, 0444);
1025
1026static inline void evmcs_write64(unsigned long field, u64 value)
1027{
1028 u16 clean_field;
1029 int offset = get_evmcs_offset(field, &clean_field);
1030
1031 if (offset < 0)
1032 return;
1033
1034 *(u64 *)((char *)current_evmcs + offset) = value;
1035
1036 current_evmcs->hv_clean_fields &= ~clean_field;
1037}
1038
1039static inline void evmcs_write32(unsigned long field, u32 value)
1040{
1041 u16 clean_field;
1042 int offset = get_evmcs_offset(field, &clean_field);
1043
1044 if (offset < 0)
1045 return;
1046
1047 *(u32 *)((char *)current_evmcs + offset) = value;
1048 current_evmcs->hv_clean_fields &= ~clean_field;
1049}
1050
1051static inline void evmcs_write16(unsigned long field, u16 value)
1052{
1053 u16 clean_field;
1054 int offset = get_evmcs_offset(field, &clean_field);
1055
1056 if (offset < 0)
1057 return;
1058
1059 *(u16 *)((char *)current_evmcs + offset) = value;
1060 current_evmcs->hv_clean_fields &= ~clean_field;
1061}
1062
1063static inline u64 evmcs_read64(unsigned long field)
1064{
1065 int offset = get_evmcs_offset(field, NULL);
1066
1067 if (offset < 0)
1068 return 0;
1069
1070 return *(u64 *)((char *)current_evmcs + offset);
1071}
1072
1073static inline u32 evmcs_read32(unsigned long field)
1074{
1075 int offset = get_evmcs_offset(field, NULL);
1076
1077 if (offset < 0)
1078 return 0;
1079
1080 return *(u32 *)((char *)current_evmcs + offset);
1081}
1082
1083static inline u16 evmcs_read16(unsigned long field)
1084{
1085 int offset = get_evmcs_offset(field, NULL);
1086
1087 if (offset < 0)
1088 return 0;
1089
1090 return *(u16 *)((char *)current_evmcs + offset);
1091}
1092
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001093static inline void evmcs_touch_msr_bitmap(void)
1094{
1095 if (unlikely(!current_evmcs))
1096 return;
1097
1098 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1099 current_evmcs->hv_clean_fields &=
1100 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1101}
1102
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001103static void evmcs_load(u64 phys_addr)
1104{
1105 struct hv_vp_assist_page *vp_ap =
1106 hv_get_vp_assist_page(smp_processor_id());
1107
1108 vp_ap->current_nested_vmcs = phys_addr;
1109 vp_ap->enlighten_vmentry = 1;
1110}
1111
1112static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1113{
1114 /*
1115 * Enlightened VMCSv1 doesn't support these:
1116 *
1117 * POSTED_INTR_NV = 0x00000002,
1118 * GUEST_INTR_STATUS = 0x00000810,
1119 * APIC_ACCESS_ADDR = 0x00002014,
1120 * POSTED_INTR_DESC_ADDR = 0x00002016,
1121 * EOI_EXIT_BITMAP0 = 0x0000201c,
1122 * EOI_EXIT_BITMAP1 = 0x0000201e,
1123 * EOI_EXIT_BITMAP2 = 0x00002020,
1124 * EOI_EXIT_BITMAP3 = 0x00002022,
1125 */
1126 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
1127 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1128 ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1129 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1130 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1131 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1132 ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
1133
1134 /*
1135 * GUEST_PML_INDEX = 0x00000812,
1136 * PML_ADDRESS = 0x0000200e,
1137 */
1138 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
1139
1140 /* VM_FUNCTION_CONTROL = 0x00002018, */
1141 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
1142
1143 /*
1144 * EPTP_LIST_ADDRESS = 0x00002024,
1145 * VMREAD_BITMAP = 0x00002026,
1146 * VMWRITE_BITMAP = 0x00002028,
1147 */
1148 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
1149
1150 /*
1151 * TSC_MULTIPLIER = 0x00002032,
1152 */
1153 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
1154
1155 /*
1156 * PLE_GAP = 0x00004020,
1157 * PLE_WINDOW = 0x00004022,
1158 */
1159 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1160
1161 /*
1162 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1163 */
1164 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1165
1166 /*
1167 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1168 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1169 */
1170 vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
1171 vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
1172
1173 /*
1174 * Currently unsupported in KVM:
1175 * GUEST_IA32_RTIT_CTL = 0x00002814,
1176 */
1177}
1178#else /* !IS_ENABLED(CONFIG_HYPERV) */
1179static inline void evmcs_write64(unsigned long field, u64 value) {}
1180static inline void evmcs_write32(unsigned long field, u32 value) {}
1181static inline void evmcs_write16(unsigned long field, u16 value) {}
1182static inline u64 evmcs_read64(unsigned long field) { return 0; }
1183static inline u32 evmcs_read32(unsigned long field) { return 0; }
1184static inline u16 evmcs_read16(unsigned long field) { return 0; }
1185static inline void evmcs_load(u64 phys_addr) {}
1186static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001187static inline void evmcs_touch_msr_bitmap(void) {}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001188#endif /* IS_ENABLED(CONFIG_HYPERV) */
1189
Jan Kiszka5bb16012016-02-09 20:14:21 +01001190static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001191{
1192 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1193 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001194 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1195}
1196
Jan Kiszka6f054852016-02-09 20:15:18 +01001197static inline bool is_debug(u32 intr_info)
1198{
1199 return is_exception_n(intr_info, DB_VECTOR);
1200}
1201
1202static inline bool is_breakpoint(u32 intr_info)
1203{
1204 return is_exception_n(intr_info, BP_VECTOR);
1205}
1206
Jan Kiszka5bb16012016-02-09 20:14:21 +01001207static inline bool is_page_fault(u32 intr_info)
1208{
1209 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001210}
1211
Gui Jianfeng31299942010-03-15 17:29:09 +08001212static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001213{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001214 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001215}
1216
Gui Jianfeng31299942010-03-15 17:29:09 +08001217static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001218{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001219 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001220}
1221
Liran Alon9e869482018-03-12 13:12:51 +02001222static inline bool is_gp_fault(u32 intr_info)
1223{
1224 return is_exception_n(intr_info, GP_VECTOR);
1225}
1226
Gui Jianfeng31299942010-03-15 17:29:09 +08001227static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001228{
1229 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1230 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1231}
1232
Gui Jianfeng31299942010-03-15 17:29:09 +08001233static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001234{
1235 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1236 INTR_INFO_VALID_MASK)) ==
1237 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1238}
1239
Linus Torvalds32d43cd2018-03-20 12:16:59 -07001240/* Undocumented: icebp/int1 */
1241static inline bool is_icebp(u32 intr_info)
1242{
1243 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1244 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1245}
1246
Gui Jianfeng31299942010-03-15 17:29:09 +08001247static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001248{
Sheng Yang04547152009-04-01 15:52:31 +08001249 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001250}
1251
Gui Jianfeng31299942010-03-15 17:29:09 +08001252static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001253{
Sheng Yang04547152009-04-01 15:52:31 +08001254 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001255}
1256
Paolo Bonzini35754c92015-07-29 12:05:37 +02001257static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001258{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001259 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001260}
1261
Gui Jianfeng31299942010-03-15 17:29:09 +08001262static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001263{
Sheng Yang04547152009-04-01 15:52:31 +08001264 return vmcs_config.cpu_based_exec_ctrl &
1265 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001266}
1267
Avi Kivity774ead32007-12-26 13:57:04 +02001268static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001269{
Sheng Yang04547152009-04-01 15:52:31 +08001270 return vmcs_config.cpu_based_2nd_exec_ctrl &
1271 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1272}
1273
Yang Zhang8d146952013-01-25 10:18:50 +08001274static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1275{
1276 return vmcs_config.cpu_based_2nd_exec_ctrl &
1277 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1278}
1279
Yang Zhang83d4c282013-01-25 10:18:49 +08001280static inline bool cpu_has_vmx_apic_register_virt(void)
1281{
1282 return vmcs_config.cpu_based_2nd_exec_ctrl &
1283 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1284}
1285
Yang Zhangc7c9c562013-01-25 10:18:51 +08001286static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1287{
1288 return vmcs_config.cpu_based_2nd_exec_ctrl &
1289 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1290}
1291
Yunhong Jiang64672c92016-06-13 14:19:59 -07001292/*
1293 * Comment's format: document - errata name - stepping - processor name.
1294 * Refer from
1295 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1296 */
1297static u32 vmx_preemption_cpu_tfms[] = {
1298/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
12990x000206E6,
1300/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1301/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1302/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
13030x00020652,
1304/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
13050x00020655,
1306/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1307/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1308/*
1309 * 320767.pdf - AAP86 - B1 -
1310 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1311 */
13120x000106E5,
1313/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
13140x000106A0,
1315/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
13160x000106A1,
1317/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
13180x000106A4,
1319 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1320 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1321 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
13220x000106A5,
1323};
1324
1325static inline bool cpu_has_broken_vmx_preemption_timer(void)
1326{
1327 u32 eax = cpuid_eax(0x00000001), i;
1328
1329 /* Clear the reserved bits */
1330 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001331 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001332 if (eax == vmx_preemption_cpu_tfms[i])
1333 return true;
1334
1335 return false;
1336}
1337
1338static inline bool cpu_has_vmx_preemption_timer(void)
1339{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001340 return vmcs_config.pin_based_exec_ctrl &
1341 PIN_BASED_VMX_PREEMPTION_TIMER;
1342}
1343
Yang Zhang01e439b2013-04-11 19:25:12 +08001344static inline bool cpu_has_vmx_posted_intr(void)
1345{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001346 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1347 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001348}
1349
1350static inline bool cpu_has_vmx_apicv(void)
1351{
1352 return cpu_has_vmx_apic_register_virt() &&
1353 cpu_has_vmx_virtual_intr_delivery() &&
1354 cpu_has_vmx_posted_intr();
1355}
1356
Sheng Yang04547152009-04-01 15:52:31 +08001357static inline bool cpu_has_vmx_flexpriority(void)
1358{
1359 return cpu_has_vmx_tpr_shadow() &&
1360 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001361}
1362
Marcelo Tosattie7997942009-06-11 12:07:40 -03001363static inline bool cpu_has_vmx_ept_execute_only(void)
1364{
Gui Jianfeng31299942010-03-15 17:29:09 +08001365 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001366}
1367
Marcelo Tosattie7997942009-06-11 12:07:40 -03001368static inline bool cpu_has_vmx_ept_2m_page(void)
1369{
Gui Jianfeng31299942010-03-15 17:29:09 +08001370 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001371}
1372
Sheng Yang878403b2010-01-05 19:02:29 +08001373static inline bool cpu_has_vmx_ept_1g_page(void)
1374{
Gui Jianfeng31299942010-03-15 17:29:09 +08001375 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001376}
1377
Sheng Yang4bc9b982010-06-02 14:05:24 +08001378static inline bool cpu_has_vmx_ept_4levels(void)
1379{
1380 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1381}
1382
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001383static inline bool cpu_has_vmx_ept_mt_wb(void)
1384{
1385 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1386}
1387
Yu Zhang855feb62017-08-24 20:27:55 +08001388static inline bool cpu_has_vmx_ept_5levels(void)
1389{
1390 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1391}
1392
Xudong Hao83c3a332012-05-28 19:33:35 +08001393static inline bool cpu_has_vmx_ept_ad_bits(void)
1394{
1395 return vmx_capability.ept & VMX_EPT_AD_BIT;
1396}
1397
Gui Jianfeng31299942010-03-15 17:29:09 +08001398static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001399{
Gui Jianfeng31299942010-03-15 17:29:09 +08001400 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001401}
1402
Gui Jianfeng31299942010-03-15 17:29:09 +08001403static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001404{
Gui Jianfeng31299942010-03-15 17:29:09 +08001405 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001406}
1407
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001408static inline bool cpu_has_vmx_invvpid_single(void)
1409{
1410 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1411}
1412
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001413static inline bool cpu_has_vmx_invvpid_global(void)
1414{
1415 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1416}
1417
Wanpeng Li08d839c2017-03-23 05:30:08 -07001418static inline bool cpu_has_vmx_invvpid(void)
1419{
1420 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1421}
1422
Gui Jianfeng31299942010-03-15 17:29:09 +08001423static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001424{
Sheng Yang04547152009-04-01 15:52:31 +08001425 return vmcs_config.cpu_based_2nd_exec_ctrl &
1426 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001427}
1428
Gui Jianfeng31299942010-03-15 17:29:09 +08001429static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001430{
1431 return vmcs_config.cpu_based_2nd_exec_ctrl &
1432 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1433}
1434
Gui Jianfeng31299942010-03-15 17:29:09 +08001435static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001436{
1437 return vmcs_config.cpu_based_2nd_exec_ctrl &
1438 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1439}
1440
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001441static inline bool cpu_has_vmx_basic_inout(void)
1442{
1443 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1444}
1445
Paolo Bonzini35754c92015-07-29 12:05:37 +02001446static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001447{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001448 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001449}
1450
Gui Jianfeng31299942010-03-15 17:29:09 +08001451static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001452{
Sheng Yang04547152009-04-01 15:52:31 +08001453 return vmcs_config.cpu_based_2nd_exec_ctrl &
1454 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001455}
1456
Gui Jianfeng31299942010-03-15 17:29:09 +08001457static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001458{
1459 return vmcs_config.cpu_based_2nd_exec_ctrl &
1460 SECONDARY_EXEC_RDTSCP;
1461}
1462
Mao, Junjiead756a12012-07-02 01:18:48 +00001463static inline bool cpu_has_vmx_invpcid(void)
1464{
1465 return vmcs_config.cpu_based_2nd_exec_ctrl &
1466 SECONDARY_EXEC_ENABLE_INVPCID;
1467}
1468
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001469static inline bool cpu_has_virtual_nmis(void)
1470{
1471 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1472}
1473
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001474static inline bool cpu_has_vmx_wbinvd_exit(void)
1475{
1476 return vmcs_config.cpu_based_2nd_exec_ctrl &
1477 SECONDARY_EXEC_WBINVD_EXITING;
1478}
1479
Abel Gordonabc4fc52013-04-18 14:35:25 +03001480static inline bool cpu_has_vmx_shadow_vmcs(void)
1481{
1482 u64 vmx_msr;
1483 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1484 /* check if the cpu supports writing r/o exit information fields */
1485 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1486 return false;
1487
1488 return vmcs_config.cpu_based_2nd_exec_ctrl &
1489 SECONDARY_EXEC_SHADOW_VMCS;
1490}
1491
Kai Huang843e4332015-01-28 10:54:28 +08001492static inline bool cpu_has_vmx_pml(void)
1493{
1494 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1495}
1496
Haozhong Zhang64903d62015-10-20 15:39:09 +08001497static inline bool cpu_has_vmx_tsc_scaling(void)
1498{
1499 return vmcs_config.cpu_based_2nd_exec_ctrl &
1500 SECONDARY_EXEC_TSC_SCALING;
1501}
1502
Bandan Das2a499e42017-08-03 15:54:41 -04001503static inline bool cpu_has_vmx_vmfunc(void)
1504{
1505 return vmcs_config.cpu_based_2nd_exec_ctrl &
1506 SECONDARY_EXEC_ENABLE_VMFUNC;
1507}
1508
Sean Christopherson64f7a112018-04-30 10:01:06 -07001509static bool vmx_umip_emulated(void)
1510{
1511 return vmcs_config.cpu_based_2nd_exec_ctrl &
1512 SECONDARY_EXEC_DESC;
1513}
1514
Sheng Yang04547152009-04-01 15:52:31 +08001515static inline bool report_flexpriority(void)
1516{
1517 return flexpriority_enabled;
1518}
1519
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001520static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1521{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001522 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001523}
1524
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001525static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1526{
1527 return vmcs12->cpu_based_vm_exec_control & bit;
1528}
1529
1530static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1531{
1532 return (vmcs12->cpu_based_vm_exec_control &
1533 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1534 (vmcs12->secondary_vm_exec_control & bit);
1535}
1536
Jan Kiszkaf4124502014-03-07 20:03:13 +01001537static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1538{
1539 return vmcs12->pin_based_vm_exec_control &
1540 PIN_BASED_VMX_PREEMPTION_TIMER;
1541}
1542
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001543static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1544{
1545 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1546}
1547
1548static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1549{
1550 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1551}
1552
Nadav Har'El155a97a2013-08-05 11:07:16 +03001553static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1554{
1555 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1556}
1557
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001558static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1559{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001560 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001561}
1562
Bandan Dasc5f983f2017-05-05 15:25:14 -04001563static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1564{
1565 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1566}
1567
Wincy Vanf2b93282015-02-03 23:56:03 +08001568static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1569{
1570 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1571}
1572
Wanpeng Li5c614b32015-10-13 09:18:36 -07001573static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1574{
1575 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1576}
1577
Wincy Van82f0dd42015-02-03 23:57:18 +08001578static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1579{
1580 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1581}
1582
Wincy Van608406e2015-02-03 23:57:51 +08001583static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1584{
1585 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1586}
1587
Wincy Van705699a2015-02-03 23:58:17 +08001588static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1589{
1590 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1591}
1592
Bandan Das27c42a12017-08-03 15:54:42 -04001593static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1594{
1595 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1596}
1597
Bandan Das41ab9372017-08-03 15:54:43 -04001598static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1599{
1600 return nested_cpu_has_vmfunc(vmcs12) &&
1601 (vmcs12->vm_function_control &
1602 VMX_VMFUNC_EPTP_SWITCHING);
1603}
1604
Jim Mattsonef85b672016-12-12 11:01:37 -08001605static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001606{
1607 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08001608 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001609}
1610
Jan Kiszka533558b2014-01-04 18:47:20 +01001611static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1612 u32 exit_intr_info,
1613 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001614static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1615 struct vmcs12 *vmcs12,
1616 u32 reason, unsigned long qualification);
1617
Rusty Russell8b9cf982007-07-30 16:31:43 +10001618static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001619{
1620 int i;
1621
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001622 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001623 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001624 return i;
1625 return -1;
1626}
1627
Sheng Yang2384d2b2008-01-17 15:14:33 +08001628static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1629{
1630 struct {
1631 u64 vpid : 16;
1632 u64 rsvd : 48;
1633 u64 gva;
1634 } operand = { vpid, 0, gva };
1635
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001636 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001637 /* CF==1 or ZF==1 --> rc = -1 */
1638 "; ja 1f ; ud2 ; 1:"
1639 : : "a"(&operand), "c"(ext) : "cc", "memory");
1640}
1641
Sheng Yang14394422008-04-28 12:24:45 +08001642static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1643{
1644 struct {
1645 u64 eptp, gpa;
1646 } operand = {eptp, gpa};
1647
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001648 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001649 /* CF==1 or ZF==1 --> rc = -1 */
1650 "; ja 1f ; ud2 ; 1:\n"
1651 : : "a" (&operand), "c" (ext) : "cc", "memory");
1652}
1653
Avi Kivity26bb0982009-09-07 11:14:12 +03001654static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001655{
1656 int i;
1657
Rusty Russell8b9cf982007-07-30 16:31:43 +10001658 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001659 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001660 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001661 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001662}
1663
Avi Kivity6aa8b732006-12-10 02:21:36 -08001664static void vmcs_clear(struct vmcs *vmcs)
1665{
1666 u64 phys_addr = __pa(vmcs);
1667 u8 error;
1668
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001669 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001670 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001671 : "cc", "memory");
1672 if (error)
1673 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1674 vmcs, phys_addr);
1675}
1676
Nadav Har'Eld462b812011-05-24 15:26:10 +03001677static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1678{
1679 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001680 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1681 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001682 loaded_vmcs->cpu = -1;
1683 loaded_vmcs->launched = 0;
1684}
1685
Dongxiao Xu7725b892010-05-11 18:29:38 +08001686static void vmcs_load(struct vmcs *vmcs)
1687{
1688 u64 phys_addr = __pa(vmcs);
1689 u8 error;
1690
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001691 if (static_branch_unlikely(&enable_evmcs))
1692 return evmcs_load(phys_addr);
1693
Dongxiao Xu7725b892010-05-11 18:29:38 +08001694 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001695 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001696 : "cc", "memory");
1697 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001698 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001699 vmcs, phys_addr);
1700}
1701
Dave Young2965faa2015-09-09 15:38:55 -07001702#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001703/*
1704 * This bitmap is used to indicate whether the vmclear
1705 * operation is enabled on all cpus. All disabled by
1706 * default.
1707 */
1708static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1709
1710static inline void crash_enable_local_vmclear(int cpu)
1711{
1712 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1713}
1714
1715static inline void crash_disable_local_vmclear(int cpu)
1716{
1717 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1718}
1719
1720static inline int crash_local_vmclear_enabled(int cpu)
1721{
1722 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1723}
1724
1725static void crash_vmclear_local_loaded_vmcss(void)
1726{
1727 int cpu = raw_smp_processor_id();
1728 struct loaded_vmcs *v;
1729
1730 if (!crash_local_vmclear_enabled(cpu))
1731 return;
1732
1733 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1734 loaded_vmcss_on_cpu_link)
1735 vmcs_clear(v->vmcs);
1736}
1737#else
1738static inline void crash_enable_local_vmclear(int cpu) { }
1739static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001740#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001741
Nadav Har'Eld462b812011-05-24 15:26:10 +03001742static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001743{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001744 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001745 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001746
Nadav Har'Eld462b812011-05-24 15:26:10 +03001747 if (loaded_vmcs->cpu != cpu)
1748 return; /* vcpu migration can race with cpu offline */
1749 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001750 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001751 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001752 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001753
1754 /*
1755 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1756 * is before setting loaded_vmcs->vcpu to -1 which is done in
1757 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1758 * then adds the vmcs into percpu list before it is deleted.
1759 */
1760 smp_wmb();
1761
Nadav Har'Eld462b812011-05-24 15:26:10 +03001762 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001763 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001764}
1765
Nadav Har'Eld462b812011-05-24 15:26:10 +03001766static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001767{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001768 int cpu = loaded_vmcs->cpu;
1769
1770 if (cpu != -1)
1771 smp_call_function_single(cpu,
1772 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001773}
1774
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001775static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001776{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001777 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001778 return;
1779
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001780 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001781 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001782}
1783
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001784static inline void vpid_sync_vcpu_global(void)
1785{
1786 if (cpu_has_vmx_invvpid_global())
1787 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1788}
1789
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001790static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001791{
1792 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001793 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001794 else
1795 vpid_sync_vcpu_global();
1796}
1797
Sheng Yang14394422008-04-28 12:24:45 +08001798static inline void ept_sync_global(void)
1799{
David Hildenbrandf5f51582017-08-24 20:51:30 +02001800 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08001801}
1802
1803static inline void ept_sync_context(u64 eptp)
1804{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02001805 if (cpu_has_vmx_invept_context())
1806 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1807 else
1808 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08001809}
1810
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001811static __always_inline void vmcs_check16(unsigned long field)
1812{
1813 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1814 "16-bit accessor invalid for 64-bit field");
1815 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1816 "16-bit accessor invalid for 64-bit high field");
1817 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1818 "16-bit accessor invalid for 32-bit high field");
1819 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1820 "16-bit accessor invalid for natural width field");
1821}
1822
1823static __always_inline void vmcs_check32(unsigned long field)
1824{
1825 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1826 "32-bit accessor invalid for 16-bit field");
1827 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1828 "32-bit accessor invalid for natural width field");
1829}
1830
1831static __always_inline void vmcs_check64(unsigned long field)
1832{
1833 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1834 "64-bit accessor invalid for 16-bit field");
1835 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1836 "64-bit accessor invalid for 64-bit high field");
1837 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1838 "64-bit accessor invalid for 32-bit field");
1839 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1840 "64-bit accessor invalid for natural width field");
1841}
1842
1843static __always_inline void vmcs_checkl(unsigned long field)
1844{
1845 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1846 "Natural width accessor invalid for 16-bit field");
1847 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1848 "Natural width accessor invalid for 64-bit field");
1849 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1850 "Natural width accessor invalid for 64-bit high field");
1851 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1852 "Natural width accessor invalid for 32-bit field");
1853}
1854
1855static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001856{
Avi Kivity5e520e62011-05-15 10:13:12 -04001857 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001858
Avi Kivity5e520e62011-05-15 10:13:12 -04001859 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1860 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001861 return value;
1862}
1863
Avi Kivity96304212011-05-15 10:13:13 -04001864static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001865{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001866 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001867 if (static_branch_unlikely(&enable_evmcs))
1868 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001869 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001870}
1871
Avi Kivity96304212011-05-15 10:13:13 -04001872static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001873{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001874 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001875 if (static_branch_unlikely(&enable_evmcs))
1876 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001877 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001878}
1879
Avi Kivity96304212011-05-15 10:13:13 -04001880static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001881{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001882 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001883 if (static_branch_unlikely(&enable_evmcs))
1884 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001885#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001886 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001887#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001888 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001889#endif
1890}
1891
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001892static __always_inline unsigned long vmcs_readl(unsigned long field)
1893{
1894 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001895 if (static_branch_unlikely(&enable_evmcs))
1896 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001897 return __vmcs_readl(field);
1898}
1899
Avi Kivitye52de1b2007-01-05 16:36:56 -08001900static noinline void vmwrite_error(unsigned long field, unsigned long value)
1901{
1902 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1903 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1904 dump_stack();
1905}
1906
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001907static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001908{
1909 u8 error;
1910
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001911 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04001912 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08001913 if (unlikely(error))
1914 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001915}
1916
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001917static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001918{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001919 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001920 if (static_branch_unlikely(&enable_evmcs))
1921 return evmcs_write16(field, value);
1922
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001923 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001924}
1925
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001926static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001927{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001928 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001929 if (static_branch_unlikely(&enable_evmcs))
1930 return evmcs_write32(field, value);
1931
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001932 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001933}
1934
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001935static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001936{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001937 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001938 if (static_branch_unlikely(&enable_evmcs))
1939 return evmcs_write64(field, value);
1940
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001941 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03001942#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001943 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001944 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001945#endif
1946}
1947
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001948static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001949{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001950 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001951 if (static_branch_unlikely(&enable_evmcs))
1952 return evmcs_write64(field, value);
1953
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001954 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001955}
1956
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001957static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001958{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001959 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1960 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001961 if (static_branch_unlikely(&enable_evmcs))
1962 return evmcs_write32(field, evmcs_read32(field) & ~mask);
1963
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001964 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
1965}
1966
1967static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
1968{
1969 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1970 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001971 if (static_branch_unlikely(&enable_evmcs))
1972 return evmcs_write32(field, evmcs_read32(field) | mask);
1973
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001974 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001975}
1976
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001977static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
1978{
1979 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
1980}
1981
Gleb Natapov2961e8762013-11-25 15:37:13 +02001982static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1983{
1984 vmcs_write32(VM_ENTRY_CONTROLS, val);
1985 vmx->vm_entry_controls_shadow = val;
1986}
1987
1988static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1989{
1990 if (vmx->vm_entry_controls_shadow != val)
1991 vm_entry_controls_init(vmx, val);
1992}
1993
1994static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1995{
1996 return vmx->vm_entry_controls_shadow;
1997}
1998
1999
2000static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2001{
2002 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2003}
2004
2005static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2006{
2007 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2008}
2009
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002010static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2011{
2012 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2013}
2014
Gleb Natapov2961e8762013-11-25 15:37:13 +02002015static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2016{
2017 vmcs_write32(VM_EXIT_CONTROLS, val);
2018 vmx->vm_exit_controls_shadow = val;
2019}
2020
2021static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2022{
2023 if (vmx->vm_exit_controls_shadow != val)
2024 vm_exit_controls_init(vmx, val);
2025}
2026
2027static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2028{
2029 return vmx->vm_exit_controls_shadow;
2030}
2031
2032
2033static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2034{
2035 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2036}
2037
2038static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2039{
2040 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2041}
2042
Avi Kivity2fb92db2011-04-27 19:42:18 +03002043static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2044{
2045 vmx->segment_cache.bitmask = 0;
2046}
2047
2048static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2049 unsigned field)
2050{
2051 bool ret;
2052 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2053
2054 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2055 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2056 vmx->segment_cache.bitmask = 0;
2057 }
2058 ret = vmx->segment_cache.bitmask & mask;
2059 vmx->segment_cache.bitmask |= mask;
2060 return ret;
2061}
2062
2063static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2064{
2065 u16 *p = &vmx->segment_cache.seg[seg].selector;
2066
2067 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2068 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2069 return *p;
2070}
2071
2072static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2073{
2074 ulong *p = &vmx->segment_cache.seg[seg].base;
2075
2076 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2077 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2078 return *p;
2079}
2080
2081static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2082{
2083 u32 *p = &vmx->segment_cache.seg[seg].limit;
2084
2085 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2086 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2087 return *p;
2088}
2089
2090static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2091{
2092 u32 *p = &vmx->segment_cache.seg[seg].ar;
2093
2094 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2095 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2096 return *p;
2097}
2098
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002099static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2100{
2101 u32 eb;
2102
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002103 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002104 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002105 /*
2106 * Guest access to VMware backdoor ports could legitimately
2107 * trigger #GP because of TSS I/O permission bitmap.
2108 * We intercept those #GP and allow access to them anyway
2109 * as VMware does.
2110 */
2111 if (enable_vmware_backdoor)
2112 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002113 if ((vcpu->guest_debug &
2114 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2115 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2116 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002117 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002118 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002119 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002120 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002121
2122 /* When we are running a nested L2 guest and L1 specified for it a
2123 * certain exception bitmap, we must trap the same exceptions and pass
2124 * them to L1. When running L2, we will only handle the exceptions
2125 * specified above if L1 did not want them.
2126 */
2127 if (is_guest_mode(vcpu))
2128 eb |= get_vmcs12(vcpu)->exception_bitmap;
2129
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002130 vmcs_write32(EXCEPTION_BITMAP, eb);
2131}
2132
Ashok Raj15d45072018-02-01 22:59:43 +01002133/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002134 * Check if MSR is intercepted for currently loaded MSR bitmap.
2135 */
2136static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2137{
2138 unsigned long *msr_bitmap;
2139 int f = sizeof(unsigned long);
2140
2141 if (!cpu_has_vmx_msr_bitmap())
2142 return true;
2143
2144 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2145
2146 if (msr <= 0x1fff) {
2147 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2148 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2149 msr &= 0x1fff;
2150 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2151 }
2152
2153 return true;
2154}
2155
2156/*
Ashok Raj15d45072018-02-01 22:59:43 +01002157 * Check if MSR is intercepted for L01 MSR bitmap.
2158 */
2159static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2160{
2161 unsigned long *msr_bitmap;
2162 int f = sizeof(unsigned long);
2163
2164 if (!cpu_has_vmx_msr_bitmap())
2165 return true;
2166
2167 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2168
2169 if (msr <= 0x1fff) {
2170 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2171 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2172 msr &= 0x1fff;
2173 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2174 }
2175
2176 return true;
2177}
2178
Gleb Natapov2961e8762013-11-25 15:37:13 +02002179static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2180 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002181{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002182 vm_entry_controls_clearbit(vmx, entry);
2183 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002184}
2185
Avi Kivity61d2ef22010-04-28 16:40:38 +03002186static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2187{
2188 unsigned i;
2189 struct msr_autoload *m = &vmx->msr_autoload;
2190
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002191 switch (msr) {
2192 case MSR_EFER:
2193 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002194 clear_atomic_switch_msr_special(vmx,
2195 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002196 VM_EXIT_LOAD_IA32_EFER);
2197 return;
2198 }
2199 break;
2200 case MSR_CORE_PERF_GLOBAL_CTRL:
2201 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002202 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002203 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2204 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2205 return;
2206 }
2207 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002208 }
2209
Avi Kivity61d2ef22010-04-28 16:40:38 +03002210 for (i = 0; i < m->nr; ++i)
2211 if (m->guest[i].index == msr)
2212 break;
2213
2214 if (i == m->nr)
2215 return;
2216 --m->nr;
2217 m->guest[i] = m->guest[m->nr];
2218 m->host[i] = m->host[m->nr];
2219 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2220 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2221}
2222
Gleb Natapov2961e8762013-11-25 15:37:13 +02002223static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2224 unsigned long entry, unsigned long exit,
2225 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2226 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002227{
2228 vmcs_write64(guest_val_vmcs, guest_val);
2229 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002230 vm_entry_controls_setbit(vmx, entry);
2231 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002232}
2233
Avi Kivity61d2ef22010-04-28 16:40:38 +03002234static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
2235 u64 guest_val, u64 host_val)
2236{
2237 unsigned i;
2238 struct msr_autoload *m = &vmx->msr_autoload;
2239
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002240 switch (msr) {
2241 case MSR_EFER:
2242 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002243 add_atomic_switch_msr_special(vmx,
2244 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002245 VM_EXIT_LOAD_IA32_EFER,
2246 GUEST_IA32_EFER,
2247 HOST_IA32_EFER,
2248 guest_val, host_val);
2249 return;
2250 }
2251 break;
2252 case MSR_CORE_PERF_GLOBAL_CTRL:
2253 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002254 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002255 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2256 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2257 GUEST_IA32_PERF_GLOBAL_CTRL,
2258 HOST_IA32_PERF_GLOBAL_CTRL,
2259 guest_val, host_val);
2260 return;
2261 }
2262 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002263 case MSR_IA32_PEBS_ENABLE:
2264 /* PEBS needs a quiescent period after being disabled (to write
2265 * a record). Disabling PEBS through VMX MSR swapping doesn't
2266 * provide that period, so a CPU could write host's record into
2267 * guest's memory.
2268 */
2269 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002270 }
2271
Avi Kivity61d2ef22010-04-28 16:40:38 +03002272 for (i = 0; i < m->nr; ++i)
2273 if (m->guest[i].index == msr)
2274 break;
2275
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002276 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002277 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002278 "Can't add msr %x\n", msr);
2279 return;
2280 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03002281 ++m->nr;
2282 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2283 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2284 }
2285
2286 m->guest[i].index = msr;
2287 m->guest[i].value = guest_val;
2288 m->host[i].index = msr;
2289 m->host[i].value = host_val;
2290}
2291
Avi Kivity92c0d902009-10-29 11:00:16 +02002292static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002293{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002294 u64 guest_efer = vmx->vcpu.arch.efer;
2295 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002296
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002297 if (!enable_ept) {
2298 /*
2299 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2300 * host CPUID is more efficient than testing guest CPUID
2301 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2302 */
2303 if (boot_cpu_has(X86_FEATURE_SMEP))
2304 guest_efer |= EFER_NX;
2305 else if (!(guest_efer & EFER_NX))
2306 ignore_bits |= EFER_NX;
2307 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002308
Avi Kivity51c6cf62007-08-29 03:48:05 +03002309 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002310 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002311 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002312 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002313#ifdef CONFIG_X86_64
2314 ignore_bits |= EFER_LMA | EFER_LME;
2315 /* SCE is meaningful only in long mode on Intel */
2316 if (guest_efer & EFER_LMA)
2317 ignore_bits &= ~(u64)EFER_SCE;
2318#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002319
2320 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002321
2322 /*
2323 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2324 * On CPUs that support "load IA32_EFER", always switch EFER
2325 * atomically, since it's faster than switching it manually.
2326 */
2327 if (cpu_has_load_ia32_efer ||
2328 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002329 if (!(guest_efer & EFER_LMA))
2330 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002331 if (guest_efer != host_efer)
2332 add_atomic_switch_msr(vmx, MSR_EFER,
2333 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002334 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002335 } else {
2336 guest_efer &= ~ignore_bits;
2337 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002338
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002339 vmx->guest_msrs[efer_offset].data = guest_efer;
2340 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2341
2342 return true;
2343 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002344}
2345
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002346#ifdef CONFIG_X86_32
2347/*
2348 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2349 * VMCS rather than the segment table. KVM uses this helper to figure
2350 * out the current bases to poke them into the VMCS before entry.
2351 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002352static unsigned long segment_base(u16 selector)
2353{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002354 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002355 unsigned long v;
2356
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002357 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002358 return 0;
2359
Thomas Garnier45fc8752017-03-14 10:05:08 -07002360 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002361
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002362 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002363 u16 ldt_selector = kvm_read_ldt();
2364
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002365 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002366 return 0;
2367
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002368 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002369 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002370 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002371 return v;
2372}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002373#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002374
Avi Kivity04d2cc72007-09-10 18:10:54 +03002375static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002376{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002377 struct vcpu_vmx *vmx = to_vmx(vcpu);
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002378#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002379 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002380#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03002381 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002382
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002383 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002384 return;
2385
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002386 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002387 /*
2388 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2389 * allow segment selectors with cpl > 0 or ti == 1.
2390 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002391 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002392 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002393
2394#ifdef CONFIG_X86_64
2395 save_fsgs_for_kvm();
2396 vmx->host_state.fs_sel = current->thread.fsindex;
2397 vmx->host_state.gs_sel = current->thread.gsindex;
2398#else
Avi Kivity9581d442010-10-19 16:46:55 +02002399 savesegment(fs, vmx->host_state.fs_sel);
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002400 savesegment(gs, vmx->host_state.gs_sel);
2401#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002402 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002403 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002404 vmx->host_state.fs_reload_needed = 0;
2405 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002406 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002407 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002408 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002409 if (!(vmx->host_state.gs_sel & 7))
2410 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002411 else {
2412 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002413 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002414 }
2415
2416#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002417 savesegment(ds, vmx->host_state.ds_sel);
2418 savesegment(es, vmx->host_state.es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002419
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002420 vmcs_writel(HOST_FS_BASE, current->thread.fsbase);
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002421 vmcs_writel(HOST_GS_BASE, cpu_kernelmode_gs_base(cpu));
Avi Kivity707c0872007-05-02 17:33:43 +03002422
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002423 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Avi Kivityc8770e72010-11-11 12:37:26 +02002424 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002425 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002426#else
2427 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2428 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
2429#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002430 if (boot_cpu_has(X86_FEATURE_MPX))
2431 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002432 for (i = 0; i < vmx->save_nmsrs; ++i)
2433 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002434 vmx->guest_msrs[i].data,
2435 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002436}
2437
Avi Kivitya9b21b62008-06-24 11:48:49 +03002438static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002439{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002440 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002441 return;
2442
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002443 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002444 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002445#ifdef CONFIG_X86_64
2446 if (is_long_mode(&vmx->vcpu))
2447 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2448#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002449 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002450 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002451#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002452 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002453#else
2454 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002455#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002456 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002457 if (vmx->host_state.fs_reload_needed)
2458 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002459#ifdef CONFIG_X86_64
2460 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2461 loadsegment(ds, vmx->host_state.ds_sel);
2462 loadsegment(es, vmx->host_state.es_sel);
2463 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002464#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002465 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002466#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002467 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002468#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002469 if (vmx->host_state.msr_host_bndcfgs)
2470 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Thomas Garnier45fc8752017-03-14 10:05:08 -07002471 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002472}
2473
Avi Kivitya9b21b62008-06-24 11:48:49 +03002474static void vmx_load_host_state(struct vcpu_vmx *vmx)
2475{
2476 preempt_disable();
2477 __vmx_load_host_state(vmx);
2478 preempt_enable();
2479}
2480
Feng Wu28b835d2015-09-18 22:29:54 +08002481static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2482{
2483 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2484 struct pi_desc old, new;
2485 unsigned int dest;
2486
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002487 /*
2488 * In case of hot-plug or hot-unplug, we may have to undo
2489 * vmx_vcpu_pi_put even if there is no assigned device. And we
2490 * always keep PI.NDST up to date for simplicity: it makes the
2491 * code easier, and CPU migration is not a fast path.
2492 */
2493 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002494 return;
2495
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002496 /*
2497 * First handle the simple case where no cmpxchg is necessary; just
2498 * allow posting non-urgent interrupts.
2499 *
2500 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2501 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2502 * expects the VCPU to be on the blocked_vcpu_list that matches
2503 * PI.NDST.
2504 */
2505 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2506 vcpu->cpu == cpu) {
2507 pi_clear_sn(pi_desc);
2508 return;
2509 }
2510
2511 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002512 do {
2513 old.control = new.control = pi_desc->control;
2514
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002515 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002516
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002517 if (x2apic_enabled())
2518 new.ndst = dest;
2519 else
2520 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002521
Feng Wu28b835d2015-09-18 22:29:54 +08002522 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02002523 } while (cmpxchg64(&pi_desc->control, old.control,
2524 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002525}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002526
Peter Feinerc95ba922016-08-17 09:36:47 -07002527static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2528{
2529 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2530 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2531}
2532
Avi Kivity6aa8b732006-12-10 02:21:36 -08002533/*
2534 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2535 * vcpu mutex is already taken.
2536 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002537static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002538{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002539 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002540 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002541
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002542 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002543 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002544 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002545 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002546
2547 /*
2548 * Read loaded_vmcs->cpu should be before fetching
2549 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2550 * See the comments in __loaded_vmcs_clear().
2551 */
2552 smp_rmb();
2553
Nadav Har'Eld462b812011-05-24 15:26:10 +03002554 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2555 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002556 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002557 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002558 }
2559
2560 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2561 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2562 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01002563 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002564 }
2565
2566 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002567 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002568 unsigned long sysenter_esp;
2569
2570 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002571
Avi Kivity6aa8b732006-12-10 02:21:36 -08002572 /*
2573 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002574 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002575 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002576 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01002577 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002578 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002579
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002580 /*
2581 * VM exits change the host TR limit to 0x67 after a VM
2582 * exit. This is okay, since 0x67 covers everything except
2583 * the IO bitmap and have have code to handle the IO bitmap
2584 * being lost after a VM exit.
2585 */
2586 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2587
Avi Kivity6aa8b732006-12-10 02:21:36 -08002588 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2589 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002590
Nadav Har'Eld462b812011-05-24 15:26:10 +03002591 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002592 }
Feng Wu28b835d2015-09-18 22:29:54 +08002593
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002594 /* Setup TSC multiplier */
2595 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002596 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2597 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002598
Feng Wu28b835d2015-09-18 22:29:54 +08002599 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002600 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08002601 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08002602}
2603
2604static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2605{
2606 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2607
2608 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002609 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2610 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002611 return;
2612
2613 /* Set SN when the vCPU is preempted */
2614 if (vcpu->preempted)
2615 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002616}
2617
2618static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2619{
Feng Wu28b835d2015-09-18 22:29:54 +08002620 vmx_vcpu_pi_put(vcpu);
2621
Avi Kivitya9b21b62008-06-24 11:48:49 +03002622 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002623}
2624
Wanpeng Lif244dee2017-07-20 01:11:54 -07002625static bool emulation_required(struct kvm_vcpu *vcpu)
2626{
2627 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2628}
2629
Avi Kivityedcafe32009-12-30 18:07:40 +02002630static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2631
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002632/*
2633 * Return the cr0 value that a nested guest would read. This is a combination
2634 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2635 * its hypervisor (cr0_read_shadow).
2636 */
2637static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2638{
2639 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2640 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2641}
2642static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2643{
2644 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2645 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2646}
2647
Avi Kivity6aa8b732006-12-10 02:21:36 -08002648static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2649{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002650 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002651
Avi Kivity6de12732011-03-07 12:51:22 +02002652 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2653 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2654 rflags = vmcs_readl(GUEST_RFLAGS);
2655 if (to_vmx(vcpu)->rmode.vm86_active) {
2656 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2657 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2658 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2659 }
2660 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002661 }
Avi Kivity6de12732011-03-07 12:51:22 +02002662 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002663}
2664
2665static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2666{
Wanpeng Lif244dee2017-07-20 01:11:54 -07002667 unsigned long old_rflags = vmx_get_rflags(vcpu);
2668
Avi Kivity6de12732011-03-07 12:51:22 +02002669 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2670 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002671 if (to_vmx(vcpu)->rmode.vm86_active) {
2672 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002673 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002674 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002675 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07002676
2677 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2678 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002679}
2680
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002681static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002682{
2683 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2684 int ret = 0;
2685
2686 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002687 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002688 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002689 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002690
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002691 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002692}
2693
2694static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2695{
2696 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2697 u32 interruptibility = interruptibility_old;
2698
2699 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2700
Jan Kiszka48005f62010-02-19 19:38:07 +01002701 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002702 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002703 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002704 interruptibility |= GUEST_INTR_STATE_STI;
2705
2706 if ((interruptibility != interruptibility_old))
2707 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2708}
2709
Avi Kivity6aa8b732006-12-10 02:21:36 -08002710static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2711{
2712 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002713
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002714 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002715 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002716 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002717
Glauber Costa2809f5d2009-05-12 16:21:05 -04002718 /* skipping an emulated instruction also counts */
2719 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002720}
2721
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002722static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
2723 unsigned long exit_qual)
2724{
2725 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2726 unsigned int nr = vcpu->arch.exception.nr;
2727 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2728
2729 if (vcpu->arch.exception.has_error_code) {
2730 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
2731 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2732 }
2733
2734 if (kvm_exception_is_soft(nr))
2735 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2736 else
2737 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2738
2739 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
2740 vmx_get_nmi_mask(vcpu))
2741 intr_info |= INTR_INFO_UNBLOCK_NMI;
2742
2743 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
2744}
2745
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002746/*
2747 * KVM wants to inject page-faults which it got to the guest. This function
2748 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002749 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002750static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002751{
2752 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002753 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002754
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002755 if (nr == PF_VECTOR) {
2756 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002757 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002758 return 1;
2759 }
2760 /*
2761 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
2762 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2763 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
2764 * can be written only when inject_pending_event runs. This should be
2765 * conditional on a new capability---if the capability is disabled,
2766 * kvm_multiple_exception would write the ancillary information to
2767 * CR2 or DR6, for backwards ABI-compatibility.
2768 */
2769 if (nested_vmx_is_page_fault_vmexit(vmcs12,
2770 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002771 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002772 return 1;
2773 }
2774 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002775 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002776 if (nr == DB_VECTOR)
2777 *exit_qual = vcpu->arch.dr6;
2778 else
2779 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002780 return 1;
2781 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002782 }
2783
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002784 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002785}
2786
Wanpeng Licaa057a2018-03-12 04:53:03 -07002787static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
2788{
2789 /*
2790 * Ensure that we clear the HLT state in the VMCS. We don't need to
2791 * explicitly skip the instruction because if the HLT state is set,
2792 * then the instruction is already executing and RIP has already been
2793 * advanced.
2794 */
2795 if (kvm_hlt_in_guest(vcpu->kvm) &&
2796 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
2797 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
2798}
2799
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002800static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02002801{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002802 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002803 unsigned nr = vcpu->arch.exception.nr;
2804 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002805 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002806 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002807
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002808 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002809 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002810 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2811 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002812
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002813 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002814 int inc_eip = 0;
2815 if (kvm_exception_is_soft(nr))
2816 inc_eip = vcpu->arch.event_exit_inst_len;
2817 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002818 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002819 return;
2820 }
2821
Sean Christophersonadd5ff72018-03-23 09:34:00 -07002822 WARN_ON_ONCE(vmx->emulation_required);
2823
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002824 if (kvm_exception_is_soft(nr)) {
2825 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2826 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002827 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2828 } else
2829 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2830
2831 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07002832
2833 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02002834}
2835
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002836static bool vmx_rdtscp_supported(void)
2837{
2838 return cpu_has_vmx_rdtscp();
2839}
2840
Mao, Junjiead756a12012-07-02 01:18:48 +00002841static bool vmx_invpcid_supported(void)
2842{
2843 return cpu_has_vmx_invpcid() && enable_ept;
2844}
2845
Avi Kivity6aa8b732006-12-10 02:21:36 -08002846/*
Eddie Donga75beee2007-05-17 18:55:15 +03002847 * Swap MSR entry in host/guest MSR entry array.
2848 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002849static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002850{
Avi Kivity26bb0982009-09-07 11:14:12 +03002851 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002852
2853 tmp = vmx->guest_msrs[to];
2854 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2855 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002856}
2857
2858/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002859 * Set up the vmcs to automatically save and restore system
2860 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2861 * mode, as fiddling with msrs is very expensive.
2862 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002863static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002864{
Avi Kivity26bb0982009-09-07 11:14:12 +03002865 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002866
Eddie Donga75beee2007-05-17 18:55:15 +03002867 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002868#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002869 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002870 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002871 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002872 move_msr_up(vmx, index, save_nmsrs++);
2873 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002874 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002875 move_msr_up(vmx, index, save_nmsrs++);
2876 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002877 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002878 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002879 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02002880 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002881 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002882 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002883 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002884 * if efer.sce is enabled.
2885 */
Brian Gerst8c065852010-07-17 09:03:26 -04002886 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002887 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002888 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002889 }
Eddie Donga75beee2007-05-17 18:55:15 +03002890#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002891 index = __find_msr_index(vmx, MSR_EFER);
2892 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002893 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002894
Avi Kivity26bb0982009-09-07 11:14:12 +03002895 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002896
Yang Zhang8d146952013-01-25 10:18:50 +08002897 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002898 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002899}
2900
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02002901static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002902{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02002903 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002904
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02002905 if (is_guest_mode(vcpu) &&
2906 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
2907 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
2908
2909 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002910}
2911
2912/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002913 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002914 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002915static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002916{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002917 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002918 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002919 * We're here if L1 chose not to trap WRMSR to TSC. According
2920 * to the spec, this should set L1's TSC; The offset that L1
2921 * set for L2 remains unchanged, and still needs to be added
2922 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002923 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002924 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002925 /* recalculate vmcs02.TSC_OFFSET: */
2926 vmcs12 = get_vmcs12(vcpu);
2927 vmcs_write64(TSC_OFFSET, offset +
2928 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2929 vmcs12->tsc_offset : 0));
2930 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002931 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2932 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002933 vmcs_write64(TSC_OFFSET, offset);
2934 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002935}
2936
Nadav Har'El801d3422011-05-25 23:02:23 +03002937/*
2938 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2939 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2940 * all guests if the "nested" module option is off, and can also be disabled
2941 * for a single guest by disabling its VMX cpuid bit.
2942 */
2943static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2944{
Radim Krčmářd6321d42017-08-05 00:12:49 +02002945 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03002946}
2947
Avi Kivity6aa8b732006-12-10 02:21:36 -08002948/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002949 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2950 * returned for the various VMX controls MSRs when nested VMX is enabled.
2951 * The same values should also be used to verify that vmcs12 control fields are
2952 * valid during nested entry from L1 to L2.
2953 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2954 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2955 * bit in the high half is on if the corresponding bit in the control field
2956 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002957 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002958static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002959{
Paolo Bonzini13893092018-02-26 13:40:09 +01002960 if (!nested) {
2961 memset(msrs, 0, sizeof(*msrs));
2962 return;
2963 }
2964
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002965 /*
2966 * Note that as a general rule, the high half of the MSRs (bits in
2967 * the control fields which may be 1) should be initialized by the
2968 * intersection of the underlying hardware's MSR (i.e., features which
2969 * can be supported) and the list of features we want to expose -
2970 * because they are known to be properly supported in our code.
2971 * Also, usually, the low half of the MSRs (bits which must be 1) can
2972 * be set to 0, meaning that L1 may turn off any of these bits. The
2973 * reason is that if one of these bits is necessary, it will appear
2974 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2975 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02002976 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002977 * These rules have exceptions below.
2978 */
2979
2980 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002981 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002982 msrs->pinbased_ctls_low,
2983 msrs->pinbased_ctls_high);
2984 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002985 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002986 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002987 PIN_BASED_EXT_INTR_MASK |
2988 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01002989 PIN_BASED_VIRTUAL_NMIS |
2990 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002991 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002992 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002993 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002994
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002995 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002996 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002997 msrs->exit_ctls_low,
2998 msrs->exit_ctls_high);
2999 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003000 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003001
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003002 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003003#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003004 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003005#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01003006 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003007 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003008 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003009 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003010 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3011
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003012 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003013 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003014
Jan Kiszka2996fca2014-06-16 13:59:43 +02003015 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003016 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003017
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003018 /* entry controls */
3019 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003020 msrs->entry_ctls_low,
3021 msrs->entry_ctls_high);
3022 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003023 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003024 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003025#ifdef CONFIG_X86_64
3026 VM_ENTRY_IA32E_MODE |
3027#endif
3028 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003029 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003030 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003031 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003032 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02003033
Jan Kiszka2996fca2014-06-16 13:59:43 +02003034 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003035 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003036
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003037 /* cpu-based controls */
3038 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003039 msrs->procbased_ctls_low,
3040 msrs->procbased_ctls_high);
3041 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003042 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003043 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003044 CPU_BASED_VIRTUAL_INTR_PENDING |
3045 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003046 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3047 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3048 CPU_BASED_CR3_STORE_EXITING |
3049#ifdef CONFIG_X86_64
3050 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3051#endif
3052 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003053 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3054 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3055 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3056 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003057 /*
3058 * We can allow some features even when not supported by the
3059 * hardware. For example, L1 can specify an MSR bitmap - and we
3060 * can use it to avoid exits to L1 - even when L0 runs L2
3061 * without MSR bitmaps.
3062 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003063 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003064 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003065 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003066
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003067 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003068 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003069 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3070
Paolo Bonzini80154d72017-08-24 13:55:35 +02003071 /*
3072 * secondary cpu-based controls. Do not include those that
3073 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3074 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003075 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003076 msrs->secondary_ctls_low,
3077 msrs->secondary_ctls_high);
3078 msrs->secondary_ctls_low = 0;
3079 msrs->secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01003080 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02003081 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003082 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003083 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003084 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003085 SECONDARY_EXEC_WBINVD_EXITING;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003086
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003087 if (enable_ept) {
3088 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003089 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003090 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003091 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003092 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003093 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003094 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003095 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003096 msrs->ept_caps &= vmx_capability.ept;
3097 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003098 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3099 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003100 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003101 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003102 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003103 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003104 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003105 }
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003106
Bandan Das27c42a12017-08-03 15:54:42 -04003107 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003108 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003109 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003110 /*
3111 * Advertise EPTP switching unconditionally
3112 * since we emulate it
3113 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003114 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003115 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003116 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003117 }
3118
Paolo Bonzinief697a72016-03-18 16:58:38 +01003119 /*
3120 * Old versions of KVM use the single-context version without
3121 * checking for support, so declare that it is supported even
3122 * though it is treated as global context. The alternative is
3123 * not failing the single-context invvpid, and it is worse.
3124 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003125 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003126 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003127 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003128 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003129 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003130 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003131
Radim Krčmář0790ec12015-03-17 14:02:32 +01003132 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003133 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003134 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3135
Jan Kiszkac18911a2013-03-13 16:06:41 +01003136 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003137 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003138 msrs->misc_low,
3139 msrs->misc_high);
3140 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3141 msrs->misc_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003142 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003143 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003144 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003145
3146 /*
3147 * This MSR reports some information about VMX support. We
3148 * should return information about the VMX we emulate for the
3149 * guest, and the VMCS structure we give it - not about the
3150 * VMX support of the underlying hardware.
3151 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003152 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003153 VMCS12_REVISION |
3154 VMX_BASIC_TRUE_CTLS |
3155 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3156 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3157
3158 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003159 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003160
3161 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003162 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003163 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3164 * We picked the standard core2 setting.
3165 */
3166#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3167#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003168 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3169 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003170
3171 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003172 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3173 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003174
3175 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003176 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003177}
3178
David Matlack38991522016-11-29 18:14:08 -08003179/*
3180 * if fixed0[i] == 1: val[i] must be 1
3181 * if fixed1[i] == 0: val[i] must be 0
3182 */
3183static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3184{
3185 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003186}
3187
3188static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3189{
David Matlack38991522016-11-29 18:14:08 -08003190 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003191}
3192
3193static inline u64 vmx_control_msr(u32 low, u32 high)
3194{
3195 return low | ((u64)high << 32);
3196}
3197
David Matlack62cc6b9d2016-11-29 18:14:07 -08003198static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3199{
3200 superset &= mask;
3201 subset &= mask;
3202
3203 return (superset | subset) == superset;
3204}
3205
3206static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3207{
3208 const u64 feature_and_reserved =
3209 /* feature (except bit 48; see below) */
3210 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3211 /* reserved */
3212 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003213 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003214
3215 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3216 return -EINVAL;
3217
3218 /*
3219 * KVM does not emulate a version of VMX that constrains physical
3220 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3221 */
3222 if (data & BIT_ULL(48))
3223 return -EINVAL;
3224
3225 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3226 vmx_basic_vmcs_revision_id(data))
3227 return -EINVAL;
3228
3229 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3230 return -EINVAL;
3231
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003232 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003233 return 0;
3234}
3235
3236static int
3237vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3238{
3239 u64 supported;
3240 u32 *lowp, *highp;
3241
3242 switch (msr_index) {
3243 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003244 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3245 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003246 break;
3247 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003248 lowp = &vmx->nested.msrs.procbased_ctls_low;
3249 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003250 break;
3251 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003252 lowp = &vmx->nested.msrs.exit_ctls_low;
3253 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003254 break;
3255 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003256 lowp = &vmx->nested.msrs.entry_ctls_low;
3257 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003258 break;
3259 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003260 lowp = &vmx->nested.msrs.secondary_ctls_low;
3261 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003262 break;
3263 default:
3264 BUG();
3265 }
3266
3267 supported = vmx_control_msr(*lowp, *highp);
3268
3269 /* Check must-be-1 bits are still 1. */
3270 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3271 return -EINVAL;
3272
3273 /* Check must-be-0 bits are still 0. */
3274 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3275 return -EINVAL;
3276
3277 *lowp = data;
3278 *highp = data >> 32;
3279 return 0;
3280}
3281
3282static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3283{
3284 const u64 feature_and_reserved_bits =
3285 /* feature */
3286 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3287 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3288 /* reserved */
3289 GENMASK_ULL(13, 9) | BIT_ULL(31);
3290 u64 vmx_misc;
3291
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003292 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3293 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003294
3295 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3296 return -EINVAL;
3297
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003298 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003299 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3300 vmx_misc_preemption_timer_rate(data) !=
3301 vmx_misc_preemption_timer_rate(vmx_misc))
3302 return -EINVAL;
3303
3304 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3305 return -EINVAL;
3306
3307 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3308 return -EINVAL;
3309
3310 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3311 return -EINVAL;
3312
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003313 vmx->nested.msrs.misc_low = data;
3314 vmx->nested.msrs.misc_high = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003315 return 0;
3316}
3317
3318static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3319{
3320 u64 vmx_ept_vpid_cap;
3321
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003322 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3323 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003324
3325 /* Every bit is either reserved or a feature bit. */
3326 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3327 return -EINVAL;
3328
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003329 vmx->nested.msrs.ept_caps = data;
3330 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003331 return 0;
3332}
3333
3334static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3335{
3336 u64 *msr;
3337
3338 switch (msr_index) {
3339 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003340 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003341 break;
3342 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003343 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003344 break;
3345 default:
3346 BUG();
3347 }
3348
3349 /*
3350 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3351 * must be 1 in the restored value.
3352 */
3353 if (!is_bitwise_subset(data, *msr, -1ULL))
3354 return -EINVAL;
3355
3356 *msr = data;
3357 return 0;
3358}
3359
3360/*
3361 * Called when userspace is restoring VMX MSRs.
3362 *
3363 * Returns 0 on success, non-0 otherwise.
3364 */
3365static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3366{
3367 struct vcpu_vmx *vmx = to_vmx(vcpu);
3368
3369 switch (msr_index) {
3370 case MSR_IA32_VMX_BASIC:
3371 return vmx_restore_vmx_basic(vmx, data);
3372 case MSR_IA32_VMX_PINBASED_CTLS:
3373 case MSR_IA32_VMX_PROCBASED_CTLS:
3374 case MSR_IA32_VMX_EXIT_CTLS:
3375 case MSR_IA32_VMX_ENTRY_CTLS:
3376 /*
3377 * The "non-true" VMX capability MSRs are generated from the
3378 * "true" MSRs, so we do not support restoring them directly.
3379 *
3380 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3381 * should restore the "true" MSRs with the must-be-1 bits
3382 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3383 * DEFAULT SETTINGS".
3384 */
3385 return -EINVAL;
3386 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3387 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3388 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3389 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3390 case MSR_IA32_VMX_PROCBASED_CTLS2:
3391 return vmx_restore_control_msr(vmx, msr_index, data);
3392 case MSR_IA32_VMX_MISC:
3393 return vmx_restore_vmx_misc(vmx, data);
3394 case MSR_IA32_VMX_CR0_FIXED0:
3395 case MSR_IA32_VMX_CR4_FIXED0:
3396 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3397 case MSR_IA32_VMX_CR0_FIXED1:
3398 case MSR_IA32_VMX_CR4_FIXED1:
3399 /*
3400 * These MSRs are generated based on the vCPU's CPUID, so we
3401 * do not support restoring them directly.
3402 */
3403 return -EINVAL;
3404 case MSR_IA32_VMX_EPT_VPID_CAP:
3405 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3406 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003407 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003408 return 0;
3409 default:
3410 /*
3411 * The rest of the VMX capability MSRs do not support restore.
3412 */
3413 return -EINVAL;
3414 }
3415}
3416
Jan Kiszkacae50132014-01-04 18:47:22 +01003417/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003418static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003419{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003420 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003421 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003422 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003423 break;
3424 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3425 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003426 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003427 msrs->pinbased_ctls_low,
3428 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003429 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3430 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003431 break;
3432 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3433 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003434 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003435 msrs->procbased_ctls_low,
3436 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003437 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3438 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003439 break;
3440 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3441 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003442 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003443 msrs->exit_ctls_low,
3444 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003445 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3446 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003447 break;
3448 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3449 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003450 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003451 msrs->entry_ctls_low,
3452 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003453 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3454 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003455 break;
3456 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003457 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003458 msrs->misc_low,
3459 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003460 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003461 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003462 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003463 break;
3464 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003465 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003466 break;
3467 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003468 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003469 break;
3470 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003471 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003472 break;
3473 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003474 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003475 break;
3476 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003477 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003478 msrs->secondary_ctls_low,
3479 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003480 break;
3481 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003482 *pdata = msrs->ept_caps |
3483 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003484 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003485 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003486 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04003487 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003488 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003489 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003490 }
3491
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003492 return 0;
3493}
3494
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003495static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3496 uint64_t val)
3497{
3498 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3499
3500 return !(val & ~valid_bits);
3501}
3502
Tom Lendacky801e4592018-02-21 13:39:51 -06003503static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3504{
Paolo Bonzini13893092018-02-26 13:40:09 +01003505 switch (msr->index) {
3506 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3507 if (!nested)
3508 return 1;
3509 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
3510 default:
3511 return 1;
3512 }
3513
3514 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06003515}
3516
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003517/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003518 * Reads an msr value (of 'msr_index') into 'pdata'.
3519 * Returns 0 on success, non-0 otherwise.
3520 * Assumes vcpu_load() was already called.
3521 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003522static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003523{
Borislav Petkova6cb0992017-12-20 12:50:28 +01003524 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003525 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003526
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003527 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003528#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003529 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003530 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003531 break;
3532 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003533 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003534 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003535 case MSR_KERNEL_GS_BASE:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003536 vmx_load_host_state(vmx);
3537 msr_info->data = vmx->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003538 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003539#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003540 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003541 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003542 case MSR_IA32_SPEC_CTRL:
3543 if (!msr_info->host_initiated &&
3544 !guest_cpuid_has(vcpu, X86_FEATURE_IBRS) &&
3545 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3546 return 1;
3547
3548 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3549 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003550 case MSR_IA32_ARCH_CAPABILITIES:
3551 if (!msr_info->host_initiated &&
3552 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3553 return 1;
3554 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3555 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003556 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003557 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003558 break;
3559 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003560 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003561 break;
3562 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003563 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003564 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003565 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003566 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003567 (!msr_info->host_initiated &&
3568 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003569 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003570 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003571 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003572 case MSR_IA32_MCG_EXT_CTL:
3573 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01003574 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08003575 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003576 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003577 msr_info->data = vcpu->arch.mcg_ext_ctl;
3578 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003579 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003580 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003581 break;
3582 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3583 if (!nested_vmx_allowed(vcpu))
3584 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003585 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
3586 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003587 case MSR_IA32_XSS:
3588 if (!vmx_xsaves_supported())
3589 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003590 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003591 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003592 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003593 if (!msr_info->host_initiated &&
3594 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003595 return 1;
3596 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003597 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003598 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003599 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003600 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003601 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003602 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003603 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003604 }
3605
Avi Kivity6aa8b732006-12-10 02:21:36 -08003606 return 0;
3607}
3608
Jan Kiszkacae50132014-01-04 18:47:22 +01003609static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3610
Avi Kivity6aa8b732006-12-10 02:21:36 -08003611/*
3612 * Writes msr value into into the appropriate "register".
3613 * Returns 0 on success, non-0 otherwise.
3614 * Assumes vcpu_load() was already called.
3615 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003616static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003617{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003618 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003619 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003620 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003621 u32 msr_index = msr_info->index;
3622 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003623
Avi Kivity6aa8b732006-12-10 02:21:36 -08003624 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003625 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003626 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003627 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003628#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003629 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003630 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003631 vmcs_writel(GUEST_FS_BASE, data);
3632 break;
3633 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003634 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003635 vmcs_writel(GUEST_GS_BASE, data);
3636 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003637 case MSR_KERNEL_GS_BASE:
3638 vmx_load_host_state(vmx);
3639 vmx->msr_guest_kernel_gs_base = data;
3640 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003641#endif
3642 case MSR_IA32_SYSENTER_CS:
3643 vmcs_write32(GUEST_SYSENTER_CS, data);
3644 break;
3645 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003646 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003647 break;
3648 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003649 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003650 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003651 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003652 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003653 (!msr_info->host_initiated &&
3654 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003655 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08003656 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07003657 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003658 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003659 vmcs_write64(GUEST_BNDCFGS, data);
3660 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003661 case MSR_IA32_SPEC_CTRL:
3662 if (!msr_info->host_initiated &&
3663 !guest_cpuid_has(vcpu, X86_FEATURE_IBRS) &&
3664 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3665 return 1;
3666
3667 /* The STIBP bit doesn't fault even if it's not advertised */
3668 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP))
3669 return 1;
3670
3671 vmx->spec_ctrl = data;
3672
3673 if (!data)
3674 break;
3675
3676 /*
3677 * For non-nested:
3678 * When it's written (to non-zero) for the first time, pass
3679 * it through.
3680 *
3681 * For nested:
3682 * The handling of the MSR bitmap for L2 guests is done in
3683 * nested_vmx_merge_msr_bitmap. We should not touch the
3684 * vmcs02.msr_bitmap here since it gets completely overwritten
3685 * in the merging. We update the vmcs01 here for L1 as well
3686 * since it will end up touching the MSR anyway now.
3687 */
3688 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3689 MSR_IA32_SPEC_CTRL,
3690 MSR_TYPE_RW);
3691 break;
Ashok Raj15d45072018-02-01 22:59:43 +01003692 case MSR_IA32_PRED_CMD:
3693 if (!msr_info->host_initiated &&
3694 !guest_cpuid_has(vcpu, X86_FEATURE_IBPB) &&
3695 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3696 return 1;
3697
3698 if (data & ~PRED_CMD_IBPB)
3699 return 1;
3700
3701 if (!data)
3702 break;
3703
3704 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3705
3706 /*
3707 * For non-nested:
3708 * When it's written (to non-zero) for the first time, pass
3709 * it through.
3710 *
3711 * For nested:
3712 * The handling of the MSR bitmap for L2 guests is done in
3713 * nested_vmx_merge_msr_bitmap. We should not touch the
3714 * vmcs02.msr_bitmap here since it gets completely overwritten
3715 * in the merging.
3716 */
3717 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3718 MSR_TYPE_W);
3719 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003720 case MSR_IA32_ARCH_CAPABILITIES:
3721 if (!msr_info->host_initiated)
3722 return 1;
3723 vmx->arch_capabilities = data;
3724 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003725 case MSR_IA32_CR_PAT:
3726 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003727 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3728 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003729 vmcs_write64(GUEST_IA32_PAT, data);
3730 vcpu->arch.pat = data;
3731 break;
3732 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003733 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003734 break;
Will Auldba904632012-11-29 12:42:50 -08003735 case MSR_IA32_TSC_ADJUST:
3736 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003737 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003738 case MSR_IA32_MCG_EXT_CTL:
3739 if ((!msr_info->host_initiated &&
3740 !(to_vmx(vcpu)->msr_ia32_feature_control &
3741 FEATURE_CONTROL_LMCE)) ||
3742 (data & ~MCG_EXT_CTL_LMCE_EN))
3743 return 1;
3744 vcpu->arch.mcg_ext_ctl = data;
3745 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003746 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003747 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003748 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003749 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3750 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003751 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003752 if (msr_info->host_initiated && data == 0)
3753 vmx_leave_nested(vcpu);
3754 break;
3755 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003756 if (!msr_info->host_initiated)
3757 return 1; /* they are read-only */
3758 if (!nested_vmx_allowed(vcpu))
3759 return 1;
3760 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08003761 case MSR_IA32_XSS:
3762 if (!vmx_xsaves_supported())
3763 return 1;
3764 /*
3765 * The only supported bit as of Skylake is bit 8, but
3766 * it is not supported on KVM.
3767 */
3768 if (data != 0)
3769 return 1;
3770 vcpu->arch.ia32_xss = data;
3771 if (vcpu->arch.ia32_xss != host_xss)
3772 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
3773 vcpu->arch.ia32_xss, host_xss);
3774 else
3775 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3776 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003777 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003778 if (!msr_info->host_initiated &&
3779 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003780 return 1;
3781 /* Check reserved bit, higher 32 bits should be zero */
3782 if ((data >> 32) != 0)
3783 return 1;
3784 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003785 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10003786 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003787 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07003788 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003789 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003790 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3791 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003792 ret = kvm_set_shared_msr(msr->index, msr->data,
3793 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03003794 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003795 if (ret)
3796 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003797 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08003798 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003799 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003800 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003801 }
3802
Eddie Dong2cc51562007-05-21 07:28:09 +03003803 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003804}
3805
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003806static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003807{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003808 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3809 switch (reg) {
3810 case VCPU_REGS_RSP:
3811 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3812 break;
3813 case VCPU_REGS_RIP:
3814 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3815 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003816 case VCPU_EXREG_PDPTR:
3817 if (enable_ept)
3818 ept_save_pdptrs(vcpu);
3819 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003820 default:
3821 break;
3822 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003823}
3824
Avi Kivity6aa8b732006-12-10 02:21:36 -08003825static __init int cpu_has_kvm_support(void)
3826{
Eduardo Habkost6210e372008-11-17 19:03:16 -02003827 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003828}
3829
3830static __init int vmx_disabled_by_bios(void)
3831{
3832 u64 msr;
3833
3834 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04003835 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08003836 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04003837 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3838 && tboot_enabled())
3839 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08003840 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04003841 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08003842 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08003843 && !tboot_enabled()) {
3844 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08003845 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04003846 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08003847 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08003848 /* launched w/o TXT and VMX disabled */
3849 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3850 && !tboot_enabled())
3851 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04003852 }
3853
3854 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003855}
3856
Dongxiao Xu7725b892010-05-11 18:29:38 +08003857static void kvm_cpu_vmxon(u64 addr)
3858{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003859 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003860 intel_pt_handle_vmx(1);
3861
Dongxiao Xu7725b892010-05-11 18:29:38 +08003862 asm volatile (ASM_VMX_VMXON_RAX
3863 : : "a"(&addr), "m"(addr)
3864 : "memory", "cc");
3865}
3866
Radim Krčmář13a34e02014-08-28 15:13:03 +02003867static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003868{
3869 int cpu = raw_smp_processor_id();
3870 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04003871 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003872
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003873 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02003874 return -EBUSY;
3875
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01003876 /*
3877 * This can happen if we hot-added a CPU but failed to allocate
3878 * VP assist page for it.
3879 */
3880 if (static_branch_unlikely(&enable_evmcs) &&
3881 !hv_get_vp_assist_page(cpu))
3882 return -EFAULT;
3883
Nadav Har'Eld462b812011-05-24 15:26:10 +03003884 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08003885 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3886 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003887
3888 /*
3889 * Now we can enable the vmclear operation in kdump
3890 * since the loaded_vmcss_on_cpu list on this cpu
3891 * has been initialized.
3892 *
3893 * Though the cpu is not in VMX operation now, there
3894 * is no problem to enable the vmclear operation
3895 * for the loaded_vmcss_on_cpu list is empty!
3896 */
3897 crash_enable_local_vmclear(cpu);
3898
Avi Kivity6aa8b732006-12-10 02:21:36 -08003899 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04003900
3901 test_bits = FEATURE_CONTROL_LOCKED;
3902 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3903 if (tboot_enabled())
3904 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3905
3906 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003907 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04003908 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3909 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003910 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02003911 if (enable_ept)
3912 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02003913
3914 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003915}
3916
Nadav Har'Eld462b812011-05-24 15:26:10 +03003917static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03003918{
3919 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03003920 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03003921
Nadav Har'Eld462b812011-05-24 15:26:10 +03003922 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3923 loaded_vmcss_on_cpu_link)
3924 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03003925}
3926
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003927
3928/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3929 * tricks.
3930 */
3931static void kvm_cpu_vmxoff(void)
3932{
3933 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003934
3935 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003936 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003937}
3938
Radim Krčmář13a34e02014-08-28 15:13:03 +02003939static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003940{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003941 vmclear_local_loaded_vmcss();
3942 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003943}
3944
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003945static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04003946 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003947{
3948 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003949 u32 ctl = ctl_min | ctl_opt;
3950
3951 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3952
3953 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3954 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3955
3956 /* Ensure minimum (required) set of control bits are supported. */
3957 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003958 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003959
3960 *result = ctl;
3961 return 0;
3962}
3963
Avi Kivity110312c2010-12-21 12:54:20 +02003964static __init bool allow_1_setting(u32 msr, u32 ctl)
3965{
3966 u32 vmx_msr_low, vmx_msr_high;
3967
3968 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3969 return vmx_msr_high & ctl;
3970}
3971
Yang, Sheng002c7f72007-07-31 14:23:01 +03003972static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003973{
3974 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08003975 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003976 u32 _pin_based_exec_control = 0;
3977 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003978 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003979 u32 _vmexit_control = 0;
3980 u32 _vmentry_control = 0;
3981
Paolo Bonzini13893092018-02-26 13:40:09 +01003982 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05303983 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003984#ifdef CONFIG_X86_64
3985 CPU_BASED_CR8_LOAD_EXITING |
3986 CPU_BASED_CR8_STORE_EXITING |
3987#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08003988 CPU_BASED_CR3_LOAD_EXITING |
3989 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e22017-12-12 16:44:21 +08003990 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003991 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03003992 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003993 CPU_BASED_MWAIT_EXITING |
3994 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02003995 CPU_BASED_INVLPG_EXITING |
3996 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06003997
Sheng Yangf78e0e22007-10-29 09:40:42 +08003998 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08003999 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004000 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004001 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4002 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004003 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004004#ifdef CONFIG_X86_64
4005 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4006 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4007 ~CPU_BASED_CR8_STORE_EXITING;
4008#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004009 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004010 min2 = 0;
4011 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004012 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004013 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004014 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004015 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004016 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004017 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004018 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004019 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004020 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004021 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004022 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004023 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004024 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004025 SECONDARY_EXEC_RDSEED_EXITING |
4026 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004027 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004028 SECONDARY_EXEC_TSC_SCALING |
4029 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08004030 if (adjust_vmx_controls(min2, opt2,
4031 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004032 &_cpu_based_2nd_exec_control) < 0)
4033 return -EIO;
4034 }
4035#ifndef CONFIG_X86_64
4036 if (!(_cpu_based_2nd_exec_control &
4037 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4038 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4039#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004040
4041 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4042 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004043 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004044 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4045 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004046
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004047 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4048 &vmx_capability.ept, &vmx_capability.vpid);
4049
Sheng Yangd56f5462008-04-25 10:13:16 +08004050 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004051 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4052 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004053 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4054 CPU_BASED_CR3_STORE_EXITING |
4055 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004056 } else if (vmx_capability.ept) {
4057 vmx_capability.ept = 0;
4058 pr_warn_once("EPT CAP should not exist if not support "
4059 "1-setting enable EPT VM-execution control\n");
4060 }
4061 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4062 vmx_capability.vpid) {
4063 vmx_capability.vpid = 0;
4064 pr_warn_once("VPID CAP should not exist if not support "
4065 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004066 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004067
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004068 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004069#ifdef CONFIG_X86_64
4070 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4071#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004072 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004073 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004074 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4075 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004076 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004077
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004078 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4079 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4080 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004081 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4082 &_pin_based_exec_control) < 0)
4083 return -EIO;
4084
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004085 if (cpu_has_broken_vmx_preemption_timer())
4086 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004087 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004088 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004089 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4090
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004091 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004092 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004093 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4094 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004095 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004096
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004097 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004098
4099 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4100 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004101 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004102
4103#ifdef CONFIG_X86_64
4104 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4105 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004106 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004107#endif
4108
4109 /* Require Write-Back (WB) memory type for VMCS accesses. */
4110 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004111 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004112
Yang, Sheng002c7f72007-07-31 14:23:01 +03004113 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004114 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004115 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004116
4117 /* KVM supports Enlightened VMCS v1 only */
4118 if (static_branch_unlikely(&enable_evmcs))
4119 vmcs_conf->revision_id = KVM_EVMCS_VERSION;
4120 else
4121 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004122
Yang, Sheng002c7f72007-07-31 14:23:01 +03004123 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4124 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004125 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004126 vmcs_conf->vmexit_ctrl = _vmexit_control;
4127 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004128
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004129 if (static_branch_unlikely(&enable_evmcs))
4130 evmcs_sanitize_exec_ctrls(vmcs_conf);
4131
Avi Kivity110312c2010-12-21 12:54:20 +02004132 cpu_has_load_ia32_efer =
4133 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4134 VM_ENTRY_LOAD_IA32_EFER)
4135 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4136 VM_EXIT_LOAD_IA32_EFER);
4137
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004138 cpu_has_load_perf_global_ctrl =
4139 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4140 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4141 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4142 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4143
4144 /*
4145 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004146 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004147 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4148 *
4149 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4150 *
4151 * AAK155 (model 26)
4152 * AAP115 (model 30)
4153 * AAT100 (model 37)
4154 * BC86,AAY89,BD102 (model 44)
4155 * BA97 (model 46)
4156 *
4157 */
4158 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4159 switch (boot_cpu_data.x86_model) {
4160 case 26:
4161 case 30:
4162 case 37:
4163 case 44:
4164 case 46:
4165 cpu_has_load_perf_global_ctrl = false;
4166 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4167 "does not work properly. Using workaround\n");
4168 break;
4169 default:
4170 break;
4171 }
4172 }
4173
Borislav Petkov782511b2016-04-04 22:25:03 +02004174 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004175 rdmsrl(MSR_IA32_XSS, host_xss);
4176
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004177 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004178}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004179
4180static struct vmcs *alloc_vmcs_cpu(int cpu)
4181{
4182 int node = cpu_to_node(cpu);
4183 struct page *pages;
4184 struct vmcs *vmcs;
4185
Vlastimil Babka96db8002015-09-08 15:03:50 -07004186 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004187 if (!pages)
4188 return NULL;
4189 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004190 memset(vmcs, 0, vmcs_config.size);
4191 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004192 return vmcs;
4193}
4194
Avi Kivity6aa8b732006-12-10 02:21:36 -08004195static void free_vmcs(struct vmcs *vmcs)
4196{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004197 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004198}
4199
Nadav Har'Eld462b812011-05-24 15:26:10 +03004200/*
4201 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4202 */
4203static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4204{
4205 if (!loaded_vmcs->vmcs)
4206 return;
4207 loaded_vmcs_clear(loaded_vmcs);
4208 free_vmcs(loaded_vmcs->vmcs);
4209 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004210 if (loaded_vmcs->msr_bitmap)
4211 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004212 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004213}
4214
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004215static struct vmcs *alloc_vmcs(void)
4216{
4217 return alloc_vmcs_cpu(raw_smp_processor_id());
4218}
4219
4220static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4221{
4222 loaded_vmcs->vmcs = alloc_vmcs();
4223 if (!loaded_vmcs->vmcs)
4224 return -ENOMEM;
4225
4226 loaded_vmcs->shadow_vmcs = NULL;
4227 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004228
4229 if (cpu_has_vmx_msr_bitmap()) {
4230 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4231 if (!loaded_vmcs->msr_bitmap)
4232 goto out_vmcs;
4233 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004234
4235 if (static_branch_unlikely(&enable_evmcs) &&
4236 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4237 struct hv_enlightened_vmcs *evmcs =
4238 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4239
4240 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4241 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004242 }
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004243 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004244
4245out_vmcs:
4246 free_loaded_vmcs(loaded_vmcs);
4247 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004248}
4249
Sam Ravnborg39959582007-06-01 00:47:13 -07004250static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004251{
4252 int cpu;
4253
Zachary Amsden3230bb42009-09-29 11:38:37 -10004254 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004255 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004256 per_cpu(vmxarea, cpu) = NULL;
4257 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004258}
4259
Jim Mattsond37f4262017-12-22 12:12:16 -08004260enum vmcs_field_width {
4261 VMCS_FIELD_WIDTH_U16 = 0,
4262 VMCS_FIELD_WIDTH_U64 = 1,
4263 VMCS_FIELD_WIDTH_U32 = 2,
4264 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004265};
4266
Jim Mattsond37f4262017-12-22 12:12:16 -08004267static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004268{
4269 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004270 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004271 return (field >> 13) & 0x3 ;
4272}
4273
4274static inline int vmcs_field_readonly(unsigned long field)
4275{
4276 return (((field >> 10) & 0x3) == 1);
4277}
4278
Bandan Dasfe2b2012014-04-21 15:20:14 -04004279static void init_vmcs_shadow_fields(void)
4280{
4281 int i, j;
4282
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004283 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4284 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004285 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004286 (i + 1 == max_shadow_read_only_fields ||
4287 shadow_read_only_fields[i + 1] != field + 1))
4288 pr_err("Missing field from shadow_read_only_field %x\n",
4289 field + 1);
4290
4291 clear_bit(field, vmx_vmread_bitmap);
4292#ifdef CONFIG_X86_64
4293 if (field & 1)
4294 continue;
4295#endif
4296 if (j < i)
4297 shadow_read_only_fields[j] = field;
4298 j++;
4299 }
4300 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004301
4302 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004303 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004304 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004305 (i + 1 == max_shadow_read_write_fields ||
4306 shadow_read_write_fields[i + 1] != field + 1))
4307 pr_err("Missing field from shadow_read_write_field %x\n",
4308 field + 1);
4309
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004310 /*
4311 * PML and the preemption timer can be emulated, but the
4312 * processor cannot vmwrite to fields that don't exist
4313 * on bare metal.
4314 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004315 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004316 case GUEST_PML_INDEX:
4317 if (!cpu_has_vmx_pml())
4318 continue;
4319 break;
4320 case VMX_PREEMPTION_TIMER_VALUE:
4321 if (!cpu_has_vmx_preemption_timer())
4322 continue;
4323 break;
4324 case GUEST_INTR_STATUS:
4325 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004326 continue;
4327 break;
4328 default:
4329 break;
4330 }
4331
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004332 clear_bit(field, vmx_vmwrite_bitmap);
4333 clear_bit(field, vmx_vmread_bitmap);
4334#ifdef CONFIG_X86_64
4335 if (field & 1)
4336 continue;
4337#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004338 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004339 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004340 j++;
4341 }
4342 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004343}
4344
Avi Kivity6aa8b732006-12-10 02:21:36 -08004345static __init int alloc_kvm_area(void)
4346{
4347 int cpu;
4348
Zachary Amsden3230bb42009-09-29 11:38:37 -10004349 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004350 struct vmcs *vmcs;
4351
4352 vmcs = alloc_vmcs_cpu(cpu);
4353 if (!vmcs) {
4354 free_kvm_area();
4355 return -ENOMEM;
4356 }
4357
4358 per_cpu(vmxarea, cpu) = vmcs;
4359 }
4360 return 0;
4361}
4362
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004363static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004364 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004365{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004366 if (!emulate_invalid_guest_state) {
4367 /*
4368 * CS and SS RPL should be equal during guest entry according
4369 * to VMX spec, but in reality it is not always so. Since vcpu
4370 * is in the middle of the transition from real mode to
4371 * protected mode it is safe to assume that RPL 0 is a good
4372 * default value.
4373 */
4374 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004375 save->selector &= ~SEGMENT_RPL_MASK;
4376 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004377 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004378 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004379 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004380}
4381
4382static void enter_pmode(struct kvm_vcpu *vcpu)
4383{
4384 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004385 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004386
Gleb Natapovd99e4152012-12-20 16:57:45 +02004387 /*
4388 * Update real mode segment cache. It may be not up-to-date if sement
4389 * register was written while vcpu was in a guest mode.
4390 */
4391 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4392 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4393 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4394 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4395 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4396 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4397
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004398 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004399
Avi Kivity2fb92db2011-04-27 19:42:18 +03004400 vmx_segment_cache_clear(vmx);
4401
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004402 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004403
4404 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004405 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4406 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004407 vmcs_writel(GUEST_RFLAGS, flags);
4408
Rusty Russell66aee912007-07-17 23:34:16 +10004409 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4410 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004411
4412 update_exception_bitmap(vcpu);
4413
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004414 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4415 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4416 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4417 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4418 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4419 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004420}
4421
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004422static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004423{
Mathias Krause772e0312012-08-30 01:30:19 +02004424 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004425 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004426
Gleb Natapovd99e4152012-12-20 16:57:45 +02004427 var.dpl = 0x3;
4428 if (seg == VCPU_SREG_CS)
4429 var.type = 0x3;
4430
4431 if (!emulate_invalid_guest_state) {
4432 var.selector = var.base >> 4;
4433 var.base = var.base & 0xffff0;
4434 var.limit = 0xffff;
4435 var.g = 0;
4436 var.db = 0;
4437 var.present = 1;
4438 var.s = 1;
4439 var.l = 0;
4440 var.unusable = 0;
4441 var.type = 0x3;
4442 var.avl = 0;
4443 if (save->base & 0xf)
4444 printk_once(KERN_WARNING "kvm: segment base is not "
4445 "paragraph aligned when entering "
4446 "protected mode (seg=%d)", seg);
4447 }
4448
4449 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004450 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004451 vmcs_write32(sf->limit, var.limit);
4452 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004453}
4454
4455static void enter_rmode(struct kvm_vcpu *vcpu)
4456{
4457 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004458 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004459 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004460
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004461 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4462 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4463 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4464 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4465 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004466 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4467 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004468
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004469 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004470
Gleb Natapov776e58e2011-03-13 12:34:27 +02004471 /*
4472 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004473 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004474 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004475 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004476 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4477 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004478
Avi Kivity2fb92db2011-04-27 19:42:18 +03004479 vmx_segment_cache_clear(vmx);
4480
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004481 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004482 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004483 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4484
4485 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004486 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004487
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004488 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004489
4490 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004491 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004492 update_exception_bitmap(vcpu);
4493
Gleb Natapovd99e4152012-12-20 16:57:45 +02004494 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4495 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4496 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4497 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4498 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4499 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004500
Eddie Dong8668a3c2007-10-10 14:26:45 +08004501 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004502}
4503
Amit Shah401d10d2009-02-20 22:53:37 +05304504static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4505{
4506 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004507 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4508
4509 if (!msr)
4510 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304511
Avi Kivity44ea2b12009-09-06 15:55:37 +03004512 /*
4513 * Force kernel_gs_base reloading before EFER changes, as control
4514 * of this msr depends on is_long_mode().
4515 */
4516 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004517 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304518 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004519 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304520 msr->data = efer;
4521 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004522 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304523
4524 msr->data = efer & ~EFER_LME;
4525 }
4526 setup_msrs(vmx);
4527}
4528
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004529#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004530
4531static void enter_lmode(struct kvm_vcpu *vcpu)
4532{
4533 u32 guest_tr_ar;
4534
Avi Kivity2fb92db2011-04-27 19:42:18 +03004535 vmx_segment_cache_clear(to_vmx(vcpu));
4536
Avi Kivity6aa8b732006-12-10 02:21:36 -08004537 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004538 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004539 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4540 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004541 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004542 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4543 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004544 }
Avi Kivityda38f432010-07-06 11:30:49 +03004545 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004546}
4547
4548static void exit_lmode(struct kvm_vcpu *vcpu)
4549{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004550 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004551 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004552}
4553
4554#endif
4555
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004556static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4557 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004558{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004559 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004560 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4561 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004562 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004563 } else {
4564 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004565 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004566}
4567
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004568static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004569{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004570 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004571}
4572
Avi Kivitye8467fd2009-12-29 18:43:06 +02004573static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4574{
4575 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4576
4577 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4578 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4579}
4580
Avi Kivityaff48ba2010-12-05 18:56:11 +02004581static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4582{
Sean Christophersonb4d18512018-03-05 12:04:40 -08004583 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02004584 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4585 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4586}
4587
Anthony Liguori25c4c272007-04-27 09:29:21 +03004588static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004589{
Avi Kivityfc78f512009-12-07 12:16:48 +02004590 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4591
4592 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4593 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004594}
4595
Sheng Yang14394422008-04-28 12:24:45 +08004596static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4597{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004598 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4599
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004600 if (!test_bit(VCPU_EXREG_PDPTR,
4601 (unsigned long *)&vcpu->arch.regs_dirty))
4602 return;
4603
Sheng Yang14394422008-04-28 12:24:45 +08004604 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004605 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4606 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4607 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4608 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004609 }
4610}
4611
Avi Kivity8f5d5492009-05-31 18:41:29 +03004612static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4613{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004614 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4615
Avi Kivity8f5d5492009-05-31 18:41:29 +03004616 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004617 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4618 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4619 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4620 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004621 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004622
4623 __set_bit(VCPU_EXREG_PDPTR,
4624 (unsigned long *)&vcpu->arch.regs_avail);
4625 __set_bit(VCPU_EXREG_PDPTR,
4626 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004627}
4628
David Matlack38991522016-11-29 18:14:08 -08004629static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4630{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004631 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4632 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004633 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4634
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004635 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08004636 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4637 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4638 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4639
4640 return fixed_bits_valid(val, fixed0, fixed1);
4641}
4642
4643static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4644{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004645 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4646 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004647
4648 return fixed_bits_valid(val, fixed0, fixed1);
4649}
4650
4651static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4652{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004653 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
4654 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004655
4656 return fixed_bits_valid(val, fixed0, fixed1);
4657}
4658
4659/* No difference in the restrictions on guest and host CR4 in VMX operation. */
4660#define nested_guest_cr4_valid nested_cr4_valid
4661#define nested_host_cr4_valid nested_cr4_valid
4662
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004663static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004664
4665static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4666 unsigned long cr0,
4667 struct kvm_vcpu *vcpu)
4668{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004669 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4670 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004671 if (!(cr0 & X86_CR0_PG)) {
4672 /* From paging/starting to nonpaging */
4673 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004674 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004675 (CPU_BASED_CR3_LOAD_EXITING |
4676 CPU_BASED_CR3_STORE_EXITING));
4677 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004678 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004679 } else if (!is_paging(vcpu)) {
4680 /* From nonpaging to paging */
4681 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004682 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004683 ~(CPU_BASED_CR3_LOAD_EXITING |
4684 CPU_BASED_CR3_STORE_EXITING));
4685 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004686 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004687 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004688
4689 if (!(cr0 & X86_CR0_WP))
4690 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004691}
4692
Avi Kivity6aa8b732006-12-10 02:21:36 -08004693static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4694{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004695 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004696 unsigned long hw_cr0;
4697
Gleb Natapov50378782013-02-04 16:00:28 +02004698 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004699 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004700 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004701 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004702 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004703
Gleb Natapov218e7632013-01-21 15:36:45 +02004704 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4705 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004706
Gleb Natapov218e7632013-01-21 15:36:45 +02004707 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4708 enter_rmode(vcpu);
4709 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004710
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004711#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004712 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10004713 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004714 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10004715 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004716 exit_lmode(vcpu);
4717 }
4718#endif
4719
Sean Christophersonb4d18512018-03-05 12:04:40 -08004720 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08004721 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4722
Avi Kivity6aa8b732006-12-10 02:21:36 -08004723 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004724 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004725 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004726
4727 /* depends on vcpu->arch.cr0 to be set to a new value */
4728 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004729}
4730
Yu Zhang855feb62017-08-24 20:27:55 +08004731static int get_ept_level(struct kvm_vcpu *vcpu)
4732{
4733 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
4734 return 5;
4735 return 4;
4736}
4737
Peter Feiner995f00a2017-06-30 17:26:32 -07004738static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08004739{
Yu Zhang855feb62017-08-24 20:27:55 +08004740 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08004741
Yu Zhang855feb62017-08-24 20:27:55 +08004742 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08004743
Peter Feiner995f00a2017-06-30 17:26:32 -07004744 if (enable_ept_ad_bits &&
4745 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02004746 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08004747 eptp |= (root_hpa & PAGE_MASK);
4748
4749 return eptp;
4750}
4751
Avi Kivity6aa8b732006-12-10 02:21:36 -08004752static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4753{
Sheng Yang14394422008-04-28 12:24:45 +08004754 unsigned long guest_cr3;
4755 u64 eptp;
4756
4757 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02004758 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07004759 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08004760 vmcs_write64(EPT_POINTER, eptp);
Sean Christophersone90008d2018-03-05 12:04:37 -08004761 if (enable_unrestricted_guest || is_paging(vcpu) ||
4762 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02004763 guest_cr3 = kvm_read_cr3(vcpu);
4764 else
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004765 guest_cr3 = to_kvm_vmx(vcpu->kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02004766 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004767 }
4768
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004769 vmx_flush_tlb(vcpu, true);
Sheng Yang14394422008-04-28 12:24:45 +08004770 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004771}
4772
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004773static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004774{
Ben Serebrin085e68e2015-04-16 11:58:05 -07004775 /*
4776 * Pass through host's Machine Check Enable value to hw_cr4, which
4777 * is in force while we are in guest mode. Do not let guests control
4778 * this bit, even if host CR4.MCE == 0.
4779 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08004780 unsigned long hw_cr4;
4781
4782 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
4783 if (enable_unrestricted_guest)
4784 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
4785 else if (to_vmx(vcpu)->rmode.vm86_active)
4786 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
4787 else
4788 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004789
Sean Christopherson64f7a112018-04-30 10:01:06 -07004790 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
4791 if (cr4 & X86_CR4_UMIP) {
4792 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02004793 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07004794 hw_cr4 &= ~X86_CR4_UMIP;
4795 } else if (!is_guest_mode(vcpu) ||
4796 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
4797 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
4798 SECONDARY_EXEC_DESC);
4799 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02004800
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004801 if (cr4 & X86_CR4_VMXE) {
4802 /*
4803 * To use VMXON (and later other VMX instructions), a guest
4804 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4805 * So basically the check on whether to allow nested VMX
4806 * is here.
4807 */
4808 if (!nested_vmx_allowed(vcpu))
4809 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004810 }
David Matlack38991522016-11-29 18:14:08 -08004811
4812 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004813 return 1;
4814
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004815 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08004816
Sean Christopherson5dc1f042018-03-05 12:04:39 -08004817 if (!enable_unrestricted_guest) {
4818 if (enable_ept) {
4819 if (!is_paging(vcpu)) {
4820 hw_cr4 &= ~X86_CR4_PAE;
4821 hw_cr4 |= X86_CR4_PSE;
4822 } else if (!(cr4 & X86_CR4_PAE)) {
4823 hw_cr4 &= ~X86_CR4_PAE;
4824 }
4825 }
4826
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004827 /*
Huaitong Handdba2622016-03-22 16:51:15 +08004828 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4829 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4830 * to be manually disabled when guest switches to non-paging
4831 * mode.
4832 *
4833 * If !enable_unrestricted_guest, the CPU is always running
4834 * with CR0.PG=1 and CR4 needs to be modified.
4835 * If enable_unrestricted_guest, the CPU automatically
4836 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004837 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08004838 if (!is_paging(vcpu))
4839 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
4840 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004841
Sheng Yang14394422008-04-28 12:24:45 +08004842 vmcs_writel(CR4_READ_SHADOW, cr4);
4843 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004844 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004845}
4846
Avi Kivity6aa8b732006-12-10 02:21:36 -08004847static void vmx_get_segment(struct kvm_vcpu *vcpu,
4848 struct kvm_segment *var, int seg)
4849{
Avi Kivitya9179492011-01-03 14:28:52 +02004850 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004851 u32 ar;
4852
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004853 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004854 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02004855 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03004856 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004857 return;
Avi Kivity1390a282012-08-21 17:07:08 +03004858 var->base = vmx_read_guest_seg_base(vmx, seg);
4859 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4860 return;
Avi Kivitya9179492011-01-03 14:28:52 +02004861 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004862 var->base = vmx_read_guest_seg_base(vmx, seg);
4863 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4864 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4865 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03004866 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004867 var->type = ar & 15;
4868 var->s = (ar >> 4) & 1;
4869 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03004870 /*
4871 * Some userspaces do not preserve unusable property. Since usable
4872 * segment has to be present according to VMX spec we can use present
4873 * property to amend userspace bug by making unusable segment always
4874 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4875 * segment as unusable.
4876 */
4877 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004878 var->avl = (ar >> 12) & 1;
4879 var->l = (ar >> 13) & 1;
4880 var->db = (ar >> 14) & 1;
4881 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004882}
4883
Avi Kivitya9179492011-01-03 14:28:52 +02004884static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4885{
Avi Kivitya9179492011-01-03 14:28:52 +02004886 struct kvm_segment s;
4887
4888 if (to_vmx(vcpu)->rmode.vm86_active) {
4889 vmx_get_segment(vcpu, &s, seg);
4890 return s.base;
4891 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004892 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02004893}
4894
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004895static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02004896{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004897 struct vcpu_vmx *vmx = to_vmx(vcpu);
4898
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004899 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02004900 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004901 else {
4902 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004903 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02004904 }
Avi Kivity69c73022011-03-07 15:26:44 +02004905}
4906
Avi Kivity653e3102007-05-07 10:55:37 +03004907static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004908{
Avi Kivity6aa8b732006-12-10 02:21:36 -08004909 u32 ar;
4910
Avi Kivityf0495f92012-06-07 17:06:10 +03004911 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004912 ar = 1 << 16;
4913 else {
4914 ar = var->type & 15;
4915 ar |= (var->s & 1) << 4;
4916 ar |= (var->dpl & 3) << 5;
4917 ar |= (var->present & 1) << 7;
4918 ar |= (var->avl & 1) << 12;
4919 ar |= (var->l & 1) << 13;
4920 ar |= (var->db & 1) << 14;
4921 ar |= (var->g & 1) << 15;
4922 }
Avi Kivity653e3102007-05-07 10:55:37 +03004923
4924 return ar;
4925}
4926
4927static void vmx_set_segment(struct kvm_vcpu *vcpu,
4928 struct kvm_segment *var, int seg)
4929{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004930 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02004931 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03004932
Avi Kivity2fb92db2011-04-27 19:42:18 +03004933 vmx_segment_cache_clear(vmx);
4934
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004935 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4936 vmx->rmode.segs[seg] = *var;
4937 if (seg == VCPU_SREG_TR)
4938 vmcs_write16(sf->selector, var->selector);
4939 else if (var->s)
4940 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004941 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03004942 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004943
Avi Kivity653e3102007-05-07 10:55:37 +03004944 vmcs_writel(sf->base, var->base);
4945 vmcs_write32(sf->limit, var->limit);
4946 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004947
4948 /*
4949 * Fix the "Accessed" bit in AR field of segment registers for older
4950 * qemu binaries.
4951 * IA32 arch specifies that at the time of processor reset the
4952 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08004953 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004954 * state vmexit when "unrestricted guest" mode is turned on.
4955 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4956 * tree. Newer qemu binaries with that qemu fix would not need this
4957 * kvm hack.
4958 */
4959 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02004960 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004961
Gleb Natapovf924d662012-12-12 19:10:55 +02004962 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02004963
4964out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01004965 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004966}
4967
Avi Kivity6aa8b732006-12-10 02:21:36 -08004968static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4969{
Avi Kivity2fb92db2011-04-27 19:42:18 +03004970 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004971
4972 *db = (ar >> 14) & 1;
4973 *l = (ar >> 13) & 1;
4974}
4975
Gleb Natapov89a27f42010-02-16 10:51:48 +02004976static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004977{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004978 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4979 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004980}
4981
Gleb Natapov89a27f42010-02-16 10:51:48 +02004982static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004983{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004984 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4985 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004986}
4987
Gleb Natapov89a27f42010-02-16 10:51:48 +02004988static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004989{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004990 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4991 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004992}
4993
Gleb Natapov89a27f42010-02-16 10:51:48 +02004994static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004995{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004996 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4997 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004998}
4999
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005000static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5001{
5002 struct kvm_segment var;
5003 u32 ar;
5004
5005 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005006 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005007 if (seg == VCPU_SREG_CS)
5008 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005009 ar = vmx_segment_access_rights(&var);
5010
5011 if (var.base != (var.selector << 4))
5012 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005013 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005014 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005015 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005016 return false;
5017
5018 return true;
5019}
5020
5021static bool code_segment_valid(struct kvm_vcpu *vcpu)
5022{
5023 struct kvm_segment cs;
5024 unsigned int cs_rpl;
5025
5026 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005027 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005028
Avi Kivity1872a3f2009-01-04 23:26:52 +02005029 if (cs.unusable)
5030 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005031 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005032 return false;
5033 if (!cs.s)
5034 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005035 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005036 if (cs.dpl > cs_rpl)
5037 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005038 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005039 if (cs.dpl != cs_rpl)
5040 return false;
5041 }
5042 if (!cs.present)
5043 return false;
5044
5045 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5046 return true;
5047}
5048
5049static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5050{
5051 struct kvm_segment ss;
5052 unsigned int ss_rpl;
5053
5054 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005055 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005056
Avi Kivity1872a3f2009-01-04 23:26:52 +02005057 if (ss.unusable)
5058 return true;
5059 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005060 return false;
5061 if (!ss.s)
5062 return false;
5063 if (ss.dpl != ss_rpl) /* DPL != RPL */
5064 return false;
5065 if (!ss.present)
5066 return false;
5067
5068 return true;
5069}
5070
5071static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5072{
5073 struct kvm_segment var;
5074 unsigned int rpl;
5075
5076 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005077 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005078
Avi Kivity1872a3f2009-01-04 23:26:52 +02005079 if (var.unusable)
5080 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005081 if (!var.s)
5082 return false;
5083 if (!var.present)
5084 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005085 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005086 if (var.dpl < rpl) /* DPL < RPL */
5087 return false;
5088 }
5089
5090 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5091 * rights flags
5092 */
5093 return true;
5094}
5095
5096static bool tr_valid(struct kvm_vcpu *vcpu)
5097{
5098 struct kvm_segment tr;
5099
5100 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5101
Avi Kivity1872a3f2009-01-04 23:26:52 +02005102 if (tr.unusable)
5103 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005104 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005105 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005106 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005107 return false;
5108 if (!tr.present)
5109 return false;
5110
5111 return true;
5112}
5113
5114static bool ldtr_valid(struct kvm_vcpu *vcpu)
5115{
5116 struct kvm_segment ldtr;
5117
5118 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5119
Avi Kivity1872a3f2009-01-04 23:26:52 +02005120 if (ldtr.unusable)
5121 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005122 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005123 return false;
5124 if (ldtr.type != 2)
5125 return false;
5126 if (!ldtr.present)
5127 return false;
5128
5129 return true;
5130}
5131
5132static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5133{
5134 struct kvm_segment cs, ss;
5135
5136 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5137 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5138
Nadav Amitb32a9912015-03-29 16:33:04 +03005139 return ((cs.selector & SEGMENT_RPL_MASK) ==
5140 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005141}
5142
5143/*
5144 * Check if guest state is valid. Returns true if valid, false if
5145 * not.
5146 * We assume that registers are always usable
5147 */
5148static bool guest_state_valid(struct kvm_vcpu *vcpu)
5149{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005150 if (enable_unrestricted_guest)
5151 return true;
5152
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005153 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005154 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005155 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5156 return false;
5157 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5158 return false;
5159 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5160 return false;
5161 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5162 return false;
5163 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5164 return false;
5165 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5166 return false;
5167 } else {
5168 /* protected mode guest state checks */
5169 if (!cs_ss_rpl_check(vcpu))
5170 return false;
5171 if (!code_segment_valid(vcpu))
5172 return false;
5173 if (!stack_segment_valid(vcpu))
5174 return false;
5175 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5176 return false;
5177 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5178 return false;
5179 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5180 return false;
5181 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5182 return false;
5183 if (!tr_valid(vcpu))
5184 return false;
5185 if (!ldtr_valid(vcpu))
5186 return false;
5187 }
5188 /* TODO:
5189 * - Add checks on RIP
5190 * - Add checks on RFLAGS
5191 */
5192
5193 return true;
5194}
5195
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005196static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5197{
5198 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5199}
5200
Mike Dayd77c26f2007-10-08 09:02:08 -04005201static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005202{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005203 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005204 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005205 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005206
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005207 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005208 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005209 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5210 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005211 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005212 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005213 r = kvm_write_guest_page(kvm, fn++, &data,
5214 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005215 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005216 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005217 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5218 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005219 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005220 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5221 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005222 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005223 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005224 r = kvm_write_guest_page(kvm, fn, &data,
5225 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5226 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005227out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005228 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005229 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005230}
5231
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005232static int init_rmode_identity_map(struct kvm *kvm)
5233{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005234 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005235 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005236 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005237 u32 tmp;
5238
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005239 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005240 mutex_lock(&kvm->slots_lock);
5241
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005242 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005243 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005244
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005245 if (!kvm_vmx->ept_identity_map_addr)
5246 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5247 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005248
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005249 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005250 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005251 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005252 goto out2;
5253
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005254 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005255 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5256 if (r < 0)
5257 goto out;
5258 /* Set up identity-mapping pagetable for EPT in real mode */
5259 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5260 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5261 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5262 r = kvm_write_guest_page(kvm, identity_map_pfn,
5263 &tmp, i * sizeof(tmp), sizeof(tmp));
5264 if (r < 0)
5265 goto out;
5266 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005267 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005268
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005269out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005270 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005271
5272out2:
5273 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005274 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005275}
5276
Avi Kivity6aa8b732006-12-10 02:21:36 -08005277static void seg_setup(int seg)
5278{
Mathias Krause772e0312012-08-30 01:30:19 +02005279 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005280 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005281
5282 vmcs_write16(sf->selector, 0);
5283 vmcs_writel(sf->base, 0);
5284 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005285 ar = 0x93;
5286 if (seg == VCPU_SREG_CS)
5287 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005288
5289 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005290}
5291
Sheng Yangf78e0e22007-10-29 09:40:42 +08005292static int alloc_apic_access_page(struct kvm *kvm)
5293{
Xiao Guangrong44841412012-09-07 14:14:20 +08005294 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005295 int r = 0;
5296
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005297 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005298 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005299 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005300 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5301 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005302 if (r)
5303 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005304
Tang Chen73a6d942014-09-11 13:38:00 +08005305 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005306 if (is_error_page(page)) {
5307 r = -EFAULT;
5308 goto out;
5309 }
5310
Tang Chenc24ae0d2014-09-24 15:57:58 +08005311 /*
5312 * Do not pin the page in memory, so that memory hot-unplug
5313 * is able to migrate it.
5314 */
5315 put_page(page);
5316 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005317out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005318 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005319 return r;
5320}
5321
Wanpeng Li991e7a02015-09-16 17:30:05 +08005322static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005323{
5324 int vpid;
5325
Avi Kivity919818a2009-03-23 18:01:29 +02005326 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005327 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005328 spin_lock(&vmx_vpid_lock);
5329 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005330 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005331 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005332 else
5333 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005334 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005335 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005336}
5337
Wanpeng Li991e7a02015-09-16 17:30:05 +08005338static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005339{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005340 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005341 return;
5342 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005343 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005344 spin_unlock(&vmx_vpid_lock);
5345}
5346
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005347static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5348 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005349{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005350 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005351
5352 if (!cpu_has_vmx_msr_bitmap())
5353 return;
5354
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005355 if (static_branch_unlikely(&enable_evmcs))
5356 evmcs_touch_msr_bitmap();
5357
Sheng Yang25c5f222008-03-28 13:18:56 +08005358 /*
5359 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5360 * have the write-low and read-high bitmap offsets the wrong way round.
5361 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5362 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005363 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005364 if (type & MSR_TYPE_R)
5365 /* read-low */
5366 __clear_bit(msr, msr_bitmap + 0x000 / f);
5367
5368 if (type & MSR_TYPE_W)
5369 /* write-low */
5370 __clear_bit(msr, msr_bitmap + 0x800 / f);
5371
Sheng Yang25c5f222008-03-28 13:18:56 +08005372 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5373 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005374 if (type & MSR_TYPE_R)
5375 /* read-high */
5376 __clear_bit(msr, msr_bitmap + 0x400 / f);
5377
5378 if (type & MSR_TYPE_W)
5379 /* write-high */
5380 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5381
5382 }
5383}
5384
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005385static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5386 u32 msr, int type)
5387{
5388 int f = sizeof(unsigned long);
5389
5390 if (!cpu_has_vmx_msr_bitmap())
5391 return;
5392
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005393 if (static_branch_unlikely(&enable_evmcs))
5394 evmcs_touch_msr_bitmap();
5395
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005396 /*
5397 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5398 * have the write-low and read-high bitmap offsets the wrong way round.
5399 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5400 */
5401 if (msr <= 0x1fff) {
5402 if (type & MSR_TYPE_R)
5403 /* read-low */
5404 __set_bit(msr, msr_bitmap + 0x000 / f);
5405
5406 if (type & MSR_TYPE_W)
5407 /* write-low */
5408 __set_bit(msr, msr_bitmap + 0x800 / f);
5409
5410 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5411 msr &= 0x1fff;
5412 if (type & MSR_TYPE_R)
5413 /* read-high */
5414 __set_bit(msr, msr_bitmap + 0x400 / f);
5415
5416 if (type & MSR_TYPE_W)
5417 /* write-high */
5418 __set_bit(msr, msr_bitmap + 0xc00 / f);
5419
5420 }
5421}
5422
5423static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5424 u32 msr, int type, bool value)
5425{
5426 if (value)
5427 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5428 else
5429 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5430}
5431
Wincy Vanf2b93282015-02-03 23:56:03 +08005432/*
5433 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5434 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5435 */
5436static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5437 unsigned long *msr_bitmap_nested,
5438 u32 msr, int type)
5439{
5440 int f = sizeof(unsigned long);
5441
Wincy Vanf2b93282015-02-03 23:56:03 +08005442 /*
5443 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5444 * have the write-low and read-high bitmap offsets the wrong way round.
5445 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5446 */
5447 if (msr <= 0x1fff) {
5448 if (type & MSR_TYPE_R &&
5449 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5450 /* read-low */
5451 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5452
5453 if (type & MSR_TYPE_W &&
5454 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5455 /* write-low */
5456 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5457
5458 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5459 msr &= 0x1fff;
5460 if (type & MSR_TYPE_R &&
5461 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5462 /* read-high */
5463 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5464
5465 if (type & MSR_TYPE_W &&
5466 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5467 /* write-high */
5468 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5469
5470 }
5471}
5472
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005473static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02005474{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005475 u8 mode = 0;
5476
5477 if (cpu_has_secondary_exec_ctrls() &&
5478 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5479 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5480 mode |= MSR_BITMAP_MODE_X2APIC;
5481 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5482 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5483 }
5484
5485 if (is_long_mode(vcpu))
5486 mode |= MSR_BITMAP_MODE_LM;
5487
5488 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005489}
5490
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005491#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5492
5493static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5494 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005495{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005496 int msr;
5497
5498 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5499 unsigned word = msr / BITS_PER_LONG;
5500 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5501 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005502 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005503
5504 if (mode & MSR_BITMAP_MODE_X2APIC) {
5505 /*
5506 * TPR reads and writes can be virtualized even if virtual interrupt
5507 * delivery is not in use.
5508 */
5509 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5510 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5511 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5512 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5513 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5514 }
5515 }
5516}
5517
5518static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5519{
5520 struct vcpu_vmx *vmx = to_vmx(vcpu);
5521 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5522 u8 mode = vmx_msr_bitmap_mode(vcpu);
5523 u8 changed = mode ^ vmx->msr_bitmap_mode;
5524
5525 if (!changed)
5526 return;
5527
5528 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5529 !(mode & MSR_BITMAP_MODE_LM));
5530
5531 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5532 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5533
5534 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005535}
5536
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005537static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005538{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005539 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005540}
5541
David Matlackc9f04402017-08-01 14:00:40 -07005542static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5543{
5544 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5545 gfn_t gfn;
5546
5547 /*
5548 * Don't need to mark the APIC access page dirty; it is never
5549 * written to by the CPU during APIC virtualization.
5550 */
5551
5552 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5553 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5554 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5555 }
5556
5557 if (nested_cpu_has_posted_intr(vmcs12)) {
5558 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5559 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5560 }
5561}
5562
5563
David Hildenbrand6342c502017-01-25 11:58:58 +01005564static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005565{
5566 struct vcpu_vmx *vmx = to_vmx(vcpu);
5567 int max_irr;
5568 void *vapic_page;
5569 u16 status;
5570
David Matlackc9f04402017-08-01 14:00:40 -07005571 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5572 return;
Wincy Van705699a2015-02-03 23:58:17 +08005573
David Matlackc9f04402017-08-01 14:00:40 -07005574 vmx->nested.pi_pending = false;
5575 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5576 return;
Wincy Van705699a2015-02-03 23:58:17 +08005577
David Matlackc9f04402017-08-01 14:00:40 -07005578 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5579 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005580 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02005581 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5582 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08005583 kunmap(vmx->nested.virtual_apic_page);
5584
5585 status = vmcs_read16(GUEST_INTR_STATUS);
5586 if ((u8)max_irr > ((u8)status & 0xff)) {
5587 status &= ~0xff;
5588 status |= (u8)max_irr;
5589 vmcs_write16(GUEST_INTR_STATUS, status);
5590 }
5591 }
David Matlackc9f04402017-08-01 14:00:40 -07005592
5593 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005594}
5595
Wincy Van06a55242017-04-28 13:13:59 +08005596static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5597 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005598{
5599#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005600 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5601
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005602 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005603 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005604 * The vector of interrupt to be delivered to vcpu had
5605 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005606 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005607 * Following cases will be reached in this block, and
5608 * we always send a notification event in all cases as
5609 * explained below.
5610 *
5611 * Case 1: vcpu keeps in non-root mode. Sending a
5612 * notification event posts the interrupt to vcpu.
5613 *
5614 * Case 2: vcpu exits to root mode and is still
5615 * runnable. PIR will be synced to vIRR before the
5616 * next vcpu entry. Sending a notification event in
5617 * this case has no effect, as vcpu is not in root
5618 * mode.
5619 *
5620 * Case 3: vcpu exits to root mode and is blocked.
5621 * vcpu_block() has already synced PIR to vIRR and
5622 * never blocks vcpu if vIRR is not cleared. Therefore,
5623 * a blocked vcpu here does not wait for any requested
5624 * interrupts in PIR, and sending a notification event
5625 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005626 */
Feng Wu28b835d2015-09-18 22:29:54 +08005627
Wincy Van06a55242017-04-28 13:13:59 +08005628 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005629 return true;
5630 }
5631#endif
5632 return false;
5633}
5634
Wincy Van705699a2015-02-03 23:58:17 +08005635static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5636 int vector)
5637{
5638 struct vcpu_vmx *vmx = to_vmx(vcpu);
5639
5640 if (is_guest_mode(vcpu) &&
5641 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005642 /*
5643 * If a posted intr is not recognized by hardware,
5644 * we will accomplish it in the next vmentry.
5645 */
5646 vmx->nested.pi_pending = true;
5647 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02005648 /* the PIR and ON have been set by L1. */
5649 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
5650 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005651 return 0;
5652 }
5653 return -1;
5654}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005655/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005656 * Send interrupt to vcpu via posted interrupt way.
5657 * 1. If target vcpu is running(non-root mode), send posted interrupt
5658 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5659 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5660 * interrupt from PIR in next vmentry.
5661 */
5662static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5663{
5664 struct vcpu_vmx *vmx = to_vmx(vcpu);
5665 int r;
5666
Wincy Van705699a2015-02-03 23:58:17 +08005667 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5668 if (!r)
5669 return;
5670
Yang Zhanga20ed542013-04-11 19:25:15 +08005671 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5672 return;
5673
Paolo Bonzinib95234c2016-12-19 13:57:33 +01005674 /* If a previous notification has sent the IPI, nothing to do. */
5675 if (pi_test_and_set_on(&vmx->pi_desc))
5676 return;
5677
Wincy Van06a55242017-04-28 13:13:59 +08005678 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08005679 kvm_vcpu_kick(vcpu);
5680}
5681
Avi Kivity6aa8b732006-12-10 02:21:36 -08005682/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005683 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5684 * will not change in the lifetime of the guest.
5685 * Note that host-state that does change is set elsewhere. E.g., host-state
5686 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5687 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005688static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005689{
5690 u32 low32, high32;
5691 unsigned long tmpl;
5692 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005693 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005694
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07005695 cr0 = read_cr0();
5696 WARN_ON(cr0 & X86_CR0_TS);
5697 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005698
5699 /*
5700 * Save the most likely value for this task's CR3 in the VMCS.
5701 * We can't use __get_current_cr3_fast() because we're not atomic.
5702 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07005703 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005704 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02005705 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005706
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005707 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005708 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005709 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02005710 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005711
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005712 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005713#ifdef CONFIG_X86_64
5714 /*
5715 * Load null selectors, so we can avoid reloading them in
5716 * __vmx_load_host_state(), in case userspace uses the null selectors
5717 * too (the expected case).
5718 */
5719 vmcs_write16(HOST_DS_SELECTOR, 0);
5720 vmcs_write16(HOST_ES_SELECTOR, 0);
5721#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005722 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5723 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005724#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005725 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5726 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5727
Juergen Gross87930012017-09-04 12:25:27 +02005728 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005729 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005730 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005731
Avi Kivity83287ea422012-09-16 15:10:57 +03005732 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005733
5734 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5735 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5736 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5737 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5738
5739 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5740 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5741 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5742 }
5743}
5744
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005745static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5746{
5747 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5748 if (enable_ept)
5749 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005750 if (is_guest_mode(&vmx->vcpu))
5751 vmx->vcpu.arch.cr4_guest_owned_bits &=
5752 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005753 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5754}
5755
Yang Zhang01e439b2013-04-11 19:25:12 +08005756static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5757{
5758 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5759
Andrey Smetanind62caab2015-11-10 15:36:33 +03005760 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005761 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005762
5763 if (!enable_vnmi)
5764 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
5765
Yunhong Jiang64672c92016-06-13 14:19:59 -07005766 /* Enable the preemption timer dynamically */
5767 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08005768 return pin_based_exec_ctrl;
5769}
5770
Andrey Smetanind62caab2015-11-10 15:36:33 +03005771static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5772{
5773 struct vcpu_vmx *vmx = to_vmx(vcpu);
5774
5775 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03005776 if (cpu_has_secondary_exec_ctrls()) {
5777 if (kvm_vcpu_apicv_active(vcpu))
5778 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5779 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5780 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5781 else
5782 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5783 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5784 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5785 }
5786
5787 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005788 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03005789}
5790
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005791static u32 vmx_exec_control(struct vcpu_vmx *vmx)
5792{
5793 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01005794
5795 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
5796 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5797
Paolo Bonzini35754c92015-07-29 12:05:37 +02005798 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005799 exec_control &= ~CPU_BASED_TPR_SHADOW;
5800#ifdef CONFIG_X86_64
5801 exec_control |= CPU_BASED_CR8_STORE_EXITING |
5802 CPU_BASED_CR8_LOAD_EXITING;
5803#endif
5804 }
5805 if (!enable_ept)
5806 exec_control |= CPU_BASED_CR3_STORE_EXITING |
5807 CPU_BASED_CR3_LOAD_EXITING |
5808 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07005809 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
5810 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
5811 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07005812 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
5813 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005814 return exec_control;
5815}
5816
Jim Mattson45ec3682017-08-23 16:32:04 -07005817static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005818{
Jim Mattson45ec3682017-08-23 16:32:04 -07005819 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02005820 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005821}
5822
Jim Mattson75f4fc82017-08-23 16:32:03 -07005823static bool vmx_rdseed_supported(void)
5824{
5825 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02005826 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005827}
5828
Paolo Bonzini80154d72017-08-24 13:55:35 +02005829static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005830{
Paolo Bonzini80154d72017-08-24 13:55:35 +02005831 struct kvm_vcpu *vcpu = &vmx->vcpu;
5832
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005833 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02005834
Paolo Bonzini80154d72017-08-24 13:55:35 +02005835 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005836 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5837 if (vmx->vpid == 0)
5838 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
5839 if (!enable_ept) {
5840 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
5841 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00005842 /* Enable INVPCID for non-ept guests may cause performance regression. */
5843 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005844 }
5845 if (!enable_unrestricted_guest)
5846 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07005847 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005848 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02005849 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08005850 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
5851 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08005852 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02005853
5854 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
5855 * in vmx_set_cr4. */
5856 exec_control &= ~SECONDARY_EXEC_DESC;
5857
Abel Gordonabc4fc52013-04-18 14:35:25 +03005858 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
5859 (handle_vmptrld).
5860 We can NOT enable shadow_vmcs here because we don't have yet
5861 a current VMCS12
5862 */
5863 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08005864
5865 if (!enable_pml)
5866 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08005867
Paolo Bonzini3db13482017-08-24 14:48:03 +02005868 if (vmx_xsaves_supported()) {
5869 /* Exposing XSAVES only when XSAVE is exposed */
5870 bool xsaves_enabled =
5871 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
5872 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
5873
5874 if (!xsaves_enabled)
5875 exec_control &= ~SECONDARY_EXEC_XSAVES;
5876
5877 if (nested) {
5878 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005879 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02005880 SECONDARY_EXEC_XSAVES;
5881 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005882 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02005883 ~SECONDARY_EXEC_XSAVES;
5884 }
5885 }
5886
Paolo Bonzini80154d72017-08-24 13:55:35 +02005887 if (vmx_rdtscp_supported()) {
5888 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
5889 if (!rdtscp_enabled)
5890 exec_control &= ~SECONDARY_EXEC_RDTSCP;
5891
5892 if (nested) {
5893 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005894 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02005895 SECONDARY_EXEC_RDTSCP;
5896 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005897 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02005898 ~SECONDARY_EXEC_RDTSCP;
5899 }
5900 }
5901
5902 if (vmx_invpcid_supported()) {
5903 /* Exposing INVPCID only when PCID is exposed */
5904 bool invpcid_enabled =
5905 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
5906 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
5907
5908 if (!invpcid_enabled) {
5909 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
5910 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
5911 }
5912
5913 if (nested) {
5914 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005915 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02005916 SECONDARY_EXEC_ENABLE_INVPCID;
5917 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005918 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02005919 ~SECONDARY_EXEC_ENABLE_INVPCID;
5920 }
5921 }
5922
Jim Mattson45ec3682017-08-23 16:32:04 -07005923 if (vmx_rdrand_supported()) {
5924 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
5925 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02005926 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005927
5928 if (nested) {
5929 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005930 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005931 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005932 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005933 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005934 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005935 }
5936 }
5937
Jim Mattson75f4fc82017-08-23 16:32:03 -07005938 if (vmx_rdseed_supported()) {
5939 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
5940 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02005941 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005942
5943 if (nested) {
5944 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005945 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005946 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005947 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005948 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005949 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005950 }
5951 }
5952
Paolo Bonzini80154d72017-08-24 13:55:35 +02005953 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005954}
5955
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005956static void ept_set_mmio_spte_mask(void)
5957{
5958 /*
5959 * EPT Misconfigurations can be generated if the value of bits 2:0
5960 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005961 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07005962 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
5963 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005964}
5965
Wanpeng Lif53cd632014-12-02 19:14:58 +08005966#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005967/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005968 * Sets up the vmcs for emulated real mode.
5969 */
David Hildenbrand12d79912017-08-24 20:51:26 +02005970static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005971{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005972#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005973 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005974#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08005975 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005976
Abel Gordon4607c2d2013-04-18 14:35:55 +03005977 if (enable_shadow_vmcs) {
5978 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5979 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
5980 }
Sheng Yang25c5f222008-03-28 13:18:56 +08005981 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005982 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08005983
Avi Kivity6aa8b732006-12-10 02:21:36 -08005984 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5985
Avi Kivity6aa8b732006-12-10 02:21:36 -08005986 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08005987 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07005988 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005989
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005990 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005991
Dan Williamsdfa169b2016-06-02 11:17:24 -07005992 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02005993 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005994 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02005995 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07005996 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08005997
Andrey Smetanind62caab2015-11-10 15:36:33 +03005998 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08005999 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6000 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6001 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6002 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6003
6004 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006005
Li RongQing0bcf2612015-12-03 13:29:34 +08006006 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006007 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006008 }
6009
Wanpeng Lib31c1142018-03-12 04:53:04 -07006010 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006011 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006012 vmx->ple_window = ple_window;
6013 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006014 }
6015
Xiao Guangrongc3707952011-07-12 03:28:04 +08006016 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6017 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006018 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6019
Avi Kivity9581d442010-10-19 16:46:55 +02006020 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6021 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006022 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006023#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006024 rdmsrl(MSR_FS_BASE, a);
6025 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
6026 rdmsrl(MSR_GS_BASE, a);
6027 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
6028#else
6029 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6030 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
6031#endif
6032
Bandan Das2a499e42017-08-03 15:54:41 -04006033 if (cpu_has_vmx_vmfunc())
6034 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6035
Eddie Dong2cc51562007-05-21 07:28:09 +03006036 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6037 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006038 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03006039 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006040 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006041
Radim Krčmář74545702015-04-27 15:11:25 +02006042 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6043 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006044
Paolo Bonzini03916db2014-07-24 14:21:57 +02006045 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006046 u32 index = vmx_msr_index[i];
6047 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006048 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006049
6050 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6051 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006052 if (wrmsr_safe(index, data_low, data_high) < 0)
6053 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006054 vmx->guest_msrs[j].index = i;
6055 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006056 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006057 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006058 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006059
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01006060 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
6061 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02006062
6063 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006064
6065 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006066 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006067
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006068 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6069 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6070
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006071 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006072
Wanpeng Lif53cd632014-12-02 19:14:58 +08006073 if (vmx_xsaves_supported())
6074 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6075
Peter Feiner4e595162016-07-07 14:49:58 -07006076 if (enable_pml) {
6077 ASSERT(vmx->pml_pg);
6078 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6079 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6080 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006081}
6082
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006083static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006084{
6085 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006086 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006087 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006088
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006089 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006090 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006091
Wanpeng Li518e7b92018-02-28 14:03:31 +08006092 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006093 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006094 kvm_set_cr8(vcpu, 0);
6095
6096 if (!init_event) {
6097 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6098 MSR_IA32_APICBASE_ENABLE;
6099 if (kvm_vcpu_is_reset_bsp(vcpu))
6100 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6101 apic_base_msr.host_initiated = true;
6102 kvm_set_apic_base(vcpu, &apic_base_msr);
6103 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006104
Avi Kivity2fb92db2011-04-27 19:42:18 +03006105 vmx_segment_cache_clear(vmx);
6106
Avi Kivity5706be02008-08-20 15:07:31 +03006107 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006108 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006109 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006110
6111 seg_setup(VCPU_SREG_DS);
6112 seg_setup(VCPU_SREG_ES);
6113 seg_setup(VCPU_SREG_FS);
6114 seg_setup(VCPU_SREG_GS);
6115 seg_setup(VCPU_SREG_SS);
6116
6117 vmcs_write16(GUEST_TR_SELECTOR, 0);
6118 vmcs_writel(GUEST_TR_BASE, 0);
6119 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6120 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6121
6122 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6123 vmcs_writel(GUEST_LDTR_BASE, 0);
6124 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6125 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6126
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006127 if (!init_event) {
6128 vmcs_write32(GUEST_SYSENTER_CS, 0);
6129 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6130 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6131 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6132 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006133
Wanpeng Lic37c2872017-11-20 14:52:21 -08006134 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006135 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006136
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006137 vmcs_writel(GUEST_GDTR_BASE, 0);
6138 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6139
6140 vmcs_writel(GUEST_IDTR_BASE, 0);
6141 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6142
Anthony Liguori443381a2010-12-06 10:53:38 -06006143 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006144 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006145 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006146 if (kvm_mpx_supported())
6147 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006148
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006149 setup_msrs(vmx);
6150
Avi Kivity6aa8b732006-12-10 02:21:36 -08006151 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6152
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006153 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006154 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006155 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006156 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006157 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006158 vmcs_write32(TPR_THRESHOLD, 0);
6159 }
6160
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006161 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006162
Sheng Yang2384d2b2008-01-17 15:14:33 +08006163 if (vmx->vpid != 0)
6164 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6165
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006166 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006167 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006168 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006169 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006170 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006171
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006172 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006173
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006174 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006175 if (init_event)
6176 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006177}
6178
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006179/*
6180 * In nested virtualization, check if L1 asked to exit on external interrupts.
6181 * For most existing hypervisors, this will always return true.
6182 */
6183static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6184{
6185 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6186 PIN_BASED_EXT_INTR_MASK;
6187}
6188
Bandan Das77b0f5d2014-04-19 18:17:45 -04006189/*
6190 * In nested virtualization, check if L1 has set
6191 * VM_EXIT_ACK_INTR_ON_EXIT
6192 */
6193static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6194{
6195 return get_vmcs12(vcpu)->vm_exit_controls &
6196 VM_EXIT_ACK_INTR_ON_EXIT;
6197}
6198
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006199static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6200{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006201 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006202}
6203
Jan Kiszkac9a79532014-03-07 20:03:15 +01006204static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006205{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006206 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6207 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006208}
6209
Jan Kiszkac9a79532014-03-07 20:03:15 +01006210static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006211{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006212 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006213 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006214 enable_irq_window(vcpu);
6215 return;
6216 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006217
Paolo Bonzini47c01522016-12-19 11:44:07 +01006218 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6219 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006220}
6221
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006222static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006223{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006224 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006225 uint32_t intr;
6226 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006227
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006228 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006229
Avi Kivityfa89a812008-09-01 15:57:51 +03006230 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006231 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006232 int inc_eip = 0;
6233 if (vcpu->arch.interrupt.soft)
6234 inc_eip = vcpu->arch.event_exit_inst_len;
6235 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006236 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006237 return;
6238 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006239 intr = irq | INTR_INFO_VALID_MASK;
6240 if (vcpu->arch.interrupt.soft) {
6241 intr |= INTR_TYPE_SOFT_INTR;
6242 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6243 vmx->vcpu.arch.event_exit_inst_len);
6244 } else
6245 intr |= INTR_TYPE_EXT_INTR;
6246 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006247
6248 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006249}
6250
Sheng Yangf08864b2008-05-15 18:23:25 +08006251static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6252{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006253 struct vcpu_vmx *vmx = to_vmx(vcpu);
6254
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006255 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006256 /*
6257 * Tracking the NMI-blocked state in software is built upon
6258 * finding the next open IRQ window. This, in turn, depends on
6259 * well-behaving guests: They have to keep IRQs disabled at
6260 * least as long as the NMI handler runs. Otherwise we may
6261 * cause NMI nesting, maybe breaking the guest. But as this is
6262 * highly unlikely, we can live with the residual risk.
6263 */
6264 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6265 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6266 }
6267
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006268 ++vcpu->stat.nmi_injections;
6269 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006270
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006271 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006272 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006273 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006274 return;
6275 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006276
Sheng Yangf08864b2008-05-15 18:23:25 +08006277 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6278 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006279
6280 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006281}
6282
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006283static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6284{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006285 struct vcpu_vmx *vmx = to_vmx(vcpu);
6286 bool masked;
6287
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006288 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006289 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006290 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006291 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006292 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6293 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6294 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006295}
6296
6297static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6298{
6299 struct vcpu_vmx *vmx = to_vmx(vcpu);
6300
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006301 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006302 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6303 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6304 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6305 }
6306 } else {
6307 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6308 if (masked)
6309 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6310 GUEST_INTR_STATE_NMI);
6311 else
6312 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6313 GUEST_INTR_STATE_NMI);
6314 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006315}
6316
Jan Kiszka2505dc92013-04-14 12:12:47 +02006317static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6318{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006319 if (to_vmx(vcpu)->nested.nested_run_pending)
6320 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006321
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006322 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006323 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6324 return 0;
6325
Jan Kiszka2505dc92013-04-14 12:12:47 +02006326 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6327 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6328 | GUEST_INTR_STATE_NMI));
6329}
6330
Gleb Natapov78646122009-03-23 12:12:11 +02006331static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6332{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006333 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6334 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006335 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6336 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006337}
6338
Izik Eiduscbc94022007-10-25 00:29:55 +02006339static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6340{
6341 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006342
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006343 if (enable_unrestricted_guest)
6344 return 0;
6345
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006346 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6347 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006348 if (ret)
6349 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006350 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006351 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006352}
6353
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006354static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6355{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006356 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006357 return 0;
6358}
6359
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006360static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006361{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006362 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006363 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006364 /*
6365 * Update instruction length as we may reinject the exception
6366 * from user space while in guest debugging mode.
6367 */
6368 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6369 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006370 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006371 return false;
6372 /* fall through */
6373 case DB_VECTOR:
6374 if (vcpu->guest_debug &
6375 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6376 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006377 /* fall through */
6378 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006379 case OF_VECTOR:
6380 case BR_VECTOR:
6381 case UD_VECTOR:
6382 case DF_VECTOR:
6383 case SS_VECTOR:
6384 case GP_VECTOR:
6385 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006386 return true;
6387 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006388 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006389 return false;
6390}
6391
6392static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6393 int vec, u32 err_code)
6394{
6395 /*
6396 * Instruction with address size override prefix opcode 0x67
6397 * Cause the #SS fault with 0 error code in VM86 mode.
6398 */
6399 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6400 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6401 if (vcpu->arch.halt_request) {
6402 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006403 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006404 }
6405 return 1;
6406 }
6407 return 0;
6408 }
6409
6410 /*
6411 * Forward all other exceptions that are valid in real mode.
6412 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6413 * the required debugging infrastructure rework.
6414 */
6415 kvm_queue_exception(vcpu, vec);
6416 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006417}
6418
Andi Kleena0861c02009-06-08 17:37:09 +08006419/*
6420 * Trigger machine check on the host. We assume all the MSRs are already set up
6421 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6422 * We pass a fake environment to the machine check handler because we want
6423 * the guest to be always treated like user space, no matter what context
6424 * it used internally.
6425 */
6426static void kvm_machine_check(void)
6427{
6428#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6429 struct pt_regs regs = {
6430 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6431 .flags = X86_EFLAGS_IF,
6432 };
6433
6434 do_machine_check(&regs, 0);
6435#endif
6436}
6437
Avi Kivity851ba692009-08-24 11:10:17 +03006438static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08006439{
6440 /* already handled by vcpu_run */
6441 return 1;
6442}
6443
Avi Kivity851ba692009-08-24 11:10:17 +03006444static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006445{
Avi Kivity1155f762007-11-22 11:30:47 +02006446 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006447 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006448 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006449 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006450 u32 vect_info;
6451 enum emulation_result er;
6452
Avi Kivity1155f762007-11-22 11:30:47 +02006453 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02006454 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006455
Andi Kleena0861c02009-06-08 17:37:09 +08006456 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03006457 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006458
Jim Mattsonef85b672016-12-12 11:01:37 -08006459 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02006460 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03006461
Wanpeng Li082d06e2018-04-03 16:28:48 -07006462 if (is_invalid_opcode(intr_info))
6463 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006464
Avi Kivity6aa8b732006-12-10 02:21:36 -08006465 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06006466 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006467 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006468
Liran Alon9e869482018-03-12 13:12:51 +02006469 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
6470 WARN_ON_ONCE(!enable_vmware_backdoor);
6471 er = emulate_instruction(vcpu,
6472 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
6473 if (er == EMULATE_USER_EXIT)
6474 return 0;
6475 else if (er != EMULATE_DONE)
6476 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
6477 return 1;
6478 }
6479
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006480 /*
6481 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6482 * MMIO, it is better to report an internal error.
6483 * See the comments in vmx_handle_exit.
6484 */
6485 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6486 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6487 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6488 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006489 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006490 vcpu->run->internal.data[0] = vect_info;
6491 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006492 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006493 return 0;
6494 }
6495
Avi Kivity6aa8b732006-12-10 02:21:36 -08006496 if (is_page_fault(intr_info)) {
6497 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006498 /* EPT won't cause page fault directly */
6499 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02006500 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006501 }
6502
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006503 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006504
6505 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6506 return handle_rmode_exception(vcpu, ex_no, error_code);
6507
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006508 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01006509 case AC_VECTOR:
6510 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6511 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006512 case DB_VECTOR:
6513 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6514 if (!(vcpu->guest_debug &
6515 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01006516 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006517 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07006518 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01006519 skip_emulated_instruction(vcpu);
6520
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006521 kvm_queue_exception(vcpu, DB_VECTOR);
6522 return 1;
6523 }
6524 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6525 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6526 /* fall through */
6527 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006528 /*
6529 * Update instruction length as we may reinject #BP from
6530 * user space while in guest debugging mode. Reading it for
6531 * #DB as well causes no harm, it is not used in that case.
6532 */
6533 vmx->vcpu.arch.event_exit_inst_len =
6534 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006535 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006536 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006537 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6538 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006539 break;
6540 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006541 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6542 kvm_run->ex.exception = ex_no;
6543 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006544 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006545 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006546 return 0;
6547}
6548
Avi Kivity851ba692009-08-24 11:10:17 +03006549static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006550{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006551 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006552 return 1;
6553}
6554
Avi Kivity851ba692009-08-24 11:10:17 +03006555static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006556{
Avi Kivity851ba692009-08-24 11:10:17 +03006557 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006558 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006559 return 0;
6560}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006561
Avi Kivity851ba692009-08-24 11:10:17 +03006562static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006563{
He, Qingbfdaab02007-09-12 14:18:28 +08006564 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08006565 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02006566 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006567
He, Qingbfdaab02007-09-12 14:18:28 +08006568 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006569 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006570
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006571 ++vcpu->stat.io_exits;
6572
Sean Christopherson432baf62018-03-08 08:57:26 -08006573 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01006574 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006575
6576 port = exit_qualification >> 16;
6577 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08006578 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006579
Sean Christophersondca7f122018-03-08 08:57:27 -08006580 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006581}
6582
Ingo Molnar102d8322007-02-19 14:37:47 +02006583static void
6584vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6585{
6586 /*
6587 * Patch in the VMCALL instruction:
6588 */
6589 hypercall[0] = 0x0f;
6590 hypercall[1] = 0x01;
6591 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006592}
6593
Guo Chao0fa06072012-06-28 15:16:19 +08006594/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006595static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6596{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006597 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006598 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6599 unsigned long orig_val = val;
6600
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006601 /*
6602 * We get here when L2 changed cr0 in a way that did not change
6603 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006604 * but did change L0 shadowed bits. So we first calculate the
6605 * effective cr0 value that L1 would like to write into the
6606 * hardware. It consists of the L2-owned bits from the new
6607 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006608 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006609 val = (val & ~vmcs12->cr0_guest_host_mask) |
6610 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6611
David Matlack38991522016-11-29 18:14:08 -08006612 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006613 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006614
6615 if (kvm_set_cr0(vcpu, val))
6616 return 1;
6617 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006618 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006619 } else {
6620 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08006621 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006622 return 1;
David Matlack38991522016-11-29 18:14:08 -08006623
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006624 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006625 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006626}
6627
6628static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6629{
6630 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006631 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6632 unsigned long orig_val = val;
6633
6634 /* analogously to handle_set_cr0 */
6635 val = (val & ~vmcs12->cr4_guest_host_mask) |
6636 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6637 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006638 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006639 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006640 return 0;
6641 } else
6642 return kvm_set_cr4(vcpu, val);
6643}
6644
Paolo Bonzini0367f202016-07-12 10:44:55 +02006645static int handle_desc(struct kvm_vcpu *vcpu)
6646{
6647 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
6648 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6649}
6650
Avi Kivity851ba692009-08-24 11:10:17 +03006651static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006652{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006653 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006654 int cr;
6655 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03006656 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006657 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006658
He, Qingbfdaab02007-09-12 14:18:28 +08006659 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006660 cr = exit_qualification & 15;
6661 reg = (exit_qualification >> 8) & 15;
6662 switch ((exit_qualification >> 4) & 3) {
6663 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03006664 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006665 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006666 switch (cr) {
6667 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006668 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006669 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006670 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006671 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03006672 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006673 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006674 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006675 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006676 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006677 case 8: {
6678 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03006679 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006680 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006681 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006682 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08006683 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006684 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006685 return ret;
6686 /*
6687 * TODO: we might be squashing a
6688 * KVM_GUESTDBG_SINGLESTEP-triggered
6689 * KVM_EXIT_DEBUG here.
6690 */
Avi Kivity851ba692009-08-24 11:10:17 +03006691 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006692 return 0;
6693 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02006694 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006695 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03006696 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006697 WARN_ONCE(1, "Guest should always own CR0.TS");
6698 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02006699 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08006700 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006701 case 1: /*mov from cr*/
6702 switch (cr) {
6703 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006704 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02006705 val = kvm_read_cr3(vcpu);
6706 kvm_register_write(vcpu, reg, val);
6707 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006708 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006709 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006710 val = kvm_get_cr8(vcpu);
6711 kvm_register_write(vcpu, reg, val);
6712 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006713 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006714 }
6715 break;
6716 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02006717 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02006718 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02006719 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006720
Kyle Huey6affcbe2016-11-29 12:40:40 -08006721 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006722 default:
6723 break;
6724 }
Avi Kivity851ba692009-08-24 11:10:17 +03006725 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03006726 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08006727 (int)(exit_qualification >> 4) & 3, cr);
6728 return 0;
6729}
6730
Avi Kivity851ba692009-08-24 11:10:17 +03006731static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006732{
He, Qingbfdaab02007-09-12 14:18:28 +08006733 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006734 int dr, dr7, reg;
6735
6736 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6737 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6738
6739 /* First, if DR does not exist, trigger UD */
6740 if (!kvm_require_dr(vcpu, dr))
6741 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006742
Jan Kiszkaf2483412010-01-20 18:20:20 +01006743 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03006744 if (!kvm_require_cpl(vcpu, 0))
6745 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006746 dr7 = vmcs_readl(GUEST_DR7);
6747 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006748 /*
6749 * As the vm-exit takes precedence over the debug trap, we
6750 * need to emulate the latter, either for the host or the
6751 * guest debugging itself.
6752 */
6753 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03006754 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006755 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02006756 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006757 vcpu->run->debug.arch.exception = DB_VECTOR;
6758 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006759 return 0;
6760 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02006761 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006762 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006763 kvm_queue_exception(vcpu, DB_VECTOR);
6764 return 1;
6765 }
6766 }
6767
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006768 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01006769 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6770 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006771
6772 /*
6773 * No more DR vmexits; force a reload of the debug registers
6774 * and reenter on this instruction. The next vmexit will
6775 * retrieve the full state of the debug registers.
6776 */
6777 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
6778 return 1;
6779 }
6780
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006781 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
6782 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03006783 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006784
6785 if (kvm_get_dr(vcpu, dr, &val))
6786 return 1;
6787 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03006788 } else
Nadav Amit57773922014-06-18 17:19:23 +03006789 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006790 return 1;
6791
Kyle Huey6affcbe2016-11-29 12:40:40 -08006792 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006793}
6794
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01006795static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
6796{
6797 return vcpu->arch.dr6;
6798}
6799
6800static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
6801{
6802}
6803
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006804static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
6805{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006806 get_debugreg(vcpu->arch.db[0], 0);
6807 get_debugreg(vcpu->arch.db[1], 1);
6808 get_debugreg(vcpu->arch.db[2], 2);
6809 get_debugreg(vcpu->arch.db[3], 3);
6810 get_debugreg(vcpu->arch.dr6, 6);
6811 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
6812
6813 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01006814 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006815}
6816
Gleb Natapov020df072010-04-13 10:05:23 +03006817static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
6818{
6819 vmcs_writel(GUEST_DR7, val);
6820}
6821
Avi Kivity851ba692009-08-24 11:10:17 +03006822static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006823{
Kyle Huey6a908b62016-11-29 12:40:37 -08006824 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006825}
6826
Avi Kivity851ba692009-08-24 11:10:17 +03006827static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006828{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006829 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006830 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006831
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006832 msr_info.index = ecx;
6833 msr_info.host_initiated = false;
6834 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02006835 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006836 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006837 return 1;
6838 }
6839
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006840 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006841
Avi Kivity6aa8b732006-12-10 02:21:36 -08006842 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006843 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
6844 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006845 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006846}
6847
Avi Kivity851ba692009-08-24 11:10:17 +03006848static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006849{
Will Auld8fe8ab42012-11-29 12:42:12 -08006850 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006851 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6852 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
6853 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006854
Will Auld8fe8ab42012-11-29 12:42:12 -08006855 msr.data = data;
6856 msr.index = ecx;
6857 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03006858 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02006859 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006860 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006861 return 1;
6862 }
6863
Avi Kivity59200272010-01-25 19:47:02 +02006864 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006865 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006866}
6867
Avi Kivity851ba692009-08-24 11:10:17 +03006868static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006869{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01006870 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006871 return 1;
6872}
6873
Avi Kivity851ba692009-08-24 11:10:17 +03006874static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006875{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006876 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6877 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006878
Avi Kivity3842d132010-07-27 12:30:24 +03006879 kvm_make_request(KVM_REQ_EVENT, vcpu);
6880
Jan Kiszkaa26bf122008-09-26 09:30:45 +02006881 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006882 return 1;
6883}
6884
Avi Kivity851ba692009-08-24 11:10:17 +03006885static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006886{
Avi Kivityd3bef152007-06-05 15:53:05 +03006887 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006888}
6889
Avi Kivity851ba692009-08-24 11:10:17 +03006890static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02006891{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03006892 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02006893}
6894
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006895static int handle_invd(struct kvm_vcpu *vcpu)
6896{
Andre Przywara51d8b662010-12-21 11:12:02 +01006897 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006898}
6899
Avi Kivity851ba692009-08-24 11:10:17 +03006900static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03006901{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006902 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006903
6904 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006905 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006906}
6907
Avi Kivityfee84b02011-11-10 14:57:25 +02006908static int handle_rdpmc(struct kvm_vcpu *vcpu)
6909{
6910 int err;
6911
6912 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006913 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02006914}
6915
Avi Kivity851ba692009-08-24 11:10:17 +03006916static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02006917{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006918 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02006919}
6920
Dexuan Cui2acf9232010-06-10 11:27:12 +08006921static int handle_xsetbv(struct kvm_vcpu *vcpu)
6922{
6923 u64 new_bv = kvm_read_edx_eax(vcpu);
6924 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
6925
6926 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006927 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08006928 return 1;
6929}
6930
Wanpeng Lif53cd632014-12-02 19:14:58 +08006931static int handle_xsaves(struct kvm_vcpu *vcpu)
6932{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006933 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08006934 WARN(1, "this should never happen\n");
6935 return 1;
6936}
6937
6938static int handle_xrstors(struct kvm_vcpu *vcpu)
6939{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006940 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08006941 WARN(1, "this should never happen\n");
6942 return 1;
6943}
6944
Avi Kivity851ba692009-08-24 11:10:17 +03006945static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08006946{
Kevin Tian58fbbf22011-08-30 13:56:17 +03006947 if (likely(fasteoi)) {
6948 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6949 int access_type, offset;
6950
6951 access_type = exit_qualification & APIC_ACCESS_TYPE;
6952 offset = exit_qualification & APIC_ACCESS_OFFSET;
6953 /*
6954 * Sane guest uses MOV to write EOI, with written value
6955 * not cared. So make a short-circuit here by avoiding
6956 * heavy instruction emulation.
6957 */
6958 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6959 (offset == APIC_EOI)) {
6960 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006961 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03006962 }
6963 }
Andre Przywara51d8b662010-12-21 11:12:02 +01006964 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08006965}
6966
Yang Zhangc7c9c562013-01-25 10:18:51 +08006967static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6968{
6969 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6970 int vector = exit_qualification & 0xff;
6971
6972 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6973 kvm_apic_set_eoi_accelerated(vcpu, vector);
6974 return 1;
6975}
6976
Yang Zhang83d4c282013-01-25 10:18:49 +08006977static int handle_apic_write(struct kvm_vcpu *vcpu)
6978{
6979 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6980 u32 offset = exit_qualification & 0xfff;
6981
6982 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6983 kvm_apic_write_nodecode(vcpu, offset);
6984 return 1;
6985}
6986
Avi Kivity851ba692009-08-24 11:10:17 +03006987static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02006988{
Jan Kiszka60637aa2008-09-26 09:30:47 +02006989 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02006990 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02006991 bool has_error_code = false;
6992 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02006993 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006994 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006995
6996 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006997 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006998 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02006999
7000 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7001
7002 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007003 if (reason == TASK_SWITCH_GATE && idt_v) {
7004 switch (type) {
7005 case INTR_TYPE_NMI_INTR:
7006 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007007 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007008 break;
7009 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007010 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007011 kvm_clear_interrupt_queue(vcpu);
7012 break;
7013 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007014 if (vmx->idt_vectoring_info &
7015 VECTORING_INFO_DELIVER_CODE_MASK) {
7016 has_error_code = true;
7017 error_code =
7018 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7019 }
7020 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007021 case INTR_TYPE_SOFT_EXCEPTION:
7022 kvm_clear_exception_queue(vcpu);
7023 break;
7024 default:
7025 break;
7026 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007027 }
Izik Eidus37817f22008-03-24 23:14:53 +02007028 tss_selector = exit_qualification;
7029
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007030 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7031 type != INTR_TYPE_EXT_INTR &&
7032 type != INTR_TYPE_NMI_INTR))
7033 skip_emulated_instruction(vcpu);
7034
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007035 if (kvm_task_switch(vcpu, tss_selector,
7036 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7037 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007038 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7039 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7040 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007041 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007042 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007043
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007044 /*
7045 * TODO: What about debug traps on tss switch?
7046 * Are we supposed to inject them and update dr6?
7047 */
7048
7049 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007050}
7051
Avi Kivity851ba692009-08-24 11:10:17 +03007052static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007053{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007054 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007055 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007056 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007057
Sheng Yangf9c617f2009-03-25 10:08:52 +08007058 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007059
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007060 /*
7061 * EPT violation happened while executing iret from NMI,
7062 * "blocked by NMI" bit has to be set before next VM entry.
7063 * There are errata that may cause this bit to not be set:
7064 * AAK134, BY25.
7065 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007066 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007067 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007068 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007069 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7070
Sheng Yang14394422008-04-28 12:24:45 +08007071 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007072 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007073
Junaid Shahid27959a42016-12-06 16:46:10 -08007074 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007075 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007076 ? PFERR_USER_MASK : 0;
7077 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007078 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007079 ? PFERR_WRITE_MASK : 0;
7080 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007081 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007082 ? PFERR_FETCH_MASK : 0;
7083 /* ept page table entry is present? */
7084 error_code |= (exit_qualification &
7085 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7086 EPT_VIOLATION_EXECUTABLE))
7087 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007088
Paolo Bonzinieebed242016-11-28 14:39:58 +01007089 error_code |= (exit_qualification & 0x100) != 0 ?
7090 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007091
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007092 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007093 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007094}
7095
Avi Kivity851ba692009-08-24 11:10:17 +03007096static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007097{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007098 gpa_t gpa;
7099
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007100 /*
7101 * A nested guest cannot optimize MMIO vmexits, because we have an
7102 * nGPA here instead of the required GPA.
7103 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007104 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007105 if (!is_guest_mode(vcpu) &&
7106 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007107 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007108 /*
7109 * Doing kvm_skip_emulated_instruction() depends on undefined
7110 * behavior: Intel's manual doesn't mandate
7111 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7112 * occurs and while on real hardware it was observed to be set,
7113 * other hypervisors (namely Hyper-V) don't set it, we end up
7114 * advancing IP with some random value. Disable fast mmio when
7115 * running nested and keep it for real hardware in hope that
7116 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7117 */
7118 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7119 return kvm_skip_emulated_instruction(vcpu);
7120 else
7121 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
7122 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007123 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007124
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007125 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007126}
7127
Avi Kivity851ba692009-08-24 11:10:17 +03007128static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007129{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007130 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007131 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7132 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007133 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007134 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007135
7136 return 1;
7137}
7138
Mohammed Gamal80ced182009-09-01 12:48:18 +02007139static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007140{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007141 struct vcpu_vmx *vmx = to_vmx(vcpu);
7142 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007143 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007144 u32 cpu_exec_ctrl;
7145 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007146 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007147
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007148 /*
7149 * We should never reach the point where we are emulating L2
7150 * due to invalid guest state as that means we incorrectly
7151 * allowed a nested VMEntry with an invalid vmcs12.
7152 */
7153 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7154
Avi Kivity49e9d552010-09-19 14:34:08 +02007155 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7156 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007157
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007158 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007159 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007160 return handle_interrupt_window(&vmx->vcpu);
7161
Radim Krčmář72875d82017-04-26 22:32:19 +02007162 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007163 return 1;
7164
Liran Alon9b8ae632017-11-05 16:56:34 +02007165 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007166
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007167 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007168 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007169 ret = 0;
7170 goto out;
7171 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007172
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007173 if (err != EMULATE_DONE)
7174 goto emulation_error;
7175
7176 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7177 vcpu->arch.exception.pending)
7178 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007179
Gleb Natapov8d76c492013-05-08 18:38:44 +03007180 if (vcpu->arch.halt_request) {
7181 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007182 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007183 goto out;
7184 }
7185
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007186 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007187 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007188 if (need_resched())
7189 schedule();
7190 }
7191
Mohammed Gamal80ced182009-09-01 12:48:18 +02007192out:
7193 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007194
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007195emulation_error:
7196 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7197 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7198 vcpu->run->internal.ndata = 0;
7199 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007200}
7201
7202static void grow_ple_window(struct kvm_vcpu *vcpu)
7203{
7204 struct vcpu_vmx *vmx = to_vmx(vcpu);
7205 int old = vmx->ple_window;
7206
Babu Mogerc8e88712018-03-16 16:37:24 -04007207 vmx->ple_window = __grow_ple_window(old, ple_window,
7208 ple_window_grow,
7209 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007210
7211 if (vmx->ple_window != old)
7212 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007213
7214 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007215}
7216
7217static void shrink_ple_window(struct kvm_vcpu *vcpu)
7218{
7219 struct vcpu_vmx *vmx = to_vmx(vcpu);
7220 int old = vmx->ple_window;
7221
Babu Mogerc8e88712018-03-16 16:37:24 -04007222 vmx->ple_window = __shrink_ple_window(old, ple_window,
7223 ple_window_shrink,
7224 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007225
7226 if (vmx->ple_window != old)
7227 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007228
7229 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007230}
7231
7232/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007233 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7234 */
7235static void wakeup_handler(void)
7236{
7237 struct kvm_vcpu *vcpu;
7238 int cpu = smp_processor_id();
7239
7240 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7241 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7242 blocked_vcpu_list) {
7243 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7244
7245 if (pi_test_on(pi_desc) == 1)
7246 kvm_vcpu_kick(vcpu);
7247 }
7248 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7249}
7250
Peng Haoe01bca22018-04-07 05:47:32 +08007251static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007252{
7253 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7254 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7255 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7256 0ull, VMX_EPT_EXECUTABLE_MASK,
7257 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007258 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007259
7260 ept_set_mmio_spte_mask();
7261 kvm_enable_tdp();
7262}
7263
Tiejun Chenf2c76482014-10-28 10:14:47 +08007264static __init int hardware_setup(void)
7265{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007266 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007267
7268 rdmsrl_safe(MSR_EFER, &host_efer);
7269
7270 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7271 kvm_define_shared_msr(i, vmx_msr_index[i]);
7272
Radim Krčmář23611332016-09-29 22:41:33 +02007273 for (i = 0; i < VMX_BITMAP_NR; i++) {
7274 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7275 if (!vmx_bitmap[i])
7276 goto out;
7277 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007278
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007279 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7280 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7281
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007282 if (setup_vmcs_config(&vmcs_config) < 0) {
7283 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007284 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007285 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007286
7287 if (boot_cpu_has(X86_FEATURE_NX))
7288 kvm_enable_efer_bits(EFER_NX);
7289
Wanpeng Li08d839c2017-03-23 05:30:08 -07007290 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7291 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007292 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007293
Tiejun Chenf2c76482014-10-28 10:14:47 +08007294 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007295 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007296 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007297 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007298 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007299
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007300 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007301 enable_ept_ad_bits = 0;
7302
Wanpeng Li8ad81822017-10-09 15:51:53 -07007303 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007304 enable_unrestricted_guest = 0;
7305
Paolo Bonziniad15a292015-01-30 16:18:49 +01007306 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007307 flexpriority_enabled = 0;
7308
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007309 if (!cpu_has_virtual_nmis())
7310 enable_vnmi = 0;
7311
Paolo Bonziniad15a292015-01-30 16:18:49 +01007312 /*
7313 * set_apic_access_page_addr() is used to reload apic access
7314 * page upon invalidation. No need to do anything if not
7315 * using the APIC_ACCESS_ADDR VMCS field.
7316 */
7317 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007318 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007319
7320 if (!cpu_has_vmx_tpr_shadow())
7321 kvm_x86_ops->update_cr8_intercept = NULL;
7322
7323 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7324 kvm_disable_largepages();
7325
Wanpeng Li0f107682017-09-28 18:06:24 -07007326 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007327 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007328 ple_window = 0;
7329 ple_window_grow = 0;
7330 ple_window_max = 0;
7331 ple_window_shrink = 0;
7332 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007333
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007334 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007335 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007336 kvm_x86_ops->sync_pir_to_irr = NULL;
7337 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007338
Haozhong Zhang64903d62015-10-20 15:39:09 +08007339 if (cpu_has_vmx_tsc_scaling()) {
7340 kvm_has_tsc_control = true;
7341 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7342 kvm_tsc_scaling_ratio_frac_bits = 48;
7343 }
7344
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007345 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7346
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007347 if (enable_ept)
7348 vmx_enable_tdp();
7349 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007350 kvm_disable_tdp();
7351
Kai Huang843e4332015-01-28 10:54:28 +08007352 /*
7353 * Only enable PML when hardware supports PML feature, and both EPT
7354 * and EPT A/D bit features are enabled -- PML depends on them to work.
7355 */
7356 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7357 enable_pml = 0;
7358
7359 if (!enable_pml) {
7360 kvm_x86_ops->slot_enable_log_dirty = NULL;
7361 kvm_x86_ops->slot_disable_log_dirty = NULL;
7362 kvm_x86_ops->flush_log_dirty = NULL;
7363 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7364 }
7365
Yunhong Jiang64672c92016-06-13 14:19:59 -07007366 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7367 u64 vmx_msr;
7368
7369 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7370 cpu_preemption_timer_multi =
7371 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7372 } else {
7373 kvm_x86_ops->set_hv_timer = NULL;
7374 kvm_x86_ops->cancel_hv_timer = NULL;
7375 }
7376
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007377 if (!cpu_has_vmx_shadow_vmcs())
7378 enable_shadow_vmcs = 0;
7379 if (enable_shadow_vmcs)
7380 init_vmcs_shadow_fields();
7381
Feng Wubf9f6ac2015-09-18 22:29:55 +08007382 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007383 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007384
Ashok Rajc45dcc72016-06-22 14:59:56 +08007385 kvm_mce_cap_supported |= MCG_LMCE_P;
7386
Tiejun Chenf2c76482014-10-28 10:14:47 +08007387 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007388
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007389out:
Radim Krčmář23611332016-09-29 22:41:33 +02007390 for (i = 0; i < VMX_BITMAP_NR; i++)
7391 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007392
7393 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007394}
7395
7396static __exit void hardware_unsetup(void)
7397{
Radim Krčmář23611332016-09-29 22:41:33 +02007398 int i;
7399
7400 for (i = 0; i < VMX_BITMAP_NR; i++)
7401 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007402
Tiejun Chenf2c76482014-10-28 10:14:47 +08007403 free_kvm_area();
7404}
7405
Avi Kivity6aa8b732006-12-10 02:21:36 -08007406/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007407 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7408 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7409 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03007410static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007411{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007412 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007413 grow_ple_window(vcpu);
7414
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08007415 /*
7416 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7417 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7418 * never set PAUSE_EXITING and just set PLE if supported,
7419 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7420 */
7421 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007422 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007423}
7424
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007425static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08007426{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007427 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08007428}
7429
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007430static int handle_mwait(struct kvm_vcpu *vcpu)
7431{
7432 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7433 return handle_nop(vcpu);
7434}
7435
Jim Mattson45ec3682017-08-23 16:32:04 -07007436static int handle_invalid_op(struct kvm_vcpu *vcpu)
7437{
7438 kvm_queue_exception(vcpu, UD_VECTOR);
7439 return 1;
7440}
7441
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007442static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7443{
7444 return 1;
7445}
7446
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007447static int handle_monitor(struct kvm_vcpu *vcpu)
7448{
7449 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7450 return handle_nop(vcpu);
7451}
7452
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007453/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007454 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7455 * set the success or error code of an emulated VMX instruction, as specified
7456 * by Vol 2B, VMX Instruction Reference, "Conventions".
7457 */
7458static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7459{
7460 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7461 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7462 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7463}
7464
7465static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7466{
7467 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7468 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7469 X86_EFLAGS_SF | X86_EFLAGS_OF))
7470 | X86_EFLAGS_CF);
7471}
7472
Abel Gordon145c28d2013-04-18 14:36:55 +03007473static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007474 u32 vm_instruction_error)
7475{
7476 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7477 /*
7478 * failValid writes the error number to the current VMCS, which
7479 * can't be done there isn't a current VMCS.
7480 */
7481 nested_vmx_failInvalid(vcpu);
7482 return;
7483 }
7484 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7485 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7486 X86_EFLAGS_SF | X86_EFLAGS_OF))
7487 | X86_EFLAGS_ZF);
7488 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7489 /*
7490 * We don't need to force a shadow sync because
7491 * VM_INSTRUCTION_ERROR is not shadowed
7492 */
7493}
Abel Gordon145c28d2013-04-18 14:36:55 +03007494
Wincy Vanff651cb2014-12-11 08:52:58 +03007495static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7496{
7497 /* TODO: not to reset guest simply here. */
7498 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007499 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007500}
7501
Jan Kiszkaf4124502014-03-07 20:03:13 +01007502static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7503{
7504 struct vcpu_vmx *vmx =
7505 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7506
7507 vmx->nested.preemption_timer_expired = true;
7508 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7509 kvm_vcpu_kick(&vmx->vcpu);
7510
7511 return HRTIMER_NORESTART;
7512}
7513
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007514/*
Bandan Das19677e32014-05-06 02:19:15 -04007515 * Decode the memory-address operand of a vmx instruction, as recorded on an
7516 * exit caused by such an instruction (run by a guest hypervisor).
7517 * On success, returns 0. When the operand is invalid, returns 1 and throws
7518 * #UD or #GP.
7519 */
7520static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7521 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007522 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007523{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007524 gva_t off;
7525 bool exn;
7526 struct kvm_segment s;
7527
Bandan Das19677e32014-05-06 02:19:15 -04007528 /*
7529 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7530 * Execution", on an exit, vmx_instruction_info holds most of the
7531 * addressing components of the operand. Only the displacement part
7532 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7533 * For how an actual address is calculated from all these components,
7534 * refer to Vol. 1, "Operand Addressing".
7535 */
7536 int scaling = vmx_instruction_info & 3;
7537 int addr_size = (vmx_instruction_info >> 7) & 7;
7538 bool is_reg = vmx_instruction_info & (1u << 10);
7539 int seg_reg = (vmx_instruction_info >> 15) & 7;
7540 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7541 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7542 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7543 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7544
7545 if (is_reg) {
7546 kvm_queue_exception(vcpu, UD_VECTOR);
7547 return 1;
7548 }
7549
7550 /* Addr = segment_base + offset */
7551 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007552 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007553 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007554 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007555 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007556 off += kvm_register_read(vcpu, index_reg)<<scaling;
7557 vmx_get_segment(vcpu, &s, seg_reg);
7558 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007559
7560 if (addr_size == 1) /* 32 bit */
7561 *ret &= 0xffffffff;
7562
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007563 /* Checks for #GP/#SS exceptions. */
7564 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007565 if (is_long_mode(vcpu)) {
7566 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7567 * non-canonical form. This is the only check on the memory
7568 * destination for long mode!
7569 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007570 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007571 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007572 /* Protected mode: apply checks for segment validity in the
7573 * following order:
7574 * - segment type check (#GP(0) may be thrown)
7575 * - usability check (#GP(0)/#SS(0))
7576 * - limit check (#GP(0)/#SS(0))
7577 */
7578 if (wr)
7579 /* #GP(0) if the destination operand is located in a
7580 * read-only data segment or any code segment.
7581 */
7582 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7583 else
7584 /* #GP(0) if the source operand is located in an
7585 * execute-only code segment
7586 */
7587 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007588 if (exn) {
7589 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7590 return 1;
7591 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007592 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7593 */
7594 exn = (s.unusable != 0);
7595 /* Protected mode: #GP(0)/#SS(0) if the memory
7596 * operand is outside the segment limit.
7597 */
7598 exn = exn || (off + sizeof(u64) > s.limit);
7599 }
7600 if (exn) {
7601 kvm_queue_exception_e(vcpu,
7602 seg_reg == VCPU_SREG_SS ?
7603 SS_VECTOR : GP_VECTOR,
7604 0);
7605 return 1;
7606 }
7607
Bandan Das19677e32014-05-06 02:19:15 -04007608 return 0;
7609}
7610
Radim Krčmářcbf71272017-05-19 15:48:51 +02007611static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007612{
7613 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04007614 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04007615
7616 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007617 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007618 return 1;
7619
Radim Krčmářcbf71272017-05-19 15:48:51 +02007620 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, vmpointer,
7621 sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007622 kvm_inject_page_fault(vcpu, &e);
7623 return 1;
7624 }
7625
Bandan Das3573e222014-05-06 02:19:16 -04007626 return 0;
7627}
7628
Jim Mattsone29acc52016-11-30 12:03:43 -08007629static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7630{
7631 struct vcpu_vmx *vmx = to_vmx(vcpu);
7632 struct vmcs *shadow_vmcs;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007633 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08007634
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007635 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7636 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06007637 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08007638
7639 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7640 if (!vmx->nested.cached_vmcs12)
7641 goto out_cached_vmcs12;
7642
7643 if (enable_shadow_vmcs) {
7644 shadow_vmcs = alloc_vmcs();
7645 if (!shadow_vmcs)
7646 goto out_shadow_vmcs;
7647 /* mark vmcs as shadow */
7648 shadow_vmcs->revision_id |= (1u << 31);
7649 /* init shadow vmcs */
7650 vmcs_clear(shadow_vmcs);
7651 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7652 }
7653
Jim Mattsone29acc52016-11-30 12:03:43 -08007654 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7655 HRTIMER_MODE_REL_PINNED);
7656 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7657
7658 vmx->nested.vmxon = true;
7659 return 0;
7660
7661out_shadow_vmcs:
7662 kfree(vmx->nested.cached_vmcs12);
7663
7664out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06007665 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08007666
Jim Mattsonde3a0022017-11-27 17:22:25 -06007667out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08007668 return -ENOMEM;
7669}
7670
Bandan Das3573e222014-05-06 02:19:16 -04007671/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007672 * Emulate the VMXON instruction.
7673 * Currently, we just remember that VMX is active, and do not save or even
7674 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7675 * do not currently need to store anything in that guest-allocated memory
7676 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7677 * argument is different from the VMXON pointer (which the spec says they do).
7678 */
7679static int handle_vmon(struct kvm_vcpu *vcpu)
7680{
Jim Mattsone29acc52016-11-30 12:03:43 -08007681 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007682 gpa_t vmptr;
7683 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007684 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007685 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7686 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007687
Jim Mattson70f3aac2017-04-26 08:53:46 -07007688 /*
7689 * The Intel VMX Instruction Reference lists a bunch of bits that are
7690 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
7691 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
7692 * Otherwise, we should fail with #UD. But most faulting conditions
7693 * have already been checked by hardware, prior to the VM-exit for
7694 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
7695 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007696 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07007697 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007698 kvm_queue_exception(vcpu, UD_VECTOR);
7699 return 1;
7700 }
7701
Abel Gordon145c28d2013-04-18 14:36:55 +03007702 if (vmx->nested.vmxon) {
7703 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007704 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03007705 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007706
Haozhong Zhang3b840802016-06-22 14:59:54 +08007707 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007708 != VMXON_NEEDED_FEATURES) {
7709 kvm_inject_gp(vcpu, 0);
7710 return 1;
7711 }
7712
Radim Krčmářcbf71272017-05-19 15:48:51 +02007713 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08007714 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007715
7716 /*
7717 * SDM 3: 24.11.5
7718 * The first 4 bytes of VMXON region contain the supported
7719 * VMCS revision identifier
7720 *
7721 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
7722 * which replaces physical address width with 32
7723 */
7724 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7725 nested_vmx_failInvalid(vcpu);
7726 return kvm_skip_emulated_instruction(vcpu);
7727 }
7728
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02007729 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7730 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02007731 nested_vmx_failInvalid(vcpu);
7732 return kvm_skip_emulated_instruction(vcpu);
7733 }
7734 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
7735 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007736 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007737 nested_vmx_failInvalid(vcpu);
7738 return kvm_skip_emulated_instruction(vcpu);
7739 }
7740 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007741 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007742
7743 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08007744 ret = enter_vmx_operation(vcpu);
7745 if (ret)
7746 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007747
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007748 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007749 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007750}
7751
7752/*
7753 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7754 * for running VMX instructions (except VMXON, whose prerequisites are
7755 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07007756 * Note that many of these exceptions have priority over VM exits, so they
7757 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007758 */
7759static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7760{
Jim Mattson70f3aac2017-04-26 08:53:46 -07007761 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007762 kvm_queue_exception(vcpu, UD_VECTOR);
7763 return 0;
7764 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007765 return 1;
7766}
7767
David Matlack8ca44e82017-08-01 14:00:39 -07007768static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
7769{
7770 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
7771 vmcs_write64(VMCS_LINK_POINTER, -1ull);
7772}
7773
Abel Gordone7953d72013-04-18 14:37:55 +03007774static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7775{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007776 if (vmx->nested.current_vmptr == -1ull)
7777 return;
7778
Abel Gordon012f83c2013-04-18 14:39:25 +03007779 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007780 /* copy to memory all shadowed fields in case
7781 they were modified */
7782 copy_shadow_to_vmcs12(vmx);
7783 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07007784 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03007785 }
Wincy Van705699a2015-02-03 23:58:17 +08007786 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07007787
7788 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02007789 kvm_vcpu_write_guest_page(&vmx->vcpu,
7790 vmx->nested.current_vmptr >> PAGE_SHIFT,
7791 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07007792
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007793 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03007794}
7795
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007796/*
7797 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7798 * just stops using VMX.
7799 */
7800static void free_nested(struct vcpu_vmx *vmx)
7801{
Wanpeng Lib7455822017-11-22 14:04:00 -08007802 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007803 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007804
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007805 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08007806 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07007807 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07007808 vmx->nested.posted_intr_nv = -1;
7809 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007810 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07007811 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007812 vmcs_clear(vmx->vmcs01.shadow_vmcs);
7813 free_vmcs(vmx->vmcs01.shadow_vmcs);
7814 vmx->vmcs01.shadow_vmcs = NULL;
7815 }
David Matlack4f2777b2016-07-13 17:16:37 -07007816 kfree(vmx->nested.cached_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06007817 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007818 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02007819 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007820 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007821 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007822 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02007823 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007824 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007825 }
Wincy Van705699a2015-02-03 23:58:17 +08007826 if (vmx->nested.pi_desc_page) {
7827 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007828 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08007829 vmx->nested.pi_desc_page = NULL;
7830 vmx->nested.pi_desc = NULL;
7831 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007832
Jim Mattsonde3a0022017-11-27 17:22:25 -06007833 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007834}
7835
7836/* Emulate the VMXOFF instruction */
7837static int handle_vmoff(struct kvm_vcpu *vcpu)
7838{
7839 if (!nested_vmx_check_permission(vcpu))
7840 return 1;
7841 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007842 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007843 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007844}
7845
Nadav Har'El27d6c862011-05-25 23:06:59 +03007846/* Emulate the VMCLEAR instruction */
7847static int handle_vmclear(struct kvm_vcpu *vcpu)
7848{
7849 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08007850 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007851 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007852
7853 if (!nested_vmx_check_permission(vcpu))
7854 return 1;
7855
Radim Krčmářcbf71272017-05-19 15:48:51 +02007856 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03007857 return 1;
7858
Radim Krčmářcbf71272017-05-19 15:48:51 +02007859 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7860 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
7861 return kvm_skip_emulated_instruction(vcpu);
7862 }
7863
7864 if (vmptr == vmx->nested.vmxon_ptr) {
7865 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
7866 return kvm_skip_emulated_instruction(vcpu);
7867 }
7868
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007869 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03007870 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007871
Jim Mattson587d7e722017-03-02 12:41:48 -08007872 kvm_vcpu_write_guest(vcpu,
7873 vmptr + offsetof(struct vmcs12, launch_state),
7874 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03007875
Nadav Har'El27d6c862011-05-25 23:06:59 +03007876 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007877 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007878}
7879
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007880static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7881
7882/* Emulate the VMLAUNCH instruction */
7883static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7884{
7885 return nested_vmx_run(vcpu, true);
7886}
7887
7888/* Emulate the VMRESUME instruction */
7889static int handle_vmresume(struct kvm_vcpu *vcpu)
7890{
7891
7892 return nested_vmx_run(vcpu, false);
7893}
7894
Nadav Har'El49f705c2011-05-25 23:08:30 +03007895/*
7896 * Read a vmcs12 field. Since these can have varying lengths and we return
7897 * one type, we chose the biggest type (u64) and zero-extend the return value
7898 * to that size. Note that the caller, handle_vmread, might need to use only
7899 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7900 * 64-bit fields are to be returned).
7901 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007902static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7903 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03007904{
7905 short offset = vmcs_field_to_offset(field);
7906 char *p;
7907
7908 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007909 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007910
7911 p = ((char *)(get_vmcs12(vcpu))) + offset;
7912
Jim Mattsond37f4262017-12-22 12:12:16 -08007913 switch (vmcs_field_width(field)) {
7914 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007915 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007916 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007917 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007918 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007919 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007920 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007921 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007922 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007923 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007924 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007925 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007926 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007927 WARN_ON(1);
7928 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007929 }
7930}
7931
Abel Gordon20b97fe2013-04-18 14:36:25 +03007932
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007933static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7934 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03007935 short offset = vmcs_field_to_offset(field);
7936 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7937 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007938 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007939
Jim Mattsond37f4262017-12-22 12:12:16 -08007940 switch (vmcs_field_width(field)) {
7941 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007942 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007943 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007944 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007945 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007946 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007947 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007948 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007949 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007950 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007951 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007952 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007953 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007954 WARN_ON(1);
7955 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007956 }
7957
7958}
7959
Abel Gordon16f5b902013-04-18 14:38:25 +03007960static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7961{
7962 int i;
7963 unsigned long field;
7964 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007965 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007966 const u16 *fields = shadow_read_write_fields;
Mathias Krausec2bae892013-06-26 20:36:21 +02007967 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03007968
Jan Kiszka282da872014-10-08 18:05:39 +02007969 preempt_disable();
7970
Abel Gordon16f5b902013-04-18 14:38:25 +03007971 vmcs_load(shadow_vmcs);
7972
7973 for (i = 0; i < num_fields; i++) {
7974 field = fields[i];
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007975 field_value = __vmcs_readl(field);
Abel Gordon16f5b902013-04-18 14:38:25 +03007976 vmcs12_write_any(&vmx->vcpu, field, field_value);
7977 }
7978
7979 vmcs_clear(shadow_vmcs);
7980 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02007981
7982 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03007983}
7984
Abel Gordonc3114422013-04-18 14:38:55 +03007985static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7986{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007987 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02007988 shadow_read_write_fields,
7989 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03007990 };
Mathias Krausec2bae892013-06-26 20:36:21 +02007991 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03007992 max_shadow_read_write_fields,
7993 max_shadow_read_only_fields
7994 };
7995 int i, q;
7996 unsigned long field;
7997 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007998 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03007999
8000 vmcs_load(shadow_vmcs);
8001
Mathias Krausec2bae892013-06-26 20:36:21 +02008002 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008003 for (i = 0; i < max_fields[q]; i++) {
8004 field = fields[q][i];
8005 vmcs12_read_any(&vmx->vcpu, field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008006 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008007 }
8008 }
8009
8010 vmcs_clear(shadow_vmcs);
8011 vmcs_load(vmx->loaded_vmcs->vmcs);
8012}
8013
Nadav Har'El49f705c2011-05-25 23:08:30 +03008014/*
8015 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8016 * used before) all generate the same failure when it is missing.
8017 */
8018static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8019{
8020 struct vcpu_vmx *vmx = to_vmx(vcpu);
8021 if (vmx->nested.current_vmptr == -1ull) {
8022 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008023 return 0;
8024 }
8025 return 1;
8026}
8027
8028static int handle_vmread(struct kvm_vcpu *vcpu)
8029{
8030 unsigned long field;
8031 u64 field_value;
8032 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8033 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8034 gva_t gva = 0;
8035
Kyle Hueyeb277562016-11-29 12:40:39 -08008036 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008037 return 1;
8038
Kyle Huey6affcbe2016-11-29 12:40:40 -08008039 if (!nested_vmx_check_vmcs12(vcpu))
8040 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008041
Nadav Har'El49f705c2011-05-25 23:08:30 +03008042 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008043 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008044 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008045 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008046 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008047 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008048 }
8049 /*
8050 * Now copy part of this value to register or memory, as requested.
8051 * Note that the number of bits actually copied is 32 or 64 depending
8052 * on the guest's mode (32 or 64 bit), not on the given field's length.
8053 */
8054 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008055 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008056 field_value);
8057 } else {
8058 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008059 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008060 return 1;
Jim Mattson70f3aac2017-04-26 08:53:46 -07008061 /* _system ok, as hardware has verified cpl=0 */
Nadav Har'El49f705c2011-05-25 23:08:30 +03008062 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
8063 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
8064 }
8065
8066 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008067 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008068}
8069
8070
8071static int handle_vmwrite(struct kvm_vcpu *vcpu)
8072{
8073 unsigned long field;
8074 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008075 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008076 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8077 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008078
Nadav Har'El49f705c2011-05-25 23:08:30 +03008079 /* The value to write might be 32 or 64 bits, depending on L1's long
8080 * mode, and eventually we need to write that into a field of several
8081 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008082 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008083 * bits into the vmcs12 field.
8084 */
8085 u64 field_value = 0;
8086 struct x86_exception e;
8087
Kyle Hueyeb277562016-11-29 12:40:39 -08008088 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008089 return 1;
8090
Kyle Huey6affcbe2016-11-29 12:40:40 -08008091 if (!nested_vmx_check_vmcs12(vcpu))
8092 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008093
Nadav Har'El49f705c2011-05-25 23:08:30 +03008094 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008095 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008096 (((vmx_instruction_info) >> 3) & 0xf));
8097 else {
8098 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008099 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008100 return 1;
8101 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
Nadav Amit27e6fb52014-06-18 17:19:26 +03008102 &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008103 kvm_inject_page_fault(vcpu, &e);
8104 return 1;
8105 }
8106 }
8107
8108
Nadav Amit27e6fb52014-06-18 17:19:26 +03008109 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008110 if (vmcs_field_readonly(field)) {
8111 nested_vmx_failValid(vcpu,
8112 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008113 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008114 }
8115
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008116 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008117 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008118 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008119 }
8120
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008121 switch (field) {
8122#define SHADOW_FIELD_RW(x) case x:
8123#include "vmx_shadow_fields.h"
8124 /*
8125 * The fields that can be updated by L1 without a vmexit are
8126 * always updated in the vmcs02, the others go down the slow
8127 * path of prepare_vmcs02.
8128 */
8129 break;
8130 default:
8131 vmx->nested.dirty_vmcs12 = true;
8132 break;
8133 }
8134
Nadav Har'El49f705c2011-05-25 23:08:30 +03008135 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008136 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008137}
8138
Jim Mattsona8bc2842016-11-30 12:03:44 -08008139static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8140{
8141 vmx->nested.current_vmptr = vmptr;
8142 if (enable_shadow_vmcs) {
8143 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8144 SECONDARY_EXEC_SHADOW_VMCS);
8145 vmcs_write64(VMCS_LINK_POINTER,
8146 __pa(vmx->vmcs01.shadow_vmcs));
8147 vmx->nested.sync_shadow_vmcs = true;
8148 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008149 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008150}
8151
Nadav Har'El63846662011-05-25 23:07:29 +03008152/* Emulate the VMPTRLD instruction */
8153static int handle_vmptrld(struct kvm_vcpu *vcpu)
8154{
8155 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008156 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008157
8158 if (!nested_vmx_check_permission(vcpu))
8159 return 1;
8160
Radim Krčmářcbf71272017-05-19 15:48:51 +02008161 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008162 return 1;
8163
Radim Krčmářcbf71272017-05-19 15:48:51 +02008164 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8165 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8166 return kvm_skip_emulated_instruction(vcpu);
8167 }
8168
8169 if (vmptr == vmx->nested.vmxon_ptr) {
8170 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8171 return kvm_skip_emulated_instruction(vcpu);
8172 }
8173
Nadav Har'El63846662011-05-25 23:07:29 +03008174 if (vmx->nested.current_vmptr != vmptr) {
8175 struct vmcs12 *new_vmcs12;
8176 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008177 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8178 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008179 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008180 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008181 }
8182 new_vmcs12 = kmap(page);
8183 if (new_vmcs12->revision_id != VMCS12_REVISION) {
8184 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008185 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008186 nested_vmx_failValid(vcpu,
8187 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008188 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008189 }
Nadav Har'El63846662011-05-25 23:07:29 +03008190
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008191 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008192 /*
8193 * Load VMCS12 from guest memory since it is not already
8194 * cached.
8195 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008196 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8197 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008198 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008199
Jim Mattsona8bc2842016-11-30 12:03:44 -08008200 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008201 }
8202
8203 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008204 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008205}
8206
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008207/* Emulate the VMPTRST instruction */
8208static int handle_vmptrst(struct kvm_vcpu *vcpu)
8209{
8210 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8211 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8212 gva_t vmcs_gva;
8213 struct x86_exception e;
8214
8215 if (!nested_vmx_check_permission(vcpu))
8216 return 1;
8217
8218 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008219 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008220 return 1;
Jim Mattson70f3aac2017-04-26 08:53:46 -07008221 /* ok to use *_system, as hardware has verified cpl=0 */
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008222 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
8223 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8224 sizeof(u64), &e)) {
8225 kvm_inject_page_fault(vcpu, &e);
8226 return 1;
8227 }
8228 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008229 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008230}
8231
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008232/* Emulate the INVEPT instruction */
8233static int handle_invept(struct kvm_vcpu *vcpu)
8234{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008235 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008236 u32 vmx_instruction_info, types;
8237 unsigned long type;
8238 gva_t gva;
8239 struct x86_exception e;
8240 struct {
8241 u64 eptp, gpa;
8242 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008243
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008244 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008245 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008246 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008247 kvm_queue_exception(vcpu, UD_VECTOR);
8248 return 1;
8249 }
8250
8251 if (!nested_vmx_check_permission(vcpu))
8252 return 1;
8253
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008254 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008255 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008256
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008257 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008258
Jim Mattson85c856b2016-10-26 08:38:38 -07008259 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008260 nested_vmx_failValid(vcpu,
8261 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008262 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008263 }
8264
8265 /* According to the Intel VMX instruction reference, the memory
8266 * operand is read even if it isn't needed (e.g., for type==global)
8267 */
8268 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008269 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008270 return 1;
8271 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
8272 sizeof(operand), &e)) {
8273 kvm_inject_page_fault(vcpu, &e);
8274 return 1;
8275 }
8276
8277 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008278 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008279 /*
8280 * TODO: track mappings and invalidate
8281 * single context requests appropriately
8282 */
8283 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008284 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008285 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008286 nested_vmx_succeed(vcpu);
8287 break;
8288 default:
8289 BUG_ON(1);
8290 break;
8291 }
8292
Kyle Huey6affcbe2016-11-29 12:40:40 -08008293 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008294}
8295
Petr Matouseka642fc32014-09-23 20:22:30 +02008296static int handle_invvpid(struct kvm_vcpu *vcpu)
8297{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008298 struct vcpu_vmx *vmx = to_vmx(vcpu);
8299 u32 vmx_instruction_info;
8300 unsigned long type, types;
8301 gva_t gva;
8302 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008303 struct {
8304 u64 vpid;
8305 u64 gla;
8306 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008307
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008308 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008309 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008310 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008311 kvm_queue_exception(vcpu, UD_VECTOR);
8312 return 1;
8313 }
8314
8315 if (!nested_vmx_check_permission(vcpu))
8316 return 1;
8317
8318 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8319 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8320
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008321 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008322 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008323
Jim Mattson85c856b2016-10-26 08:38:38 -07008324 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008325 nested_vmx_failValid(vcpu,
8326 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008327 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008328 }
8329
8330 /* according to the intel vmx instruction reference, the memory
8331 * operand is read even if it isn't needed (e.g., for type==global)
8332 */
8333 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8334 vmx_instruction_info, false, &gva))
8335 return 1;
Jim Mattson40352602017-06-28 09:37:37 -07008336 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
8337 sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008338 kvm_inject_page_fault(vcpu, &e);
8339 return 1;
8340 }
Jim Mattson40352602017-06-28 09:37:37 -07008341 if (operand.vpid >> 16) {
8342 nested_vmx_failValid(vcpu,
8343 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8344 return kvm_skip_emulated_instruction(vcpu);
8345 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008346
8347 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008348 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Yu Zhangfd8cb432017-08-24 20:27:56 +08008349 if (is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07008350 nested_vmx_failValid(vcpu,
8351 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8352 return kvm_skip_emulated_instruction(vcpu);
8353 }
8354 /* fall through */
Paolo Bonzinief697a72016-03-18 16:58:38 +01008355 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008356 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07008357 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008358 nested_vmx_failValid(vcpu,
8359 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008360 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008361 }
8362 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008363 case VMX_VPID_EXTENT_ALL_CONTEXT:
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008364 break;
8365 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008366 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008367 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008368 }
8369
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08008370 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008371 nested_vmx_succeed(vcpu);
8372
Kyle Huey6affcbe2016-11-29 12:40:40 -08008373 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02008374}
8375
Kai Huang843e4332015-01-28 10:54:28 +08008376static int handle_pml_full(struct kvm_vcpu *vcpu)
8377{
8378 unsigned long exit_qualification;
8379
8380 trace_kvm_pml_full(vcpu->vcpu_id);
8381
8382 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8383
8384 /*
8385 * PML buffer FULL happened while executing iret from NMI,
8386 * "blocked by NMI" bit has to be set before next VM entry.
8387 */
8388 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008389 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08008390 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8391 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8392 GUEST_INTR_STATE_NMI);
8393
8394 /*
8395 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8396 * here.., and there's no userspace involvement needed for PML.
8397 */
8398 return 1;
8399}
8400
Yunhong Jiang64672c92016-06-13 14:19:59 -07008401static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8402{
8403 kvm_lapic_expired_hv_timer(vcpu);
8404 return 1;
8405}
8406
Bandan Das41ab9372017-08-03 15:54:43 -04008407static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8408{
8409 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04008410 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8411
8412 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008413 switch (address & VMX_EPTP_MT_MASK) {
8414 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008415 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008416 return false;
8417 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02008418 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008419 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008420 return false;
8421 break;
8422 default:
8423 return false;
8424 }
8425
David Hildenbrandbb97a012017-08-10 23:15:28 +02008426 /* only 4 levels page-walk length are valid */
8427 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04008428 return false;
8429
8430 /* Reserved bits should not be set */
8431 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8432 return false;
8433
8434 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008435 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008436 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008437 return false;
8438 }
8439
8440 return true;
8441}
8442
8443static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8444 struct vmcs12 *vmcs12)
8445{
8446 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8447 u64 address;
8448 bool accessed_dirty;
8449 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8450
8451 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8452 !nested_cpu_has_ept(vmcs12))
8453 return 1;
8454
8455 if (index >= VMFUNC_EPTP_ENTRIES)
8456 return 1;
8457
8458
8459 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8460 &address, index * 8, 8))
8461 return 1;
8462
David Hildenbrandbb97a012017-08-10 23:15:28 +02008463 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04008464
8465 /*
8466 * If the (L2) guest does a vmfunc to the currently
8467 * active ept pointer, we don't have to do anything else
8468 */
8469 if (vmcs12->ept_pointer != address) {
8470 if (!valid_ept_address(vcpu, address))
8471 return 1;
8472
8473 kvm_mmu_unload(vcpu);
8474 mmu->ept_ad = accessed_dirty;
8475 mmu->base_role.ad_disabled = !accessed_dirty;
8476 vmcs12->ept_pointer = address;
8477 /*
8478 * TODO: Check what's the correct approach in case
8479 * mmu reload fails. Currently, we just let the next
8480 * reload potentially fail
8481 */
8482 kvm_mmu_reload(vcpu);
8483 }
8484
8485 return 0;
8486}
8487
Bandan Das2a499e42017-08-03 15:54:41 -04008488static int handle_vmfunc(struct kvm_vcpu *vcpu)
8489{
Bandan Das27c42a12017-08-03 15:54:42 -04008490 struct vcpu_vmx *vmx = to_vmx(vcpu);
8491 struct vmcs12 *vmcs12;
8492 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8493
8494 /*
8495 * VMFUNC is only supported for nested guests, but we always enable the
8496 * secondary control for simplicity; for non-nested mode, fake that we
8497 * didn't by injecting #UD.
8498 */
8499 if (!is_guest_mode(vcpu)) {
8500 kvm_queue_exception(vcpu, UD_VECTOR);
8501 return 1;
8502 }
8503
8504 vmcs12 = get_vmcs12(vcpu);
8505 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8506 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04008507
8508 switch (function) {
8509 case 0:
8510 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8511 goto fail;
8512 break;
8513 default:
8514 goto fail;
8515 }
8516 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04008517
8518fail:
8519 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8520 vmcs_read32(VM_EXIT_INTR_INFO),
8521 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04008522 return 1;
8523}
8524
Nadav Har'El0140cae2011-05-25 23:06:28 +03008525/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08008526 * The exit handlers return 1 if the exit was handled fully and guest execution
8527 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8528 * to be done to userspace and return 0.
8529 */
Mathias Krause772e0312012-08-30 01:30:19 +02008530static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08008531 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8532 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08008533 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08008534 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008535 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008536 [EXIT_REASON_CR_ACCESS] = handle_cr,
8537 [EXIT_REASON_DR_ACCESS] = handle_dr,
8538 [EXIT_REASON_CPUID] = handle_cpuid,
8539 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8540 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8541 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8542 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02008543 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03008544 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02008545 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02008546 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03008547 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008548 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03008549 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008550 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008551 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008552 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008553 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008554 [EXIT_REASON_VMOFF] = handle_vmoff,
8555 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08008556 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8557 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08008558 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008559 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02008560 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08008561 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02008562 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08008563 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02008564 [EXIT_REASON_GDTR_IDTR] = handle_desc,
8565 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03008566 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8567 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008568 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008569 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008570 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008571 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008572 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02008573 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07008574 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07008575 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08008576 [EXIT_REASON_XSAVES] = handle_xsaves,
8577 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08008578 [EXIT_REASON_PML_FULL] = handle_pml_full,
Bandan Das2a499e42017-08-03 15:54:41 -04008579 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07008580 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008581};
8582
8583static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04008584 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008585
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008586static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8587 struct vmcs12 *vmcs12)
8588{
8589 unsigned long exit_qualification;
8590 gpa_t bitmap, last_bitmap;
8591 unsigned int port;
8592 int size;
8593 u8 b;
8594
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008595 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05008596 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008597
8598 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8599
8600 port = exit_qualification >> 16;
8601 size = (exit_qualification & 7) + 1;
8602
8603 last_bitmap = (gpa_t)-1;
8604 b = -1;
8605
8606 while (size > 0) {
8607 if (port < 0x8000)
8608 bitmap = vmcs12->io_bitmap_a;
8609 else if (port < 0x10000)
8610 bitmap = vmcs12->io_bitmap_b;
8611 else
Joe Perches1d804d02015-03-30 16:46:09 -07008612 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008613 bitmap += (port & 0x7fff) / 8;
8614
8615 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008616 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008617 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008618 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07008619 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008620
8621 port++;
8622 size--;
8623 last_bitmap = bitmap;
8624 }
8625
Joe Perches1d804d02015-03-30 16:46:09 -07008626 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008627}
8628
Nadav Har'El644d7112011-05-25 23:12:35 +03008629/*
8630 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8631 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8632 * disinterest in the current event (read or write a specific MSR) by using an
8633 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8634 */
8635static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8636 struct vmcs12 *vmcs12, u32 exit_reason)
8637{
8638 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8639 gpa_t bitmap;
8640
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01008641 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07008642 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008643
8644 /*
8645 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8646 * for the four combinations of read/write and low/high MSR numbers.
8647 * First we need to figure out which of the four to use:
8648 */
8649 bitmap = vmcs12->msr_bitmap;
8650 if (exit_reason == EXIT_REASON_MSR_WRITE)
8651 bitmap += 2048;
8652 if (msr_index >= 0xc0000000) {
8653 msr_index -= 0xc0000000;
8654 bitmap += 1024;
8655 }
8656
8657 /* Then read the msr_index'th bit from this bitmap: */
8658 if (msr_index < 1024*8) {
8659 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008660 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008661 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008662 return 1 & (b >> (msr_index & 7));
8663 } else
Joe Perches1d804d02015-03-30 16:46:09 -07008664 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03008665}
8666
8667/*
8668 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8669 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8670 * intercept (via guest_host_mask etc.) the current event.
8671 */
8672static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8673 struct vmcs12 *vmcs12)
8674{
8675 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8676 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008677 int reg;
8678 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03008679
8680 switch ((exit_qualification >> 4) & 3) {
8681 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008682 reg = (exit_qualification >> 8) & 15;
8683 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03008684 switch (cr) {
8685 case 0:
8686 if (vmcs12->cr0_guest_host_mask &
8687 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008688 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008689 break;
8690 case 3:
8691 if ((vmcs12->cr3_target_count >= 1 &&
8692 vmcs12->cr3_target_value0 == val) ||
8693 (vmcs12->cr3_target_count >= 2 &&
8694 vmcs12->cr3_target_value1 == val) ||
8695 (vmcs12->cr3_target_count >= 3 &&
8696 vmcs12->cr3_target_value2 == val) ||
8697 (vmcs12->cr3_target_count >= 4 &&
8698 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07008699 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008700 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008701 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008702 break;
8703 case 4:
8704 if (vmcs12->cr4_guest_host_mask &
8705 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07008706 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008707 break;
8708 case 8:
8709 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008710 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008711 break;
8712 }
8713 break;
8714 case 2: /* clts */
8715 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
8716 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008717 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008718 break;
8719 case 1: /* mov from cr */
8720 switch (cr) {
8721 case 3:
8722 if (vmcs12->cpu_based_vm_exec_control &
8723 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008724 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008725 break;
8726 case 8:
8727 if (vmcs12->cpu_based_vm_exec_control &
8728 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008729 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008730 break;
8731 }
8732 break;
8733 case 3: /* lmsw */
8734 /*
8735 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
8736 * cr0. Other attempted changes are ignored, with no exit.
8737 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008738 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03008739 if (vmcs12->cr0_guest_host_mask & 0xe &
8740 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008741 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008742 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
8743 !(vmcs12->cr0_read_shadow & 0x1) &&
8744 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07008745 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008746 break;
8747 }
Joe Perches1d804d02015-03-30 16:46:09 -07008748 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008749}
8750
8751/*
8752 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8753 * should handle it ourselves in L0 (and then continue L2). Only call this
8754 * when in is_guest_mode (L2).
8755 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02008756static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03008757{
Nadav Har'El644d7112011-05-25 23:12:35 +03008758 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8759 struct vcpu_vmx *vmx = to_vmx(vcpu);
8760 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8761
Jim Mattson4f350c62017-09-14 16:31:44 -07008762 if (vmx->nested.nested_run_pending)
8763 return false;
8764
8765 if (unlikely(vmx->fail)) {
8766 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8767 vmcs_read32(VM_INSTRUCTION_ERROR));
8768 return true;
8769 }
Jan Kiszka542060e2014-01-04 18:47:21 +01008770
David Matlackc9f04402017-08-01 14:00:40 -07008771 /*
8772 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06008773 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
8774 * Page). The CPU may write to these pages via their host
8775 * physical address while L2 is running, bypassing any
8776 * address-translation-based dirty tracking (e.g. EPT write
8777 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07008778 *
8779 * Mark them dirty on every exit from L2 to prevent them from
8780 * getting out of sync with dirty tracking.
8781 */
8782 nested_mark_vmcs12_pages_dirty(vcpu);
8783
Jim Mattson4f350c62017-09-14 16:31:44 -07008784 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
8785 vmcs_readl(EXIT_QUALIFICATION),
8786 vmx->idt_vectoring_info,
8787 intr_info,
8788 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8789 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03008790
8791 switch (exit_reason) {
8792 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08008793 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07008794 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008795 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07008796 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01008797 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01008798 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008799 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01008800 else if (is_debug(intr_info) &&
8801 vcpu->guest_debug &
8802 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8803 return false;
8804 else if (is_breakpoint(intr_info) &&
8805 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8806 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008807 return vmcs12->exception_bitmap &
8808 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8809 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07008810 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008811 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07008812 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008813 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008814 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008815 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008816 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008817 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07008818 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008819 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07008820 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008821 case EXIT_REASON_HLT:
8822 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8823 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07008824 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008825 case EXIT_REASON_INVLPG:
8826 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8827 case EXIT_REASON_RDPMC:
8828 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02008829 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02008830 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02008831 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02008832 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01008833 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03008834 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8835 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8836 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8837 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8838 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8839 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02008840 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03008841 /*
8842 * VMX instructions trap unconditionally. This allows L1 to
8843 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8844 */
Joe Perches1d804d02015-03-30 16:46:09 -07008845 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008846 case EXIT_REASON_CR_ACCESS:
8847 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8848 case EXIT_REASON_DR_ACCESS:
8849 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8850 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008851 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02008852 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
8853 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03008854 case EXIT_REASON_MSR_READ:
8855 case EXIT_REASON_MSR_WRITE:
8856 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8857 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07008858 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008859 case EXIT_REASON_MWAIT_INSTRUCTION:
8860 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008861 case EXIT_REASON_MONITOR_TRAP_FLAG:
8862 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03008863 case EXIT_REASON_MONITOR_INSTRUCTION:
8864 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8865 case EXIT_REASON_PAUSE_INSTRUCTION:
8866 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8867 nested_cpu_has2(vmcs12,
8868 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8869 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07008870 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008871 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008872 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03008873 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08008874 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08008875 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04008876 /*
8877 * The controls for "virtualize APIC accesses," "APIC-
8878 * register virtualization," and "virtual-interrupt
8879 * delivery" only come from vmcs12.
8880 */
Joe Perches1d804d02015-03-30 16:46:09 -07008881 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008882 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008883 /*
8884 * L0 always deals with the EPT violation. If nested EPT is
8885 * used, and the nested mmu code discovers that the address is
8886 * missing in the guest EPT table (EPT12), the EPT violation
8887 * will be injected with nested_ept_inject_page_fault()
8888 */
Joe Perches1d804d02015-03-30 16:46:09 -07008889 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008890 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008891 /*
8892 * L2 never uses directly L1's EPT, but rather L0's own EPT
8893 * table (shadow on EPT) or a merged EPT table that L0 built
8894 * (EPT on EPT). So any problems with the structure of the
8895 * table is L0's fault.
8896 */
Joe Perches1d804d02015-03-30 16:46:09 -07008897 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02008898 case EXIT_REASON_INVPCID:
8899 return
8900 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
8901 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008902 case EXIT_REASON_WBINVD:
8903 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8904 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07008905 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08008906 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8907 /*
8908 * This should never happen, since it is not possible to
8909 * set XSS to a non-zero value---neither in L1 nor in L2.
8910 * If if it were, XSS would have to be checked against
8911 * the XSS exit bitmap in vmcs12.
8912 */
8913 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08008914 case EXIT_REASON_PREEMPTION_TIMER:
8915 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02008916 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04008917 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02008918 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04008919 case EXIT_REASON_VMFUNC:
8920 /* VM functions are emulated through L2->L0 vmexits. */
8921 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008922 default:
Joe Perches1d804d02015-03-30 16:46:09 -07008923 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008924 }
8925}
8926
Paolo Bonzini7313c692017-07-27 10:31:25 +02008927static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
8928{
8929 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8930
8931 /*
8932 * At this point, the exit interruption info in exit_intr_info
8933 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
8934 * we need to query the in-kernel LAPIC.
8935 */
8936 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
8937 if ((exit_intr_info &
8938 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
8939 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
8940 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8941 vmcs12->vm_exit_intr_error_code =
8942 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
8943 }
8944
8945 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
8946 vmcs_readl(EXIT_QUALIFICATION));
8947 return 1;
8948}
8949
Avi Kivity586f9602010-11-18 13:09:54 +02008950static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8951{
8952 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8953 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8954}
8955
Kai Huanga3eaa862015-11-04 13:46:05 +08008956static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08008957{
Kai Huanga3eaa862015-11-04 13:46:05 +08008958 if (vmx->pml_pg) {
8959 __free_page(vmx->pml_pg);
8960 vmx->pml_pg = NULL;
8961 }
Kai Huang843e4332015-01-28 10:54:28 +08008962}
8963
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008964static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08008965{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008966 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008967 u64 *pml_buf;
8968 u16 pml_idx;
8969
8970 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8971
8972 /* Do nothing if PML buffer is empty */
8973 if (pml_idx == (PML_ENTITY_NUM - 1))
8974 return;
8975
8976 /* PML index always points to next available PML buffer entity */
8977 if (pml_idx >= PML_ENTITY_NUM)
8978 pml_idx = 0;
8979 else
8980 pml_idx++;
8981
8982 pml_buf = page_address(vmx->pml_pg);
8983 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8984 u64 gpa;
8985
8986 gpa = pml_buf[pml_idx];
8987 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008988 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08008989 }
8990
8991 /* reset PML index */
8992 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8993}
8994
8995/*
8996 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8997 * Called before reporting dirty_bitmap to userspace.
8998 */
8999static void kvm_flush_pml_buffers(struct kvm *kvm)
9000{
9001 int i;
9002 struct kvm_vcpu *vcpu;
9003 /*
9004 * We only need to kick vcpu out of guest mode here, as PML buffer
9005 * is flushed at beginning of all VMEXITs, and it's obvious that only
9006 * vcpus running in guest are possible to have unflushed GPAs in PML
9007 * buffer.
9008 */
9009 kvm_for_each_vcpu(i, vcpu, kvm)
9010 kvm_vcpu_kick(vcpu);
9011}
9012
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009013static void vmx_dump_sel(char *name, uint32_t sel)
9014{
9015 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009016 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009017 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9018 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9019 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9020}
9021
9022static void vmx_dump_dtsel(char *name, uint32_t limit)
9023{
9024 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9025 name, vmcs_read32(limit),
9026 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9027}
9028
9029static void dump_vmcs(void)
9030{
9031 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9032 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9033 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9034 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9035 u32 secondary_exec_control = 0;
9036 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009037 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009038 int i, n;
9039
9040 if (cpu_has_secondary_exec_ctrls())
9041 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9042
9043 pr_err("*** Guest State ***\n");
9044 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9045 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9046 vmcs_readl(CR0_GUEST_HOST_MASK));
9047 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9048 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9049 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9050 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9051 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9052 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009053 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9054 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9055 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9056 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009057 }
9058 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9059 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9060 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9061 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9062 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9063 vmcs_readl(GUEST_SYSENTER_ESP),
9064 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9065 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9066 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9067 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9068 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9069 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9070 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9071 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9072 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9073 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9074 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9075 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9076 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009077 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9078 efer, vmcs_read64(GUEST_IA32_PAT));
9079 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9080 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009081 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009082 if (cpu_has_load_perf_global_ctrl &&
9083 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009084 pr_err("PerfGlobCtl = 0x%016llx\n",
9085 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009086 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009087 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009088 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9089 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9090 vmcs_read32(GUEST_ACTIVITY_STATE));
9091 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9092 pr_err("InterruptStatus = %04x\n",
9093 vmcs_read16(GUEST_INTR_STATUS));
9094
9095 pr_err("*** Host State ***\n");
9096 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9097 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9098 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9099 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9100 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9101 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9102 vmcs_read16(HOST_TR_SELECTOR));
9103 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9104 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9105 vmcs_readl(HOST_TR_BASE));
9106 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9107 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9108 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9109 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9110 vmcs_readl(HOST_CR4));
9111 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9112 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9113 vmcs_read32(HOST_IA32_SYSENTER_CS),
9114 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9115 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009116 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9117 vmcs_read64(HOST_IA32_EFER),
9118 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009119 if (cpu_has_load_perf_global_ctrl &&
9120 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009121 pr_err("PerfGlobCtl = 0x%016llx\n",
9122 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009123
9124 pr_err("*** Control State ***\n");
9125 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9126 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9127 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9128 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9129 vmcs_read32(EXCEPTION_BITMAP),
9130 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9131 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9132 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9133 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9134 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9135 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9136 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9137 vmcs_read32(VM_EXIT_INTR_INFO),
9138 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9139 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9140 pr_err(" reason=%08x qualification=%016lx\n",
9141 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9142 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9143 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9144 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009145 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009146 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009147 pr_err("TSC Multiplier = 0x%016llx\n",
9148 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009149 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9150 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9151 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9152 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9153 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009154 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009155 n = vmcs_read32(CR3_TARGET_COUNT);
9156 for (i = 0; i + 1 < n; i += 4)
9157 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9158 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9159 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9160 if (i < n)
9161 pr_err("CR3 target%u=%016lx\n",
9162 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9163 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9164 pr_err("PLE Gap=%08x Window=%08x\n",
9165 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9166 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9167 pr_err("Virtual processor ID = 0x%04x\n",
9168 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9169}
9170
Avi Kivity6aa8b732006-12-10 02:21:36 -08009171/*
9172 * The guest has exited. See if we can fix it or if we need userspace
9173 * assistance.
9174 */
Avi Kivity851ba692009-08-24 11:10:17 +03009175static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009176{
Avi Kivity29bd8a72007-09-10 17:27:03 +03009177 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08009178 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02009179 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03009180
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01009181 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
9182
Kai Huang843e4332015-01-28 10:54:28 +08009183 /*
9184 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
9185 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
9186 * querying dirty_bitmap, we only need to kick all vcpus out of guest
9187 * mode as if vcpus is in root mode, the PML buffer must has been
9188 * flushed already.
9189 */
9190 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009191 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009192
Mohammed Gamal80ced182009-09-01 12:48:18 +02009193 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02009194 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02009195 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01009196
Paolo Bonzini7313c692017-07-27 10:31:25 +02009197 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
9198 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03009199
Mohammed Gamal51207022010-05-31 22:40:54 +03009200 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009201 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03009202 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9203 vcpu->run->fail_entry.hardware_entry_failure_reason
9204 = exit_reason;
9205 return 0;
9206 }
9207
Avi Kivity29bd8a72007-09-10 17:27:03 +03009208 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009209 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9210 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03009211 = vmcs_read32(VM_INSTRUCTION_ERROR);
9212 return 0;
9213 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009214
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009215 /*
9216 * Note:
9217 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9218 * delivery event since it indicates guest is accessing MMIO.
9219 * The vm-exit can be triggered again after return to guest that
9220 * will cause infinite loop.
9221 */
Mike Dayd77c26f2007-10-08 09:02:08 -04009222 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08009223 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02009224 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00009225 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009226 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9227 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9228 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009229 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009230 vcpu->run->internal.data[0] = vectoring_info;
9231 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009232 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9233 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9234 vcpu->run->internal.ndata++;
9235 vcpu->run->internal.data[3] =
9236 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9237 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009238 return 0;
9239 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009240
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009241 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009242 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9243 if (vmx_interrupt_allowed(vcpu)) {
9244 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9245 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9246 vcpu->arch.nmi_pending) {
9247 /*
9248 * This CPU don't support us in finding the end of an
9249 * NMI-blocked window if the guest runs with IRQs
9250 * disabled. So we pull the trigger after 1 s of
9251 * futile waiting, but inform the user about this.
9252 */
9253 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9254 "state on VCPU %d after 1 s timeout\n",
9255 __func__, vcpu->vcpu_id);
9256 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9257 }
9258 }
9259
Avi Kivity6aa8b732006-12-10 02:21:36 -08009260 if (exit_reason < kvm_vmx_max_exit_handlers
9261 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03009262 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009263 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01009264 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9265 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03009266 kvm_queue_exception(vcpu, UD_VECTOR);
9267 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009268 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009269}
9270
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009271static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009272{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009273 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9274
9275 if (is_guest_mode(vcpu) &&
9276 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9277 return;
9278
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009279 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009280 vmcs_write32(TPR_THRESHOLD, 0);
9281 return;
9282 }
9283
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009284 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009285}
9286
Jim Mattson8d860bb2018-05-09 16:56:05 -04009287static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08009288{
9289 u32 sec_exec_control;
9290
Jim Mattson8d860bb2018-05-09 16:56:05 -04009291 if (!lapic_in_kernel(vcpu))
9292 return;
9293
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009294 /* Postpone execution until vmcs01 is the current VMCS. */
9295 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -04009296 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009297 return;
9298 }
9299
Paolo Bonzini35754c92015-07-29 12:05:37 +02009300 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08009301 return;
9302
9303 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -04009304 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9305 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08009306
Jim Mattson8d860bb2018-05-09 16:56:05 -04009307 switch (kvm_get_apic_mode(vcpu)) {
9308 case LAPIC_MODE_INVALID:
9309 WARN_ONCE(true, "Invalid local APIC state");
9310 case LAPIC_MODE_DISABLED:
9311 break;
9312 case LAPIC_MODE_XAPIC:
9313 if (flexpriority_enabled) {
9314 sec_exec_control |=
9315 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9316 vmx_flush_tlb(vcpu, true);
9317 }
9318 break;
9319 case LAPIC_MODE_X2APIC:
9320 if (cpu_has_vmx_virtualize_x2apic_mode())
9321 sec_exec_control |=
9322 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9323 break;
Yang Zhang8d146952013-01-25 10:18:50 +08009324 }
9325 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9326
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009327 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009328}
9329
Tang Chen38b99172014-09-24 15:57:54 +08009330static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9331{
Jim Mattsonab5df312018-05-09 17:02:03 -04009332 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +08009333 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -07009334 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009335 }
Tang Chen38b99172014-09-24 15:57:54 +08009336}
9337
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009338static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009339{
9340 u16 status;
9341 u8 old;
9342
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009343 if (max_isr == -1)
9344 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009345
9346 status = vmcs_read16(GUEST_INTR_STATUS);
9347 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009348 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08009349 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009350 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009351 vmcs_write16(GUEST_INTR_STATUS, status);
9352 }
9353}
9354
9355static void vmx_set_rvi(int vector)
9356{
9357 u16 status;
9358 u8 old;
9359
Wei Wang4114c272014-11-05 10:53:43 +08009360 if (vector == -1)
9361 vector = 0;
9362
Yang Zhangc7c9c562013-01-25 10:18:51 +08009363 status = vmcs_read16(GUEST_INTR_STATUS);
9364 old = (u8)status & 0xff;
9365 if ((u8)vector != old) {
9366 status &= ~0xff;
9367 status |= (u8)vector;
9368 vmcs_write16(GUEST_INTR_STATUS, status);
9369 }
9370}
9371
9372static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9373{
Liran Alon851c1a182017-12-24 18:12:56 +02009374 /*
9375 * When running L2, updating RVI is only relevant when
9376 * vmcs12 virtual-interrupt-delivery enabled.
9377 * However, it can be enabled only when L1 also
9378 * intercepts external-interrupts and in that case
9379 * we should not update vmcs02 RVI but instead intercept
9380 * interrupt. Therefore, do nothing when running L2.
9381 */
9382 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08009383 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009384}
9385
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009386static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009387{
9388 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009389 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02009390 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009391
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009392 WARN_ON(!vcpu->arch.apicv_active);
9393 if (pi_test_on(&vmx->pi_desc)) {
9394 pi_clear_on(&vmx->pi_desc);
9395 /*
9396 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9397 * But on x86 this is just a compiler barrier anyway.
9398 */
9399 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02009400 max_irr_updated =
9401 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
9402
9403 /*
9404 * If we are running L2 and L1 has a new pending interrupt
9405 * which can be injected, we should re-evaluate
9406 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02009407 * If L1 intercepts external-interrupts, we should
9408 * exit from L2 to L1. Otherwise, interrupt should be
9409 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02009410 */
Liran Alon851c1a182017-12-24 18:12:56 +02009411 if (is_guest_mode(vcpu) && max_irr_updated) {
9412 if (nested_exit_on_intr(vcpu))
9413 kvm_vcpu_exiting_guest_mode(vcpu);
9414 else
9415 kvm_make_request(KVM_REQ_EVENT, vcpu);
9416 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009417 } else {
9418 max_irr = kvm_lapic_find_highest_irr(vcpu);
9419 }
9420 vmx_hwapic_irr_update(vcpu, max_irr);
9421 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009422}
9423
Andrey Smetanin63086302015-11-10 15:36:32 +03009424static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009425{
Andrey Smetanind62caab2015-11-10 15:36:33 +03009426 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08009427 return;
9428
Yang Zhangc7c9c562013-01-25 10:18:51 +08009429 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9430 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9431 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9432 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9433}
9434
Paolo Bonzini967235d2016-12-19 14:03:45 +01009435static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9436{
9437 struct vcpu_vmx *vmx = to_vmx(vcpu);
9438
9439 pi_clear_on(&vmx->pi_desc);
9440 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9441}
9442
Avi Kivity51aa01d2010-07-20 14:31:20 +03009443static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03009444{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009445 u32 exit_intr_info = 0;
9446 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02009447
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009448 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9449 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02009450 return;
9451
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009452 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9453 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9454 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08009455
Wanpeng Li1261bfa2017-07-13 18:30:40 -07009456 /* if exit due to PF check for async PF */
9457 if (is_page_fault(exit_intr_info))
9458 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9459
Andi Kleena0861c02009-06-08 17:37:09 +08009460 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009461 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9462 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08009463 kvm_machine_check();
9464
Gleb Natapov20f65982009-05-11 13:35:55 +03009465 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08009466 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -07009467 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03009468 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -07009469 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009470 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03009471}
Gleb Natapov20f65982009-05-11 13:35:55 +03009472
Yang Zhanga547c6d2013-04-11 19:25:10 +08009473static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9474{
9475 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9476
Yang Zhanga547c6d2013-04-11 19:25:10 +08009477 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9478 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9479 unsigned int vector;
9480 unsigned long entry;
9481 gate_desc *desc;
9482 struct vcpu_vmx *vmx = to_vmx(vcpu);
9483#ifdef CONFIG_X86_64
9484 unsigned long tmp;
9485#endif
9486
9487 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9488 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02009489 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009490 asm volatile(
9491#ifdef CONFIG_X86_64
9492 "mov %%" _ASM_SP ", %[sp]\n\t"
9493 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
9494 "push $%c[ss]\n\t"
9495 "push %[sp]\n\t"
9496#endif
9497 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08009498 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009499 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08009500 :
9501#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06009502 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009503#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -05009504 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +08009505 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009506 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009507 [ss]"i"(__KERNEL_DS),
9508 [cs]"i"(__KERNEL_CS)
9509 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02009510 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08009511}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009512STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009513
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009514static bool vmx_has_high_real_mode_segbase(void)
9515{
9516 return enable_unrestricted_guest || emulate_invalid_guest_state;
9517}
9518
Liu, Jinsongda8999d2014-02-24 10:55:46 +00009519static bool vmx_mpx_supported(void)
9520{
9521 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
9522 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
9523}
9524
Wanpeng Li55412b22014-12-02 19:21:30 +08009525static bool vmx_xsaves_supported(void)
9526{
9527 return vmcs_config.cpu_based_2nd_exec_ctrl &
9528 SECONDARY_EXEC_XSAVES;
9529}
9530
Avi Kivity51aa01d2010-07-20 14:31:20 +03009531static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9532{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02009533 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03009534 bool unblock_nmi;
9535 u8 vector;
9536 bool idtv_info_valid;
9537
9538 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03009539
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009540 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009541 if (vmx->loaded_vmcs->nmi_known_unmasked)
9542 return;
9543 /*
9544 * Can't use vmx->exit_intr_info since we're not sure what
9545 * the exit reason is.
9546 */
9547 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9548 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9549 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9550 /*
9551 * SDM 3: 27.7.1.2 (September 2008)
9552 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9553 * a guest IRET fault.
9554 * SDM 3: 23.2.2 (September 2008)
9555 * Bit 12 is undefined in any of the following cases:
9556 * If the VM exit sets the valid bit in the IDT-vectoring
9557 * information field.
9558 * If the VM exit is due to a double fault.
9559 */
9560 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9561 vector != DF_VECTOR && !idtv_info_valid)
9562 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9563 GUEST_INTR_STATE_NMI);
9564 else
9565 vmx->loaded_vmcs->nmi_known_unmasked =
9566 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9567 & GUEST_INTR_STATE_NMI);
9568 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
9569 vmx->loaded_vmcs->vnmi_blocked_time +=
9570 ktime_to_ns(ktime_sub(ktime_get(),
9571 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03009572}
9573
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009574static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03009575 u32 idt_vectoring_info,
9576 int instr_len_field,
9577 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03009578{
Avi Kivity51aa01d2010-07-20 14:31:20 +03009579 u8 vector;
9580 int type;
9581 bool idtv_info_valid;
9582
9583 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03009584
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009585 vcpu->arch.nmi_injected = false;
9586 kvm_clear_exception_queue(vcpu);
9587 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009588
9589 if (!idtv_info_valid)
9590 return;
9591
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009592 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03009593
Avi Kivity668f6122008-07-02 09:28:55 +03009594 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
9595 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009596
Gleb Natapov64a7ec02009-03-30 16:03:29 +03009597 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03009598 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009599 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03009600 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009601 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03009602 * Clear bit "block by NMI" before VM entry if a NMI
9603 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03009604 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009605 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009606 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009607 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009608 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009609 /* fall through */
9610 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03009611 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03009612 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03009613 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03009614 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03009615 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009616 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009617 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009618 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009619 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03009620 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009621 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009622 break;
9623 default:
9624 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03009625 }
Avi Kivitycf393f72008-07-01 16:20:21 +03009626}
9627
Avi Kivity83422e12010-07-20 14:43:23 +03009628static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
9629{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009630 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03009631 VM_EXIT_INSTRUCTION_LEN,
9632 IDT_VECTORING_ERROR_CODE);
9633}
9634
Avi Kivityb463a6f2010-07-20 15:06:17 +03009635static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
9636{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009637 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03009638 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9639 VM_ENTRY_INSTRUCTION_LEN,
9640 VM_ENTRY_EXCEPTION_ERROR_CODE);
9641
9642 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
9643}
9644
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009645static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
9646{
9647 int i, nr_msrs;
9648 struct perf_guest_switch_msr *msrs;
9649
9650 msrs = perf_guest_get_msrs(&nr_msrs);
9651
9652 if (!msrs)
9653 return;
9654
9655 for (i = 0; i < nr_msrs; i++)
9656 if (msrs[i].host == msrs[i].guest)
9657 clear_atomic_switch_msr(vmx, msrs[i].msr);
9658 else
9659 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
9660 msrs[i].host);
9661}
9662
Jiang Biao33365e72016-11-03 15:03:37 +08009663static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07009664{
9665 struct vcpu_vmx *vmx = to_vmx(vcpu);
9666 u64 tscl;
9667 u32 delta_tsc;
9668
9669 if (vmx->hv_deadline_tsc == -1)
9670 return;
9671
9672 tscl = rdtsc();
9673 if (vmx->hv_deadline_tsc > tscl)
9674 /* sure to be 32 bit only because checked on set_hv_timer */
9675 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
9676 cpu_preemption_timer_multi);
9677 else
9678 delta_tsc = 0;
9679
9680 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
9681}
9682
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08009683static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009684{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009685 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009686 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +02009687
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009688 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009689 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009690 vmx->loaded_vmcs->soft_vnmi_blocked))
9691 vmx->loaded_vmcs->entry_time = ktime_get();
9692
Avi Kivity104f2262010-11-18 13:12:52 +02009693 /* Don't enter VMX if guest state is invalid, let the exit handler
9694 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02009695 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02009696 return;
9697
Radim Krčmářa7653ec2014-08-21 18:08:07 +02009698 if (vmx->ple_window_dirty) {
9699 vmx->ple_window_dirty = false;
9700 vmcs_write32(PLE_WINDOW, vmx->ple_window);
9701 }
9702
Abel Gordon012f83c2013-04-18 14:39:25 +03009703 if (vmx->nested.sync_shadow_vmcs) {
9704 copy_vmcs12_to_shadow(vmx);
9705 vmx->nested.sync_shadow_vmcs = false;
9706 }
9707
Avi Kivity104f2262010-11-18 13:12:52 +02009708 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
9709 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
9710 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
9711 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
9712
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009713 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +02009714 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009715 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +02009716 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009717 }
9718
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07009719 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +02009720 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009721 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +02009722 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009723 }
9724
Avi Kivity104f2262010-11-18 13:12:52 +02009725 /* When single-stepping over STI and MOV SS, we must clear the
9726 * corresponding interruptibility bits in the guest state. Otherwise
9727 * vmentry fails as it then expects bit 14 (BS) in pending debug
9728 * exceptions being set, but that's not correct for the guest debugging
9729 * case. */
9730 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9731 vmx_set_interrupt_shadow(vcpu, 0);
9732
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02009733 if (static_cpu_has(X86_FEATURE_PKU) &&
9734 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
9735 vcpu->arch.pkru != vmx->host_pkru)
9736 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009737
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009738 atomic_switch_perf_msrs(vmx);
9739
Yunhong Jiang64672c92016-06-13 14:19:59 -07009740 vmx_arm_hv_timer(vcpu);
9741
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009742 /*
9743 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
9744 * it's non-zero. Since vmentry is serialising on affected CPUs, there
9745 * is no need to worry about the conditional branch over the wrmsr
9746 * being speculatively taken.
9747 */
9748 if (vmx->spec_ctrl)
Paolo Bonziniecb586b2018-02-22 16:43:17 +01009749 native_wrmsrl(MSR_IA32_SPEC_CTRL, vmx->spec_ctrl);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009750
Nadav Har'Eld462b812011-05-24 15:26:10 +03009751 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009752
9753 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
9754 (unsigned long)&current_evmcs->host_rsp : 0;
9755
Avi Kivity104f2262010-11-18 13:12:52 +02009756 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08009757 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009758 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
9759 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
9760 "push %%" _ASM_CX " \n\t"
9761 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03009762 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009763 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009764 /* Avoid VMWRITE when Enlightened VMCS is in use */
9765 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
9766 "jz 2f \n\t"
9767 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
9768 "jmp 1f \n\t"
9769 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009770 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03009771 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009772 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009773 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
9774 "mov %%cr2, %%" _ASM_DX " \n\t"
9775 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009776 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009777 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009778 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009779 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02009780 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009781 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009782 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
9783 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
9784 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
9785 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
9786 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
9787 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009788#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009789 "mov %c[r8](%0), %%r8 \n\t"
9790 "mov %c[r9](%0), %%r9 \n\t"
9791 "mov %c[r10](%0), %%r10 \n\t"
9792 "mov %c[r11](%0), %%r11 \n\t"
9793 "mov %c[r12](%0), %%r12 \n\t"
9794 "mov %c[r13](%0), %%r13 \n\t"
9795 "mov %c[r14](%0), %%r14 \n\t"
9796 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009797#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009798 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03009799
Avi Kivity6aa8b732006-12-10 02:21:36 -08009800 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03009801 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009802 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009803 "jmp 2f \n\t"
9804 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
9805 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08009806 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009807 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02009808 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -08009809 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009810 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
9811 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
9812 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
9813 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
9814 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
9815 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
9816 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009817#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009818 "mov %%r8, %c[r8](%0) \n\t"
9819 "mov %%r9, %c[r9](%0) \n\t"
9820 "mov %%r10, %c[r10](%0) \n\t"
9821 "mov %%r11, %c[r11](%0) \n\t"
9822 "mov %%r12, %c[r12](%0) \n\t"
9823 "mov %%r13, %c[r13](%0) \n\t"
9824 "mov %%r14, %c[r14](%0) \n\t"
9825 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -08009826 "xor %%r8d, %%r8d \n\t"
9827 "xor %%r9d, %%r9d \n\t"
9828 "xor %%r10d, %%r10d \n\t"
9829 "xor %%r11d, %%r11d \n\t"
9830 "xor %%r12d, %%r12d \n\t"
9831 "xor %%r13d, %%r13d \n\t"
9832 "xor %%r14d, %%r14d \n\t"
9833 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009834#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009835 "mov %%cr2, %%" _ASM_AX " \n\t"
9836 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03009837
Jim Mattson0cb5b302018-01-03 14:31:38 -08009838 "xor %%eax, %%eax \n\t"
9839 "xor %%ebx, %%ebx \n\t"
9840 "xor %%esi, %%esi \n\t"
9841 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009842 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009843 ".pushsection .rodata \n\t"
9844 ".global vmx_return \n\t"
9845 "vmx_return: " _ASM_PTR " 2b \n\t"
9846 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009847 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +03009848 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02009849 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +03009850 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009851 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
9852 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
9853 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
9854 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
9855 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
9856 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
9857 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009858#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009859 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
9860 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
9861 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
9862 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
9863 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
9864 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
9865 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
9866 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08009867#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02009868 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
9869 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02009870 : "cc", "memory"
9871#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009872 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009873 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009874#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009875 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009876#endif
9877 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08009878
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009879 /*
9880 * We do not use IBRS in the kernel. If this vCPU has used the
9881 * SPEC_CTRL MSR it may have left it on; save the value and
9882 * turn it off. This is much more efficient than blindly adding
9883 * it to the atomic save/restore list. Especially as the former
9884 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
9885 *
9886 * For non-nested case:
9887 * If the L01 MSR bitmap does not intercept the MSR, then we need to
9888 * save it.
9889 *
9890 * For nested case:
9891 * If the L02 MSR bitmap does not intercept the MSR, then we need to
9892 * save it.
9893 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +01009894 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +01009895 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009896
9897 if (vmx->spec_ctrl)
Paolo Bonziniecb586b2018-02-22 16:43:17 +01009898 native_wrmsrl(MSR_IA32_SPEC_CTRL, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009899
David Woodhouse117cc7a2018-01-12 11:11:27 +00009900 /* Eliminate branch target predictions from guest mode */
9901 vmexit_fill_RSB();
9902
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009903 /* All fields are clean at this point */
9904 if (static_branch_unlikely(&enable_evmcs))
9905 current_evmcs->hv_clean_fields |=
9906 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
9907
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009908 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -08009909 if (vmx->host_debugctlmsr)
9910 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009911
Avi Kivityaa67f602012-08-01 16:48:03 +03009912#ifndef CONFIG_X86_64
9913 /*
9914 * The sysexit path does not restore ds/es, so we must set them to
9915 * a reasonable value ourselves.
9916 *
9917 * We can't defer this to vmx_load_host_state() since that function
9918 * may be executed in interrupt context, which saves and restore segments
9919 * around it, nullifying its effect.
9920 */
9921 loadsegment(ds, __USER_DS);
9922 loadsegment(es, __USER_DS);
9923#endif
9924
Avi Kivity6de4f3a2009-05-31 22:58:47 +03009925 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02009926 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009927 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03009928 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009929 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009930 vcpu->arch.regs_dirty = 0;
9931
Gleb Natapove0b890d2013-09-25 12:51:33 +03009932 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009933 * eager fpu is enabled if PKEY is supported and CR4 is switched
9934 * back on host, so it is safe to read guest PKRU from current
9935 * XSAVE.
9936 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02009937 if (static_cpu_has(X86_FEATURE_PKU) &&
9938 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
9939 vcpu->arch.pkru = __read_pkru();
9940 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009941 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009942 }
9943
Gleb Natapove0b890d2013-09-25 12:51:33 +03009944 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -07009945 vmx->idt_vectoring_info = 0;
9946
9947 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
9948 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9949 return;
9950
9951 vmx->loaded_vmcs->launched = 1;
9952 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +03009953
Avi Kivity51aa01d2010-07-20 14:31:20 +03009954 vmx_complete_atomic_exit(vmx);
9955 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03009956 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009957}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009958STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009959
Sean Christopherson434a1e92018-03-20 12:17:18 -07009960static struct kvm *vmx_vm_alloc(void)
9961{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07009962 struct kvm_vmx *kvm_vmx = kzalloc(sizeof(struct kvm_vmx), GFP_KERNEL);
9963 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -07009964}
9965
9966static void vmx_vm_free(struct kvm *kvm)
9967{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07009968 kfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -07009969}
9970
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009971static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009972{
9973 struct vcpu_vmx *vmx = to_vmx(vcpu);
9974 int cpu;
9975
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009976 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009977 return;
9978
9979 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009980 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009981 vmx_vcpu_put(vcpu);
9982 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009983 put_cpu();
9984}
9985
Jim Mattson2f1fe812016-07-08 15:36:06 -07009986/*
9987 * Ensure that the current vmcs of the logical processor is the
9988 * vmcs01 of the vcpu before calling free_nested().
9989 */
9990static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9991{
9992 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009993
Christoffer Dallec7660c2017-12-04 21:35:23 +01009994 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009995 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009996 free_nested(vmx);
9997 vcpu_put(vcpu);
9998}
9999
Avi Kivity6aa8b732006-12-10 02:21:36 -080010000static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10001{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010002 struct vcpu_vmx *vmx = to_vmx(vcpu);
10003
Kai Huang843e4332015-01-28 10:54:28 +080010004 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010005 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010006 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010007 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010008 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010009 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010010 kfree(vmx->guest_msrs);
10011 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010012 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010013}
10014
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010015static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010016{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010017 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010018 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010019 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010020 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010021
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010022 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010023 return ERR_PTR(-ENOMEM);
10024
Wanpeng Li991e7a02015-09-16 17:30:05 +080010025 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010026
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010027 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10028 if (err)
10029 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010030
Peter Feiner4e595162016-07-07 14:49:58 -070010031 err = -ENOMEM;
10032
10033 /*
10034 * If PML is turned on, failure on enabling PML just results in failure
10035 * of creating the vcpu, therefore we can simplify PML logic (by
10036 * avoiding dealing with cases, such as enabling PML partially on vcpus
10037 * for the guest, etc.
10038 */
10039 if (enable_pml) {
10040 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10041 if (!vmx->pml_pg)
10042 goto uninit_vcpu;
10043 }
10044
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010045 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020010046 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10047 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030010048
Peter Feiner4e595162016-07-07 14:49:58 -070010049 if (!vmx->guest_msrs)
10050 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010051
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010052 err = alloc_loaded_vmcs(&vmx->vmcs01);
10053 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010054 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010055
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010056 msr_bitmap = vmx->vmcs01.msr_bitmap;
10057 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10058 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10059 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10060 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10061 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10062 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10063 vmx->msr_bitmap_mode = 0;
10064
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010065 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030010066 cpu = get_cpu();
10067 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100010068 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020010069 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010070 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030010071 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020010072 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020010073 err = alloc_apic_access_page(kvm);
10074 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020010075 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020010076 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080010077
Sean Christophersone90008d2018-03-05 12:04:37 -080010078 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080010079 err = init_rmode_identity_map(kvm);
10080 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020010081 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080010082 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080010083
Wanpeng Li5c614b32015-10-13 09:18:36 -070010084 if (nested) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010085 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
10086 kvm_vcpu_apicv_active(&vmx->vcpu));
Wanpeng Li5c614b32015-10-13 09:18:36 -070010087 vmx->nested.vpid02 = allocate_vpid();
10088 }
Wincy Vanb9c237b2015-02-03 23:56:30 +080010089
Wincy Van705699a2015-02-03 23:58:17 +080010090 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010091 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010092
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010093 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
10094
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020010095 /*
10096 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
10097 * or POSTED_INTR_WAKEUP_VECTOR.
10098 */
10099 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
10100 vmx->pi_desc.sn = 1;
10101
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010102 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010103
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010104free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -070010105 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080010106 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010107free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010108 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070010109free_pml:
10110 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010111uninit_vcpu:
10112 kvm_vcpu_uninit(&vmx->vcpu);
10113free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080010114 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100010115 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010116 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010117}
10118
Wanpeng Lib31c1142018-03-12 04:53:04 -070010119static int vmx_vm_init(struct kvm *kvm)
10120{
10121 if (!ple_gap)
10122 kvm->arch.pause_in_guest = true;
10123 return 0;
10124}
10125
Yang, Sheng002c7f72007-07-31 14:23:01 +030010126static void __init vmx_check_processor_compat(void *rtn)
10127{
10128 struct vmcs_config vmcs_conf;
10129
10130 *(int *)rtn = 0;
10131 if (setup_vmcs_config(&vmcs_conf) < 0)
10132 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010010133 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030010134 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
10135 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
10136 smp_processor_id());
10137 *(int *)rtn = -EIO;
10138 }
10139}
10140
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010141static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080010142{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010143 u8 cache;
10144 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010145
Sheng Yang522c68c2009-04-27 20:35:43 +080010146 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020010147 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080010148 * 2. EPT with VT-d:
10149 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020010150 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080010151 * b. VT-d with snooping control feature: snooping control feature of
10152 * VT-d engine can guarantee the cache correctness. Just set it
10153 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080010154 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080010155 * consistent with host MTRR
10156 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020010157 if (is_mmio) {
10158 cache = MTRR_TYPE_UNCACHABLE;
10159 goto exit;
10160 }
10161
10162 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010163 ipat = VMX_EPT_IPAT_BIT;
10164 cache = MTRR_TYPE_WRBACK;
10165 goto exit;
10166 }
10167
10168 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
10169 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020010170 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080010171 cache = MTRR_TYPE_WRBACK;
10172 else
10173 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010174 goto exit;
10175 }
10176
Xiao Guangrongff536042015-06-15 16:55:22 +080010177 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010178
10179exit:
10180 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080010181}
10182
Sheng Yang17cc3932010-01-05 19:02:27 +080010183static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020010184{
Sheng Yang878403b2010-01-05 19:02:29 +080010185 if (enable_ept && !cpu_has_vmx_ept_1g_page())
10186 return PT_DIRECTORY_LEVEL;
10187 else
10188 /* For shadow and EPT supported 1GB page */
10189 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020010190}
10191
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010192static void vmcs_set_secondary_exec_control(u32 new_ctl)
10193{
10194 /*
10195 * These bits in the secondary execution controls field
10196 * are dynamic, the others are mostly based on the hypervisor
10197 * architecture and the guest's CPUID. Do not touch the
10198 * dynamic bits.
10199 */
10200 u32 mask =
10201 SECONDARY_EXEC_SHADOW_VMCS |
10202 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020010203 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10204 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010205
10206 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10207
10208 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10209 (new_ctl & ~mask) | (cur_ctl & mask));
10210}
10211
David Matlack8322ebb2016-11-29 18:14:09 -080010212/*
10213 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10214 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10215 */
10216static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10217{
10218 struct vcpu_vmx *vmx = to_vmx(vcpu);
10219 struct kvm_cpuid_entry2 *entry;
10220
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010221 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
10222 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080010223
10224#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10225 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010226 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080010227} while (0)
10228
10229 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10230 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10231 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10232 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10233 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10234 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10235 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10236 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10237 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10238 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10239 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10240 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10241 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10242 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10243 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10244
10245 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10246 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10247 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10248 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10249 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010010250 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080010251
10252#undef cr4_fixed1_update
10253}
10254
Sheng Yang0e851882009-12-18 16:48:46 +080010255static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10256{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010257 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010258
Paolo Bonzini80154d72017-08-24 13:55:35 +020010259 if (cpu_has_secondary_exec_ctrls()) {
10260 vmx_compute_secondary_exec_control(vmx);
10261 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010262 }
Mao, Junjiead756a12012-07-02 01:18:48 +000010263
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010264 if (nested_vmx_allowed(vcpu))
10265 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10266 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10267 else
10268 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10269 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080010270
10271 if (nested_vmx_allowed(vcpu))
10272 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +080010273}
10274
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010275static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10276{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030010277 if (func == 1 && nested)
10278 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010279}
10280
Yang Zhang25d92082013-08-06 12:00:32 +030010281static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10282 struct x86_exception *fault)
10283{
Jan Kiszka533558b2014-01-04 18:47:20 +010010284 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040010285 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010286 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010287 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030010288
Bandan Dasc5f983f2017-05-05 15:25:14 -040010289 if (vmx->nested.pml_full) {
10290 exit_reason = EXIT_REASON_PML_FULL;
10291 vmx->nested.pml_full = false;
10292 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10293 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010010294 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030010295 else
Jan Kiszka533558b2014-01-04 18:47:20 +010010296 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010297
10298 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030010299 vmcs12->guest_physical_address = fault->address;
10300}
10301
Peter Feiner995f00a2017-06-30 17:26:32 -070010302static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10303{
David Hildenbrandbb97a012017-08-10 23:15:28 +020010304 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070010305}
10306
Nadav Har'El155a97a2013-08-05 11:07:16 +030010307/* Callbacks for nested_ept_init_mmu_context: */
10308
10309static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10310{
10311 /* return the page table to be shadowed - in our case, EPT12 */
10312 return get_vmcs12(vcpu)->ept_pointer;
10313}
10314
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010315static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030010316{
Paolo Bonziniad896af2013-10-02 16:56:14 +020010317 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020010318 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010319 return 1;
10320
10321 kvm_mmu_unload(vcpu);
Paolo Bonziniad896af2013-10-02 16:56:14 +020010322 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010323 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010324 VMX_EPT_EXECUTE_ONLY_BIT,
David Hildenbranda057e0e2017-08-10 23:36:54 +020010325 nested_ept_ad_enabled(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030010326 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10327 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10328 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10329
10330 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010331 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030010332}
10333
10334static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10335{
10336 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10337}
10338
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030010339static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10340 u16 error_code)
10341{
10342 bool inequality, bit;
10343
10344 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10345 inequality =
10346 (error_code & vmcs12->page_fault_error_code_mask) !=
10347 vmcs12->page_fault_error_code_match;
10348 return inequality ^ bit;
10349}
10350
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010351static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10352 struct x86_exception *fault)
10353{
10354 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10355
10356 WARN_ON(!is_guest_mode(vcpu));
10357
Wanpeng Li305d0ab2017-09-28 18:16:44 -070010358 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10359 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020010360 vmcs12->vm_exit_intr_error_code = fault->error_code;
10361 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10362 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10363 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10364 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010365 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010366 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010367 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010368}
10369
Paolo Bonzinic9923842017-12-13 14:16:30 +010010370static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10371 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010372
10373static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010374 struct vmcs12 *vmcs12)
10375{
10376 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010377 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010378 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010379
10380 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010381 /*
10382 * Translate L1 physical address to host physical
10383 * address for vmcs02. Keep the page pinned, so this
10384 * physical address remains valid. We keep a reference
10385 * to it so we can release it later.
10386 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010387 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010388 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010389 vmx->nested.apic_access_page = NULL;
10390 }
10391 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010392 /*
10393 * If translation failed, no matter: This feature asks
10394 * to exit when accessing the given address, and if it
10395 * can never be accessed, this feature won't do
10396 * anything anyway.
10397 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010398 if (!is_error_page(page)) {
10399 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010400 hpa = page_to_phys(vmx->nested.apic_access_page);
10401 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10402 } else {
10403 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
10404 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10405 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010406 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010407
10408 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010409 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010410 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010411 vmx->nested.virtual_apic_page = NULL;
10412 }
10413 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010414
10415 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010416 * If translation failed, VM entry will fail because
10417 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10418 * Failing the vm entry is _not_ what the processor
10419 * does but it's basically the only possibility we
10420 * have. We could still enter the guest if CR8 load
10421 * exits are enabled, CR8 store exits are enabled, and
10422 * virtualize APIC access is disabled; in this case
10423 * the processor would never use the TPR shadow and we
10424 * could simply clear the bit from the execution
10425 * control. But such a configuration is useless, so
10426 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010427 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010428 if (!is_error_page(page)) {
10429 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010430 hpa = page_to_phys(vmx->nested.virtual_apic_page);
10431 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
10432 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010433 }
10434
Wincy Van705699a2015-02-03 23:58:17 +080010435 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010436 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
10437 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010438 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010439 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080010440 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010441 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
10442 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010443 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010444 vmx->nested.pi_desc_page = page;
10445 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080010446 vmx->nested.pi_desc =
10447 (struct pi_desc *)((void *)vmx->nested.pi_desc +
10448 (unsigned long)(vmcs12->posted_intr_desc_addr &
10449 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010450 vmcs_write64(POSTED_INTR_DESC_ADDR,
10451 page_to_phys(vmx->nested.pi_desc_page) +
10452 (unsigned long)(vmcs12->posted_intr_desc_addr &
10453 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080010454 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080010455 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000010456 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
10457 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010458 else
10459 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
10460 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010461}
10462
Jan Kiszkaf4124502014-03-07 20:03:13 +010010463static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
10464{
10465 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
10466 struct vcpu_vmx *vmx = to_vmx(vcpu);
10467
10468 if (vcpu->arch.virtual_tsc_khz == 0)
10469 return;
10470
10471 /* Make sure short timeouts reliably trigger an immediate vmexit.
10472 * hrtimer_start does not guarantee this. */
10473 if (preemption_timeout <= 1) {
10474 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
10475 return;
10476 }
10477
10478 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10479 preemption_timeout *= 1000000;
10480 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
10481 hrtimer_start(&vmx->nested.preemption_timer,
10482 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
10483}
10484
Jim Mattson56a20512017-07-06 16:33:06 -070010485static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
10486 struct vmcs12 *vmcs12)
10487{
10488 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
10489 return 0;
10490
10491 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
10492 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
10493 return -EINVAL;
10494
10495 return 0;
10496}
10497
Wincy Van3af18d92015-02-03 23:49:31 +080010498static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
10499 struct vmcs12 *vmcs12)
10500{
Wincy Van3af18d92015-02-03 23:49:31 +080010501 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10502 return 0;
10503
Jim Mattson5fa99cb2017-07-06 16:33:07 -070010504 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080010505 return -EINVAL;
10506
10507 return 0;
10508}
10509
Jim Mattson712b12d2017-08-24 13:24:47 -070010510static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
10511 struct vmcs12 *vmcs12)
10512{
10513 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10514 return 0;
10515
10516 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
10517 return -EINVAL;
10518
10519 return 0;
10520}
10521
Wincy Van3af18d92015-02-03 23:49:31 +080010522/*
10523 * Merge L0's and L1's MSR bitmap, return false to indicate that
10524 * we do not use the hardware.
10525 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010010526static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10527 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080010528{
Wincy Van82f0dd42015-02-03 23:57:18 +080010529 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080010530 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020010531 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010532 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010010533 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010534 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010010535 *
10536 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
10537 * ensures that we do not accidentally generate an L02 MSR bitmap
10538 * from the L12 MSR bitmap that is too permissive.
10539 * 2. That L1 or L2s have actually used the MSR. This avoids
10540 * unnecessarily merging of the bitmap if the MSR is unused. This
10541 * works properly because we only update the L01 MSR bitmap lazily.
10542 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
10543 * updated to reflect this when L1 (or its L2s) actually write to
10544 * the MSR.
10545 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000010546 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
10547 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080010548
Paolo Bonzinic9923842017-12-13 14:16:30 +010010549 /* Nothing to do if the MSR bitmap is not in use. */
10550 if (!cpu_has_vmx_msr_bitmap() ||
10551 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10552 return false;
10553
Ashok Raj15d45072018-02-01 22:59:43 +010010554 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010555 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080010556 return false;
10557
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010558 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
10559 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080010560 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010010561
Radim Krčmářd048c092016-08-08 20:16:22 +020010562 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010010563 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
10564 /*
10565 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
10566 * just lets the processor take the value from the virtual-APIC page;
10567 * take those 256 bits directly from the L1 bitmap.
10568 */
10569 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10570 unsigned word = msr / BITS_PER_LONG;
10571 msr_bitmap_l0[word] = msr_bitmap_l1[word];
10572 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080010573 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010010574 } else {
10575 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10576 unsigned word = msr / BITS_PER_LONG;
10577 msr_bitmap_l0[word] = ~0;
10578 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
10579 }
10580 }
10581
10582 nested_vmx_disable_intercept_for_msr(
10583 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010584 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010585 MSR_TYPE_W);
10586
10587 if (nested_cpu_has_vid(vmcs12)) {
10588 nested_vmx_disable_intercept_for_msr(
10589 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010590 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010591 MSR_TYPE_W);
10592 nested_vmx_disable_intercept_for_msr(
10593 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010594 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010595 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080010596 }
Ashok Raj15d45072018-02-01 22:59:43 +010010597
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010598 if (spec_ctrl)
10599 nested_vmx_disable_intercept_for_msr(
10600 msr_bitmap_l1, msr_bitmap_l0,
10601 MSR_IA32_SPEC_CTRL,
10602 MSR_TYPE_R | MSR_TYPE_W);
10603
Ashok Raj15d45072018-02-01 22:59:43 +010010604 if (pred_cmd)
10605 nested_vmx_disable_intercept_for_msr(
10606 msr_bitmap_l1, msr_bitmap_l0,
10607 MSR_IA32_PRED_CMD,
10608 MSR_TYPE_W);
10609
Wincy Vanf2b93282015-02-03 23:56:03 +080010610 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010611 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080010612
10613 return true;
10614}
10615
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040010616static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
10617 struct vmcs12 *vmcs12)
10618{
10619 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
10620 !page_address_valid(vcpu, vmcs12->apic_access_addr))
10621 return -EINVAL;
10622 else
10623 return 0;
10624}
10625
Wincy Vanf2b93282015-02-03 23:56:03 +080010626static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
10627 struct vmcs12 *vmcs12)
10628{
Wincy Van82f0dd42015-02-03 23:57:18 +080010629 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080010630 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080010631 !nested_cpu_has_vid(vmcs12) &&
10632 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080010633 return 0;
10634
10635 /*
10636 * If virtualize x2apic mode is enabled,
10637 * virtualize apic access must be disabled.
10638 */
Wincy Van82f0dd42015-02-03 23:57:18 +080010639 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10640 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080010641 return -EINVAL;
10642
Wincy Van608406e2015-02-03 23:57:51 +080010643 /*
10644 * If virtual interrupt delivery is enabled,
10645 * we must exit on external interrupts.
10646 */
10647 if (nested_cpu_has_vid(vmcs12) &&
10648 !nested_exit_on_intr(vcpu))
10649 return -EINVAL;
10650
Wincy Van705699a2015-02-03 23:58:17 +080010651 /*
10652 * bits 15:8 should be zero in posted_intr_nv,
10653 * the descriptor address has been already checked
10654 * in nested_get_vmcs12_pages.
10655 */
10656 if (nested_cpu_has_posted_intr(vmcs12) &&
10657 (!nested_cpu_has_vid(vmcs12) ||
10658 !nested_exit_intr_ack_set(vcpu) ||
10659 vmcs12->posted_intr_nv & 0xff00))
10660 return -EINVAL;
10661
Wincy Vanf2b93282015-02-03 23:56:03 +080010662 /* tpr shadow is needed by all apicv features. */
10663 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10664 return -EINVAL;
10665
10666 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080010667}
10668
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010669static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
10670 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010671 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030010672{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010673 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010674 u64 count, addr;
10675
10676 if (vmcs12_read_any(vcpu, count_field, &count) ||
10677 vmcs12_read_any(vcpu, addr_field, &addr)) {
10678 WARN_ON(1);
10679 return -EINVAL;
10680 }
10681 if (count == 0)
10682 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010683 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010684 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
10685 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010686 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010687 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
10688 addr_field, maxphyaddr, count, addr);
10689 return -EINVAL;
10690 }
10691 return 0;
10692}
10693
10694static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
10695 struct vmcs12 *vmcs12)
10696{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010697 if (vmcs12->vm_exit_msr_load_count == 0 &&
10698 vmcs12->vm_exit_msr_store_count == 0 &&
10699 vmcs12->vm_entry_msr_load_count == 0)
10700 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010701 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010702 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010703 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010704 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010705 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010706 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030010707 return -EINVAL;
10708 return 0;
10709}
10710
Bandan Dasc5f983f2017-05-05 15:25:14 -040010711static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
10712 struct vmcs12 *vmcs12)
10713{
10714 u64 address = vmcs12->pml_address;
10715 int maxphyaddr = cpuid_maxphyaddr(vcpu);
10716
10717 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
10718 if (!nested_cpu_has_ept(vmcs12) ||
10719 !IS_ALIGNED(address, 4096) ||
10720 address >> maxphyaddr)
10721 return -EINVAL;
10722 }
10723
10724 return 0;
10725}
10726
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010727static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
10728 struct vmx_msr_entry *e)
10729{
10730 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020010731 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010732 return -EINVAL;
10733 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
10734 e->index == MSR_IA32_UCODE_REV)
10735 return -EINVAL;
10736 if (e->reserved != 0)
10737 return -EINVAL;
10738 return 0;
10739}
10740
10741static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
10742 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030010743{
10744 if (e->index == MSR_FS_BASE ||
10745 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010746 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
10747 nested_vmx_msr_check_common(vcpu, e))
10748 return -EINVAL;
10749 return 0;
10750}
10751
10752static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
10753 struct vmx_msr_entry *e)
10754{
10755 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
10756 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030010757 return -EINVAL;
10758 return 0;
10759}
10760
10761/*
10762 * Load guest's/host's msr at nested entry/exit.
10763 * return 0 for success, entry index for failure.
10764 */
10765static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10766{
10767 u32 i;
10768 struct vmx_msr_entry e;
10769 struct msr_data msr;
10770
10771 msr.host_initiated = false;
10772 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010773 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
10774 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010775 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010776 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10777 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010778 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010779 }
10780 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010781 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010782 "%s check failed (%u, 0x%x, 0x%x)\n",
10783 __func__, i, e.index, e.reserved);
10784 goto fail;
10785 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010786 msr.index = e.index;
10787 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010788 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010789 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010790 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10791 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030010792 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010793 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010794 }
10795 return 0;
10796fail:
10797 return i + 1;
10798}
10799
10800static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10801{
10802 u32 i;
10803 struct vmx_msr_entry e;
10804
10805 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010806 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010807 if (kvm_vcpu_read_guest(vcpu,
10808 gpa + i * sizeof(e),
10809 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010810 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010811 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10812 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010813 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010814 }
10815 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010816 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010817 "%s check failed (%u, 0x%x, 0x%x)\n",
10818 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030010819 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010820 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010821 msr_info.host_initiated = false;
10822 msr_info.index = e.index;
10823 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010824 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010825 "%s cannot read MSR (%u, 0x%x)\n",
10826 __func__, i, e.index);
10827 return -EINVAL;
10828 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010829 if (kvm_vcpu_write_guest(vcpu,
10830 gpa + i * sizeof(e) +
10831 offsetof(struct vmx_msr_entry, value),
10832 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010833 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010834 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010835 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010836 return -EINVAL;
10837 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010838 }
10839 return 0;
10840}
10841
Ladi Prosek1dc35da2016-11-30 16:03:11 +010010842static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
10843{
10844 unsigned long invalid_mask;
10845
10846 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
10847 return (val & invalid_mask) == 0;
10848}
10849
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010850/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010851 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
10852 * emulating VM entry into a guest with EPT enabled.
10853 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10854 * is assigned to entry_failure_code on failure.
10855 */
10856static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080010857 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010858{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010859 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010010860 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010861 *entry_failure_code = ENTRY_FAIL_DEFAULT;
10862 return 1;
10863 }
10864
10865 /*
10866 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
10867 * must not be dereferenced.
10868 */
10869 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
10870 !nested_ept) {
10871 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
10872 *entry_failure_code = ENTRY_FAIL_PDPTE;
10873 return 1;
10874 }
10875 }
10876
10877 vcpu->arch.cr3 = cr3;
10878 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
10879 }
10880
10881 kvm_mmu_reset_context(vcpu);
10882 return 0;
10883}
10884
Jim Mattson6514dc32018-04-26 16:09:12 -070010885static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010010886{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010010887 struct vcpu_vmx *vmx = to_vmx(vcpu);
10888
10889 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
10890 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
10891 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
10892 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
10893 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
10894 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
10895 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
10896 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
10897 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
10898 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
10899 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
10900 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
10901 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
10902 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
10903 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
10904 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
10905 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
10906 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
10907 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
10908 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
10909 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
10910 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
10911 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
10912 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
10913 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
10914 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
10915 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
10916 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
10917 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
10918 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
10919 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010010920
10921 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
10922 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
10923 vmcs12->guest_pending_dbg_exceptions);
10924 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
10925 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
10926
10927 if (nested_cpu_has_xsaves(vmcs12))
10928 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
10929 vmcs_write64(VMCS_LINK_POINTER, -1ull);
10930
10931 if (cpu_has_vmx_posted_intr())
10932 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
10933
10934 /*
10935 * Whether page-faults are trapped is determined by a combination of
10936 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
10937 * If enable_ept, L0 doesn't care about page faults and we should
10938 * set all of these to L1's desires. However, if !enable_ept, L0 does
10939 * care about (at least some) page faults, and because it is not easy
10940 * (if at all possible?) to merge L0 and L1's desires, we simply ask
10941 * to exit on each and every L2 page fault. This is done by setting
10942 * MASK=MATCH=0 and (see below) EB.PF=1.
10943 * Note that below we don't need special code to set EB.PF beyond the
10944 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
10945 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
10946 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
10947 */
10948 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
10949 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
10950 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
10951 enable_ept ? vmcs12->page_fault_error_code_match : 0);
10952
10953 /* All VMFUNCs are currently emulated through L0 vmexits. */
10954 if (cpu_has_vmx_vmfunc())
10955 vmcs_write64(VM_FUNCTION_CONTROL, 0);
10956
10957 if (cpu_has_vmx_apicv()) {
10958 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
10959 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
10960 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
10961 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
10962 }
10963
10964 /*
10965 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
10966 * Some constant fields are set here by vmx_set_constant_host_state().
10967 * Other fields are different per CPU, and will be set later when
10968 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
10969 */
10970 vmx_set_constant_host_state(vmx);
10971
10972 /*
10973 * Set the MSR load/store lists to match L0's settings.
10974 */
10975 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
10976 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10977 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
10978 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10979 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
10980
10981 set_cr4_guest_host_mask(vmx);
10982
10983 if (vmx_mpx_supported())
10984 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
10985
10986 if (enable_vpid) {
10987 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
10988 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
10989 else
10990 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
10991 }
10992
10993 /*
10994 * L1 may access the L2's PDPTR, so save them to construct vmcs12
10995 */
10996 if (enable_ept) {
10997 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
10998 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
10999 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
11000 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
11001 }
Radim Krčmář80132f42018-02-02 18:26:58 +010011002
11003 if (cpu_has_vmx_msr_bitmap())
11004 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011005}
11006
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011007/*
11008 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
11009 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080011010 * 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 +030011011 * guest in a way that will both be appropriate to L1's requests, and our
11012 * needs. In addition to modifying the active vmcs (which is vmcs02), this
11013 * function also has additional necessary side-effects, like setting various
11014 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010011015 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11016 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011017 */
Ladi Prosekee146c12016-11-30 16:03:09 +010011018static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattson6514dc32018-04-26 16:09:12 -070011019 u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011020{
11021 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040011022 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011023
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011024 if (vmx->nested.dirty_vmcs12) {
Jim Mattson6514dc32018-04-26 16:09:12 -070011025 prepare_vmcs02_full(vcpu, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011026 vmx->nested.dirty_vmcs12 = false;
11027 }
11028
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011029 /*
11030 * First, the fields that are shadowed. This must be kept in sync
11031 * with vmx_shadow_fields.h.
11032 */
11033
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011034 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011035 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011036 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011037 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
11038 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011039
11040 /*
11041 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
11042 * HOST_FS_BASE, HOST_GS_BASE.
11043 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011044
Jim Mattson6514dc32018-04-26 16:09:12 -070011045 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011046 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020011047 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
11048 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
11049 } else {
11050 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
11051 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
11052 }
Jim Mattson6514dc32018-04-26 16:09:12 -070011053 if (vmx->nested.nested_run_pending) {
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011054 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
11055 vmcs12->vm_entry_intr_info_field);
11056 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
11057 vmcs12->vm_entry_exception_error_code);
11058 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
11059 vmcs12->vm_entry_instruction_len);
11060 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
11061 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070011062 vmx->loaded_vmcs->nmi_known_unmasked =
11063 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011064 } else {
11065 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
11066 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030011067 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011068
Jan Kiszkaf4124502014-03-07 20:03:13 +010011069 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080011070
Paolo Bonzini93140062016-07-06 13:23:51 +020011071 /* Preemption timer setting is only taken from vmcs01. */
11072 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11073 exec_control |= vmcs_config.pin_based_exec_ctrl;
11074 if (vmx->hv_deadline_tsc == -1)
11075 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11076
11077 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080011078 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011079 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
11080 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011081 } else {
Wincy Van705699a2015-02-03 23:58:17 +080011082 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011083 }
Wincy Van705699a2015-02-03 23:58:17 +080011084
Jan Kiszkaf4124502014-03-07 20:03:13 +010011085 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011086
Jan Kiszkaf4124502014-03-07 20:03:13 +010011087 vmx->nested.preemption_timer_expired = false;
11088 if (nested_cpu_has_preemption_timer(vmcs12))
11089 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010011090
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011091 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020011092 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080011093
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011094 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011095 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020011096 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010011097 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020011098 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011099 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040011100 SECONDARY_EXEC_APIC_REGISTER_VIRT |
11101 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011102 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040011103 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
11104 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
11105 ~SECONDARY_EXEC_ENABLE_PML;
11106 exec_control |= vmcs12_exec_ctrl;
11107 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011108
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011109 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080011110 vmcs_write16(GUEST_INTR_STATUS,
11111 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080011112
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011113 /*
11114 * Write an illegal value to APIC_ACCESS_ADDR. Later,
11115 * nested_get_vmcs12_pages will either fix it up or
11116 * remove the VM execution control.
11117 */
11118 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
11119 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
11120
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011121 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
11122 }
11123
Jim Mattson83bafef2016-10-04 10:48:38 -070011124 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011125 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
11126 * entry, but only if the current (host) sp changed from the value
11127 * we wrote last (vmx->host_rsp). This cache is no longer relevant
11128 * if we switch vmcs, and rather than hold a separate cache per vmcs,
11129 * here we just force the write to happen on entry.
11130 */
11131 vmx->host_rsp = 0;
11132
11133 exec_control = vmx_exec_control(vmx); /* L0's desires */
11134 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
11135 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
11136 exec_control &= ~CPU_BASED_TPR_SHADOW;
11137 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011138
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011139 /*
11140 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
11141 * nested_get_vmcs12_pages can't fix it up, the illegal value
11142 * will result in a VM entry failure.
11143 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011144 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011145 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011146 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070011147 } else {
11148#ifdef CONFIG_X86_64
11149 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
11150 CPU_BASED_CR8_STORE_EXITING;
11151#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011152 }
11153
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011154 /*
Quan Xu8eb73e22017-12-12 16:44:21 +080011155 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
11156 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011157 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011158 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
11159 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
11160
11161 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
11162
11163 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
11164 * bitwise-or of what L1 wants to trap for L2, and what we want to
11165 * trap. Note that CR0.TS also needs updating - we do this later.
11166 */
11167 update_exception_bitmap(vcpu);
11168 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
11169 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11170
Nadav Har'El8049d652013-08-05 11:07:06 +030011171 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
11172 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
11173 * bits are further modified by vmx_set_efer() below.
11174 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010011175 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030011176
11177 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
11178 * emulated by vmx_set_efer(), below.
11179 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020011180 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030011181 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
11182 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011183 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
11184
Jim Mattson6514dc32018-04-26 16:09:12 -070011185 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011186 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011187 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011188 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011189 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011190 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011191 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011192
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011193 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11194
Peter Feinerc95ba922016-08-17 09:36:47 -070011195 if (kvm_has_tsc_control)
11196 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011197
11198 if (enable_vpid) {
11199 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070011200 * There is no direct mapping between vpid02 and vpid12, the
11201 * vpid02 is per-vCPU for L0 and reused while the value of
11202 * vpid12 is changed w/ one invvpid during nested vmentry.
11203 * The vpid12 is allocated by L1 for L2, so it will not
11204 * influence global bitmap(for vpid01 and vpid02 allocation)
11205 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011206 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070011207 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070011208 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
11209 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011210 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011211 }
11212 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011213 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011214 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011215 }
11216
Ladi Prosek1fb883b2017-04-04 14:18:53 +020011217 if (enable_pml) {
11218 /*
11219 * Conceptually we want to copy the PML address and index from
11220 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11221 * since we always flush the log on each vmexit, this happens
11222 * to be equivalent to simply resetting the fields in vmcs02.
11223 */
11224 ASSERT(vmx->pml_pg);
11225 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11226 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11227 }
11228
Nadav Har'El155a97a2013-08-05 11:07:16 +030011229 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011230 if (nested_ept_init_mmu_context(vcpu)) {
11231 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11232 return 1;
11233 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011234 } else if (nested_cpu_has2(vmcs12,
11235 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070011236 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011237 }
11238
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011239 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011240 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11241 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011242 * The CR0_READ_SHADOW is what L2 should have expected to read given
11243 * the specifications by L1; It's not enough to take
11244 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11245 * have more bits than L1 expected.
11246 */
11247 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11248 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11249
11250 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11251 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11252
Jim Mattson6514dc32018-04-26 16:09:12 -070011253 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011254 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080011255 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11256 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11257 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11258 else
11259 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11260 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11261 vmx_set_efer(vcpu, vcpu->arch.efer);
11262
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011263 /*
11264 * Guest state is invalid and unrestricted guest is disabled,
11265 * which means L1 attempted VMEntry to L2 with invalid state.
11266 * Fail the VMEntry.
11267 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010011268 if (vmx->emulation_required) {
11269 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011270 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010011271 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011272
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011273 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010011274 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011275 entry_failure_code))
11276 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010011277
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011278 if (!enable_ept)
11279 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11280
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011281 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11282 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010011283 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011284}
11285
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011286static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11287{
11288 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11289 nested_cpu_has_virtual_nmis(vmcs12))
11290 return -EINVAL;
11291
11292 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11293 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
11294 return -EINVAL;
11295
11296 return 0;
11297}
11298
Jim Mattsonca0bde22016-11-30 12:03:46 -080011299static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11300{
11301 struct vcpu_vmx *vmx = to_vmx(vcpu);
11302
11303 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
11304 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11305 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11306
Jim Mattson56a20512017-07-06 16:33:06 -070011307 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11308 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11309
Jim Mattsonca0bde22016-11-30 12:03:46 -080011310 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11311 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11312
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011313 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
11314 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11315
Jim Mattson712b12d2017-08-24 13:24:47 -070011316 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11317 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11318
Jim Mattsonca0bde22016-11-30 12:03:46 -080011319 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
11320 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11321
11322 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11323 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11324
Bandan Dasc5f983f2017-05-05 15:25:14 -040011325 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
11326 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11327
Jim Mattsonca0bde22016-11-30 12:03:46 -080011328 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011329 vmx->nested.msrs.procbased_ctls_low,
11330 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070011331 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
11332 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011333 vmx->nested.msrs.secondary_ctls_low,
11334 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011335 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011336 vmx->nested.msrs.pinbased_ctls_low,
11337 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011338 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011339 vmx->nested.msrs.exit_ctls_low,
11340 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011341 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011342 vmx->nested.msrs.entry_ctls_low,
11343 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011344 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11345
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011346 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011347 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11348
Bandan Das41ab9372017-08-03 15:54:43 -040011349 if (nested_cpu_has_vmfunc(vmcs12)) {
11350 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011351 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040011352 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11353
11354 if (nested_cpu_has_eptp_switching(vmcs12)) {
11355 if (!nested_cpu_has_ept(vmcs12) ||
11356 !page_address_valid(vcpu, vmcs12->eptp_list_address))
11357 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11358 }
11359 }
Bandan Das27c42a12017-08-03 15:54:42 -040011360
Jim Mattsonc7c2c702017-05-05 11:28:09 -070011361 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
11362 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11363
Jim Mattsonca0bde22016-11-30 12:03:46 -080011364 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
11365 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
11366 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
11367 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11368
11369 return 0;
11370}
11371
11372static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11373 u32 *exit_qual)
11374{
11375 bool ia32e;
11376
11377 *exit_qual = ENTRY_FAIL_DEFAULT;
11378
11379 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
11380 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
11381 return 1;
11382
11383 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
11384 vmcs12->vmcs_link_pointer != -1ull) {
11385 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
11386 return 1;
11387 }
11388
11389 /*
11390 * If the load IA32_EFER VM-entry control is 1, the following checks
11391 * are performed on the field for the IA32_EFER MSR:
11392 * - Bits reserved in the IA32_EFER MSR must be 0.
11393 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
11394 * the IA-32e mode guest VM-exit control. It must also be identical
11395 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
11396 * CR0.PG) is 1.
11397 */
11398 if (to_vmx(vcpu)->nested.nested_run_pending &&
11399 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
11400 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
11401 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
11402 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
11403 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
11404 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
11405 return 1;
11406 }
11407
11408 /*
11409 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
11410 * IA32_EFER MSR must be 0 in the field for that register. In addition,
11411 * the values of the LMA and LME bits in the field must each be that of
11412 * the host address-space size VM-exit control.
11413 */
11414 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
11415 ia32e = (vmcs12->vm_exit_controls &
11416 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
11417 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
11418 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
11419 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
11420 return 1;
11421 }
11422
Wanpeng Lif1b026a2017-11-05 16:54:48 -080011423 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
11424 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
11425 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
11426 return 1;
11427
Jim Mattsonca0bde22016-11-30 12:03:46 -080011428 return 0;
11429}
11430
Jim Mattson6514dc32018-04-26 16:09:12 -070011431static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu)
Jim Mattson858e25c2016-11-30 12:03:47 -080011432{
11433 struct vcpu_vmx *vmx = to_vmx(vcpu);
11434 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080011435 u32 msr_entry_idx;
11436 u32 exit_qual;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011437 int r;
Jim Mattson858e25c2016-11-30 12:03:47 -080011438
Jim Mattson858e25c2016-11-30 12:03:47 -080011439 enter_guest_mode(vcpu);
11440
11441 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
11442 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11443
Jim Mattsonde3a0022017-11-27 17:22:25 -060011444 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080011445 vmx_segment_cache_clear(vmx);
11446
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011447 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11448 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
11449
11450 r = EXIT_REASON_INVALID_STATE;
Jim Mattson6514dc32018-04-26 16:09:12 -070011451 if (prepare_vmcs02(vcpu, vmcs12, &exit_qual))
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011452 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080011453
11454 nested_get_vmcs12_pages(vcpu, vmcs12);
11455
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011456 r = EXIT_REASON_MSR_LOAD_FAIL;
Jim Mattson858e25c2016-11-30 12:03:47 -080011457 msr_entry_idx = nested_vmx_load_msr(vcpu,
11458 vmcs12->vm_entry_msr_load_addr,
11459 vmcs12->vm_entry_msr_load_count);
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011460 if (msr_entry_idx)
11461 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080011462
Jim Mattson858e25c2016-11-30 12:03:47 -080011463 /*
11464 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
11465 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
11466 * returned as far as L1 is concerned. It will only return (and set
11467 * the success flag) when L2 exits (see nested_vmx_vmexit()).
11468 */
11469 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011470
11471fail:
11472 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11473 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
11474 leave_guest_mode(vcpu);
11475 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
11476 nested_vmx_entry_failure(vcpu, vmcs12, r, exit_qual);
11477 return 1;
Jim Mattson858e25c2016-11-30 12:03:47 -080011478}
11479
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011480/*
11481 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
11482 * for running an L2 nested guest.
11483 */
11484static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
11485{
11486 struct vmcs12 *vmcs12;
11487 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011488 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080011489 u32 exit_qual;
11490 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011491
Kyle Hueyeb277562016-11-29 12:40:39 -080011492 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011493 return 1;
11494
Kyle Hueyeb277562016-11-29 12:40:39 -080011495 if (!nested_vmx_check_vmcs12(vcpu))
11496 goto out;
11497
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011498 vmcs12 = get_vmcs12(vcpu);
11499
Abel Gordon012f83c2013-04-18 14:39:25 +030011500 if (enable_shadow_vmcs)
11501 copy_shadow_to_vmcs12(vmx);
11502
Nadav Har'El7c177932011-05-25 23:12:04 +030011503 /*
11504 * The nested entry process starts with enforcing various prerequisites
11505 * on vmcs12 as required by the Intel SDM, and act appropriately when
11506 * they fail: As the SDM explains, some conditions should cause the
11507 * instruction to fail, while others will cause the instruction to seem
11508 * to succeed, but return an EXIT_REASON_INVALID_STATE.
11509 * To speed up the normal (success) code path, we should avoid checking
11510 * for misconfigurations which will anyway be caught by the processor
11511 * when using the merged vmcs02.
11512 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011513 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
11514 nested_vmx_failValid(vcpu,
11515 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
11516 goto out;
11517 }
11518
Nadav Har'El7c177932011-05-25 23:12:04 +030011519 if (vmcs12->launch_state == launch) {
11520 nested_vmx_failValid(vcpu,
11521 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
11522 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080011523 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030011524 }
11525
Jim Mattsonca0bde22016-11-30 12:03:46 -080011526 ret = check_vmentry_prereqs(vcpu, vmcs12);
11527 if (ret) {
11528 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080011529 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020011530 }
11531
Nadav Har'El7c177932011-05-25 23:12:04 +030011532 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080011533 * After this point, the trap flag no longer triggers a singlestep trap
11534 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
11535 * This is not 100% correct; for performance reasons, we delegate most
11536 * of the checks on host state to the processor. If those fail,
11537 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020011538 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080011539 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020011540
Jim Mattsonca0bde22016-11-30 12:03:46 -080011541 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
11542 if (ret) {
11543 nested_vmx_entry_failure(vcpu, vmcs12,
11544 EXIT_REASON_INVALID_STATE, exit_qual);
11545 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020011546 }
11547
11548 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030011549 * We're finally done with prerequisite checking, and can start with
11550 * the nested entry.
11551 */
11552
Jim Mattson6514dc32018-04-26 16:09:12 -070011553 vmx->nested.nested_run_pending = 1;
11554 ret = enter_vmx_non_root_mode(vcpu);
11555 if (ret) {
11556 vmx->nested.nested_run_pending = 0;
Jim Mattson858e25c2016-11-30 12:03:47 -080011557 return ret;
Jim Mattson6514dc32018-04-26 16:09:12 -070011558 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011559
Chao Gao135a06c2018-02-11 10:06:30 +080011560 /*
11561 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
11562 * by event injection, halt vcpu.
11563 */
11564 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070011565 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
11566 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060011567 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070011568 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011569 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080011570
11571out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080011572 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011573}
11574
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011575/*
11576 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
11577 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
11578 * This function returns the new value we should put in vmcs12.guest_cr0.
11579 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
11580 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
11581 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
11582 * didn't trap the bit, because if L1 did, so would L0).
11583 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
11584 * been modified by L2, and L1 knows it. So just leave the old value of
11585 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
11586 * isn't relevant, because if L0 traps this bit it can set it to anything.
11587 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
11588 * changed these bits, and therefore they need to be updated, but L0
11589 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
11590 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
11591 */
11592static inline unsigned long
11593vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11594{
11595 return
11596 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
11597 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
11598 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
11599 vcpu->arch.cr0_guest_owned_bits));
11600}
11601
11602static inline unsigned long
11603vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11604{
11605 return
11606 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
11607 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
11608 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
11609 vcpu->arch.cr4_guest_owned_bits));
11610}
11611
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011612static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
11613 struct vmcs12 *vmcs12)
11614{
11615 u32 idt_vectoring;
11616 unsigned int nr;
11617
Wanpeng Li664f8e22017-08-24 03:35:09 -070011618 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011619 nr = vcpu->arch.exception.nr;
11620 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11621
11622 if (kvm_exception_is_soft(nr)) {
11623 vmcs12->vm_exit_instruction_len =
11624 vcpu->arch.event_exit_inst_len;
11625 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
11626 } else
11627 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
11628
11629 if (vcpu->arch.exception.has_error_code) {
11630 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
11631 vmcs12->idt_vectoring_error_code =
11632 vcpu->arch.exception.error_code;
11633 }
11634
11635 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010011636 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011637 vmcs12->idt_vectoring_info_field =
11638 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030011639 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011640 nr = vcpu->arch.interrupt.nr;
11641 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11642
11643 if (vcpu->arch.interrupt.soft) {
11644 idt_vectoring |= INTR_TYPE_SOFT_INTR;
11645 vmcs12->vm_entry_instruction_len =
11646 vcpu->arch.event_exit_inst_len;
11647 } else
11648 idt_vectoring |= INTR_TYPE_EXT_INTR;
11649
11650 vmcs12->idt_vectoring_info_field = idt_vectoring;
11651 }
11652}
11653
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011654static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
11655{
11656 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011657 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020011658 bool block_nested_events =
11659 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080011660
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011661 if (vcpu->arch.exception.pending &&
11662 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020011663 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011664 return -EBUSY;
11665 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011666 return 0;
11667 }
11668
Jan Kiszkaf4124502014-03-07 20:03:13 +010011669 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
11670 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020011671 if (block_nested_events)
Jan Kiszkaf4124502014-03-07 20:03:13 +010011672 return -EBUSY;
11673 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
11674 return 0;
11675 }
11676
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011677 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020011678 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011679 return -EBUSY;
11680 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11681 NMI_VECTOR | INTR_TYPE_NMI_INTR |
11682 INTR_INFO_VALID_MASK, 0);
11683 /*
11684 * The NMI-triggered VM exit counts as injection:
11685 * clear this one and block further NMIs.
11686 */
11687 vcpu->arch.nmi_pending = 0;
11688 vmx_set_nmi_mask(vcpu, true);
11689 return 0;
11690 }
11691
11692 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
11693 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020011694 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011695 return -EBUSY;
11696 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080011697 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011698 }
11699
David Hildenbrand6342c502017-01-25 11:58:58 +010011700 vmx_complete_nested_posted_interrupt(vcpu);
11701 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011702}
11703
Jan Kiszkaf4124502014-03-07 20:03:13 +010011704static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
11705{
11706 ktime_t remaining =
11707 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
11708 u64 value;
11709
11710 if (ktime_to_ns(remaining) <= 0)
11711 return 0;
11712
11713 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
11714 do_div(value, 1000000);
11715 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11716}
11717
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011718/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011719 * Update the guest state fields of vmcs12 to reflect changes that
11720 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
11721 * VM-entry controls is also updated, since this is really a guest
11722 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011723 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011724static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011725{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011726 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
11727 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
11728
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011729 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
11730 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
11731 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
11732
11733 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
11734 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
11735 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
11736 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
11737 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
11738 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
11739 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
11740 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
11741 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
11742 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
11743 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
11744 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
11745 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
11746 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
11747 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
11748 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
11749 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
11750 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
11751 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
11752 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
11753 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
11754 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
11755 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
11756 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
11757 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
11758 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
11759 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
11760 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
11761 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
11762 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
11763 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
11764 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
11765 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
11766 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
11767 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
11768 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
11769
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011770 vmcs12->guest_interruptibility_info =
11771 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
11772 vmcs12->guest_pending_dbg_exceptions =
11773 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010011774 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
11775 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
11776 else
11777 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011778
Jan Kiszkaf4124502014-03-07 20:03:13 +010011779 if (nested_cpu_has_preemption_timer(vmcs12)) {
11780 if (vmcs12->vm_exit_controls &
11781 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
11782 vmcs12->vmx_preemption_timer_value =
11783 vmx_get_preemption_timer_value(vcpu);
11784 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
11785 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080011786
Nadav Har'El3633cfc2013-08-05 11:07:07 +030011787 /*
11788 * In some cases (usually, nested EPT), L2 is allowed to change its
11789 * own CR3 without exiting. If it has changed it, we must keep it.
11790 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
11791 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
11792 *
11793 * Additionally, restore L2's PDPTR to vmcs12.
11794 */
11795 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010011796 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030011797 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
11798 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
11799 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
11800 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
11801 }
11802
Jim Mattsond281e132017-06-01 12:44:46 -070011803 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030011804
Wincy Van608406e2015-02-03 23:57:51 +080011805 if (nested_cpu_has_vid(vmcs12))
11806 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
11807
Jan Kiszkac18911a2013-03-13 16:06:41 +010011808 vmcs12->vm_entry_controls =
11809 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020011810 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010011811
Jan Kiszka2996fca2014-06-16 13:59:43 +020011812 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
11813 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
11814 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11815 }
11816
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011817 /* TODO: These cannot have changed unless we have MSR bitmaps and
11818 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020011819 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011820 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020011821 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
11822 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011823 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
11824 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
11825 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010011826 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011827 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011828}
11829
11830/*
11831 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
11832 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
11833 * and this function updates it to reflect the changes to the guest state while
11834 * L2 was running (and perhaps made some exits which were handled directly by L0
11835 * without going back to L1), and to reflect the exit reason.
11836 * Note that we do not have to copy here all VMCS fields, just those that
11837 * could have changed by the L2 guest or the exit - i.e., the guest-state and
11838 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
11839 * which already writes to vmcs12 directly.
11840 */
11841static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11842 u32 exit_reason, u32 exit_intr_info,
11843 unsigned long exit_qualification)
11844{
11845 /* update guest state fields: */
11846 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011847
11848 /* update exit information fields: */
11849
Jan Kiszka533558b2014-01-04 18:47:20 +010011850 vmcs12->vm_exit_reason = exit_reason;
11851 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010011852 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020011853
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011854 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011855 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
11856 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
11857
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011858 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070011859 vmcs12->launch_state = 1;
11860
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011861 /* vm_entry_intr_info_field is cleared on exit. Emulate this
11862 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011863 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011864
11865 /*
11866 * Transfer the event that L0 or L1 may wanted to inject into
11867 * L2 to IDT_VECTORING_INFO_FIELD.
11868 */
11869 vmcs12_save_pending_event(vcpu, vmcs12);
11870 }
11871
11872 /*
11873 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
11874 * preserved above and would only end up incorrectly in L1.
11875 */
11876 vcpu->arch.nmi_injected = false;
11877 kvm_clear_exception_queue(vcpu);
11878 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011879}
11880
Wanpeng Li5af41572017-11-05 16:54:49 -080011881static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
11882 struct vmcs12 *vmcs12)
11883{
11884 u32 entry_failure_code;
11885
11886 nested_ept_uninit_mmu_context(vcpu);
11887
11888 /*
11889 * Only PDPTE load can fail as the value of cr3 was checked on entry and
11890 * couldn't have changed.
11891 */
11892 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
11893 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
11894
11895 if (!enable_ept)
11896 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
11897}
11898
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011899/*
11900 * A part of what we need to when the nested L2 guest exits and we want to
11901 * run its L1 parent, is to reset L1's guest state to the host state specified
11902 * in vmcs12.
11903 * This function is to be called not only on normal nested exit, but also on
11904 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
11905 * Failures During or After Loading Guest State").
11906 * This function should be called when the active VMCS is L1's (vmcs01).
11907 */
Jan Kiszka733568f2013-02-23 15:07:47 +010011908static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
11909 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011910{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011911 struct kvm_segment seg;
11912
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011913 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
11914 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020011915 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011916 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11917 else
11918 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11919 vmx_set_efer(vcpu, vcpu->arch.efer);
11920
11921 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
11922 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070011923 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011924 /*
11925 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011926 * actually changed, because vmx_set_cr0 refers to efer set above.
11927 *
11928 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
11929 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011930 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011931 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020011932 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011933
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011934 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011935 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080011936 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011937
Wanpeng Li5af41572017-11-05 16:54:49 -080011938 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011939
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011940 if (enable_vpid) {
11941 /*
11942 * Trivially support vpid by letting L2s share their parent
11943 * L1's vpid. TODO: move to a more elaborate solution, giving
11944 * each L2 its own vpid and exposing the vpid feature to L1.
11945 */
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011946 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011947 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011948
11949 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
11950 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
11951 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
11952 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
11953 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020011954 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
11955 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011956
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011957 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
11958 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
11959 vmcs_write64(GUEST_BNDCFGS, 0);
11960
Jan Kiszka44811c02013-08-04 17:17:27 +020011961 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011962 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011963 vcpu->arch.pat = vmcs12->host_ia32_pat;
11964 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011965 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
11966 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
11967 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011968
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011969 /* Set L1 segment info according to Intel SDM
11970 27.5.2 Loading Host Segment and Descriptor-Table Registers */
11971 seg = (struct kvm_segment) {
11972 .base = 0,
11973 .limit = 0xFFFFFFFF,
11974 .selector = vmcs12->host_cs_selector,
11975 .type = 11,
11976 .present = 1,
11977 .s = 1,
11978 .g = 1
11979 };
11980 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
11981 seg.l = 1;
11982 else
11983 seg.db = 1;
11984 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
11985 seg = (struct kvm_segment) {
11986 .base = 0,
11987 .limit = 0xFFFFFFFF,
11988 .type = 3,
11989 .present = 1,
11990 .s = 1,
11991 .db = 1,
11992 .g = 1
11993 };
11994 seg.selector = vmcs12->host_ds_selector;
11995 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
11996 seg.selector = vmcs12->host_es_selector;
11997 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
11998 seg.selector = vmcs12->host_ss_selector;
11999 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
12000 seg.selector = vmcs12->host_fs_selector;
12001 seg.base = vmcs12->host_fs_base;
12002 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
12003 seg.selector = vmcs12->host_gs_selector;
12004 seg.base = vmcs12->host_gs_base;
12005 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
12006 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030012007 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012008 .limit = 0x67,
12009 .selector = vmcs12->host_tr_selector,
12010 .type = 11,
12011 .present = 1
12012 };
12013 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
12014
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012015 kvm_set_dr(vcpu, 7, 0x400);
12016 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030012017
Wincy Van3af18d92015-02-03 23:49:31 +080012018 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010012019 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080012020
Wincy Vanff651cb2014-12-11 08:52:58 +030012021 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
12022 vmcs12->vm_exit_msr_load_count))
12023 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012024}
12025
12026/*
12027 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
12028 * and modify vmcs12 to make it see what it would expect to see there if
12029 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
12030 */
Jan Kiszka533558b2014-01-04 18:47:20 +010012031static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
12032 u32 exit_intr_info,
12033 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012034{
12035 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012036 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12037
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012038 /* trying to cancel vmlaunch/vmresume is a bug */
12039 WARN_ON_ONCE(vmx->nested.nested_run_pending);
12040
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012041 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070012042 * The only expected VM-instruction error is "VM entry with
12043 * invalid control field(s)." Anything else indicates a
12044 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012045 */
Jim Mattson4f350c62017-09-14 16:31:44 -070012046 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
12047 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
12048
12049 leave_guest_mode(vcpu);
12050
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012051 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12052 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12053
Jim Mattson4f350c62017-09-14 16:31:44 -070012054 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012055 if (exit_reason == -1)
12056 sync_vmcs12(vcpu, vmcs12);
12057 else
12058 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
12059 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070012060
12061 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
12062 vmcs12->vm_exit_msr_store_count))
12063 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040012064 }
12065
Jim Mattson4f350c62017-09-14 16:31:44 -070012066 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020012067 vm_entry_controls_reset_shadow(vmx);
12068 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010012069 vmx_segment_cache_clear(vmx);
12070
Paolo Bonzini93140062016-07-06 13:23:51 +020012071 /* Update any VMCS fields that might have changed while L2 ran */
Jim Mattson83bafef2016-10-04 10:48:38 -070012072 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
12073 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010012074 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini93140062016-07-06 13:23:51 +020012075 if (vmx->hv_deadline_tsc == -1)
12076 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12077 PIN_BASED_VMX_PREEMPTION_TIMER);
12078 else
12079 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12080 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070012081 if (kvm_has_tsc_control)
12082 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012083
Jim Mattson8d860bb2018-05-09 16:56:05 -040012084 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
12085 vmx->nested.change_vmcs01_virtual_apic_mode = false;
12086 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012087 } else if (!nested_cpu_has_ept(vmcs12) &&
12088 nested_cpu_has2(vmcs12,
12089 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070012090 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020012091 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012092
12093 /* This is needed for same reason as it was needed in prepare_vmcs02 */
12094 vmx->host_rsp = 0;
12095
12096 /* Unpin physical memory we referred to in vmcs02 */
12097 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012098 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012099 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012100 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012101 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012102 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012103 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012104 }
Wincy Van705699a2015-02-03 23:58:17 +080012105 if (vmx->nested.pi_desc_page) {
12106 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012107 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080012108 vmx->nested.pi_desc_page = NULL;
12109 vmx->nested.pi_desc = NULL;
12110 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012111
12112 /*
Tang Chen38b99172014-09-24 15:57:54 +080012113 * We are now running in L2, mmu_notifier will force to reload the
12114 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
12115 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080012116 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080012117
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012118 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030012119 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012120
12121 /* in case we halted in L2 */
12122 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070012123
12124 if (likely(!vmx->fail)) {
12125 /*
12126 * TODO: SDM says that with acknowledge interrupt on
12127 * exit, bit 31 of the VM-exit interrupt information
12128 * (valid interrupt) is always set to 1 on
12129 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
12130 * need kvm_cpu_has_interrupt(). See the commit
12131 * message for details.
12132 */
12133 if (nested_exit_intr_ack_set(vcpu) &&
12134 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
12135 kvm_cpu_has_interrupt(vcpu)) {
12136 int irq = kvm_cpu_get_interrupt(vcpu);
12137 WARN_ON(irq < 0);
12138 vmcs12->vm_exit_intr_info = irq |
12139 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
12140 }
12141
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012142 if (exit_reason != -1)
12143 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
12144 vmcs12->exit_qualification,
12145 vmcs12->idt_vectoring_info_field,
12146 vmcs12->vm_exit_intr_info,
12147 vmcs12->vm_exit_intr_error_code,
12148 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070012149
12150 load_vmcs12_host_state(vcpu, vmcs12);
12151
12152 return;
12153 }
12154
12155 /*
12156 * After an early L2 VM-entry failure, we're now back
12157 * in L1 which thinks it just finished a VMLAUNCH or
12158 * VMRESUME instruction, so we need to set the failure
12159 * flag and the VM-instruction error field of the VMCS
12160 * accordingly.
12161 */
12162 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080012163
12164 load_vmcs12_mmu_host_state(vcpu, vmcs12);
12165
Jim Mattson4f350c62017-09-14 16:31:44 -070012166 /*
12167 * The emulated instruction was already skipped in
12168 * nested_vmx_run, but the updated RIP was never
12169 * written back to the vmcs01.
12170 */
12171 skip_emulated_instruction(vcpu);
12172 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012173}
12174
Nadav Har'El7c177932011-05-25 23:12:04 +030012175/*
Jan Kiszka42124922014-01-04 18:47:19 +010012176 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
12177 */
12178static void vmx_leave_nested(struct kvm_vcpu *vcpu)
12179{
Wanpeng Li2f707d92017-03-06 04:03:28 -080012180 if (is_guest_mode(vcpu)) {
12181 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010012182 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080012183 }
Jan Kiszka42124922014-01-04 18:47:19 +010012184 free_nested(to_vmx(vcpu));
12185}
12186
12187/*
Nadav Har'El7c177932011-05-25 23:12:04 +030012188 * L1's failure to enter L2 is a subset of a normal exit, as explained in
12189 * 23.7 "VM-entry failures during or after loading guest state" (this also
12190 * lists the acceptable exit-reason and exit-qualification parameters).
12191 * It should only be called before L2 actually succeeded to run, and when
12192 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
12193 */
12194static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
12195 struct vmcs12 *vmcs12,
12196 u32 reason, unsigned long qualification)
12197{
12198 load_vmcs12_host_state(vcpu, vmcs12);
12199 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
12200 vmcs12->exit_qualification = qualification;
12201 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030012202 if (enable_shadow_vmcs)
12203 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030012204}
12205
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012206static int vmx_check_intercept(struct kvm_vcpu *vcpu,
12207 struct x86_instruction_info *info,
12208 enum x86_intercept_stage stage)
12209{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020012210 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12211 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
12212
12213 /*
12214 * RDPID causes #UD if disabled through secondary execution controls.
12215 * Because it is marked as EmulateOnUD, we need to intercept it here.
12216 */
12217 if (info->intercept == x86_intercept_rdtscp &&
12218 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
12219 ctxt->exception.vector = UD_VECTOR;
12220 ctxt->exception.error_code_valid = false;
12221 return X86EMUL_PROPAGATE_FAULT;
12222 }
12223
12224 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012225 return X86EMUL_CONTINUE;
12226}
12227
Yunhong Jiang64672c92016-06-13 14:19:59 -070012228#ifdef CONFIG_X86_64
12229/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
12230static inline int u64_shl_div_u64(u64 a, unsigned int shift,
12231 u64 divisor, u64 *result)
12232{
12233 u64 low = a << shift, high = a >> (64 - shift);
12234
12235 /* To avoid the overflow on divq */
12236 if (high >= divisor)
12237 return 1;
12238
12239 /* Low hold the result, high hold rem which is discarded */
12240 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
12241 "rm" (divisor), "0" (low), "1" (high));
12242 *result = low;
12243
12244 return 0;
12245}
12246
12247static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
12248{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012249 struct vcpu_vmx *vmx;
12250 u64 tscl, guest_tscl, delta_tsc;
12251
12252 if (kvm_mwait_in_guest(vcpu->kvm))
12253 return -EOPNOTSUPP;
12254
12255 vmx = to_vmx(vcpu);
12256 tscl = rdtsc();
12257 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
12258 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012259
12260 /* Convert to host delta tsc if tsc scaling is enabled */
12261 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
12262 u64_shl_div_u64(delta_tsc,
12263 kvm_tsc_scaling_ratio_frac_bits,
12264 vcpu->arch.tsc_scaling_ratio,
12265 &delta_tsc))
12266 return -ERANGE;
12267
12268 /*
12269 * If the delta tsc can't fit in the 32 bit after the multi shift,
12270 * we can't use the preemption timer.
12271 * It's possible that it fits on later vmentries, but checking
12272 * on every vmentry is costly so we just use an hrtimer.
12273 */
12274 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
12275 return -ERANGE;
12276
12277 vmx->hv_deadline_tsc = tscl + delta_tsc;
12278 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12279 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070012280
12281 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012282}
12283
12284static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
12285{
12286 struct vcpu_vmx *vmx = to_vmx(vcpu);
12287 vmx->hv_deadline_tsc = -1;
12288 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12289 PIN_BASED_VMX_PREEMPTION_TIMER);
12290}
12291#endif
12292
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012293static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012294{
Wanpeng Lib31c1142018-03-12 04:53:04 -070012295 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020012296 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012297}
12298
Kai Huang843e4332015-01-28 10:54:28 +080012299static void vmx_slot_enable_log_dirty(struct kvm *kvm,
12300 struct kvm_memory_slot *slot)
12301{
12302 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
12303 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
12304}
12305
12306static void vmx_slot_disable_log_dirty(struct kvm *kvm,
12307 struct kvm_memory_slot *slot)
12308{
12309 kvm_mmu_slot_set_dirty(kvm, slot);
12310}
12311
12312static void vmx_flush_log_dirty(struct kvm *kvm)
12313{
12314 kvm_flush_pml_buffers(kvm);
12315}
12316
Bandan Dasc5f983f2017-05-05 15:25:14 -040012317static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
12318{
12319 struct vmcs12 *vmcs12;
12320 struct vcpu_vmx *vmx = to_vmx(vcpu);
12321 gpa_t gpa;
12322 struct page *page = NULL;
12323 u64 *pml_address;
12324
12325 if (is_guest_mode(vcpu)) {
12326 WARN_ON_ONCE(vmx->nested.pml_full);
12327
12328 /*
12329 * Check if PML is enabled for the nested guest.
12330 * Whether eptp bit 6 is set is already checked
12331 * as part of A/D emulation.
12332 */
12333 vmcs12 = get_vmcs12(vcpu);
12334 if (!nested_cpu_has_pml(vmcs12))
12335 return 0;
12336
Dan Carpenter47698862017-05-10 22:43:17 +030012337 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040012338 vmx->nested.pml_full = true;
12339 return 1;
12340 }
12341
12342 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
12343
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020012344 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
12345 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040012346 return 0;
12347
12348 pml_address = kmap(page);
12349 pml_address[vmcs12->guest_pml_index--] = gpa;
12350 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012351 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040012352 }
12353
12354 return 0;
12355}
12356
Kai Huang843e4332015-01-28 10:54:28 +080012357static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
12358 struct kvm_memory_slot *memslot,
12359 gfn_t offset, unsigned long mask)
12360{
12361 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
12362}
12363
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012364static void __pi_post_block(struct kvm_vcpu *vcpu)
12365{
12366 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12367 struct pi_desc old, new;
12368 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012369
12370 do {
12371 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012372 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
12373 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012374
12375 dest = cpu_physical_id(vcpu->cpu);
12376
12377 if (x2apic_enabled())
12378 new.ndst = dest;
12379 else
12380 new.ndst = (dest << 8) & 0xFF00;
12381
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012382 /* set 'NV' to 'notification vector' */
12383 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012384 } while (cmpxchg64(&pi_desc->control, old.control,
12385 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012386
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012387 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
12388 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012389 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012390 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012391 vcpu->pre_pcpu = -1;
12392 }
12393}
12394
Feng Wuefc64402015-09-18 22:29:51 +080012395/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080012396 * This routine does the following things for vCPU which is going
12397 * to be blocked if VT-d PI is enabled.
12398 * - Store the vCPU to the wakeup list, so when interrupts happen
12399 * we can find the right vCPU to wake up.
12400 * - Change the Posted-interrupt descriptor as below:
12401 * 'NDST' <-- vcpu->pre_pcpu
12402 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
12403 * - If 'ON' is set during this process, which means at least one
12404 * interrupt is posted for this vCPU, we cannot block it, in
12405 * this case, return 1, otherwise, return 0.
12406 *
12407 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070012408static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012409{
Feng Wubf9f6ac2015-09-18 22:29:55 +080012410 unsigned int dest;
12411 struct pi_desc old, new;
12412 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12413
12414 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012415 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12416 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080012417 return 0;
12418
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012419 WARN_ON(irqs_disabled());
12420 local_irq_disable();
12421 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
12422 vcpu->pre_pcpu = vcpu->cpu;
12423 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12424 list_add_tail(&vcpu->blocked_vcpu_list,
12425 &per_cpu(blocked_vcpu_on_cpu,
12426 vcpu->pre_pcpu));
12427 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12428 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080012429
12430 do {
12431 old.control = new.control = pi_desc->control;
12432
Feng Wubf9f6ac2015-09-18 22:29:55 +080012433 WARN((pi_desc->sn == 1),
12434 "Warning: SN field of posted-interrupts "
12435 "is set before blocking\n");
12436
12437 /*
12438 * Since vCPU can be preempted during this process,
12439 * vcpu->cpu could be different with pre_pcpu, we
12440 * need to set pre_pcpu as the destination of wakeup
12441 * notification event, then we can find the right vCPU
12442 * to wakeup in wakeup handler if interrupts happen
12443 * when the vCPU is in blocked state.
12444 */
12445 dest = cpu_physical_id(vcpu->pre_pcpu);
12446
12447 if (x2apic_enabled())
12448 new.ndst = dest;
12449 else
12450 new.ndst = (dest << 8) & 0xFF00;
12451
12452 /* set 'NV' to 'wakeup vector' */
12453 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012454 } while (cmpxchg64(&pi_desc->control, old.control,
12455 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012456
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012457 /* We should not block the vCPU if an interrupt is posted for it. */
12458 if (pi_test_on(pi_desc) == 1)
12459 __pi_post_block(vcpu);
12460
12461 local_irq_enable();
12462 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012463}
12464
Yunhong Jiangbc225122016-06-13 14:19:58 -070012465static int vmx_pre_block(struct kvm_vcpu *vcpu)
12466{
12467 if (pi_pre_block(vcpu))
12468 return 1;
12469
Yunhong Jiang64672c92016-06-13 14:19:59 -070012470 if (kvm_lapic_hv_timer_in_use(vcpu))
12471 kvm_lapic_switch_to_sw_timer(vcpu);
12472
Yunhong Jiangbc225122016-06-13 14:19:58 -070012473 return 0;
12474}
12475
12476static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012477{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012478 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012479 return;
12480
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012481 WARN_ON(irqs_disabled());
12482 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012483 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012484 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080012485}
12486
Yunhong Jiangbc225122016-06-13 14:19:58 -070012487static void vmx_post_block(struct kvm_vcpu *vcpu)
12488{
Yunhong Jiang64672c92016-06-13 14:19:59 -070012489 if (kvm_x86_ops->set_hv_timer)
12490 kvm_lapic_switch_to_hv_timer(vcpu);
12491
Yunhong Jiangbc225122016-06-13 14:19:58 -070012492 pi_post_block(vcpu);
12493}
12494
Feng Wubf9f6ac2015-09-18 22:29:55 +080012495/*
Feng Wuefc64402015-09-18 22:29:51 +080012496 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
12497 *
12498 * @kvm: kvm
12499 * @host_irq: host irq of the interrupt
12500 * @guest_irq: gsi of the interrupt
12501 * @set: set or unset PI
12502 * returns 0 on success, < 0 on failure
12503 */
12504static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
12505 uint32_t guest_irq, bool set)
12506{
12507 struct kvm_kernel_irq_routing_entry *e;
12508 struct kvm_irq_routing_table *irq_rt;
12509 struct kvm_lapic_irq irq;
12510 struct kvm_vcpu *vcpu;
12511 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012512 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080012513
12514 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012515 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12516 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080012517 return 0;
12518
12519 idx = srcu_read_lock(&kvm->irq_srcu);
12520 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012521 if (guest_irq >= irq_rt->nr_rt_entries ||
12522 hlist_empty(&irq_rt->map[guest_irq])) {
12523 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
12524 guest_irq, irq_rt->nr_rt_entries);
12525 goto out;
12526 }
Feng Wuefc64402015-09-18 22:29:51 +080012527
12528 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
12529 if (e->type != KVM_IRQ_ROUTING_MSI)
12530 continue;
12531 /*
12532 * VT-d PI cannot support posting multicast/broadcast
12533 * interrupts to a vCPU, we still use interrupt remapping
12534 * for these kind of interrupts.
12535 *
12536 * For lowest-priority interrupts, we only support
12537 * those with single CPU as the destination, e.g. user
12538 * configures the interrupts via /proc/irq or uses
12539 * irqbalance to make the interrupts single-CPU.
12540 *
12541 * We will support full lowest-priority interrupt later.
12542 */
12543
Radim Krčmář371313132016-07-12 22:09:27 +020012544 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080012545 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
12546 /*
12547 * Make sure the IRTE is in remapped mode if
12548 * we don't handle it in posted mode.
12549 */
12550 ret = irq_set_vcpu_affinity(host_irq, NULL);
12551 if (ret < 0) {
12552 printk(KERN_INFO
12553 "failed to back to remapped mode, irq: %u\n",
12554 host_irq);
12555 goto out;
12556 }
12557
Feng Wuefc64402015-09-18 22:29:51 +080012558 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080012559 }
Feng Wuefc64402015-09-18 22:29:51 +080012560
12561 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
12562 vcpu_info.vector = irq.vector;
12563
hu huajun2698d822018-04-11 15:16:40 +080012564 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080012565 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
12566
12567 if (set)
12568 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080012569 else
Feng Wuefc64402015-09-18 22:29:51 +080012570 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080012571
12572 if (ret < 0) {
12573 printk(KERN_INFO "%s: failed to update PI IRTE\n",
12574 __func__);
12575 goto out;
12576 }
12577 }
12578
12579 ret = 0;
12580out:
12581 srcu_read_unlock(&kvm->irq_srcu, idx);
12582 return ret;
12583}
12584
Ashok Rajc45dcc72016-06-22 14:59:56 +080012585static void vmx_setup_mce(struct kvm_vcpu *vcpu)
12586{
12587 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
12588 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
12589 FEATURE_CONTROL_LMCE;
12590 else
12591 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
12592 ~FEATURE_CONTROL_LMCE;
12593}
12594
Ladi Prosek72d7b372017-10-11 16:54:41 +020012595static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
12596{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012597 /* we need a nested vmexit to enter SMM, postpone if run is pending */
12598 if (to_vmx(vcpu)->nested.nested_run_pending)
12599 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020012600 return 1;
12601}
12602
Ladi Prosek0234bf82017-10-11 16:54:40 +020012603static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
12604{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012605 struct vcpu_vmx *vmx = to_vmx(vcpu);
12606
12607 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
12608 if (vmx->nested.smm.guest_mode)
12609 nested_vmx_vmexit(vcpu, -1, 0, 0);
12610
12611 vmx->nested.smm.vmxon = vmx->nested.vmxon;
12612 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070012613 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020012614 return 0;
12615}
12616
12617static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
12618{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012619 struct vcpu_vmx *vmx = to_vmx(vcpu);
12620 int ret;
12621
12622 if (vmx->nested.smm.vmxon) {
12623 vmx->nested.vmxon = true;
12624 vmx->nested.smm.vmxon = false;
12625 }
12626
12627 if (vmx->nested.smm.guest_mode) {
12628 vcpu->arch.hflags &= ~HF_SMM_MASK;
Jim Mattson6514dc32018-04-26 16:09:12 -070012629 ret = enter_vmx_non_root_mode(vcpu);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012630 vcpu->arch.hflags |= HF_SMM_MASK;
12631 if (ret)
12632 return ret;
12633
12634 vmx->nested.smm.guest_mode = false;
12635 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020012636 return 0;
12637}
12638
Ladi Prosekcc3d9672017-10-17 16:02:39 +020012639static int enable_smi_window(struct kvm_vcpu *vcpu)
12640{
12641 return 0;
12642}
12643
Kees Cook404f6aa2016-08-08 16:29:06 -070012644static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080012645 .cpu_has_kvm_support = cpu_has_kvm_support,
12646 .disabled_by_bios = vmx_disabled_by_bios,
12647 .hardware_setup = hardware_setup,
12648 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030012649 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012650 .hardware_enable = hardware_enable,
12651 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080012652 .cpu_has_accelerated_tpr = report_flexpriority,
Paolo Bonzini6d396b52015-04-01 14:25:33 +020012653 .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012654
Wanpeng Lib31c1142018-03-12 04:53:04 -070012655 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070012656 .vm_alloc = vmx_vm_alloc,
12657 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070012658
Avi Kivity6aa8b732006-12-10 02:21:36 -080012659 .vcpu_create = vmx_create_vcpu,
12660 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030012661 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012662
Avi Kivity04d2cc72007-09-10 18:10:54 +030012663 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012664 .vcpu_load = vmx_vcpu_load,
12665 .vcpu_put = vmx_vcpu_put,
12666
Paolo Bonzinia96036b2015-11-10 11:55:36 +010012667 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060012668 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012669 .get_msr = vmx_get_msr,
12670 .set_msr = vmx_set_msr,
12671 .get_segment_base = vmx_get_segment_base,
12672 .get_segment = vmx_get_segment,
12673 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020012674 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012675 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020012676 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020012677 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030012678 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012679 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012680 .set_cr3 = vmx_set_cr3,
12681 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012682 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012683 .get_idt = vmx_get_idt,
12684 .set_idt = vmx_set_idt,
12685 .get_gdt = vmx_get_gdt,
12686 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010012687 .get_dr6 = vmx_get_dr6,
12688 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030012689 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010012690 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030012691 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012692 .get_rflags = vmx_get_rflags,
12693 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080012694
Avi Kivity6aa8b732006-12-10 02:21:36 -080012695 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012696
Avi Kivity6aa8b732006-12-10 02:21:36 -080012697 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020012698 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012699 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040012700 .set_interrupt_shadow = vmx_set_interrupt_shadow,
12701 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020012702 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030012703 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012704 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020012705 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030012706 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020012707 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012708 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010012709 .get_nmi_mask = vmx_get_nmi_mask,
12710 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012711 .enable_nmi_window = enable_nmi_window,
12712 .enable_irq_window = enable_irq_window,
12713 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040012714 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080012715 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030012716 .get_enable_apicv = vmx_get_enable_apicv,
12717 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080012718 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010012719 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080012720 .hwapic_irr_update = vmx_hwapic_irr_update,
12721 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080012722 .sync_pir_to_irr = vmx_sync_pir_to_irr,
12723 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012724
Izik Eiduscbc94022007-10-25 00:29:55 +020012725 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070012726 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080012727 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080012728 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030012729
Avi Kivity586f9602010-11-18 13:09:54 +020012730 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020012731
Sheng Yang17cc3932010-01-05 19:02:27 +080012732 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080012733
12734 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080012735
12736 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000012737 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020012738
12739 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080012740
12741 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100012742
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012743 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100012744 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020012745
12746 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012747
12748 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080012749 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000012750 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080012751 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020012752 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012753
12754 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012755
12756 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080012757
12758 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
12759 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
12760 .flush_log_dirty = vmx_flush_log_dirty,
12761 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040012762 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020012763
Feng Wubf9f6ac2015-09-18 22:29:55 +080012764 .pre_block = vmx_pre_block,
12765 .post_block = vmx_post_block,
12766
Wei Huang25462f72015-06-19 15:45:05 +020012767 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080012768
12769 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070012770
12771#ifdef CONFIG_X86_64
12772 .set_hv_timer = vmx_set_hv_timer,
12773 .cancel_hv_timer = vmx_cancel_hv_timer,
12774#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080012775
12776 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020012777
Ladi Prosek72d7b372017-10-11 16:54:41 +020012778 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020012779 .pre_enter_smm = vmx_pre_enter_smm,
12780 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020012781 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012782};
12783
12784static int __init vmx_init(void)
12785{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010012786 int r;
12787
12788#if IS_ENABLED(CONFIG_HYPERV)
12789 /*
12790 * Enlightened VMCS usage should be recommended and the host needs
12791 * to support eVMCS v1 or above. We can also disable eVMCS support
12792 * with module parameter.
12793 */
12794 if (enlightened_vmcs &&
12795 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
12796 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
12797 KVM_EVMCS_VERSION) {
12798 int cpu;
12799
12800 /* Check that we have assist pages on all online CPUs */
12801 for_each_online_cpu(cpu) {
12802 if (!hv_get_vp_assist_page(cpu)) {
12803 enlightened_vmcs = false;
12804 break;
12805 }
12806 }
12807
12808 if (enlightened_vmcs) {
12809 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
12810 static_branch_enable(&enable_evmcs);
12811 }
12812 } else {
12813 enlightened_vmcs = false;
12814 }
12815#endif
12816
12817 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Tiejun Chen34a1cd62014-10-28 10:14:48 +080012818 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030012819 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080012820 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080012821
Dave Young2965faa2015-09-09 15:38:55 -070012822#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080012823 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
12824 crash_vmclear_local_loaded_vmcss);
12825#endif
12826
He, Qingfdef3ad2007-04-30 09:45:24 +030012827 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080012828}
12829
12830static void __exit vmx_exit(void)
12831{
Dave Young2965faa2015-09-09 15:38:55 -070012832#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053012833 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080012834 synchronize_rcu();
12835#endif
12836
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080012837 kvm_exit();
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010012838
12839#if IS_ENABLED(CONFIG_HYPERV)
12840 if (static_branch_unlikely(&enable_evmcs)) {
12841 int cpu;
12842 struct hv_vp_assist_page *vp_ap;
12843 /*
12844 * Reset everything to support using non-enlightened VMCS
12845 * access later (e.g. when we reload the module with
12846 * enlightened_vmcs=0)
12847 */
12848 for_each_online_cpu(cpu) {
12849 vp_ap = hv_get_vp_assist_page(cpu);
12850
12851 if (!vp_ap)
12852 continue;
12853
12854 vp_ap->current_nested_vmcs = 0;
12855 vp_ap->enlighten_vmentry = 0;
12856 }
12857
12858 static_branch_disable(&enable_evmcs);
12859 }
12860#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080012861}
12862
12863module_init(vmx_init)
12864module_exit(vmx_exit)