blob: 12ed6a8f6287ec42cb2472f07f59753634645617 [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>
Thomas Gleixner28a27752018-04-29 15:01:37 +020054#include <asm/spec-ctrl.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
Radim Krčmářdccbfcf2016-08-08 20:16:23 +0200484 bool change_vmcs01_virtual_x2apic_mode;
Nadav Har'El644d7112011-05-25 23:12:35 +0300485 /* L2 must run next, and mustn't decide to exit to L1. */
486 bool nested_run_pending;
Jim Mattsonde3a0022017-11-27 17:22:25 -0600487
488 struct loaded_vmcs vmcs02;
489
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300490 /*
Jim Mattsonde3a0022017-11-27 17:22:25 -0600491 * Guest pages referred to in the vmcs02 with host-physical
492 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300493 */
494 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800495 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800496 struct page *pi_desc_page;
497 struct pi_desc *pi_desc;
498 bool pi_pending;
499 u16 posted_intr_nv;
Jan Kiszkaf4124502014-03-07 20:03:13 +0100500
501 struct hrtimer preemption_timer;
502 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200503
504 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
505 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800506
Wanpeng Li5c614b32015-10-13 09:18:36 -0700507 u16 vpid02;
508 u16 last_vpid;
509
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100510 struct nested_vmx_msrs msrs;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200511
512 /* SMM related state */
513 struct {
514 /* in VMX operation on SMM entry? */
515 bool vmxon;
516 /* in guest mode on SMM entry? */
517 bool guest_mode;
518 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300519};
520
Yang Zhang01e439b2013-04-11 19:25:12 +0800521#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800522#define POSTED_INTR_SN 1
523
Yang Zhang01e439b2013-04-11 19:25:12 +0800524/* Posted-Interrupt Descriptor */
525struct pi_desc {
526 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800527 union {
528 struct {
529 /* bit 256 - Outstanding Notification */
530 u16 on : 1,
531 /* bit 257 - Suppress Notification */
532 sn : 1,
533 /* bit 271:258 - Reserved */
534 rsvd_1 : 14;
535 /* bit 279:272 - Notification Vector */
536 u8 nv;
537 /* bit 287:280 - Reserved */
538 u8 rsvd_2;
539 /* bit 319:288 - Notification Destination */
540 u32 ndst;
541 };
542 u64 control;
543 };
544 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800545} __aligned(64);
546
Yang Zhanga20ed542013-04-11 19:25:15 +0800547static bool pi_test_and_set_on(struct pi_desc *pi_desc)
548{
549 return test_and_set_bit(POSTED_INTR_ON,
550 (unsigned long *)&pi_desc->control);
551}
552
553static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
554{
555 return test_and_clear_bit(POSTED_INTR_ON,
556 (unsigned long *)&pi_desc->control);
557}
558
559static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
560{
561 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
562}
563
Feng Wuebbfc762015-09-18 22:29:46 +0800564static inline void pi_clear_sn(struct pi_desc *pi_desc)
565{
566 return clear_bit(POSTED_INTR_SN,
567 (unsigned long *)&pi_desc->control);
568}
569
570static inline void pi_set_sn(struct pi_desc *pi_desc)
571{
572 return set_bit(POSTED_INTR_SN,
573 (unsigned long *)&pi_desc->control);
574}
575
Paolo Bonziniad361092016-09-20 16:15:05 +0200576static inline void pi_clear_on(struct pi_desc *pi_desc)
577{
578 clear_bit(POSTED_INTR_ON,
579 (unsigned long *)&pi_desc->control);
580}
581
Feng Wuebbfc762015-09-18 22:29:46 +0800582static inline int pi_test_on(struct pi_desc *pi_desc)
583{
584 return test_bit(POSTED_INTR_ON,
585 (unsigned long *)&pi_desc->control);
586}
587
588static inline int pi_test_sn(struct pi_desc *pi_desc)
589{
590 return test_bit(POSTED_INTR_SN,
591 (unsigned long *)&pi_desc->control);
592}
593
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400594struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000595 struct kvm_vcpu vcpu;
Avi Kivity313dbd492008-07-17 18:04:30 +0300596 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300597 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100598 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300599 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200600 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200601 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300602 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400603 int nmsrs;
604 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800605 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400606#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300607 u64 msr_host_kernel_gs_base;
608 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400609#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100610
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100611 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100612 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100613
Gleb Natapov2961e8762013-11-25 15:37:13 +0200614 u32 vm_entry_controls_shadow;
615 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200616 u32 secondary_exec_control;
617
Nadav Har'Eld462b812011-05-24 15:26:10 +0300618 /*
619 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
620 * non-nested (L1) guest, it always points to vmcs01. For a nested
621 * guest (L2), it points to a different VMCS.
622 */
623 struct loaded_vmcs vmcs01;
624 struct loaded_vmcs *loaded_vmcs;
625 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300626 struct msr_autoload {
627 unsigned nr;
628 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
629 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
630 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400631 struct {
632 int loaded;
633 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300634#ifdef CONFIG_X86_64
635 u16 ds_sel, es_sel;
636#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200637 int gs_ldt_reload_needed;
638 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000639 u64 msr_host_bndcfgs;
Mike Dayd77c26f2007-10-08 09:02:08 -0400640 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200641 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300642 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300643 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300644 struct kvm_segment segs[8];
645 } rmode;
646 struct {
647 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300648 struct kvm_save_segment {
649 u16 selector;
650 unsigned long base;
651 u32 limit;
652 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300653 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300654 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800655 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300656 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200657
Andi Kleena0861c02009-06-08 17:37:09 +0800658 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800659
Yang Zhang01e439b2013-04-11 19:25:12 +0800660 /* Posted interrupt descriptor */
661 struct pi_desc pi_desc;
662
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300663 /* Support for a guest hypervisor (nested VMX) */
664 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200665
666 /* Dynamic PLE window. */
667 int ple_window;
668 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800669
670 /* Support for PML */
671#define PML_ENTITY_NUM 512
672 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800673
Yunhong Jiang64672c92016-06-13 14:19:59 -0700674 /* apic deadline value in host tsc */
675 u64 hv_deadline_tsc;
676
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800677 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800678
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800679 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800680
Wanpeng Li74c55932017-11-29 01:31:20 -0800681 unsigned long host_debugctlmsr;
682
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800683 /*
684 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
685 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
686 * in msr_ia32_feature_control_valid_bits.
687 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800688 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800689 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400690};
691
Avi Kivity2fb92db2011-04-27 19:42:18 +0300692enum segment_cache_field {
693 SEG_FIELD_SEL = 0,
694 SEG_FIELD_BASE = 1,
695 SEG_FIELD_LIMIT = 2,
696 SEG_FIELD_AR = 3,
697
698 SEG_FIELD_NR = 4
699};
700
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700701static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
702{
703 return container_of(kvm, struct kvm_vmx, kvm);
704}
705
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400706static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
707{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000708 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400709}
710
Feng Wuefc64402015-09-18 22:29:51 +0800711static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
712{
713 return &(to_vmx(vcpu)->pi_desc);
714}
715
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800716#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +0300717#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800718#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
719#define FIELD64(number, name) \
720 FIELD(number, name), \
721 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +0300722
Abel Gordon4607c2d2013-04-18 14:35:55 +0300723
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100724static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100725#define SHADOW_FIELD_RO(x) x,
726#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300727};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400728static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300729 ARRAY_SIZE(shadow_read_only_fields);
730
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100731static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100732#define SHADOW_FIELD_RW(x) x,
733#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300734};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400735static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300736 ARRAY_SIZE(shadow_read_write_fields);
737
Mathias Krause772e0312012-08-30 01:30:19 +0200738static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300739 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800740 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300741 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
742 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
743 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
744 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
745 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
746 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
747 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
748 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800749 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -0400750 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300751 FIELD(HOST_ES_SELECTOR, host_es_selector),
752 FIELD(HOST_CS_SELECTOR, host_cs_selector),
753 FIELD(HOST_SS_SELECTOR, host_ss_selector),
754 FIELD(HOST_DS_SELECTOR, host_ds_selector),
755 FIELD(HOST_FS_SELECTOR, host_fs_selector),
756 FIELD(HOST_GS_SELECTOR, host_gs_selector),
757 FIELD(HOST_TR_SELECTOR, host_tr_selector),
758 FIELD64(IO_BITMAP_A, io_bitmap_a),
759 FIELD64(IO_BITMAP_B, io_bitmap_b),
760 FIELD64(MSR_BITMAP, msr_bitmap),
761 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
762 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
763 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
764 FIELD64(TSC_OFFSET, tsc_offset),
765 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
766 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800767 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -0400768 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300769 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800770 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
771 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
772 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
773 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -0400774 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800775 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300776 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
777 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
Bandan Dasc5f983f2017-05-05 15:25:14 -0400778 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300779 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
780 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
781 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
782 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
783 FIELD64(GUEST_PDPTR0, guest_pdptr0),
784 FIELD64(GUEST_PDPTR1, guest_pdptr1),
785 FIELD64(GUEST_PDPTR2, guest_pdptr2),
786 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100787 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300788 FIELD64(HOST_IA32_PAT, host_ia32_pat),
789 FIELD64(HOST_IA32_EFER, host_ia32_efer),
790 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
791 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
792 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
793 FIELD(EXCEPTION_BITMAP, exception_bitmap),
794 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
795 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
796 FIELD(CR3_TARGET_COUNT, cr3_target_count),
797 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
798 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
799 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
800 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
801 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
802 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
803 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
804 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
805 FIELD(TPR_THRESHOLD, tpr_threshold),
806 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
807 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
808 FIELD(VM_EXIT_REASON, vm_exit_reason),
809 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
810 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
811 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
812 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
813 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
814 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
815 FIELD(GUEST_ES_LIMIT, guest_es_limit),
816 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
817 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
818 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
819 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
820 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
821 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
822 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
823 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
824 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
825 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
826 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
827 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
828 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
829 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
830 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
831 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
832 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
833 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
834 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
835 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
836 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +0100837 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300838 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
839 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
840 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
841 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
842 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
843 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
844 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
845 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
846 FIELD(EXIT_QUALIFICATION, exit_qualification),
847 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
848 FIELD(GUEST_CR0, guest_cr0),
849 FIELD(GUEST_CR3, guest_cr3),
850 FIELD(GUEST_CR4, guest_cr4),
851 FIELD(GUEST_ES_BASE, guest_es_base),
852 FIELD(GUEST_CS_BASE, guest_cs_base),
853 FIELD(GUEST_SS_BASE, guest_ss_base),
854 FIELD(GUEST_DS_BASE, guest_ds_base),
855 FIELD(GUEST_FS_BASE, guest_fs_base),
856 FIELD(GUEST_GS_BASE, guest_gs_base),
857 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
858 FIELD(GUEST_TR_BASE, guest_tr_base),
859 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
860 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
861 FIELD(GUEST_DR7, guest_dr7),
862 FIELD(GUEST_RSP, guest_rsp),
863 FIELD(GUEST_RIP, guest_rip),
864 FIELD(GUEST_RFLAGS, guest_rflags),
865 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
866 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
867 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
868 FIELD(HOST_CR0, host_cr0),
869 FIELD(HOST_CR3, host_cr3),
870 FIELD(HOST_CR4, host_cr4),
871 FIELD(HOST_FS_BASE, host_fs_base),
872 FIELD(HOST_GS_BASE, host_gs_base),
873 FIELD(HOST_TR_BASE, host_tr_base),
874 FIELD(HOST_GDTR_BASE, host_gdtr_base),
875 FIELD(HOST_IDTR_BASE, host_idtr_base),
876 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
877 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
878 FIELD(HOST_RSP, host_rsp),
879 FIELD(HOST_RIP, host_rip),
880};
Nadav Har'El22bd0352011-05-25 23:05:57 +0300881
882static inline short vmcs_field_to_offset(unsigned long field)
883{
Dan Williams085331d2018-01-31 17:47:03 -0800884 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
885 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800886 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +0100887
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800888 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -0800889 return -ENOENT;
890
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800891 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -0800892 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -0800893 return -ENOENT;
894
Linus Torvalds15303ba2018-02-10 13:16:35 -0800895 index = array_index_nospec(index, size);
896 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -0800897 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +0100898 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -0800899 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300900}
901
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300902static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
903{
David Matlack4f2777b2016-07-13 17:16:37 -0700904 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300905}
906
Peter Feiner995f00a2017-06-30 17:26:32 -0700907static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +0300908static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -0700909static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +0800910static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +0300911static void vmx_set_segment(struct kvm_vcpu *vcpu,
912 struct kvm_segment *var, int seg);
913static void vmx_get_segment(struct kvm_vcpu *vcpu,
914 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +0200915static bool guest_state_valid(struct kvm_vcpu *vcpu);
916static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +0300917static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +0200918static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
919static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
920static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
921 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100922static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +0100923static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
924 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +0300925
Avi Kivity6aa8b732006-12-10 02:21:36 -0800926static DEFINE_PER_CPU(struct vmcs *, vmxarea);
927static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300928/*
929 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
930 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
931 */
932static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800933
Feng Wubf9f6ac2015-09-18 22:29:55 +0800934/*
935 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
936 * can find which vCPU should be waken up.
937 */
938static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
939static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
940
Radim Krčmář23611332016-09-29 22:41:33 +0200941enum {
Radim Krčmář23611332016-09-29 22:41:33 +0200942 VMX_VMREAD_BITMAP,
943 VMX_VMWRITE_BITMAP,
944 VMX_BITMAP_NR
945};
946
947static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
948
Radim Krčmář23611332016-09-29 22:41:33 +0200949#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
950#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +0300951
Avi Kivity110312c2010-12-21 12:54:20 +0200952static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200953static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +0200954
Sheng Yang2384d2b2008-01-17 15:14:33 +0800955static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
956static DEFINE_SPINLOCK(vmx_vpid_lock);
957
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300958static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800959 int size;
960 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +0300961 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800962 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300963 u32 pin_based_exec_ctrl;
964 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800965 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300966 u32 vmexit_ctrl;
967 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +0100968 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300969} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800970
Hannes Ederefff9e52008-11-28 17:02:06 +0100971static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800972 u32 ept;
973 u32 vpid;
974} vmx_capability;
975
Avi Kivity6aa8b732006-12-10 02:21:36 -0800976#define VMX_SEGMENT_FIELD(seg) \
977 [VCPU_SREG_##seg] = { \
978 .selector = GUEST_##seg##_SELECTOR, \
979 .base = GUEST_##seg##_BASE, \
980 .limit = GUEST_##seg##_LIMIT, \
981 .ar_bytes = GUEST_##seg##_AR_BYTES, \
982 }
983
Mathias Krause772e0312012-08-30 01:30:19 +0200984static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800985 unsigned selector;
986 unsigned base;
987 unsigned limit;
988 unsigned ar_bytes;
989} kvm_vmx_segment_fields[] = {
990 VMX_SEGMENT_FIELD(CS),
991 VMX_SEGMENT_FIELD(DS),
992 VMX_SEGMENT_FIELD(ES),
993 VMX_SEGMENT_FIELD(FS),
994 VMX_SEGMENT_FIELD(GS),
995 VMX_SEGMENT_FIELD(SS),
996 VMX_SEGMENT_FIELD(TR),
997 VMX_SEGMENT_FIELD(LDTR),
998};
999
Avi Kivity26bb0982009-09-07 11:14:12 +03001000static u64 host_efer;
1001
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001002static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1003
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001004/*
Brian Gerst8c065852010-07-17 09:03:26 -04001005 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001006 * away by decrementing the array size.
1007 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001008static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001009#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001010 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001011#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001012 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001013};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001014
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001015DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1016
1017#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1018
1019#define KVM_EVMCS_VERSION 1
1020
1021#if IS_ENABLED(CONFIG_HYPERV)
1022static bool __read_mostly enlightened_vmcs = true;
1023module_param(enlightened_vmcs, bool, 0444);
1024
1025static inline void evmcs_write64(unsigned long field, u64 value)
1026{
1027 u16 clean_field;
1028 int offset = get_evmcs_offset(field, &clean_field);
1029
1030 if (offset < 0)
1031 return;
1032
1033 *(u64 *)((char *)current_evmcs + offset) = value;
1034
1035 current_evmcs->hv_clean_fields &= ~clean_field;
1036}
1037
1038static inline void evmcs_write32(unsigned long field, u32 value)
1039{
1040 u16 clean_field;
1041 int offset = get_evmcs_offset(field, &clean_field);
1042
1043 if (offset < 0)
1044 return;
1045
1046 *(u32 *)((char *)current_evmcs + offset) = value;
1047 current_evmcs->hv_clean_fields &= ~clean_field;
1048}
1049
1050static inline void evmcs_write16(unsigned long field, u16 value)
1051{
1052 u16 clean_field;
1053 int offset = get_evmcs_offset(field, &clean_field);
1054
1055 if (offset < 0)
1056 return;
1057
1058 *(u16 *)((char *)current_evmcs + offset) = value;
1059 current_evmcs->hv_clean_fields &= ~clean_field;
1060}
1061
1062static inline u64 evmcs_read64(unsigned long field)
1063{
1064 int offset = get_evmcs_offset(field, NULL);
1065
1066 if (offset < 0)
1067 return 0;
1068
1069 return *(u64 *)((char *)current_evmcs + offset);
1070}
1071
1072static inline u32 evmcs_read32(unsigned long field)
1073{
1074 int offset = get_evmcs_offset(field, NULL);
1075
1076 if (offset < 0)
1077 return 0;
1078
1079 return *(u32 *)((char *)current_evmcs + offset);
1080}
1081
1082static inline u16 evmcs_read16(unsigned long field)
1083{
1084 int offset = get_evmcs_offset(field, NULL);
1085
1086 if (offset < 0)
1087 return 0;
1088
1089 return *(u16 *)((char *)current_evmcs + offset);
1090}
1091
1092static void evmcs_load(u64 phys_addr)
1093{
1094 struct hv_vp_assist_page *vp_ap =
1095 hv_get_vp_assist_page(smp_processor_id());
1096
1097 vp_ap->current_nested_vmcs = phys_addr;
1098 vp_ap->enlighten_vmentry = 1;
1099}
1100
1101static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1102{
1103 /*
1104 * Enlightened VMCSv1 doesn't support these:
1105 *
1106 * POSTED_INTR_NV = 0x00000002,
1107 * GUEST_INTR_STATUS = 0x00000810,
1108 * APIC_ACCESS_ADDR = 0x00002014,
1109 * POSTED_INTR_DESC_ADDR = 0x00002016,
1110 * EOI_EXIT_BITMAP0 = 0x0000201c,
1111 * EOI_EXIT_BITMAP1 = 0x0000201e,
1112 * EOI_EXIT_BITMAP2 = 0x00002020,
1113 * EOI_EXIT_BITMAP3 = 0x00002022,
1114 */
1115 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
1116 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1117 ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1118 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1119 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1120 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1121 ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
1122
1123 /*
1124 * GUEST_PML_INDEX = 0x00000812,
1125 * PML_ADDRESS = 0x0000200e,
1126 */
1127 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
1128
1129 /* VM_FUNCTION_CONTROL = 0x00002018, */
1130 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
1131
1132 /*
1133 * EPTP_LIST_ADDRESS = 0x00002024,
1134 * VMREAD_BITMAP = 0x00002026,
1135 * VMWRITE_BITMAP = 0x00002028,
1136 */
1137 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
1138
1139 /*
1140 * TSC_MULTIPLIER = 0x00002032,
1141 */
1142 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
1143
1144 /*
1145 * PLE_GAP = 0x00004020,
1146 * PLE_WINDOW = 0x00004022,
1147 */
1148 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1149
1150 /*
1151 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1152 */
1153 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1154
1155 /*
1156 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1157 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1158 */
1159 vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
1160 vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
1161
1162 /*
1163 * Currently unsupported in KVM:
1164 * GUEST_IA32_RTIT_CTL = 0x00002814,
1165 */
1166}
1167#else /* !IS_ENABLED(CONFIG_HYPERV) */
1168static inline void evmcs_write64(unsigned long field, u64 value) {}
1169static inline void evmcs_write32(unsigned long field, u32 value) {}
1170static inline void evmcs_write16(unsigned long field, u16 value) {}
1171static inline u64 evmcs_read64(unsigned long field) { return 0; }
1172static inline u32 evmcs_read32(unsigned long field) { return 0; }
1173static inline u16 evmcs_read16(unsigned long field) { return 0; }
1174static inline void evmcs_load(u64 phys_addr) {}
1175static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
1176#endif /* IS_ENABLED(CONFIG_HYPERV) */
1177
Jan Kiszka5bb16012016-02-09 20:14:21 +01001178static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001179{
1180 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1181 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001182 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1183}
1184
Jan Kiszka6f054852016-02-09 20:15:18 +01001185static inline bool is_debug(u32 intr_info)
1186{
1187 return is_exception_n(intr_info, DB_VECTOR);
1188}
1189
1190static inline bool is_breakpoint(u32 intr_info)
1191{
1192 return is_exception_n(intr_info, BP_VECTOR);
1193}
1194
Jan Kiszka5bb16012016-02-09 20:14:21 +01001195static inline bool is_page_fault(u32 intr_info)
1196{
1197 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001198}
1199
Gui Jianfeng31299942010-03-15 17:29:09 +08001200static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001201{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001202 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001203}
1204
Gui Jianfeng31299942010-03-15 17:29:09 +08001205static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001206{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001207 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001208}
1209
Liran Alon9e869482018-03-12 13:12:51 +02001210static inline bool is_gp_fault(u32 intr_info)
1211{
1212 return is_exception_n(intr_info, GP_VECTOR);
1213}
1214
Gui Jianfeng31299942010-03-15 17:29:09 +08001215static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001216{
1217 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1218 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1219}
1220
Gui Jianfeng31299942010-03-15 17:29:09 +08001221static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001222{
1223 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1224 INTR_INFO_VALID_MASK)) ==
1225 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1226}
1227
Linus Torvalds32d43cd2018-03-20 12:16:59 -07001228/* Undocumented: icebp/int1 */
1229static inline bool is_icebp(u32 intr_info)
1230{
1231 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1232 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1233}
1234
Gui Jianfeng31299942010-03-15 17:29:09 +08001235static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001236{
Sheng Yang04547152009-04-01 15:52:31 +08001237 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001238}
1239
Gui Jianfeng31299942010-03-15 17:29:09 +08001240static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001241{
Sheng Yang04547152009-04-01 15:52:31 +08001242 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001243}
1244
Paolo Bonzini35754c92015-07-29 12:05:37 +02001245static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001246{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001247 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001248}
1249
Gui Jianfeng31299942010-03-15 17:29:09 +08001250static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001251{
Sheng Yang04547152009-04-01 15:52:31 +08001252 return vmcs_config.cpu_based_exec_ctrl &
1253 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001254}
1255
Avi Kivity774ead32007-12-26 13:57:04 +02001256static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001257{
Sheng Yang04547152009-04-01 15:52:31 +08001258 return vmcs_config.cpu_based_2nd_exec_ctrl &
1259 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1260}
1261
Yang Zhang8d146952013-01-25 10:18:50 +08001262static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1263{
1264 return vmcs_config.cpu_based_2nd_exec_ctrl &
1265 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1266}
1267
Yang Zhang83d4c282013-01-25 10:18:49 +08001268static inline bool cpu_has_vmx_apic_register_virt(void)
1269{
1270 return vmcs_config.cpu_based_2nd_exec_ctrl &
1271 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1272}
1273
Yang Zhangc7c9c562013-01-25 10:18:51 +08001274static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1275{
1276 return vmcs_config.cpu_based_2nd_exec_ctrl &
1277 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1278}
1279
Yunhong Jiang64672c92016-06-13 14:19:59 -07001280/*
1281 * Comment's format: document - errata name - stepping - processor name.
1282 * Refer from
1283 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1284 */
1285static u32 vmx_preemption_cpu_tfms[] = {
1286/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
12870x000206E6,
1288/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1289/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1290/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
12910x00020652,
1292/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
12930x00020655,
1294/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1295/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1296/*
1297 * 320767.pdf - AAP86 - B1 -
1298 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1299 */
13000x000106E5,
1301/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
13020x000106A0,
1303/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
13040x000106A1,
1305/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
13060x000106A4,
1307 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1308 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1309 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
13100x000106A5,
1311};
1312
1313static inline bool cpu_has_broken_vmx_preemption_timer(void)
1314{
1315 u32 eax = cpuid_eax(0x00000001), i;
1316
1317 /* Clear the reserved bits */
1318 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001319 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001320 if (eax == vmx_preemption_cpu_tfms[i])
1321 return true;
1322
1323 return false;
1324}
1325
1326static inline bool cpu_has_vmx_preemption_timer(void)
1327{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001328 return vmcs_config.pin_based_exec_ctrl &
1329 PIN_BASED_VMX_PREEMPTION_TIMER;
1330}
1331
Yang Zhang01e439b2013-04-11 19:25:12 +08001332static inline bool cpu_has_vmx_posted_intr(void)
1333{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001334 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1335 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001336}
1337
1338static inline bool cpu_has_vmx_apicv(void)
1339{
1340 return cpu_has_vmx_apic_register_virt() &&
1341 cpu_has_vmx_virtual_intr_delivery() &&
1342 cpu_has_vmx_posted_intr();
1343}
1344
Sheng Yang04547152009-04-01 15:52:31 +08001345static inline bool cpu_has_vmx_flexpriority(void)
1346{
1347 return cpu_has_vmx_tpr_shadow() &&
1348 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001349}
1350
Marcelo Tosattie7997942009-06-11 12:07:40 -03001351static inline bool cpu_has_vmx_ept_execute_only(void)
1352{
Gui Jianfeng31299942010-03-15 17:29:09 +08001353 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001354}
1355
Marcelo Tosattie7997942009-06-11 12:07:40 -03001356static inline bool cpu_has_vmx_ept_2m_page(void)
1357{
Gui Jianfeng31299942010-03-15 17:29:09 +08001358 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001359}
1360
Sheng Yang878403b2010-01-05 19:02:29 +08001361static inline bool cpu_has_vmx_ept_1g_page(void)
1362{
Gui Jianfeng31299942010-03-15 17:29:09 +08001363 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001364}
1365
Sheng Yang4bc9b982010-06-02 14:05:24 +08001366static inline bool cpu_has_vmx_ept_4levels(void)
1367{
1368 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1369}
1370
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001371static inline bool cpu_has_vmx_ept_mt_wb(void)
1372{
1373 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1374}
1375
Yu Zhang855feb62017-08-24 20:27:55 +08001376static inline bool cpu_has_vmx_ept_5levels(void)
1377{
1378 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1379}
1380
Xudong Hao83c3a332012-05-28 19:33:35 +08001381static inline bool cpu_has_vmx_ept_ad_bits(void)
1382{
1383 return vmx_capability.ept & VMX_EPT_AD_BIT;
1384}
1385
Gui Jianfeng31299942010-03-15 17:29:09 +08001386static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001387{
Gui Jianfeng31299942010-03-15 17:29:09 +08001388 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001389}
1390
Gui Jianfeng31299942010-03-15 17:29:09 +08001391static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001392{
Gui Jianfeng31299942010-03-15 17:29:09 +08001393 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001394}
1395
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001396static inline bool cpu_has_vmx_invvpid_single(void)
1397{
1398 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1399}
1400
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001401static inline bool cpu_has_vmx_invvpid_global(void)
1402{
1403 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1404}
1405
Wanpeng Li08d839c2017-03-23 05:30:08 -07001406static inline bool cpu_has_vmx_invvpid(void)
1407{
1408 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1409}
1410
Gui Jianfeng31299942010-03-15 17:29:09 +08001411static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001412{
Sheng Yang04547152009-04-01 15:52:31 +08001413 return vmcs_config.cpu_based_2nd_exec_ctrl &
1414 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001415}
1416
Gui Jianfeng31299942010-03-15 17:29:09 +08001417static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001418{
1419 return vmcs_config.cpu_based_2nd_exec_ctrl &
1420 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1421}
1422
Gui Jianfeng31299942010-03-15 17:29:09 +08001423static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001424{
1425 return vmcs_config.cpu_based_2nd_exec_ctrl &
1426 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1427}
1428
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001429static inline bool cpu_has_vmx_basic_inout(void)
1430{
1431 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1432}
1433
Paolo Bonzini35754c92015-07-29 12:05:37 +02001434static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001435{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001436 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001437}
1438
Gui Jianfeng31299942010-03-15 17:29:09 +08001439static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001440{
Sheng Yang04547152009-04-01 15:52:31 +08001441 return vmcs_config.cpu_based_2nd_exec_ctrl &
1442 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001443}
1444
Gui Jianfeng31299942010-03-15 17:29:09 +08001445static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001446{
1447 return vmcs_config.cpu_based_2nd_exec_ctrl &
1448 SECONDARY_EXEC_RDTSCP;
1449}
1450
Mao, Junjiead756a12012-07-02 01:18:48 +00001451static inline bool cpu_has_vmx_invpcid(void)
1452{
1453 return vmcs_config.cpu_based_2nd_exec_ctrl &
1454 SECONDARY_EXEC_ENABLE_INVPCID;
1455}
1456
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001457static inline bool cpu_has_virtual_nmis(void)
1458{
1459 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1460}
1461
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001462static inline bool cpu_has_vmx_wbinvd_exit(void)
1463{
1464 return vmcs_config.cpu_based_2nd_exec_ctrl &
1465 SECONDARY_EXEC_WBINVD_EXITING;
1466}
1467
Abel Gordonabc4fc52013-04-18 14:35:25 +03001468static inline bool cpu_has_vmx_shadow_vmcs(void)
1469{
1470 u64 vmx_msr;
1471 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1472 /* check if the cpu supports writing r/o exit information fields */
1473 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1474 return false;
1475
1476 return vmcs_config.cpu_based_2nd_exec_ctrl &
1477 SECONDARY_EXEC_SHADOW_VMCS;
1478}
1479
Kai Huang843e4332015-01-28 10:54:28 +08001480static inline bool cpu_has_vmx_pml(void)
1481{
1482 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1483}
1484
Haozhong Zhang64903d62015-10-20 15:39:09 +08001485static inline bool cpu_has_vmx_tsc_scaling(void)
1486{
1487 return vmcs_config.cpu_based_2nd_exec_ctrl &
1488 SECONDARY_EXEC_TSC_SCALING;
1489}
1490
Bandan Das2a499e42017-08-03 15:54:41 -04001491static inline bool cpu_has_vmx_vmfunc(void)
1492{
1493 return vmcs_config.cpu_based_2nd_exec_ctrl &
1494 SECONDARY_EXEC_ENABLE_VMFUNC;
1495}
1496
Sean Christopherson64f7a112018-04-30 10:01:06 -07001497static bool vmx_umip_emulated(void)
1498{
1499 return vmcs_config.cpu_based_2nd_exec_ctrl &
1500 SECONDARY_EXEC_DESC;
1501}
1502
Sheng Yang04547152009-04-01 15:52:31 +08001503static inline bool report_flexpriority(void)
1504{
1505 return flexpriority_enabled;
1506}
1507
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001508static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1509{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001510 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001511}
1512
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001513static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1514{
1515 return vmcs12->cpu_based_vm_exec_control & bit;
1516}
1517
1518static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1519{
1520 return (vmcs12->cpu_based_vm_exec_control &
1521 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1522 (vmcs12->secondary_vm_exec_control & bit);
1523}
1524
Jan Kiszkaf4124502014-03-07 20:03:13 +01001525static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1526{
1527 return vmcs12->pin_based_vm_exec_control &
1528 PIN_BASED_VMX_PREEMPTION_TIMER;
1529}
1530
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001531static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1532{
1533 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1534}
1535
1536static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1537{
1538 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1539}
1540
Nadav Har'El155a97a2013-08-05 11:07:16 +03001541static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1542{
1543 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1544}
1545
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001546static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1547{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001548 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001549}
1550
Bandan Dasc5f983f2017-05-05 15:25:14 -04001551static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1552{
1553 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1554}
1555
Wincy Vanf2b93282015-02-03 23:56:03 +08001556static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1557{
1558 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1559}
1560
Wanpeng Li5c614b32015-10-13 09:18:36 -07001561static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1562{
1563 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1564}
1565
Wincy Van82f0dd42015-02-03 23:57:18 +08001566static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1567{
1568 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1569}
1570
Wincy Van608406e2015-02-03 23:57:51 +08001571static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1572{
1573 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1574}
1575
Wincy Van705699a2015-02-03 23:58:17 +08001576static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1577{
1578 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1579}
1580
Bandan Das27c42a12017-08-03 15:54:42 -04001581static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1582{
1583 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1584}
1585
Bandan Das41ab9372017-08-03 15:54:43 -04001586static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1587{
1588 return nested_cpu_has_vmfunc(vmcs12) &&
1589 (vmcs12->vm_function_control &
1590 VMX_VMFUNC_EPTP_SWITCHING);
1591}
1592
Jim Mattsonef85b672016-12-12 11:01:37 -08001593static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001594{
1595 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08001596 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001597}
1598
Jan Kiszka533558b2014-01-04 18:47:20 +01001599static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1600 u32 exit_intr_info,
1601 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001602static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1603 struct vmcs12 *vmcs12,
1604 u32 reason, unsigned long qualification);
1605
Rusty Russell8b9cf982007-07-30 16:31:43 +10001606static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001607{
1608 int i;
1609
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001610 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001611 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001612 return i;
1613 return -1;
1614}
1615
Sheng Yang2384d2b2008-01-17 15:14:33 +08001616static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1617{
1618 struct {
1619 u64 vpid : 16;
1620 u64 rsvd : 48;
1621 u64 gva;
1622 } operand = { vpid, 0, gva };
1623
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001624 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001625 /* CF==1 or ZF==1 --> rc = -1 */
1626 "; ja 1f ; ud2 ; 1:"
1627 : : "a"(&operand), "c"(ext) : "cc", "memory");
1628}
1629
Sheng Yang14394422008-04-28 12:24:45 +08001630static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1631{
1632 struct {
1633 u64 eptp, gpa;
1634 } operand = {eptp, gpa};
1635
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001636 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001637 /* CF==1 or ZF==1 --> rc = -1 */
1638 "; ja 1f ; ud2 ; 1:\n"
1639 : : "a" (&operand), "c" (ext) : "cc", "memory");
1640}
1641
Avi Kivity26bb0982009-09-07 11:14:12 +03001642static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001643{
1644 int i;
1645
Rusty Russell8b9cf982007-07-30 16:31:43 +10001646 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001647 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001648 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001649 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001650}
1651
Avi Kivity6aa8b732006-12-10 02:21:36 -08001652static void vmcs_clear(struct vmcs *vmcs)
1653{
1654 u64 phys_addr = __pa(vmcs);
1655 u8 error;
1656
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001657 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001658 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001659 : "cc", "memory");
1660 if (error)
1661 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1662 vmcs, phys_addr);
1663}
1664
Nadav Har'Eld462b812011-05-24 15:26:10 +03001665static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1666{
1667 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001668 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1669 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001670 loaded_vmcs->cpu = -1;
1671 loaded_vmcs->launched = 0;
1672}
1673
Dongxiao Xu7725b892010-05-11 18:29:38 +08001674static void vmcs_load(struct vmcs *vmcs)
1675{
1676 u64 phys_addr = __pa(vmcs);
1677 u8 error;
1678
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001679 if (static_branch_unlikely(&enable_evmcs))
1680 return evmcs_load(phys_addr);
1681
Dongxiao Xu7725b892010-05-11 18:29:38 +08001682 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001683 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001684 : "cc", "memory");
1685 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001686 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001687 vmcs, phys_addr);
1688}
1689
Dave Young2965faa2015-09-09 15:38:55 -07001690#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001691/*
1692 * This bitmap is used to indicate whether the vmclear
1693 * operation is enabled on all cpus. All disabled by
1694 * default.
1695 */
1696static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1697
1698static inline void crash_enable_local_vmclear(int cpu)
1699{
1700 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1701}
1702
1703static inline void crash_disable_local_vmclear(int cpu)
1704{
1705 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1706}
1707
1708static inline int crash_local_vmclear_enabled(int cpu)
1709{
1710 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1711}
1712
1713static void crash_vmclear_local_loaded_vmcss(void)
1714{
1715 int cpu = raw_smp_processor_id();
1716 struct loaded_vmcs *v;
1717
1718 if (!crash_local_vmclear_enabled(cpu))
1719 return;
1720
1721 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1722 loaded_vmcss_on_cpu_link)
1723 vmcs_clear(v->vmcs);
1724}
1725#else
1726static inline void crash_enable_local_vmclear(int cpu) { }
1727static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001728#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001729
Nadav Har'Eld462b812011-05-24 15:26:10 +03001730static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001731{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001732 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001733 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001734
Nadav Har'Eld462b812011-05-24 15:26:10 +03001735 if (loaded_vmcs->cpu != cpu)
1736 return; /* vcpu migration can race with cpu offline */
1737 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001738 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001739 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001740 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001741
1742 /*
1743 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1744 * is before setting loaded_vmcs->vcpu to -1 which is done in
1745 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1746 * then adds the vmcs into percpu list before it is deleted.
1747 */
1748 smp_wmb();
1749
Nadav Har'Eld462b812011-05-24 15:26:10 +03001750 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001751 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001752}
1753
Nadav Har'Eld462b812011-05-24 15:26:10 +03001754static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001755{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001756 int cpu = loaded_vmcs->cpu;
1757
1758 if (cpu != -1)
1759 smp_call_function_single(cpu,
1760 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001761}
1762
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001763static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001764{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001765 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001766 return;
1767
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001768 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001769 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001770}
1771
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001772static inline void vpid_sync_vcpu_global(void)
1773{
1774 if (cpu_has_vmx_invvpid_global())
1775 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1776}
1777
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001778static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001779{
1780 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001781 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001782 else
1783 vpid_sync_vcpu_global();
1784}
1785
Sheng Yang14394422008-04-28 12:24:45 +08001786static inline void ept_sync_global(void)
1787{
David Hildenbrandf5f51582017-08-24 20:51:30 +02001788 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08001789}
1790
1791static inline void ept_sync_context(u64 eptp)
1792{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02001793 if (cpu_has_vmx_invept_context())
1794 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1795 else
1796 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08001797}
1798
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001799static __always_inline void vmcs_check16(unsigned long field)
1800{
1801 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1802 "16-bit accessor invalid for 64-bit field");
1803 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1804 "16-bit accessor invalid for 64-bit high field");
1805 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1806 "16-bit accessor invalid for 32-bit high field");
1807 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1808 "16-bit accessor invalid for natural width field");
1809}
1810
1811static __always_inline void vmcs_check32(unsigned long field)
1812{
1813 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1814 "32-bit accessor invalid for 16-bit field");
1815 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1816 "32-bit accessor invalid for natural width field");
1817}
1818
1819static __always_inline void vmcs_check64(unsigned long field)
1820{
1821 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1822 "64-bit accessor invalid for 16-bit field");
1823 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1824 "64-bit accessor invalid for 64-bit high field");
1825 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1826 "64-bit accessor invalid for 32-bit field");
1827 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1828 "64-bit accessor invalid for natural width field");
1829}
1830
1831static __always_inline void vmcs_checkl(unsigned long field)
1832{
1833 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1834 "Natural width accessor invalid for 16-bit field");
1835 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1836 "Natural width accessor invalid for 64-bit field");
1837 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1838 "Natural width accessor invalid for 64-bit high field");
1839 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1840 "Natural width accessor invalid for 32-bit field");
1841}
1842
1843static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001844{
Avi Kivity5e520e62011-05-15 10:13:12 -04001845 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001846
Avi Kivity5e520e62011-05-15 10:13:12 -04001847 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1848 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001849 return value;
1850}
1851
Avi Kivity96304212011-05-15 10:13:13 -04001852static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001853{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001854 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001855 if (static_branch_unlikely(&enable_evmcs))
1856 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001857 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001858}
1859
Avi Kivity96304212011-05-15 10:13:13 -04001860static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001861{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001862 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001863 if (static_branch_unlikely(&enable_evmcs))
1864 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001865 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001866}
1867
Avi Kivity96304212011-05-15 10:13:13 -04001868static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001869{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001870 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001871 if (static_branch_unlikely(&enable_evmcs))
1872 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001873#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001874 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001875#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001876 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001877#endif
1878}
1879
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001880static __always_inline unsigned long vmcs_readl(unsigned long field)
1881{
1882 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001883 if (static_branch_unlikely(&enable_evmcs))
1884 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001885 return __vmcs_readl(field);
1886}
1887
Avi Kivitye52de1b2007-01-05 16:36:56 -08001888static noinline void vmwrite_error(unsigned long field, unsigned long value)
1889{
1890 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1891 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1892 dump_stack();
1893}
1894
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001895static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001896{
1897 u8 error;
1898
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001899 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04001900 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08001901 if (unlikely(error))
1902 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001903}
1904
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001905static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001906{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001907 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001908 if (static_branch_unlikely(&enable_evmcs))
1909 return evmcs_write16(field, value);
1910
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001911 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001912}
1913
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001914static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001915{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001916 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001917 if (static_branch_unlikely(&enable_evmcs))
1918 return evmcs_write32(field, value);
1919
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001920 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001921}
1922
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001923static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001924{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001925 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001926 if (static_branch_unlikely(&enable_evmcs))
1927 return evmcs_write64(field, value);
1928
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001929 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03001930#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001931 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001932 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001933#endif
1934}
1935
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001936static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001937{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001938 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001939 if (static_branch_unlikely(&enable_evmcs))
1940 return evmcs_write64(field, value);
1941
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001942 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001943}
1944
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001945static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001946{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001947 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1948 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001949 if (static_branch_unlikely(&enable_evmcs))
1950 return evmcs_write32(field, evmcs_read32(field) & ~mask);
1951
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001952 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
1953}
1954
1955static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
1956{
1957 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1958 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001959 if (static_branch_unlikely(&enable_evmcs))
1960 return evmcs_write32(field, evmcs_read32(field) | mask);
1961
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001962 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001963}
1964
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001965static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
1966{
1967 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
1968}
1969
Gleb Natapov2961e8762013-11-25 15:37:13 +02001970static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1971{
1972 vmcs_write32(VM_ENTRY_CONTROLS, val);
1973 vmx->vm_entry_controls_shadow = val;
1974}
1975
1976static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1977{
1978 if (vmx->vm_entry_controls_shadow != val)
1979 vm_entry_controls_init(vmx, val);
1980}
1981
1982static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1983{
1984 return vmx->vm_entry_controls_shadow;
1985}
1986
1987
1988static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1989{
1990 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
1991}
1992
1993static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1994{
1995 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
1996}
1997
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001998static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
1999{
2000 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2001}
2002
Gleb Natapov2961e8762013-11-25 15:37:13 +02002003static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2004{
2005 vmcs_write32(VM_EXIT_CONTROLS, val);
2006 vmx->vm_exit_controls_shadow = val;
2007}
2008
2009static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2010{
2011 if (vmx->vm_exit_controls_shadow != val)
2012 vm_exit_controls_init(vmx, val);
2013}
2014
2015static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2016{
2017 return vmx->vm_exit_controls_shadow;
2018}
2019
2020
2021static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2022{
2023 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2024}
2025
2026static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2027{
2028 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2029}
2030
Avi Kivity2fb92db2011-04-27 19:42:18 +03002031static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2032{
2033 vmx->segment_cache.bitmask = 0;
2034}
2035
2036static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2037 unsigned field)
2038{
2039 bool ret;
2040 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2041
2042 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2043 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2044 vmx->segment_cache.bitmask = 0;
2045 }
2046 ret = vmx->segment_cache.bitmask & mask;
2047 vmx->segment_cache.bitmask |= mask;
2048 return ret;
2049}
2050
2051static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2052{
2053 u16 *p = &vmx->segment_cache.seg[seg].selector;
2054
2055 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2056 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2057 return *p;
2058}
2059
2060static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2061{
2062 ulong *p = &vmx->segment_cache.seg[seg].base;
2063
2064 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2065 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2066 return *p;
2067}
2068
2069static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2070{
2071 u32 *p = &vmx->segment_cache.seg[seg].limit;
2072
2073 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2074 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2075 return *p;
2076}
2077
2078static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2079{
2080 u32 *p = &vmx->segment_cache.seg[seg].ar;
2081
2082 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2083 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2084 return *p;
2085}
2086
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002087static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2088{
2089 u32 eb;
2090
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002091 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002092 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002093 /*
2094 * Guest access to VMware backdoor ports could legitimately
2095 * trigger #GP because of TSS I/O permission bitmap.
2096 * We intercept those #GP and allow access to them anyway
2097 * as VMware does.
2098 */
2099 if (enable_vmware_backdoor)
2100 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002101 if ((vcpu->guest_debug &
2102 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2103 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2104 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002105 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002106 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002107 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002108 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002109
2110 /* When we are running a nested L2 guest and L1 specified for it a
2111 * certain exception bitmap, we must trap the same exceptions and pass
2112 * them to L1. When running L2, we will only handle the exceptions
2113 * specified above if L1 did not want them.
2114 */
2115 if (is_guest_mode(vcpu))
2116 eb |= get_vmcs12(vcpu)->exception_bitmap;
2117
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002118 vmcs_write32(EXCEPTION_BITMAP, eb);
2119}
2120
Ashok Raj15d45072018-02-01 22:59:43 +01002121/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002122 * Check if MSR is intercepted for currently loaded MSR bitmap.
2123 */
2124static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2125{
2126 unsigned long *msr_bitmap;
2127 int f = sizeof(unsigned long);
2128
2129 if (!cpu_has_vmx_msr_bitmap())
2130 return true;
2131
2132 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2133
2134 if (msr <= 0x1fff) {
2135 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2136 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2137 msr &= 0x1fff;
2138 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2139 }
2140
2141 return true;
2142}
2143
2144/*
Ashok Raj15d45072018-02-01 22:59:43 +01002145 * Check if MSR is intercepted for L01 MSR bitmap.
2146 */
2147static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2148{
2149 unsigned long *msr_bitmap;
2150 int f = sizeof(unsigned long);
2151
2152 if (!cpu_has_vmx_msr_bitmap())
2153 return true;
2154
2155 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2156
2157 if (msr <= 0x1fff) {
2158 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2159 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2160 msr &= 0x1fff;
2161 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2162 }
2163
2164 return true;
2165}
2166
Gleb Natapov2961e8762013-11-25 15:37:13 +02002167static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2168 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002169{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002170 vm_entry_controls_clearbit(vmx, entry);
2171 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002172}
2173
Avi Kivity61d2ef22010-04-28 16:40:38 +03002174static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2175{
2176 unsigned i;
2177 struct msr_autoload *m = &vmx->msr_autoload;
2178
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002179 switch (msr) {
2180 case MSR_EFER:
2181 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002182 clear_atomic_switch_msr_special(vmx,
2183 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002184 VM_EXIT_LOAD_IA32_EFER);
2185 return;
2186 }
2187 break;
2188 case MSR_CORE_PERF_GLOBAL_CTRL:
2189 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002190 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002191 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2192 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2193 return;
2194 }
2195 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002196 }
2197
Avi Kivity61d2ef22010-04-28 16:40:38 +03002198 for (i = 0; i < m->nr; ++i)
2199 if (m->guest[i].index == msr)
2200 break;
2201
2202 if (i == m->nr)
2203 return;
2204 --m->nr;
2205 m->guest[i] = m->guest[m->nr];
2206 m->host[i] = m->host[m->nr];
2207 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2208 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2209}
2210
Gleb Natapov2961e8762013-11-25 15:37:13 +02002211static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2212 unsigned long entry, unsigned long exit,
2213 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2214 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002215{
2216 vmcs_write64(guest_val_vmcs, guest_val);
2217 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002218 vm_entry_controls_setbit(vmx, entry);
2219 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002220}
2221
Avi Kivity61d2ef22010-04-28 16:40:38 +03002222static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
2223 u64 guest_val, u64 host_val)
2224{
2225 unsigned i;
2226 struct msr_autoload *m = &vmx->msr_autoload;
2227
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002228 switch (msr) {
2229 case MSR_EFER:
2230 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002231 add_atomic_switch_msr_special(vmx,
2232 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002233 VM_EXIT_LOAD_IA32_EFER,
2234 GUEST_IA32_EFER,
2235 HOST_IA32_EFER,
2236 guest_val, host_val);
2237 return;
2238 }
2239 break;
2240 case MSR_CORE_PERF_GLOBAL_CTRL:
2241 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002242 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002243 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2244 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2245 GUEST_IA32_PERF_GLOBAL_CTRL,
2246 HOST_IA32_PERF_GLOBAL_CTRL,
2247 guest_val, host_val);
2248 return;
2249 }
2250 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002251 case MSR_IA32_PEBS_ENABLE:
2252 /* PEBS needs a quiescent period after being disabled (to write
2253 * a record). Disabling PEBS through VMX MSR swapping doesn't
2254 * provide that period, so a CPU could write host's record into
2255 * guest's memory.
2256 */
2257 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002258 }
2259
Avi Kivity61d2ef22010-04-28 16:40:38 +03002260 for (i = 0; i < m->nr; ++i)
2261 if (m->guest[i].index == msr)
2262 break;
2263
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002264 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002265 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002266 "Can't add msr %x\n", msr);
2267 return;
2268 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03002269 ++m->nr;
2270 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2271 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2272 }
2273
2274 m->guest[i].index = msr;
2275 m->guest[i].value = guest_val;
2276 m->host[i].index = msr;
2277 m->host[i].value = host_val;
2278}
2279
Avi Kivity92c0d902009-10-29 11:00:16 +02002280static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002281{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002282 u64 guest_efer = vmx->vcpu.arch.efer;
2283 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002284
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002285 if (!enable_ept) {
2286 /*
2287 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2288 * host CPUID is more efficient than testing guest CPUID
2289 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2290 */
2291 if (boot_cpu_has(X86_FEATURE_SMEP))
2292 guest_efer |= EFER_NX;
2293 else if (!(guest_efer & EFER_NX))
2294 ignore_bits |= EFER_NX;
2295 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002296
Avi Kivity51c6cf62007-08-29 03:48:05 +03002297 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002298 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002299 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002300 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002301#ifdef CONFIG_X86_64
2302 ignore_bits |= EFER_LMA | EFER_LME;
2303 /* SCE is meaningful only in long mode on Intel */
2304 if (guest_efer & EFER_LMA)
2305 ignore_bits &= ~(u64)EFER_SCE;
2306#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002307
2308 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002309
2310 /*
2311 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2312 * On CPUs that support "load IA32_EFER", always switch EFER
2313 * atomically, since it's faster than switching it manually.
2314 */
2315 if (cpu_has_load_ia32_efer ||
2316 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002317 if (!(guest_efer & EFER_LMA))
2318 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002319 if (guest_efer != host_efer)
2320 add_atomic_switch_msr(vmx, MSR_EFER,
2321 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002322 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002323 } else {
2324 guest_efer &= ~ignore_bits;
2325 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002326
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002327 vmx->guest_msrs[efer_offset].data = guest_efer;
2328 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2329
2330 return true;
2331 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002332}
2333
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002334#ifdef CONFIG_X86_32
2335/*
2336 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2337 * VMCS rather than the segment table. KVM uses this helper to figure
2338 * out the current bases to poke them into the VMCS before entry.
2339 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002340static unsigned long segment_base(u16 selector)
2341{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002342 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002343 unsigned long v;
2344
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002345 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002346 return 0;
2347
Thomas Garnier45fc8752017-03-14 10:05:08 -07002348 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002349
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002350 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002351 u16 ldt_selector = kvm_read_ldt();
2352
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002353 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002354 return 0;
2355
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002356 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002357 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002358 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002359 return v;
2360}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002361#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002362
Avi Kivity04d2cc72007-09-10 18:10:54 +03002363static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002364{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002365 struct vcpu_vmx *vmx = to_vmx(vcpu);
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002366#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002367 int cpu = raw_smp_processor_id();
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002368 unsigned long fs_base, kernel_gs_base;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002369#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03002370 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002371
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002372 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002373 return;
2374
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002375 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002376 /*
2377 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2378 * allow segment selectors with cpl > 0 or ti == 1.
2379 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002380 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002381 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002382
2383#ifdef CONFIG_X86_64
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002384 if (likely(is_64bit_mm(current->mm))) {
2385 save_fsgs_for_kvm();
2386 vmx->host_state.fs_sel = current->thread.fsindex;
2387 vmx->host_state.gs_sel = current->thread.gsindex;
2388 fs_base = current->thread.fsbase;
2389 kernel_gs_base = current->thread.gsbase;
2390 } else {
2391#endif
2392 savesegment(fs, vmx->host_state.fs_sel);
2393 savesegment(gs, vmx->host_state.gs_sel);
2394#ifdef CONFIG_X86_64
2395 fs_base = read_msr(MSR_FS_BASE);
2396 kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
2397 }
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002398#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002399 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002400 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002401 vmx->host_state.fs_reload_needed = 0;
2402 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002403 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002404 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002405 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002406 if (!(vmx->host_state.gs_sel & 7))
2407 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002408 else {
2409 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002410 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002411 }
2412
2413#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002414 savesegment(ds, vmx->host_state.ds_sel);
2415 savesegment(es, vmx->host_state.es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002416
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002417 vmcs_writel(HOST_FS_BASE, fs_base);
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002418 vmcs_writel(HOST_GS_BASE, cpu_kernelmode_gs_base(cpu));
Avi Kivity707c0872007-05-02 17:33:43 +03002419
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002420 vmx->msr_host_kernel_gs_base = kernel_gs_base;
Avi Kivityc8770e72010-11-11 12:37:26 +02002421 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002422 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002423#else
2424 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2425 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
2426#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002427 if (boot_cpu_has(X86_FEATURE_MPX))
2428 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002429 for (i = 0; i < vmx->save_nmsrs; ++i)
2430 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002431 vmx->guest_msrs[i].data,
2432 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002433}
2434
Avi Kivitya9b21b62008-06-24 11:48:49 +03002435static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002436{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002437 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002438 return;
2439
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002440 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002441 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002442#ifdef CONFIG_X86_64
2443 if (is_long_mode(&vmx->vcpu))
2444 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2445#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002446 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002447 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002448#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002449 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002450#else
2451 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002452#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002453 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002454 if (vmx->host_state.fs_reload_needed)
2455 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002456#ifdef CONFIG_X86_64
2457 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2458 loadsegment(ds, vmx->host_state.ds_sel);
2459 loadsegment(es, vmx->host_state.es_sel);
2460 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002461#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002462 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002463#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002464 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002465#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002466 if (vmx->host_state.msr_host_bndcfgs)
2467 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Thomas Garnier45fc8752017-03-14 10:05:08 -07002468 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002469}
2470
Avi Kivitya9b21b62008-06-24 11:48:49 +03002471static void vmx_load_host_state(struct vcpu_vmx *vmx)
2472{
2473 preempt_disable();
2474 __vmx_load_host_state(vmx);
2475 preempt_enable();
2476}
2477
Feng Wu28b835d2015-09-18 22:29:54 +08002478static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2479{
2480 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2481 struct pi_desc old, new;
2482 unsigned int dest;
2483
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002484 /*
2485 * In case of hot-plug or hot-unplug, we may have to undo
2486 * vmx_vcpu_pi_put even if there is no assigned device. And we
2487 * always keep PI.NDST up to date for simplicity: it makes the
2488 * code easier, and CPU migration is not a fast path.
2489 */
2490 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002491 return;
2492
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002493 /*
2494 * First handle the simple case where no cmpxchg is necessary; just
2495 * allow posting non-urgent interrupts.
2496 *
2497 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2498 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2499 * expects the VCPU to be on the blocked_vcpu_list that matches
2500 * PI.NDST.
2501 */
2502 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2503 vcpu->cpu == cpu) {
2504 pi_clear_sn(pi_desc);
2505 return;
2506 }
2507
2508 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002509 do {
2510 old.control = new.control = pi_desc->control;
2511
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002512 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002513
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002514 if (x2apic_enabled())
2515 new.ndst = dest;
2516 else
2517 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002518
Feng Wu28b835d2015-09-18 22:29:54 +08002519 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02002520 } while (cmpxchg64(&pi_desc->control, old.control,
2521 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002522}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002523
Peter Feinerc95ba922016-08-17 09:36:47 -07002524static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2525{
2526 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2527 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2528}
2529
Avi Kivity6aa8b732006-12-10 02:21:36 -08002530/*
2531 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2532 * vcpu mutex is already taken.
2533 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002534static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002535{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002536 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002537 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002538
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002539 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002540 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002541 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002542 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002543
2544 /*
2545 * Read loaded_vmcs->cpu should be before fetching
2546 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2547 * See the comments in __loaded_vmcs_clear().
2548 */
2549 smp_rmb();
2550
Nadav Har'Eld462b812011-05-24 15:26:10 +03002551 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2552 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002553 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002554 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002555 }
2556
2557 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2558 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2559 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01002560 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002561 }
2562
2563 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002564 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002565 unsigned long sysenter_esp;
2566
2567 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002568
Avi Kivity6aa8b732006-12-10 02:21:36 -08002569 /*
2570 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002571 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002572 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002573 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01002574 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002575 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002576
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002577 /*
2578 * VM exits change the host TR limit to 0x67 after a VM
2579 * exit. This is okay, since 0x67 covers everything except
2580 * the IO bitmap and have have code to handle the IO bitmap
2581 * being lost after a VM exit.
2582 */
2583 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2584
Avi Kivity6aa8b732006-12-10 02:21:36 -08002585 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2586 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002587
Nadav Har'Eld462b812011-05-24 15:26:10 +03002588 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002589 }
Feng Wu28b835d2015-09-18 22:29:54 +08002590
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002591 /* Setup TSC multiplier */
2592 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002593 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2594 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002595
Feng Wu28b835d2015-09-18 22:29:54 +08002596 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002597 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08002598 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08002599}
2600
2601static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2602{
2603 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2604
2605 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002606 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2607 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002608 return;
2609
2610 /* Set SN when the vCPU is preempted */
2611 if (vcpu->preempted)
2612 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002613}
2614
2615static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2616{
Feng Wu28b835d2015-09-18 22:29:54 +08002617 vmx_vcpu_pi_put(vcpu);
2618
Avi Kivitya9b21b62008-06-24 11:48:49 +03002619 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002620}
2621
Wanpeng Lif244dee2017-07-20 01:11:54 -07002622static bool emulation_required(struct kvm_vcpu *vcpu)
2623{
2624 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2625}
2626
Avi Kivityedcafe32009-12-30 18:07:40 +02002627static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2628
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002629/*
2630 * Return the cr0 value that a nested guest would read. This is a combination
2631 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2632 * its hypervisor (cr0_read_shadow).
2633 */
2634static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2635{
2636 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2637 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2638}
2639static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2640{
2641 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2642 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2643}
2644
Avi Kivity6aa8b732006-12-10 02:21:36 -08002645static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2646{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002647 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002648
Avi Kivity6de12732011-03-07 12:51:22 +02002649 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2650 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2651 rflags = vmcs_readl(GUEST_RFLAGS);
2652 if (to_vmx(vcpu)->rmode.vm86_active) {
2653 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2654 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2655 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2656 }
2657 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002658 }
Avi Kivity6de12732011-03-07 12:51:22 +02002659 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002660}
2661
2662static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2663{
Wanpeng Lif244dee2017-07-20 01:11:54 -07002664 unsigned long old_rflags = vmx_get_rflags(vcpu);
2665
Avi Kivity6de12732011-03-07 12:51:22 +02002666 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2667 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002668 if (to_vmx(vcpu)->rmode.vm86_active) {
2669 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002670 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002671 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002672 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07002673
2674 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2675 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002676}
2677
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002678static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002679{
2680 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2681 int ret = 0;
2682
2683 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002684 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002685 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002686 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002687
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002688 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002689}
2690
2691static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2692{
2693 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2694 u32 interruptibility = interruptibility_old;
2695
2696 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2697
Jan Kiszka48005f62010-02-19 19:38:07 +01002698 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002699 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002700 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002701 interruptibility |= GUEST_INTR_STATE_STI;
2702
2703 if ((interruptibility != interruptibility_old))
2704 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2705}
2706
Avi Kivity6aa8b732006-12-10 02:21:36 -08002707static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2708{
2709 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002710
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002711 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002712 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002713 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002714
Glauber Costa2809f5d2009-05-12 16:21:05 -04002715 /* skipping an emulated instruction also counts */
2716 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002717}
2718
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002719static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
2720 unsigned long exit_qual)
2721{
2722 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2723 unsigned int nr = vcpu->arch.exception.nr;
2724 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2725
2726 if (vcpu->arch.exception.has_error_code) {
2727 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
2728 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2729 }
2730
2731 if (kvm_exception_is_soft(nr))
2732 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2733 else
2734 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2735
2736 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
2737 vmx_get_nmi_mask(vcpu))
2738 intr_info |= INTR_INFO_UNBLOCK_NMI;
2739
2740 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
2741}
2742
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002743/*
2744 * KVM wants to inject page-faults which it got to the guest. This function
2745 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002746 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002747static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002748{
2749 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002750 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002751
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002752 if (nr == PF_VECTOR) {
2753 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002754 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002755 return 1;
2756 }
2757 /*
2758 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
2759 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2760 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
2761 * can be written only when inject_pending_event runs. This should be
2762 * conditional on a new capability---if the capability is disabled,
2763 * kvm_multiple_exception would write the ancillary information to
2764 * CR2 or DR6, for backwards ABI-compatibility.
2765 */
2766 if (nested_vmx_is_page_fault_vmexit(vmcs12,
2767 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002768 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002769 return 1;
2770 }
2771 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002772 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002773 if (nr == DB_VECTOR)
2774 *exit_qual = vcpu->arch.dr6;
2775 else
2776 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002777 return 1;
2778 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002779 }
2780
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002781 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002782}
2783
Wanpeng Licaa057a2018-03-12 04:53:03 -07002784static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
2785{
2786 /*
2787 * Ensure that we clear the HLT state in the VMCS. We don't need to
2788 * explicitly skip the instruction because if the HLT state is set,
2789 * then the instruction is already executing and RIP has already been
2790 * advanced.
2791 */
2792 if (kvm_hlt_in_guest(vcpu->kvm) &&
2793 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
2794 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
2795}
2796
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002797static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02002798{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002799 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002800 unsigned nr = vcpu->arch.exception.nr;
2801 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002802 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002803 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002804
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002805 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002806 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002807 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2808 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002809
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002810 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002811 int inc_eip = 0;
2812 if (kvm_exception_is_soft(nr))
2813 inc_eip = vcpu->arch.event_exit_inst_len;
2814 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002815 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002816 return;
2817 }
2818
Sean Christophersonadd5ff72018-03-23 09:34:00 -07002819 WARN_ON_ONCE(vmx->emulation_required);
2820
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002821 if (kvm_exception_is_soft(nr)) {
2822 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2823 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002824 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2825 } else
2826 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2827
2828 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07002829
2830 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02002831}
2832
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002833static bool vmx_rdtscp_supported(void)
2834{
2835 return cpu_has_vmx_rdtscp();
2836}
2837
Mao, Junjiead756a12012-07-02 01:18:48 +00002838static bool vmx_invpcid_supported(void)
2839{
2840 return cpu_has_vmx_invpcid() && enable_ept;
2841}
2842
Avi Kivity6aa8b732006-12-10 02:21:36 -08002843/*
Eddie Donga75beee2007-05-17 18:55:15 +03002844 * Swap MSR entry in host/guest MSR entry array.
2845 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002846static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002847{
Avi Kivity26bb0982009-09-07 11:14:12 +03002848 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002849
2850 tmp = vmx->guest_msrs[to];
2851 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2852 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002853}
2854
2855/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002856 * Set up the vmcs to automatically save and restore system
2857 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2858 * mode, as fiddling with msrs is very expensive.
2859 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002860static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002861{
Avi Kivity26bb0982009-09-07 11:14:12 +03002862 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002863
Eddie Donga75beee2007-05-17 18:55:15 +03002864 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002865#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002866 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002867 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002868 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002869 move_msr_up(vmx, index, save_nmsrs++);
2870 index = __find_msr_index(vmx, MSR_LSTAR);
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_CSTAR);
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++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002876 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02002877 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002878 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002879 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002880 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002881 * if efer.sce is enabled.
2882 */
Brian Gerst8c065852010-07-17 09:03:26 -04002883 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002884 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002885 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002886 }
Eddie Donga75beee2007-05-17 18:55:15 +03002887#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002888 index = __find_msr_index(vmx, MSR_EFER);
2889 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002890 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002891
Avi Kivity26bb0982009-09-07 11:14:12 +03002892 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002893
Yang Zhang8d146952013-01-25 10:18:50 +08002894 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002895 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002896}
2897
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02002898static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002899{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02002900 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002901
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02002902 if (is_guest_mode(vcpu) &&
2903 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
2904 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
2905
2906 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002907}
2908
2909/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002910 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002911 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002912static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002913{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002914 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002915 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002916 * We're here if L1 chose not to trap WRMSR to TSC. According
2917 * to the spec, this should set L1's TSC; The offset that L1
2918 * set for L2 remains unchanged, and still needs to be added
2919 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002920 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002921 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002922 /* recalculate vmcs02.TSC_OFFSET: */
2923 vmcs12 = get_vmcs12(vcpu);
2924 vmcs_write64(TSC_OFFSET, offset +
2925 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2926 vmcs12->tsc_offset : 0));
2927 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002928 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2929 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002930 vmcs_write64(TSC_OFFSET, offset);
2931 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002932}
2933
Nadav Har'El801d3422011-05-25 23:02:23 +03002934/*
2935 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2936 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2937 * all guests if the "nested" module option is off, and can also be disabled
2938 * for a single guest by disabling its VMX cpuid bit.
2939 */
2940static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2941{
Radim Krčmářd6321d42017-08-05 00:12:49 +02002942 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03002943}
2944
Avi Kivity6aa8b732006-12-10 02:21:36 -08002945/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002946 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2947 * returned for the various VMX controls MSRs when nested VMX is enabled.
2948 * The same values should also be used to verify that vmcs12 control fields are
2949 * valid during nested entry from L1 to L2.
2950 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2951 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2952 * bit in the high half is on if the corresponding bit in the control field
2953 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002954 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002955static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002956{
Paolo Bonzini13893092018-02-26 13:40:09 +01002957 if (!nested) {
2958 memset(msrs, 0, sizeof(*msrs));
2959 return;
2960 }
2961
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002962 /*
2963 * Note that as a general rule, the high half of the MSRs (bits in
2964 * the control fields which may be 1) should be initialized by the
2965 * intersection of the underlying hardware's MSR (i.e., features which
2966 * can be supported) and the list of features we want to expose -
2967 * because they are known to be properly supported in our code.
2968 * Also, usually, the low half of the MSRs (bits which must be 1) can
2969 * be set to 0, meaning that L1 may turn off any of these bits. The
2970 * reason is that if one of these bits is necessary, it will appear
2971 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2972 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02002973 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002974 * These rules have exceptions below.
2975 */
2976
2977 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002978 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002979 msrs->pinbased_ctls_low,
2980 msrs->pinbased_ctls_high);
2981 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002982 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002983 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002984 PIN_BASED_EXT_INTR_MASK |
2985 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01002986 PIN_BASED_VIRTUAL_NMIS |
2987 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002988 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002989 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002990 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002991
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002992 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002993 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002994 msrs->exit_ctls_low,
2995 msrs->exit_ctls_high);
2996 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08002997 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04002998
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002999 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003000#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003001 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003002#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01003003 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003004 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003005 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003006 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003007 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3008
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003009 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003010 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003011
Jan Kiszka2996fca2014-06-16 13:59:43 +02003012 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003013 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003014
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003015 /* entry controls */
3016 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003017 msrs->entry_ctls_low,
3018 msrs->entry_ctls_high);
3019 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003020 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003021 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003022#ifdef CONFIG_X86_64
3023 VM_ENTRY_IA32E_MODE |
3024#endif
3025 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003026 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003027 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003028 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003029 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02003030
Jan Kiszka2996fca2014-06-16 13:59:43 +02003031 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003032 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003033
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003034 /* cpu-based controls */
3035 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003036 msrs->procbased_ctls_low,
3037 msrs->procbased_ctls_high);
3038 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003039 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003040 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003041 CPU_BASED_VIRTUAL_INTR_PENDING |
3042 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003043 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3044 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3045 CPU_BASED_CR3_STORE_EXITING |
3046#ifdef CONFIG_X86_64
3047 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3048#endif
3049 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003050 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3051 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3052 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3053 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003054 /*
3055 * We can allow some features even when not supported by the
3056 * hardware. For example, L1 can specify an MSR bitmap - and we
3057 * can use it to avoid exits to L1 - even when L0 runs L2
3058 * without MSR bitmaps.
3059 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003060 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003061 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003062 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003063
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003064 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003065 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003066 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3067
Paolo Bonzini80154d72017-08-24 13:55:35 +02003068 /*
3069 * secondary cpu-based controls. Do not include those that
3070 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3071 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003072 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003073 msrs->secondary_ctls_low,
3074 msrs->secondary_ctls_high);
3075 msrs->secondary_ctls_low = 0;
3076 msrs->secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01003077 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02003078 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003079 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003080 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003081 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003082 SECONDARY_EXEC_WBINVD_EXITING;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003083
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003084 if (enable_ept) {
3085 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003086 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003087 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003088 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003089 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003090 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003091 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003092 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003093 msrs->ept_caps &= vmx_capability.ept;
3094 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003095 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3096 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003097 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003098 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003099 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003100 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003101 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003102 }
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003103
Bandan Das27c42a12017-08-03 15:54:42 -04003104 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003105 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003106 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003107 /*
3108 * Advertise EPTP switching unconditionally
3109 * since we emulate it
3110 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003111 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003112 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003113 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003114 }
3115
Paolo Bonzinief697a72016-03-18 16:58:38 +01003116 /*
3117 * Old versions of KVM use the single-context version without
3118 * checking for support, so declare that it is supported even
3119 * though it is treated as global context. The alternative is
3120 * not failing the single-context invvpid, and it is worse.
3121 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003122 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003123 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003124 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003125 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003126 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003127 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003128
Radim Krčmář0790ec12015-03-17 14:02:32 +01003129 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003130 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003131 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3132
Jan Kiszkac18911a2013-03-13 16:06:41 +01003133 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003134 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003135 msrs->misc_low,
3136 msrs->misc_high);
3137 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3138 msrs->misc_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003139 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003140 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003141 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003142
3143 /*
3144 * This MSR reports some information about VMX support. We
3145 * should return information about the VMX we emulate for the
3146 * guest, and the VMCS structure we give it - not about the
3147 * VMX support of the underlying hardware.
3148 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003149 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003150 VMCS12_REVISION |
3151 VMX_BASIC_TRUE_CTLS |
3152 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3153 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3154
3155 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003156 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003157
3158 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003159 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003160 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3161 * We picked the standard core2 setting.
3162 */
3163#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3164#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003165 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3166 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003167
3168 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003169 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3170 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003171
3172 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003173 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003174}
3175
David Matlack38991522016-11-29 18:14:08 -08003176/*
3177 * if fixed0[i] == 1: val[i] must be 1
3178 * if fixed1[i] == 0: val[i] must be 0
3179 */
3180static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3181{
3182 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003183}
3184
3185static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3186{
David Matlack38991522016-11-29 18:14:08 -08003187 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003188}
3189
3190static inline u64 vmx_control_msr(u32 low, u32 high)
3191{
3192 return low | ((u64)high << 32);
3193}
3194
David Matlack62cc6b9d2016-11-29 18:14:07 -08003195static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3196{
3197 superset &= mask;
3198 subset &= mask;
3199
3200 return (superset | subset) == superset;
3201}
3202
3203static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3204{
3205 const u64 feature_and_reserved =
3206 /* feature (except bit 48; see below) */
3207 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3208 /* reserved */
3209 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003210 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003211
3212 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3213 return -EINVAL;
3214
3215 /*
3216 * KVM does not emulate a version of VMX that constrains physical
3217 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3218 */
3219 if (data & BIT_ULL(48))
3220 return -EINVAL;
3221
3222 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3223 vmx_basic_vmcs_revision_id(data))
3224 return -EINVAL;
3225
3226 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3227 return -EINVAL;
3228
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003229 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003230 return 0;
3231}
3232
3233static int
3234vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3235{
3236 u64 supported;
3237 u32 *lowp, *highp;
3238
3239 switch (msr_index) {
3240 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003241 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3242 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003243 break;
3244 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003245 lowp = &vmx->nested.msrs.procbased_ctls_low;
3246 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003247 break;
3248 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003249 lowp = &vmx->nested.msrs.exit_ctls_low;
3250 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003251 break;
3252 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003253 lowp = &vmx->nested.msrs.entry_ctls_low;
3254 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003255 break;
3256 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003257 lowp = &vmx->nested.msrs.secondary_ctls_low;
3258 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003259 break;
3260 default:
3261 BUG();
3262 }
3263
3264 supported = vmx_control_msr(*lowp, *highp);
3265
3266 /* Check must-be-1 bits are still 1. */
3267 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3268 return -EINVAL;
3269
3270 /* Check must-be-0 bits are still 0. */
3271 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3272 return -EINVAL;
3273
3274 *lowp = data;
3275 *highp = data >> 32;
3276 return 0;
3277}
3278
3279static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3280{
3281 const u64 feature_and_reserved_bits =
3282 /* feature */
3283 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3284 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3285 /* reserved */
3286 GENMASK_ULL(13, 9) | BIT_ULL(31);
3287 u64 vmx_misc;
3288
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003289 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3290 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003291
3292 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3293 return -EINVAL;
3294
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003295 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003296 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3297 vmx_misc_preemption_timer_rate(data) !=
3298 vmx_misc_preemption_timer_rate(vmx_misc))
3299 return -EINVAL;
3300
3301 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3302 return -EINVAL;
3303
3304 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3305 return -EINVAL;
3306
3307 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3308 return -EINVAL;
3309
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003310 vmx->nested.msrs.misc_low = data;
3311 vmx->nested.msrs.misc_high = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003312 return 0;
3313}
3314
3315static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3316{
3317 u64 vmx_ept_vpid_cap;
3318
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003319 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3320 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003321
3322 /* Every bit is either reserved or a feature bit. */
3323 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3324 return -EINVAL;
3325
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003326 vmx->nested.msrs.ept_caps = data;
3327 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003328 return 0;
3329}
3330
3331static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3332{
3333 u64 *msr;
3334
3335 switch (msr_index) {
3336 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003337 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003338 break;
3339 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003340 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003341 break;
3342 default:
3343 BUG();
3344 }
3345
3346 /*
3347 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3348 * must be 1 in the restored value.
3349 */
3350 if (!is_bitwise_subset(data, *msr, -1ULL))
3351 return -EINVAL;
3352
3353 *msr = data;
3354 return 0;
3355}
3356
3357/*
3358 * Called when userspace is restoring VMX MSRs.
3359 *
3360 * Returns 0 on success, non-0 otherwise.
3361 */
3362static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3363{
3364 struct vcpu_vmx *vmx = to_vmx(vcpu);
3365
3366 switch (msr_index) {
3367 case MSR_IA32_VMX_BASIC:
3368 return vmx_restore_vmx_basic(vmx, data);
3369 case MSR_IA32_VMX_PINBASED_CTLS:
3370 case MSR_IA32_VMX_PROCBASED_CTLS:
3371 case MSR_IA32_VMX_EXIT_CTLS:
3372 case MSR_IA32_VMX_ENTRY_CTLS:
3373 /*
3374 * The "non-true" VMX capability MSRs are generated from the
3375 * "true" MSRs, so we do not support restoring them directly.
3376 *
3377 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3378 * should restore the "true" MSRs with the must-be-1 bits
3379 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3380 * DEFAULT SETTINGS".
3381 */
3382 return -EINVAL;
3383 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3384 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3385 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3386 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3387 case MSR_IA32_VMX_PROCBASED_CTLS2:
3388 return vmx_restore_control_msr(vmx, msr_index, data);
3389 case MSR_IA32_VMX_MISC:
3390 return vmx_restore_vmx_misc(vmx, data);
3391 case MSR_IA32_VMX_CR0_FIXED0:
3392 case MSR_IA32_VMX_CR4_FIXED0:
3393 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3394 case MSR_IA32_VMX_CR0_FIXED1:
3395 case MSR_IA32_VMX_CR4_FIXED1:
3396 /*
3397 * These MSRs are generated based on the vCPU's CPUID, so we
3398 * do not support restoring them directly.
3399 */
3400 return -EINVAL;
3401 case MSR_IA32_VMX_EPT_VPID_CAP:
3402 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3403 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003404 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003405 return 0;
3406 default:
3407 /*
3408 * The rest of the VMX capability MSRs do not support restore.
3409 */
3410 return -EINVAL;
3411 }
3412}
3413
Jan Kiszkacae50132014-01-04 18:47:22 +01003414/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003415static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003416{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003417 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003418 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003419 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003420 break;
3421 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3422 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003423 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003424 msrs->pinbased_ctls_low,
3425 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003426 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3427 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003428 break;
3429 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3430 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003431 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003432 msrs->procbased_ctls_low,
3433 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003434 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3435 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003436 break;
3437 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3438 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003439 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003440 msrs->exit_ctls_low,
3441 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003442 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3443 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003444 break;
3445 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3446 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003447 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003448 msrs->entry_ctls_low,
3449 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003450 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3451 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003452 break;
3453 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003454 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003455 msrs->misc_low,
3456 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003457 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003458 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003459 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003460 break;
3461 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003462 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003463 break;
3464 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003465 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003466 break;
3467 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003468 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003469 break;
3470 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003471 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003472 break;
3473 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003474 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003475 msrs->secondary_ctls_low,
3476 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003477 break;
3478 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003479 *pdata = msrs->ept_caps |
3480 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003481 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003482 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003483 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04003484 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003485 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003486 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003487 }
3488
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003489 return 0;
3490}
3491
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003492static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3493 uint64_t val)
3494{
3495 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3496
3497 return !(val & ~valid_bits);
3498}
3499
Tom Lendacky801e4592018-02-21 13:39:51 -06003500static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3501{
Paolo Bonzini13893092018-02-26 13:40:09 +01003502 switch (msr->index) {
3503 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3504 if (!nested)
3505 return 1;
3506 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
3507 default:
3508 return 1;
3509 }
3510
3511 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06003512}
3513
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003514/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003515 * Reads an msr value (of 'msr_index') into 'pdata'.
3516 * Returns 0 on success, non-0 otherwise.
3517 * Assumes vcpu_load() was already called.
3518 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003519static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003520{
Borislav Petkova6cb0992017-12-20 12:50:28 +01003521 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003522 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003523
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003524 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003525#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003526 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003527 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003528 break;
3529 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003530 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003531 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003532 case MSR_KERNEL_GS_BASE:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003533 vmx_load_host_state(vmx);
3534 msr_info->data = vmx->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003535 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003536#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003537 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003538 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003539 case MSR_IA32_SPEC_CTRL:
3540 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003541 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3542 return 1;
3543
3544 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3545 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003546 case MSR_IA32_ARCH_CAPABILITIES:
3547 if (!msr_info->host_initiated &&
3548 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3549 return 1;
3550 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3551 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003552 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003553 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003554 break;
3555 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003556 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003557 break;
3558 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003559 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003560 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003561 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003562 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003563 (!msr_info->host_initiated &&
3564 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003565 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003566 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003567 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003568 case MSR_IA32_MCG_EXT_CTL:
3569 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01003570 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08003571 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003572 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003573 msr_info->data = vcpu->arch.mcg_ext_ctl;
3574 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003575 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003576 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003577 break;
3578 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3579 if (!nested_vmx_allowed(vcpu))
3580 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003581 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
3582 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003583 case MSR_IA32_XSS:
3584 if (!vmx_xsaves_supported())
3585 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003586 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003587 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003588 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003589 if (!msr_info->host_initiated &&
3590 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003591 return 1;
3592 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003593 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003594 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003595 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003596 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003597 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003598 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003599 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003600 }
3601
Avi Kivity6aa8b732006-12-10 02:21:36 -08003602 return 0;
3603}
3604
Jan Kiszkacae50132014-01-04 18:47:22 +01003605static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3606
Avi Kivity6aa8b732006-12-10 02:21:36 -08003607/*
3608 * Writes msr value into into the appropriate "register".
3609 * Returns 0 on success, non-0 otherwise.
3610 * Assumes vcpu_load() was already called.
3611 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003612static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003613{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003614 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003615 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003616 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003617 u32 msr_index = msr_info->index;
3618 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003619
Avi Kivity6aa8b732006-12-10 02:21:36 -08003620 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003621 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003622 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003623 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003624#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003625 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003626 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003627 vmcs_writel(GUEST_FS_BASE, data);
3628 break;
3629 case MSR_GS_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_GS_BASE, data);
3632 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003633 case MSR_KERNEL_GS_BASE:
3634 vmx_load_host_state(vmx);
3635 vmx->msr_guest_kernel_gs_base = data;
3636 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003637#endif
3638 case MSR_IA32_SYSENTER_CS:
3639 vmcs_write32(GUEST_SYSENTER_CS, data);
3640 break;
3641 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003642 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003643 break;
3644 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003645 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003646 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003647 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003648 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003649 (!msr_info->host_initiated &&
3650 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003651 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08003652 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07003653 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003654 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003655 vmcs_write64(GUEST_BNDCFGS, data);
3656 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003657 case MSR_IA32_SPEC_CTRL:
3658 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003659 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3660 return 1;
3661
3662 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02003663 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003664 return 1;
3665
3666 vmx->spec_ctrl = data;
3667
3668 if (!data)
3669 break;
3670
3671 /*
3672 * For non-nested:
3673 * When it's written (to non-zero) for the first time, pass
3674 * it through.
3675 *
3676 * For nested:
3677 * The handling of the MSR bitmap for L2 guests is done in
3678 * nested_vmx_merge_msr_bitmap. We should not touch the
3679 * vmcs02.msr_bitmap here since it gets completely overwritten
3680 * in the merging. We update the vmcs01 here for L1 as well
3681 * since it will end up touching the MSR anyway now.
3682 */
3683 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3684 MSR_IA32_SPEC_CTRL,
3685 MSR_TYPE_RW);
3686 break;
Ashok Raj15d45072018-02-01 22:59:43 +01003687 case MSR_IA32_PRED_CMD:
3688 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01003689 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3690 return 1;
3691
3692 if (data & ~PRED_CMD_IBPB)
3693 return 1;
3694
3695 if (!data)
3696 break;
3697
3698 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3699
3700 /*
3701 * For non-nested:
3702 * When it's written (to non-zero) for the first time, pass
3703 * it through.
3704 *
3705 * For nested:
3706 * The handling of the MSR bitmap for L2 guests is done in
3707 * nested_vmx_merge_msr_bitmap. We should not touch the
3708 * vmcs02.msr_bitmap here since it gets completely overwritten
3709 * in the merging.
3710 */
3711 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3712 MSR_TYPE_W);
3713 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003714 case MSR_IA32_ARCH_CAPABILITIES:
3715 if (!msr_info->host_initiated)
3716 return 1;
3717 vmx->arch_capabilities = data;
3718 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003719 case MSR_IA32_CR_PAT:
3720 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003721 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3722 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003723 vmcs_write64(GUEST_IA32_PAT, data);
3724 vcpu->arch.pat = data;
3725 break;
3726 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003727 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003728 break;
Will Auldba904632012-11-29 12:42:50 -08003729 case MSR_IA32_TSC_ADJUST:
3730 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003731 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003732 case MSR_IA32_MCG_EXT_CTL:
3733 if ((!msr_info->host_initiated &&
3734 !(to_vmx(vcpu)->msr_ia32_feature_control &
3735 FEATURE_CONTROL_LMCE)) ||
3736 (data & ~MCG_EXT_CTL_LMCE_EN))
3737 return 1;
3738 vcpu->arch.mcg_ext_ctl = data;
3739 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003740 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003741 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003742 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003743 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3744 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003745 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003746 if (msr_info->host_initiated && data == 0)
3747 vmx_leave_nested(vcpu);
3748 break;
3749 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003750 if (!msr_info->host_initiated)
3751 return 1; /* they are read-only */
3752 if (!nested_vmx_allowed(vcpu))
3753 return 1;
3754 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08003755 case MSR_IA32_XSS:
3756 if (!vmx_xsaves_supported())
3757 return 1;
3758 /*
3759 * The only supported bit as of Skylake is bit 8, but
3760 * it is not supported on KVM.
3761 */
3762 if (data != 0)
3763 return 1;
3764 vcpu->arch.ia32_xss = data;
3765 if (vcpu->arch.ia32_xss != host_xss)
3766 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
3767 vcpu->arch.ia32_xss, host_xss);
3768 else
3769 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3770 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003771 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003772 if (!msr_info->host_initiated &&
3773 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003774 return 1;
3775 /* Check reserved bit, higher 32 bits should be zero */
3776 if ((data >> 32) != 0)
3777 return 1;
3778 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003779 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10003780 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003781 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07003782 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003783 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003784 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3785 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003786 ret = kvm_set_shared_msr(msr->index, msr->data,
3787 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03003788 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003789 if (ret)
3790 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003791 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08003792 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003793 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003794 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003795 }
3796
Eddie Dong2cc51562007-05-21 07:28:09 +03003797 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003798}
3799
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003800static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003801{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003802 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3803 switch (reg) {
3804 case VCPU_REGS_RSP:
3805 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3806 break;
3807 case VCPU_REGS_RIP:
3808 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3809 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003810 case VCPU_EXREG_PDPTR:
3811 if (enable_ept)
3812 ept_save_pdptrs(vcpu);
3813 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003814 default:
3815 break;
3816 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003817}
3818
Avi Kivity6aa8b732006-12-10 02:21:36 -08003819static __init int cpu_has_kvm_support(void)
3820{
Eduardo Habkost6210e372008-11-17 19:03:16 -02003821 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003822}
3823
3824static __init int vmx_disabled_by_bios(void)
3825{
3826 u64 msr;
3827
3828 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04003829 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08003830 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04003831 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3832 && tboot_enabled())
3833 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08003834 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04003835 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08003836 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08003837 && !tboot_enabled()) {
3838 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08003839 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04003840 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08003841 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08003842 /* launched w/o TXT and VMX disabled */
3843 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3844 && !tboot_enabled())
3845 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04003846 }
3847
3848 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003849}
3850
Dongxiao Xu7725b892010-05-11 18:29:38 +08003851static void kvm_cpu_vmxon(u64 addr)
3852{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003853 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003854 intel_pt_handle_vmx(1);
3855
Dongxiao Xu7725b892010-05-11 18:29:38 +08003856 asm volatile (ASM_VMX_VMXON_RAX
3857 : : "a"(&addr), "m"(addr)
3858 : "memory", "cc");
3859}
3860
Radim Krčmář13a34e02014-08-28 15:13:03 +02003861static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003862{
3863 int cpu = raw_smp_processor_id();
3864 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04003865 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003866
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003867 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02003868 return -EBUSY;
3869
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01003870 /*
3871 * This can happen if we hot-added a CPU but failed to allocate
3872 * VP assist page for it.
3873 */
3874 if (static_branch_unlikely(&enable_evmcs) &&
3875 !hv_get_vp_assist_page(cpu))
3876 return -EFAULT;
3877
Nadav Har'Eld462b812011-05-24 15:26:10 +03003878 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08003879 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3880 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003881
3882 /*
3883 * Now we can enable the vmclear operation in kdump
3884 * since the loaded_vmcss_on_cpu list on this cpu
3885 * has been initialized.
3886 *
3887 * Though the cpu is not in VMX operation now, there
3888 * is no problem to enable the vmclear operation
3889 * for the loaded_vmcss_on_cpu list is empty!
3890 */
3891 crash_enable_local_vmclear(cpu);
3892
Avi Kivity6aa8b732006-12-10 02:21:36 -08003893 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04003894
3895 test_bits = FEATURE_CONTROL_LOCKED;
3896 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3897 if (tboot_enabled())
3898 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3899
3900 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003901 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04003902 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3903 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003904 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02003905 if (enable_ept)
3906 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02003907
3908 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003909}
3910
Nadav Har'Eld462b812011-05-24 15:26:10 +03003911static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03003912{
3913 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03003914 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03003915
Nadav Har'Eld462b812011-05-24 15:26:10 +03003916 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3917 loaded_vmcss_on_cpu_link)
3918 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03003919}
3920
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003921
3922/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3923 * tricks.
3924 */
3925static void kvm_cpu_vmxoff(void)
3926{
3927 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003928
3929 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003930 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003931}
3932
Radim Krčmář13a34e02014-08-28 15:13:03 +02003933static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003934{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003935 vmclear_local_loaded_vmcss();
3936 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003937}
3938
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003939static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04003940 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003941{
3942 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003943 u32 ctl = ctl_min | ctl_opt;
3944
3945 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3946
3947 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3948 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3949
3950 /* Ensure minimum (required) set of control bits are supported. */
3951 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003952 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003953
3954 *result = ctl;
3955 return 0;
3956}
3957
Avi Kivity110312c2010-12-21 12:54:20 +02003958static __init bool allow_1_setting(u32 msr, u32 ctl)
3959{
3960 u32 vmx_msr_low, vmx_msr_high;
3961
3962 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3963 return vmx_msr_high & ctl;
3964}
3965
Yang, Sheng002c7f72007-07-31 14:23:01 +03003966static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003967{
3968 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08003969 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003970 u32 _pin_based_exec_control = 0;
3971 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003972 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003973 u32 _vmexit_control = 0;
3974 u32 _vmentry_control = 0;
3975
Paolo Bonzini13893092018-02-26 13:40:09 +01003976 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05303977 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003978#ifdef CONFIG_X86_64
3979 CPU_BASED_CR8_LOAD_EXITING |
3980 CPU_BASED_CR8_STORE_EXITING |
3981#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08003982 CPU_BASED_CR3_LOAD_EXITING |
3983 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e22017-12-12 16:44:21 +08003984 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003985 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03003986 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003987 CPU_BASED_MWAIT_EXITING |
3988 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02003989 CPU_BASED_INVLPG_EXITING |
3990 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06003991
Sheng Yangf78e0e22007-10-29 09:40:42 +08003992 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08003993 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08003994 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003995 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3996 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003997 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003998#ifdef CONFIG_X86_64
3999 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4000 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4001 ~CPU_BASED_CR8_STORE_EXITING;
4002#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004003 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004004 min2 = 0;
4005 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004006 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004007 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004008 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004009 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004010 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004011 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004012 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004013 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004014 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004015 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004016 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004017 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004018 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004019 SECONDARY_EXEC_RDSEED_EXITING |
4020 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004021 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004022 SECONDARY_EXEC_TSC_SCALING |
4023 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08004024 if (adjust_vmx_controls(min2, opt2,
4025 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004026 &_cpu_based_2nd_exec_control) < 0)
4027 return -EIO;
4028 }
4029#ifndef CONFIG_X86_64
4030 if (!(_cpu_based_2nd_exec_control &
4031 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4032 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4033#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004034
4035 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4036 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004037 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004038 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4039 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004040
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004041 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4042 &vmx_capability.ept, &vmx_capability.vpid);
4043
Sheng Yangd56f5462008-04-25 10:13:16 +08004044 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004045 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4046 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004047 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4048 CPU_BASED_CR3_STORE_EXITING |
4049 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004050 } else if (vmx_capability.ept) {
4051 vmx_capability.ept = 0;
4052 pr_warn_once("EPT CAP should not exist if not support "
4053 "1-setting enable EPT VM-execution control\n");
4054 }
4055 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4056 vmx_capability.vpid) {
4057 vmx_capability.vpid = 0;
4058 pr_warn_once("VPID CAP should not exist if not support "
4059 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004060 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004061
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004062 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004063#ifdef CONFIG_X86_64
4064 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4065#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004066 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004067 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004068 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4069 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004070 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004071
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004072 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4073 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4074 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004075 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4076 &_pin_based_exec_control) < 0)
4077 return -EIO;
4078
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004079 if (cpu_has_broken_vmx_preemption_timer())
4080 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004081 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004082 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004083 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4084
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004085 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004086 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004087 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4088 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004089 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004090
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004091 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004092
4093 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4094 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004095 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004096
4097#ifdef CONFIG_X86_64
4098 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4099 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004100 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004101#endif
4102
4103 /* Require Write-Back (WB) memory type for VMCS accesses. */
4104 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004105 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004106
Yang, Sheng002c7f72007-07-31 14:23:01 +03004107 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004108 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004109 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004110
4111 /* KVM supports Enlightened VMCS v1 only */
4112 if (static_branch_unlikely(&enable_evmcs))
4113 vmcs_conf->revision_id = KVM_EVMCS_VERSION;
4114 else
4115 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004116
Yang, Sheng002c7f72007-07-31 14:23:01 +03004117 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4118 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004119 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004120 vmcs_conf->vmexit_ctrl = _vmexit_control;
4121 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004122
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004123 if (static_branch_unlikely(&enable_evmcs))
4124 evmcs_sanitize_exec_ctrls(vmcs_conf);
4125
Avi Kivity110312c2010-12-21 12:54:20 +02004126 cpu_has_load_ia32_efer =
4127 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4128 VM_ENTRY_LOAD_IA32_EFER)
4129 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4130 VM_EXIT_LOAD_IA32_EFER);
4131
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004132 cpu_has_load_perf_global_ctrl =
4133 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4134 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4135 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4136 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4137
4138 /*
4139 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004140 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004141 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4142 *
4143 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4144 *
4145 * AAK155 (model 26)
4146 * AAP115 (model 30)
4147 * AAT100 (model 37)
4148 * BC86,AAY89,BD102 (model 44)
4149 * BA97 (model 46)
4150 *
4151 */
4152 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4153 switch (boot_cpu_data.x86_model) {
4154 case 26:
4155 case 30:
4156 case 37:
4157 case 44:
4158 case 46:
4159 cpu_has_load_perf_global_ctrl = false;
4160 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4161 "does not work properly. Using workaround\n");
4162 break;
4163 default:
4164 break;
4165 }
4166 }
4167
Borislav Petkov782511b2016-04-04 22:25:03 +02004168 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004169 rdmsrl(MSR_IA32_XSS, host_xss);
4170
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004171 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004172}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004173
4174static struct vmcs *alloc_vmcs_cpu(int cpu)
4175{
4176 int node = cpu_to_node(cpu);
4177 struct page *pages;
4178 struct vmcs *vmcs;
4179
Vlastimil Babka96db8002015-09-08 15:03:50 -07004180 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004181 if (!pages)
4182 return NULL;
4183 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004184 memset(vmcs, 0, vmcs_config.size);
4185 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004186 return vmcs;
4187}
4188
Avi Kivity6aa8b732006-12-10 02:21:36 -08004189static void free_vmcs(struct vmcs *vmcs)
4190{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004191 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004192}
4193
Nadav Har'Eld462b812011-05-24 15:26:10 +03004194/*
4195 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4196 */
4197static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4198{
4199 if (!loaded_vmcs->vmcs)
4200 return;
4201 loaded_vmcs_clear(loaded_vmcs);
4202 free_vmcs(loaded_vmcs->vmcs);
4203 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004204 if (loaded_vmcs->msr_bitmap)
4205 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004206 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004207}
4208
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004209static struct vmcs *alloc_vmcs(void)
4210{
4211 return alloc_vmcs_cpu(raw_smp_processor_id());
4212}
4213
4214static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4215{
4216 loaded_vmcs->vmcs = alloc_vmcs();
4217 if (!loaded_vmcs->vmcs)
4218 return -ENOMEM;
4219
4220 loaded_vmcs->shadow_vmcs = NULL;
4221 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004222
4223 if (cpu_has_vmx_msr_bitmap()) {
4224 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4225 if (!loaded_vmcs->msr_bitmap)
4226 goto out_vmcs;
4227 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
4228 }
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004229 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004230
4231out_vmcs:
4232 free_loaded_vmcs(loaded_vmcs);
4233 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004234}
4235
Sam Ravnborg39959582007-06-01 00:47:13 -07004236static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004237{
4238 int cpu;
4239
Zachary Amsden3230bb42009-09-29 11:38:37 -10004240 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004241 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004242 per_cpu(vmxarea, cpu) = NULL;
4243 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004244}
4245
Jim Mattsond37f4262017-12-22 12:12:16 -08004246enum vmcs_field_width {
4247 VMCS_FIELD_WIDTH_U16 = 0,
4248 VMCS_FIELD_WIDTH_U64 = 1,
4249 VMCS_FIELD_WIDTH_U32 = 2,
4250 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004251};
4252
Jim Mattsond37f4262017-12-22 12:12:16 -08004253static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004254{
4255 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004256 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004257 return (field >> 13) & 0x3 ;
4258}
4259
4260static inline int vmcs_field_readonly(unsigned long field)
4261{
4262 return (((field >> 10) & 0x3) == 1);
4263}
4264
Bandan Dasfe2b2012014-04-21 15:20:14 -04004265static void init_vmcs_shadow_fields(void)
4266{
4267 int i, j;
4268
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004269 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4270 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004271 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004272 (i + 1 == max_shadow_read_only_fields ||
4273 shadow_read_only_fields[i + 1] != field + 1))
4274 pr_err("Missing field from shadow_read_only_field %x\n",
4275 field + 1);
4276
4277 clear_bit(field, vmx_vmread_bitmap);
4278#ifdef CONFIG_X86_64
4279 if (field & 1)
4280 continue;
4281#endif
4282 if (j < i)
4283 shadow_read_only_fields[j] = field;
4284 j++;
4285 }
4286 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004287
4288 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004289 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004290 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004291 (i + 1 == max_shadow_read_write_fields ||
4292 shadow_read_write_fields[i + 1] != field + 1))
4293 pr_err("Missing field from shadow_read_write_field %x\n",
4294 field + 1);
4295
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004296 /*
4297 * PML and the preemption timer can be emulated, but the
4298 * processor cannot vmwrite to fields that don't exist
4299 * on bare metal.
4300 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004301 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004302 case GUEST_PML_INDEX:
4303 if (!cpu_has_vmx_pml())
4304 continue;
4305 break;
4306 case VMX_PREEMPTION_TIMER_VALUE:
4307 if (!cpu_has_vmx_preemption_timer())
4308 continue;
4309 break;
4310 case GUEST_INTR_STATUS:
4311 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004312 continue;
4313 break;
4314 default:
4315 break;
4316 }
4317
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004318 clear_bit(field, vmx_vmwrite_bitmap);
4319 clear_bit(field, vmx_vmread_bitmap);
4320#ifdef CONFIG_X86_64
4321 if (field & 1)
4322 continue;
4323#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004324 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004325 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004326 j++;
4327 }
4328 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004329}
4330
Avi Kivity6aa8b732006-12-10 02:21:36 -08004331static __init int alloc_kvm_area(void)
4332{
4333 int cpu;
4334
Zachary Amsden3230bb42009-09-29 11:38:37 -10004335 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004336 struct vmcs *vmcs;
4337
4338 vmcs = alloc_vmcs_cpu(cpu);
4339 if (!vmcs) {
4340 free_kvm_area();
4341 return -ENOMEM;
4342 }
4343
4344 per_cpu(vmxarea, cpu) = vmcs;
4345 }
4346 return 0;
4347}
4348
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004349static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004350 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004351{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004352 if (!emulate_invalid_guest_state) {
4353 /*
4354 * CS and SS RPL should be equal during guest entry according
4355 * to VMX spec, but in reality it is not always so. Since vcpu
4356 * is in the middle of the transition from real mode to
4357 * protected mode it is safe to assume that RPL 0 is a good
4358 * default value.
4359 */
4360 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004361 save->selector &= ~SEGMENT_RPL_MASK;
4362 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004363 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004364 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004365 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004366}
4367
4368static void enter_pmode(struct kvm_vcpu *vcpu)
4369{
4370 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004371 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004372
Gleb Natapovd99e4152012-12-20 16:57:45 +02004373 /*
4374 * Update real mode segment cache. It may be not up-to-date if sement
4375 * register was written while vcpu was in a guest mode.
4376 */
4377 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4378 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4379 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4380 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4381 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4382 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4383
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004384 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004385
Avi Kivity2fb92db2011-04-27 19:42:18 +03004386 vmx_segment_cache_clear(vmx);
4387
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004388 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004389
4390 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004391 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4392 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004393 vmcs_writel(GUEST_RFLAGS, flags);
4394
Rusty Russell66aee912007-07-17 23:34:16 +10004395 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4396 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004397
4398 update_exception_bitmap(vcpu);
4399
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004400 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4401 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4402 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4403 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4404 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4405 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004406}
4407
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004408static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004409{
Mathias Krause772e0312012-08-30 01:30:19 +02004410 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004411 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004412
Gleb Natapovd99e4152012-12-20 16:57:45 +02004413 var.dpl = 0x3;
4414 if (seg == VCPU_SREG_CS)
4415 var.type = 0x3;
4416
4417 if (!emulate_invalid_guest_state) {
4418 var.selector = var.base >> 4;
4419 var.base = var.base & 0xffff0;
4420 var.limit = 0xffff;
4421 var.g = 0;
4422 var.db = 0;
4423 var.present = 1;
4424 var.s = 1;
4425 var.l = 0;
4426 var.unusable = 0;
4427 var.type = 0x3;
4428 var.avl = 0;
4429 if (save->base & 0xf)
4430 printk_once(KERN_WARNING "kvm: segment base is not "
4431 "paragraph aligned when entering "
4432 "protected mode (seg=%d)", seg);
4433 }
4434
4435 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004436 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004437 vmcs_write32(sf->limit, var.limit);
4438 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004439}
4440
4441static void enter_rmode(struct kvm_vcpu *vcpu)
4442{
4443 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004444 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004445 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004446
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004447 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4448 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4449 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4450 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4451 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004452 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4453 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004454
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004455 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004456
Gleb Natapov776e58e2011-03-13 12:34:27 +02004457 /*
4458 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004459 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004460 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004461 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004462 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4463 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004464
Avi Kivity2fb92db2011-04-27 19:42:18 +03004465 vmx_segment_cache_clear(vmx);
4466
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004467 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004468 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004469 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4470
4471 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004472 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004473
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004474 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004475
4476 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004477 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004478 update_exception_bitmap(vcpu);
4479
Gleb Natapovd99e4152012-12-20 16:57:45 +02004480 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4481 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4482 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4483 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4484 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4485 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004486
Eddie Dong8668a3c2007-10-10 14:26:45 +08004487 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004488}
4489
Amit Shah401d10d2009-02-20 22:53:37 +05304490static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4491{
4492 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004493 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4494
4495 if (!msr)
4496 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304497
Avi Kivity44ea2b12009-09-06 15:55:37 +03004498 /*
4499 * Force kernel_gs_base reloading before EFER changes, as control
4500 * of this msr depends on is_long_mode().
4501 */
4502 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004503 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304504 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004505 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304506 msr->data = efer;
4507 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004508 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304509
4510 msr->data = efer & ~EFER_LME;
4511 }
4512 setup_msrs(vmx);
4513}
4514
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004515#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004516
4517static void enter_lmode(struct kvm_vcpu *vcpu)
4518{
4519 u32 guest_tr_ar;
4520
Avi Kivity2fb92db2011-04-27 19:42:18 +03004521 vmx_segment_cache_clear(to_vmx(vcpu));
4522
Avi Kivity6aa8b732006-12-10 02:21:36 -08004523 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004524 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004525 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4526 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004527 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004528 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4529 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004530 }
Avi Kivityda38f432010-07-06 11:30:49 +03004531 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004532}
4533
4534static void exit_lmode(struct kvm_vcpu *vcpu)
4535{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004536 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004537 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004538}
4539
4540#endif
4541
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004542static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4543 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004544{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004545 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004546 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4547 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004548 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004549 } else {
4550 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004551 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004552}
4553
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004554static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004555{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004556 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004557}
4558
Avi Kivitye8467fd2009-12-29 18:43:06 +02004559static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4560{
4561 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4562
4563 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4564 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4565}
4566
Avi Kivityaff48ba2010-12-05 18:56:11 +02004567static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4568{
Sean Christophersonb4d18512018-03-05 12:04:40 -08004569 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02004570 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4571 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4572}
4573
Anthony Liguori25c4c272007-04-27 09:29:21 +03004574static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004575{
Avi Kivityfc78f512009-12-07 12:16:48 +02004576 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4577
4578 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4579 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004580}
4581
Sheng Yang14394422008-04-28 12:24:45 +08004582static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4583{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004584 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4585
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004586 if (!test_bit(VCPU_EXREG_PDPTR,
4587 (unsigned long *)&vcpu->arch.regs_dirty))
4588 return;
4589
Sheng Yang14394422008-04-28 12:24:45 +08004590 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004591 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4592 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4593 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4594 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004595 }
4596}
4597
Avi Kivity8f5d5492009-05-31 18:41:29 +03004598static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4599{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004600 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4601
Avi Kivity8f5d5492009-05-31 18:41:29 +03004602 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004603 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4604 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4605 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4606 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004607 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004608
4609 __set_bit(VCPU_EXREG_PDPTR,
4610 (unsigned long *)&vcpu->arch.regs_avail);
4611 __set_bit(VCPU_EXREG_PDPTR,
4612 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004613}
4614
David Matlack38991522016-11-29 18:14:08 -08004615static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4616{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004617 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4618 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004619 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4620
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004621 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08004622 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4623 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4624 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4625
4626 return fixed_bits_valid(val, fixed0, fixed1);
4627}
4628
4629static bool nested_host_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
4634 return fixed_bits_valid(val, fixed0, fixed1);
4635}
4636
4637static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4638{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004639 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
4640 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004641
4642 return fixed_bits_valid(val, fixed0, fixed1);
4643}
4644
4645/* No difference in the restrictions on guest and host CR4 in VMX operation. */
4646#define nested_guest_cr4_valid nested_cr4_valid
4647#define nested_host_cr4_valid nested_cr4_valid
4648
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004649static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004650
4651static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4652 unsigned long cr0,
4653 struct kvm_vcpu *vcpu)
4654{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004655 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4656 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004657 if (!(cr0 & X86_CR0_PG)) {
4658 /* From paging/starting to nonpaging */
4659 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004660 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004661 (CPU_BASED_CR3_LOAD_EXITING |
4662 CPU_BASED_CR3_STORE_EXITING));
4663 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004664 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004665 } else if (!is_paging(vcpu)) {
4666 /* From nonpaging to paging */
4667 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004668 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004669 ~(CPU_BASED_CR3_LOAD_EXITING |
4670 CPU_BASED_CR3_STORE_EXITING));
4671 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004672 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004673 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004674
4675 if (!(cr0 & X86_CR0_WP))
4676 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004677}
4678
Avi Kivity6aa8b732006-12-10 02:21:36 -08004679static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4680{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004681 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004682 unsigned long hw_cr0;
4683
Gleb Natapov50378782013-02-04 16:00:28 +02004684 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004685 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004686 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004687 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004688 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004689
Gleb Natapov218e7632013-01-21 15:36:45 +02004690 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4691 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004692
Gleb Natapov218e7632013-01-21 15:36:45 +02004693 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4694 enter_rmode(vcpu);
4695 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004696
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004697#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004698 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10004699 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004700 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10004701 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004702 exit_lmode(vcpu);
4703 }
4704#endif
4705
Sean Christophersonb4d18512018-03-05 12:04:40 -08004706 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08004707 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4708
Avi Kivity6aa8b732006-12-10 02:21:36 -08004709 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004710 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004711 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004712
4713 /* depends on vcpu->arch.cr0 to be set to a new value */
4714 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004715}
4716
Yu Zhang855feb62017-08-24 20:27:55 +08004717static int get_ept_level(struct kvm_vcpu *vcpu)
4718{
4719 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
4720 return 5;
4721 return 4;
4722}
4723
Peter Feiner995f00a2017-06-30 17:26:32 -07004724static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08004725{
Yu Zhang855feb62017-08-24 20:27:55 +08004726 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08004727
Yu Zhang855feb62017-08-24 20:27:55 +08004728 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08004729
Peter Feiner995f00a2017-06-30 17:26:32 -07004730 if (enable_ept_ad_bits &&
4731 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02004732 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08004733 eptp |= (root_hpa & PAGE_MASK);
4734
4735 return eptp;
4736}
4737
Avi Kivity6aa8b732006-12-10 02:21:36 -08004738static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4739{
Sheng Yang14394422008-04-28 12:24:45 +08004740 unsigned long guest_cr3;
4741 u64 eptp;
4742
4743 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02004744 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07004745 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08004746 vmcs_write64(EPT_POINTER, eptp);
Sean Christophersone90008d2018-03-05 12:04:37 -08004747 if (enable_unrestricted_guest || is_paging(vcpu) ||
4748 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02004749 guest_cr3 = kvm_read_cr3(vcpu);
4750 else
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004751 guest_cr3 = to_kvm_vmx(vcpu->kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02004752 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004753 }
4754
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004755 vmx_flush_tlb(vcpu, true);
Sheng Yang14394422008-04-28 12:24:45 +08004756 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004757}
4758
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004759static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004760{
Ben Serebrin085e68e2015-04-16 11:58:05 -07004761 /*
4762 * Pass through host's Machine Check Enable value to hw_cr4, which
4763 * is in force while we are in guest mode. Do not let guests control
4764 * this bit, even if host CR4.MCE == 0.
4765 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08004766 unsigned long hw_cr4;
4767
4768 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
4769 if (enable_unrestricted_guest)
4770 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
4771 else if (to_vmx(vcpu)->rmode.vm86_active)
4772 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
4773 else
4774 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004775
Sean Christopherson64f7a112018-04-30 10:01:06 -07004776 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
4777 if (cr4 & X86_CR4_UMIP) {
4778 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02004779 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07004780 hw_cr4 &= ~X86_CR4_UMIP;
4781 } else if (!is_guest_mode(vcpu) ||
4782 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
4783 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
4784 SECONDARY_EXEC_DESC);
4785 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02004786
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004787 if (cr4 & X86_CR4_VMXE) {
4788 /*
4789 * To use VMXON (and later other VMX instructions), a guest
4790 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4791 * So basically the check on whether to allow nested VMX
4792 * is here.
4793 */
4794 if (!nested_vmx_allowed(vcpu))
4795 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004796 }
David Matlack38991522016-11-29 18:14:08 -08004797
4798 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004799 return 1;
4800
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004801 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08004802
Sean Christopherson5dc1f042018-03-05 12:04:39 -08004803 if (!enable_unrestricted_guest) {
4804 if (enable_ept) {
4805 if (!is_paging(vcpu)) {
4806 hw_cr4 &= ~X86_CR4_PAE;
4807 hw_cr4 |= X86_CR4_PSE;
4808 } else if (!(cr4 & X86_CR4_PAE)) {
4809 hw_cr4 &= ~X86_CR4_PAE;
4810 }
4811 }
4812
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004813 /*
Huaitong Handdba2622016-03-22 16:51:15 +08004814 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4815 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4816 * to be manually disabled when guest switches to non-paging
4817 * mode.
4818 *
4819 * If !enable_unrestricted_guest, the CPU is always running
4820 * with CR0.PG=1 and CR4 needs to be modified.
4821 * If enable_unrestricted_guest, the CPU automatically
4822 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004823 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08004824 if (!is_paging(vcpu))
4825 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
4826 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004827
Sheng Yang14394422008-04-28 12:24:45 +08004828 vmcs_writel(CR4_READ_SHADOW, cr4);
4829 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004830 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004831}
4832
Avi Kivity6aa8b732006-12-10 02:21:36 -08004833static void vmx_get_segment(struct kvm_vcpu *vcpu,
4834 struct kvm_segment *var, int seg)
4835{
Avi Kivitya9179492011-01-03 14:28:52 +02004836 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004837 u32 ar;
4838
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004839 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004840 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02004841 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03004842 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004843 return;
Avi Kivity1390a282012-08-21 17:07:08 +03004844 var->base = vmx_read_guest_seg_base(vmx, seg);
4845 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4846 return;
Avi Kivitya9179492011-01-03 14:28:52 +02004847 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004848 var->base = vmx_read_guest_seg_base(vmx, seg);
4849 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4850 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4851 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03004852 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004853 var->type = ar & 15;
4854 var->s = (ar >> 4) & 1;
4855 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03004856 /*
4857 * Some userspaces do not preserve unusable property. Since usable
4858 * segment has to be present according to VMX spec we can use present
4859 * property to amend userspace bug by making unusable segment always
4860 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4861 * segment as unusable.
4862 */
4863 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004864 var->avl = (ar >> 12) & 1;
4865 var->l = (ar >> 13) & 1;
4866 var->db = (ar >> 14) & 1;
4867 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004868}
4869
Avi Kivitya9179492011-01-03 14:28:52 +02004870static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4871{
Avi Kivitya9179492011-01-03 14:28:52 +02004872 struct kvm_segment s;
4873
4874 if (to_vmx(vcpu)->rmode.vm86_active) {
4875 vmx_get_segment(vcpu, &s, seg);
4876 return s.base;
4877 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004878 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02004879}
4880
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004881static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02004882{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004883 struct vcpu_vmx *vmx = to_vmx(vcpu);
4884
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004885 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02004886 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004887 else {
4888 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004889 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02004890 }
Avi Kivity69c73022011-03-07 15:26:44 +02004891}
4892
Avi Kivity653e3102007-05-07 10:55:37 +03004893static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004894{
Avi Kivity6aa8b732006-12-10 02:21:36 -08004895 u32 ar;
4896
Avi Kivityf0495f92012-06-07 17:06:10 +03004897 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004898 ar = 1 << 16;
4899 else {
4900 ar = var->type & 15;
4901 ar |= (var->s & 1) << 4;
4902 ar |= (var->dpl & 3) << 5;
4903 ar |= (var->present & 1) << 7;
4904 ar |= (var->avl & 1) << 12;
4905 ar |= (var->l & 1) << 13;
4906 ar |= (var->db & 1) << 14;
4907 ar |= (var->g & 1) << 15;
4908 }
Avi Kivity653e3102007-05-07 10:55:37 +03004909
4910 return ar;
4911}
4912
4913static void vmx_set_segment(struct kvm_vcpu *vcpu,
4914 struct kvm_segment *var, int seg)
4915{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004916 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02004917 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03004918
Avi Kivity2fb92db2011-04-27 19:42:18 +03004919 vmx_segment_cache_clear(vmx);
4920
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004921 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4922 vmx->rmode.segs[seg] = *var;
4923 if (seg == VCPU_SREG_TR)
4924 vmcs_write16(sf->selector, var->selector);
4925 else if (var->s)
4926 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004927 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03004928 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004929
Avi Kivity653e3102007-05-07 10:55:37 +03004930 vmcs_writel(sf->base, var->base);
4931 vmcs_write32(sf->limit, var->limit);
4932 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004933
4934 /*
4935 * Fix the "Accessed" bit in AR field of segment registers for older
4936 * qemu binaries.
4937 * IA32 arch specifies that at the time of processor reset the
4938 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08004939 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004940 * state vmexit when "unrestricted guest" mode is turned on.
4941 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4942 * tree. Newer qemu binaries with that qemu fix would not need this
4943 * kvm hack.
4944 */
4945 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02004946 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004947
Gleb Natapovf924d662012-12-12 19:10:55 +02004948 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02004949
4950out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01004951 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004952}
4953
Avi Kivity6aa8b732006-12-10 02:21:36 -08004954static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4955{
Avi Kivity2fb92db2011-04-27 19:42:18 +03004956 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004957
4958 *db = (ar >> 14) & 1;
4959 *l = (ar >> 13) & 1;
4960}
4961
Gleb Natapov89a27f42010-02-16 10:51:48 +02004962static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004963{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004964 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4965 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004966}
4967
Gleb Natapov89a27f42010-02-16 10:51:48 +02004968static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004969{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004970 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4971 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004972}
4973
Gleb Natapov89a27f42010-02-16 10:51:48 +02004974static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004975{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004976 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4977 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004978}
4979
Gleb Natapov89a27f42010-02-16 10:51:48 +02004980static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004981{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004982 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4983 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004984}
4985
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004986static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4987{
4988 struct kvm_segment var;
4989 u32 ar;
4990
4991 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02004992 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02004993 if (seg == VCPU_SREG_CS)
4994 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004995 ar = vmx_segment_access_rights(&var);
4996
4997 if (var.base != (var.selector << 4))
4998 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02004999 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005000 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005001 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005002 return false;
5003
5004 return true;
5005}
5006
5007static bool code_segment_valid(struct kvm_vcpu *vcpu)
5008{
5009 struct kvm_segment cs;
5010 unsigned int cs_rpl;
5011
5012 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005013 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005014
Avi Kivity1872a3f2009-01-04 23:26:52 +02005015 if (cs.unusable)
5016 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005017 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005018 return false;
5019 if (!cs.s)
5020 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005021 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005022 if (cs.dpl > cs_rpl)
5023 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005024 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005025 if (cs.dpl != cs_rpl)
5026 return false;
5027 }
5028 if (!cs.present)
5029 return false;
5030
5031 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5032 return true;
5033}
5034
5035static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5036{
5037 struct kvm_segment ss;
5038 unsigned int ss_rpl;
5039
5040 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005041 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005042
Avi Kivity1872a3f2009-01-04 23:26:52 +02005043 if (ss.unusable)
5044 return true;
5045 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005046 return false;
5047 if (!ss.s)
5048 return false;
5049 if (ss.dpl != ss_rpl) /* DPL != RPL */
5050 return false;
5051 if (!ss.present)
5052 return false;
5053
5054 return true;
5055}
5056
5057static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5058{
5059 struct kvm_segment var;
5060 unsigned int rpl;
5061
5062 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005063 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005064
Avi Kivity1872a3f2009-01-04 23:26:52 +02005065 if (var.unusable)
5066 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005067 if (!var.s)
5068 return false;
5069 if (!var.present)
5070 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005071 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005072 if (var.dpl < rpl) /* DPL < RPL */
5073 return false;
5074 }
5075
5076 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5077 * rights flags
5078 */
5079 return true;
5080}
5081
5082static bool tr_valid(struct kvm_vcpu *vcpu)
5083{
5084 struct kvm_segment tr;
5085
5086 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5087
Avi Kivity1872a3f2009-01-04 23:26:52 +02005088 if (tr.unusable)
5089 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005090 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005091 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005092 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005093 return false;
5094 if (!tr.present)
5095 return false;
5096
5097 return true;
5098}
5099
5100static bool ldtr_valid(struct kvm_vcpu *vcpu)
5101{
5102 struct kvm_segment ldtr;
5103
5104 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5105
Avi Kivity1872a3f2009-01-04 23:26:52 +02005106 if (ldtr.unusable)
5107 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005108 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005109 return false;
5110 if (ldtr.type != 2)
5111 return false;
5112 if (!ldtr.present)
5113 return false;
5114
5115 return true;
5116}
5117
5118static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5119{
5120 struct kvm_segment cs, ss;
5121
5122 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5123 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5124
Nadav Amitb32a9912015-03-29 16:33:04 +03005125 return ((cs.selector & SEGMENT_RPL_MASK) ==
5126 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005127}
5128
5129/*
5130 * Check if guest state is valid. Returns true if valid, false if
5131 * not.
5132 * We assume that registers are always usable
5133 */
5134static bool guest_state_valid(struct kvm_vcpu *vcpu)
5135{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005136 if (enable_unrestricted_guest)
5137 return true;
5138
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005139 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005140 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005141 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5142 return false;
5143 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5144 return false;
5145 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5146 return false;
5147 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5148 return false;
5149 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5150 return false;
5151 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5152 return false;
5153 } else {
5154 /* protected mode guest state checks */
5155 if (!cs_ss_rpl_check(vcpu))
5156 return false;
5157 if (!code_segment_valid(vcpu))
5158 return false;
5159 if (!stack_segment_valid(vcpu))
5160 return false;
5161 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5162 return false;
5163 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5164 return false;
5165 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5166 return false;
5167 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5168 return false;
5169 if (!tr_valid(vcpu))
5170 return false;
5171 if (!ldtr_valid(vcpu))
5172 return false;
5173 }
5174 /* TODO:
5175 * - Add checks on RIP
5176 * - Add checks on RFLAGS
5177 */
5178
5179 return true;
5180}
5181
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005182static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5183{
5184 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5185}
5186
Mike Dayd77c26f2007-10-08 09:02:08 -04005187static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005188{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005189 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005190 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005191 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005192
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005193 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005194 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005195 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5196 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005197 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005198 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005199 r = kvm_write_guest_page(kvm, fn++, &data,
5200 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005201 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005202 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005203 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5204 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005205 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005206 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5207 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005208 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005209 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005210 r = kvm_write_guest_page(kvm, fn, &data,
5211 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5212 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005213out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005214 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005215 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005216}
5217
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005218static int init_rmode_identity_map(struct kvm *kvm)
5219{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005220 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005221 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005222 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005223 u32 tmp;
5224
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005225 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005226 mutex_lock(&kvm->slots_lock);
5227
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005228 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005229 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005230
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005231 if (!kvm_vmx->ept_identity_map_addr)
5232 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5233 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005234
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005235 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005236 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005237 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005238 goto out2;
5239
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005240 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005241 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5242 if (r < 0)
5243 goto out;
5244 /* Set up identity-mapping pagetable for EPT in real mode */
5245 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5246 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5247 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5248 r = kvm_write_guest_page(kvm, identity_map_pfn,
5249 &tmp, i * sizeof(tmp), sizeof(tmp));
5250 if (r < 0)
5251 goto out;
5252 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005253 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005254
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005255out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005256 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005257
5258out2:
5259 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005260 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005261}
5262
Avi Kivity6aa8b732006-12-10 02:21:36 -08005263static void seg_setup(int seg)
5264{
Mathias Krause772e0312012-08-30 01:30:19 +02005265 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005266 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005267
5268 vmcs_write16(sf->selector, 0);
5269 vmcs_writel(sf->base, 0);
5270 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005271 ar = 0x93;
5272 if (seg == VCPU_SREG_CS)
5273 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005274
5275 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005276}
5277
Sheng Yangf78e0e22007-10-29 09:40:42 +08005278static int alloc_apic_access_page(struct kvm *kvm)
5279{
Xiao Guangrong44841412012-09-07 14:14:20 +08005280 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005281 int r = 0;
5282
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005283 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005284 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005285 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005286 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5287 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005288 if (r)
5289 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005290
Tang Chen73a6d942014-09-11 13:38:00 +08005291 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005292 if (is_error_page(page)) {
5293 r = -EFAULT;
5294 goto out;
5295 }
5296
Tang Chenc24ae0d2014-09-24 15:57:58 +08005297 /*
5298 * Do not pin the page in memory, so that memory hot-unplug
5299 * is able to migrate it.
5300 */
5301 put_page(page);
5302 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005303out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005304 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005305 return r;
5306}
5307
Wanpeng Li991e7a02015-09-16 17:30:05 +08005308static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005309{
5310 int vpid;
5311
Avi Kivity919818a2009-03-23 18:01:29 +02005312 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005313 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005314 spin_lock(&vmx_vpid_lock);
5315 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005316 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005317 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005318 else
5319 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005320 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005321 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005322}
5323
Wanpeng Li991e7a02015-09-16 17:30:05 +08005324static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005325{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005326 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005327 return;
5328 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005329 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005330 spin_unlock(&vmx_vpid_lock);
5331}
5332
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005333static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5334 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005335{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005336 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005337
5338 if (!cpu_has_vmx_msr_bitmap())
5339 return;
5340
5341 /*
5342 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5343 * have the write-low and read-high bitmap offsets the wrong way round.
5344 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5345 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005346 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005347 if (type & MSR_TYPE_R)
5348 /* read-low */
5349 __clear_bit(msr, msr_bitmap + 0x000 / f);
5350
5351 if (type & MSR_TYPE_W)
5352 /* write-low */
5353 __clear_bit(msr, msr_bitmap + 0x800 / f);
5354
Sheng Yang25c5f222008-03-28 13:18:56 +08005355 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5356 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005357 if (type & MSR_TYPE_R)
5358 /* read-high */
5359 __clear_bit(msr, msr_bitmap + 0x400 / f);
5360
5361 if (type & MSR_TYPE_W)
5362 /* write-high */
5363 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5364
5365 }
5366}
5367
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005368static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5369 u32 msr, int type)
5370{
5371 int f = sizeof(unsigned long);
5372
5373 if (!cpu_has_vmx_msr_bitmap())
5374 return;
5375
5376 /*
5377 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5378 * have the write-low and read-high bitmap offsets the wrong way round.
5379 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5380 */
5381 if (msr <= 0x1fff) {
5382 if (type & MSR_TYPE_R)
5383 /* read-low */
5384 __set_bit(msr, msr_bitmap + 0x000 / f);
5385
5386 if (type & MSR_TYPE_W)
5387 /* write-low */
5388 __set_bit(msr, msr_bitmap + 0x800 / f);
5389
5390 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5391 msr &= 0x1fff;
5392 if (type & MSR_TYPE_R)
5393 /* read-high */
5394 __set_bit(msr, msr_bitmap + 0x400 / f);
5395
5396 if (type & MSR_TYPE_W)
5397 /* write-high */
5398 __set_bit(msr, msr_bitmap + 0xc00 / f);
5399
5400 }
5401}
5402
5403static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5404 u32 msr, int type, bool value)
5405{
5406 if (value)
5407 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5408 else
5409 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5410}
5411
Wincy Vanf2b93282015-02-03 23:56:03 +08005412/*
5413 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5414 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5415 */
5416static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5417 unsigned long *msr_bitmap_nested,
5418 u32 msr, int type)
5419{
5420 int f = sizeof(unsigned long);
5421
Wincy Vanf2b93282015-02-03 23:56:03 +08005422 /*
5423 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5424 * have the write-low and read-high bitmap offsets the wrong way round.
5425 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5426 */
5427 if (msr <= 0x1fff) {
5428 if (type & MSR_TYPE_R &&
5429 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5430 /* read-low */
5431 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5432
5433 if (type & MSR_TYPE_W &&
5434 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5435 /* write-low */
5436 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5437
5438 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5439 msr &= 0x1fff;
5440 if (type & MSR_TYPE_R &&
5441 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5442 /* read-high */
5443 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5444
5445 if (type & MSR_TYPE_W &&
5446 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5447 /* write-high */
5448 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5449
5450 }
5451}
5452
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005453static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02005454{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005455 u8 mode = 0;
5456
5457 if (cpu_has_secondary_exec_ctrls() &&
5458 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5459 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5460 mode |= MSR_BITMAP_MODE_X2APIC;
5461 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5462 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5463 }
5464
5465 if (is_long_mode(vcpu))
5466 mode |= MSR_BITMAP_MODE_LM;
5467
5468 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005469}
5470
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005471#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5472
5473static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5474 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005475{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005476 int msr;
5477
5478 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5479 unsigned word = msr / BITS_PER_LONG;
5480 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5481 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005482 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005483
5484 if (mode & MSR_BITMAP_MODE_X2APIC) {
5485 /*
5486 * TPR reads and writes can be virtualized even if virtual interrupt
5487 * delivery is not in use.
5488 */
5489 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5490 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5491 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5492 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5493 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5494 }
5495 }
5496}
5497
5498static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5499{
5500 struct vcpu_vmx *vmx = to_vmx(vcpu);
5501 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5502 u8 mode = vmx_msr_bitmap_mode(vcpu);
5503 u8 changed = mode ^ vmx->msr_bitmap_mode;
5504
5505 if (!changed)
5506 return;
5507
5508 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5509 !(mode & MSR_BITMAP_MODE_LM));
5510
5511 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5512 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5513
5514 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005515}
5516
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005517static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005518{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005519 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005520}
5521
David Matlackc9f04402017-08-01 14:00:40 -07005522static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5523{
5524 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5525 gfn_t gfn;
5526
5527 /*
5528 * Don't need to mark the APIC access page dirty; it is never
5529 * written to by the CPU during APIC virtualization.
5530 */
5531
5532 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5533 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5534 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5535 }
5536
5537 if (nested_cpu_has_posted_intr(vmcs12)) {
5538 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5539 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5540 }
5541}
5542
5543
David Hildenbrand6342c502017-01-25 11:58:58 +01005544static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005545{
5546 struct vcpu_vmx *vmx = to_vmx(vcpu);
5547 int max_irr;
5548 void *vapic_page;
5549 u16 status;
5550
David Matlackc9f04402017-08-01 14:00:40 -07005551 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5552 return;
Wincy Van705699a2015-02-03 23:58:17 +08005553
David Matlackc9f04402017-08-01 14:00:40 -07005554 vmx->nested.pi_pending = false;
5555 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5556 return;
Wincy Van705699a2015-02-03 23:58:17 +08005557
David Matlackc9f04402017-08-01 14:00:40 -07005558 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5559 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005560 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02005561 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5562 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08005563 kunmap(vmx->nested.virtual_apic_page);
5564
5565 status = vmcs_read16(GUEST_INTR_STATUS);
5566 if ((u8)max_irr > ((u8)status & 0xff)) {
5567 status &= ~0xff;
5568 status |= (u8)max_irr;
5569 vmcs_write16(GUEST_INTR_STATUS, status);
5570 }
5571 }
David Matlackc9f04402017-08-01 14:00:40 -07005572
5573 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005574}
5575
Wincy Van06a55242017-04-28 13:13:59 +08005576static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5577 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005578{
5579#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005580 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5581
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005582 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005583 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005584 * The vector of interrupt to be delivered to vcpu had
5585 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005586 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005587 * Following cases will be reached in this block, and
5588 * we always send a notification event in all cases as
5589 * explained below.
5590 *
5591 * Case 1: vcpu keeps in non-root mode. Sending a
5592 * notification event posts the interrupt to vcpu.
5593 *
5594 * Case 2: vcpu exits to root mode and is still
5595 * runnable. PIR will be synced to vIRR before the
5596 * next vcpu entry. Sending a notification event in
5597 * this case has no effect, as vcpu is not in root
5598 * mode.
5599 *
5600 * Case 3: vcpu exits to root mode and is blocked.
5601 * vcpu_block() has already synced PIR to vIRR and
5602 * never blocks vcpu if vIRR is not cleared. Therefore,
5603 * a blocked vcpu here does not wait for any requested
5604 * interrupts in PIR, and sending a notification event
5605 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005606 */
Feng Wu28b835d2015-09-18 22:29:54 +08005607
Wincy Van06a55242017-04-28 13:13:59 +08005608 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005609 return true;
5610 }
5611#endif
5612 return false;
5613}
5614
Wincy Van705699a2015-02-03 23:58:17 +08005615static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5616 int vector)
5617{
5618 struct vcpu_vmx *vmx = to_vmx(vcpu);
5619
5620 if (is_guest_mode(vcpu) &&
5621 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005622 /*
5623 * If a posted intr is not recognized by hardware,
5624 * we will accomplish it in the next vmentry.
5625 */
5626 vmx->nested.pi_pending = true;
5627 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02005628 /* the PIR and ON have been set by L1. */
5629 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
5630 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005631 return 0;
5632 }
5633 return -1;
5634}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005635/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005636 * Send interrupt to vcpu via posted interrupt way.
5637 * 1. If target vcpu is running(non-root mode), send posted interrupt
5638 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5639 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5640 * interrupt from PIR in next vmentry.
5641 */
5642static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5643{
5644 struct vcpu_vmx *vmx = to_vmx(vcpu);
5645 int r;
5646
Wincy Van705699a2015-02-03 23:58:17 +08005647 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5648 if (!r)
5649 return;
5650
Yang Zhanga20ed542013-04-11 19:25:15 +08005651 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5652 return;
5653
Paolo Bonzinib95234c2016-12-19 13:57:33 +01005654 /* If a previous notification has sent the IPI, nothing to do. */
5655 if (pi_test_and_set_on(&vmx->pi_desc))
5656 return;
5657
Wincy Van06a55242017-04-28 13:13:59 +08005658 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08005659 kvm_vcpu_kick(vcpu);
5660}
5661
Avi Kivity6aa8b732006-12-10 02:21:36 -08005662/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005663 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5664 * will not change in the lifetime of the guest.
5665 * Note that host-state that does change is set elsewhere. E.g., host-state
5666 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5667 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005668static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005669{
5670 u32 low32, high32;
5671 unsigned long tmpl;
5672 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005673 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005674
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07005675 cr0 = read_cr0();
5676 WARN_ON(cr0 & X86_CR0_TS);
5677 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005678
5679 /*
5680 * Save the most likely value for this task's CR3 in the VMCS.
5681 * We can't use __get_current_cr3_fast() because we're not atomic.
5682 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07005683 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005684 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02005685 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005686
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005687 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005688 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005689 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02005690 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005691
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005692 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005693#ifdef CONFIG_X86_64
5694 /*
5695 * Load null selectors, so we can avoid reloading them in
5696 * __vmx_load_host_state(), in case userspace uses the null selectors
5697 * too (the expected case).
5698 */
5699 vmcs_write16(HOST_DS_SELECTOR, 0);
5700 vmcs_write16(HOST_ES_SELECTOR, 0);
5701#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005702 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5703 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005704#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005705 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5706 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5707
Juergen Gross87930012017-09-04 12:25:27 +02005708 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005709 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005710 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005711
Avi Kivity83287ea422012-09-16 15:10:57 +03005712 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005713
5714 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5715 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5716 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5717 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5718
5719 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5720 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5721 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5722 }
5723}
5724
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005725static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5726{
5727 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5728 if (enable_ept)
5729 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005730 if (is_guest_mode(&vmx->vcpu))
5731 vmx->vcpu.arch.cr4_guest_owned_bits &=
5732 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005733 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5734}
5735
Yang Zhang01e439b2013-04-11 19:25:12 +08005736static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5737{
5738 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5739
Andrey Smetanind62caab2015-11-10 15:36:33 +03005740 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005741 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005742
5743 if (!enable_vnmi)
5744 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
5745
Yunhong Jiang64672c92016-06-13 14:19:59 -07005746 /* Enable the preemption timer dynamically */
5747 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08005748 return pin_based_exec_ctrl;
5749}
5750
Andrey Smetanind62caab2015-11-10 15:36:33 +03005751static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5752{
5753 struct vcpu_vmx *vmx = to_vmx(vcpu);
5754
5755 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03005756 if (cpu_has_secondary_exec_ctrls()) {
5757 if (kvm_vcpu_apicv_active(vcpu))
5758 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5759 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5760 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5761 else
5762 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5763 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5764 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5765 }
5766
5767 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005768 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03005769}
5770
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005771static u32 vmx_exec_control(struct vcpu_vmx *vmx)
5772{
5773 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01005774
5775 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
5776 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5777
Paolo Bonzini35754c92015-07-29 12:05:37 +02005778 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005779 exec_control &= ~CPU_BASED_TPR_SHADOW;
5780#ifdef CONFIG_X86_64
5781 exec_control |= CPU_BASED_CR8_STORE_EXITING |
5782 CPU_BASED_CR8_LOAD_EXITING;
5783#endif
5784 }
5785 if (!enable_ept)
5786 exec_control |= CPU_BASED_CR3_STORE_EXITING |
5787 CPU_BASED_CR3_LOAD_EXITING |
5788 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07005789 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
5790 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
5791 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07005792 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
5793 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005794 return exec_control;
5795}
5796
Jim Mattson45ec3682017-08-23 16:32:04 -07005797static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005798{
Jim Mattson45ec3682017-08-23 16:32:04 -07005799 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02005800 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005801}
5802
Jim Mattson75f4fc82017-08-23 16:32:03 -07005803static bool vmx_rdseed_supported(void)
5804{
5805 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02005806 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005807}
5808
Paolo Bonzini80154d72017-08-24 13:55:35 +02005809static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005810{
Paolo Bonzini80154d72017-08-24 13:55:35 +02005811 struct kvm_vcpu *vcpu = &vmx->vcpu;
5812
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005813 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02005814
Paolo Bonzini80154d72017-08-24 13:55:35 +02005815 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005816 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5817 if (vmx->vpid == 0)
5818 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
5819 if (!enable_ept) {
5820 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
5821 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00005822 /* Enable INVPCID for non-ept guests may cause performance regression. */
5823 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005824 }
5825 if (!enable_unrestricted_guest)
5826 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07005827 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005828 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02005829 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08005830 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
5831 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08005832 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02005833
5834 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
5835 * in vmx_set_cr4. */
5836 exec_control &= ~SECONDARY_EXEC_DESC;
5837
Abel Gordonabc4fc52013-04-18 14:35:25 +03005838 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
5839 (handle_vmptrld).
5840 We can NOT enable shadow_vmcs here because we don't have yet
5841 a current VMCS12
5842 */
5843 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08005844
5845 if (!enable_pml)
5846 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08005847
Paolo Bonzini3db13482017-08-24 14:48:03 +02005848 if (vmx_xsaves_supported()) {
5849 /* Exposing XSAVES only when XSAVE is exposed */
5850 bool xsaves_enabled =
5851 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
5852 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
5853
5854 if (!xsaves_enabled)
5855 exec_control &= ~SECONDARY_EXEC_XSAVES;
5856
5857 if (nested) {
5858 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005859 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02005860 SECONDARY_EXEC_XSAVES;
5861 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005862 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02005863 ~SECONDARY_EXEC_XSAVES;
5864 }
5865 }
5866
Paolo Bonzini80154d72017-08-24 13:55:35 +02005867 if (vmx_rdtscp_supported()) {
5868 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
5869 if (!rdtscp_enabled)
5870 exec_control &= ~SECONDARY_EXEC_RDTSCP;
5871
5872 if (nested) {
5873 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005874 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02005875 SECONDARY_EXEC_RDTSCP;
5876 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005877 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02005878 ~SECONDARY_EXEC_RDTSCP;
5879 }
5880 }
5881
5882 if (vmx_invpcid_supported()) {
5883 /* Exposing INVPCID only when PCID is exposed */
5884 bool invpcid_enabled =
5885 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
5886 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
5887
5888 if (!invpcid_enabled) {
5889 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
5890 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
5891 }
5892
5893 if (nested) {
5894 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005895 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02005896 SECONDARY_EXEC_ENABLE_INVPCID;
5897 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005898 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02005899 ~SECONDARY_EXEC_ENABLE_INVPCID;
5900 }
5901 }
5902
Jim Mattson45ec3682017-08-23 16:32:04 -07005903 if (vmx_rdrand_supported()) {
5904 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
5905 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02005906 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005907
5908 if (nested) {
5909 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005910 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005911 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005912 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005913 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005914 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005915 }
5916 }
5917
Jim Mattson75f4fc82017-08-23 16:32:03 -07005918 if (vmx_rdseed_supported()) {
5919 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
5920 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02005921 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005922
5923 if (nested) {
5924 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005925 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005926 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005927 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005928 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005929 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005930 }
5931 }
5932
Paolo Bonzini80154d72017-08-24 13:55:35 +02005933 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005934}
5935
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005936static void ept_set_mmio_spte_mask(void)
5937{
5938 /*
5939 * EPT Misconfigurations can be generated if the value of bits 2:0
5940 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005941 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07005942 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
5943 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005944}
5945
Wanpeng Lif53cd632014-12-02 19:14:58 +08005946#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005947/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005948 * Sets up the vmcs for emulated real mode.
5949 */
David Hildenbrand12d79912017-08-24 20:51:26 +02005950static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005951{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005952#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005953 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005954#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08005955 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005956
Abel Gordon4607c2d2013-04-18 14:35:55 +03005957 if (enable_shadow_vmcs) {
5958 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5959 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
5960 }
Sheng Yang25c5f222008-03-28 13:18:56 +08005961 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005962 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08005963
Avi Kivity6aa8b732006-12-10 02:21:36 -08005964 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5965
Avi Kivity6aa8b732006-12-10 02:21:36 -08005966 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08005967 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07005968 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005969
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005970 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005971
Dan Williamsdfa169b2016-06-02 11:17:24 -07005972 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02005973 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005974 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02005975 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07005976 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08005977
Andrey Smetanind62caab2015-11-10 15:36:33 +03005978 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08005979 vmcs_write64(EOI_EXIT_BITMAP0, 0);
5980 vmcs_write64(EOI_EXIT_BITMAP1, 0);
5981 vmcs_write64(EOI_EXIT_BITMAP2, 0);
5982 vmcs_write64(EOI_EXIT_BITMAP3, 0);
5983
5984 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08005985
Li RongQing0bcf2612015-12-03 13:29:34 +08005986 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08005987 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08005988 }
5989
Wanpeng Lib31c1142018-03-12 04:53:04 -07005990 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005991 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02005992 vmx->ple_window = ple_window;
5993 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005994 }
5995
Xiao Guangrongc3707952011-07-12 03:28:04 +08005996 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5997 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005998 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5999
Avi Kivity9581d442010-10-19 16:46:55 +02006000 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6001 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006002 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006003#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006004 rdmsrl(MSR_FS_BASE, a);
6005 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
6006 rdmsrl(MSR_GS_BASE, a);
6007 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
6008#else
6009 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6010 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
6011#endif
6012
Bandan Das2a499e42017-08-03 15:54:41 -04006013 if (cpu_has_vmx_vmfunc())
6014 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6015
Eddie Dong2cc51562007-05-21 07:28:09 +03006016 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6017 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006018 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03006019 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006020 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006021
Radim Krčmář74545702015-04-27 15:11:25 +02006022 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6023 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006024
Paolo Bonzini03916db2014-07-24 14:21:57 +02006025 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006026 u32 index = vmx_msr_index[i];
6027 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006028 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006029
6030 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6031 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006032 if (wrmsr_safe(index, data_low, data_high) < 0)
6033 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006034 vmx->guest_msrs[j].index = i;
6035 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006036 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006037 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006038 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006039
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01006040 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
6041 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02006042
6043 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006044
6045 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006046 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006047
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006048 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6049 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6050
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006051 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006052
Wanpeng Lif53cd632014-12-02 19:14:58 +08006053 if (vmx_xsaves_supported())
6054 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6055
Peter Feiner4e595162016-07-07 14:49:58 -07006056 if (enable_pml) {
6057 ASSERT(vmx->pml_pg);
6058 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6059 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6060 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006061}
6062
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006063static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006064{
6065 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006066 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006067 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006068
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006069 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006070 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006071
Wanpeng Li518e7b92018-02-28 14:03:31 +08006072 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006073 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006074 kvm_set_cr8(vcpu, 0);
6075
6076 if (!init_event) {
6077 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6078 MSR_IA32_APICBASE_ENABLE;
6079 if (kvm_vcpu_is_reset_bsp(vcpu))
6080 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6081 apic_base_msr.host_initiated = true;
6082 kvm_set_apic_base(vcpu, &apic_base_msr);
6083 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006084
Avi Kivity2fb92db2011-04-27 19:42:18 +03006085 vmx_segment_cache_clear(vmx);
6086
Avi Kivity5706be02008-08-20 15:07:31 +03006087 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006088 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006089 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006090
6091 seg_setup(VCPU_SREG_DS);
6092 seg_setup(VCPU_SREG_ES);
6093 seg_setup(VCPU_SREG_FS);
6094 seg_setup(VCPU_SREG_GS);
6095 seg_setup(VCPU_SREG_SS);
6096
6097 vmcs_write16(GUEST_TR_SELECTOR, 0);
6098 vmcs_writel(GUEST_TR_BASE, 0);
6099 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6100 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6101
6102 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6103 vmcs_writel(GUEST_LDTR_BASE, 0);
6104 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6105 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6106
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006107 if (!init_event) {
6108 vmcs_write32(GUEST_SYSENTER_CS, 0);
6109 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6110 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6111 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6112 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006113
Wanpeng Lic37c2872017-11-20 14:52:21 -08006114 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006115 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006116
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006117 vmcs_writel(GUEST_GDTR_BASE, 0);
6118 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6119
6120 vmcs_writel(GUEST_IDTR_BASE, 0);
6121 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6122
Anthony Liguori443381a2010-12-06 10:53:38 -06006123 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006124 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006125 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006126 if (kvm_mpx_supported())
6127 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006128
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006129 setup_msrs(vmx);
6130
Avi Kivity6aa8b732006-12-10 02:21:36 -08006131 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6132
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006133 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006134 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006135 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006136 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006137 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006138 vmcs_write32(TPR_THRESHOLD, 0);
6139 }
6140
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006141 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006142
Sheng Yang2384d2b2008-01-17 15:14:33 +08006143 if (vmx->vpid != 0)
6144 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6145
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006146 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006147 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006148 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006149 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006150 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006151
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006152 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006153
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006154 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006155 if (init_event)
6156 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006157}
6158
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006159/*
6160 * In nested virtualization, check if L1 asked to exit on external interrupts.
6161 * For most existing hypervisors, this will always return true.
6162 */
6163static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6164{
6165 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6166 PIN_BASED_EXT_INTR_MASK;
6167}
6168
Bandan Das77b0f5d2014-04-19 18:17:45 -04006169/*
6170 * In nested virtualization, check if L1 has set
6171 * VM_EXIT_ACK_INTR_ON_EXIT
6172 */
6173static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6174{
6175 return get_vmcs12(vcpu)->vm_exit_controls &
6176 VM_EXIT_ACK_INTR_ON_EXIT;
6177}
6178
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006179static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6180{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006181 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006182}
6183
Jan Kiszkac9a79532014-03-07 20:03:15 +01006184static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006185{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006186 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6187 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006188}
6189
Jan Kiszkac9a79532014-03-07 20:03:15 +01006190static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006191{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006192 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006193 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006194 enable_irq_window(vcpu);
6195 return;
6196 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006197
Paolo Bonzini47c01522016-12-19 11:44:07 +01006198 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6199 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006200}
6201
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006202static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006203{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006204 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006205 uint32_t intr;
6206 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006207
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006208 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006209
Avi Kivityfa89a812008-09-01 15:57:51 +03006210 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006211 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006212 int inc_eip = 0;
6213 if (vcpu->arch.interrupt.soft)
6214 inc_eip = vcpu->arch.event_exit_inst_len;
6215 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006216 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006217 return;
6218 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006219 intr = irq | INTR_INFO_VALID_MASK;
6220 if (vcpu->arch.interrupt.soft) {
6221 intr |= INTR_TYPE_SOFT_INTR;
6222 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6223 vmx->vcpu.arch.event_exit_inst_len);
6224 } else
6225 intr |= INTR_TYPE_EXT_INTR;
6226 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006227
6228 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006229}
6230
Sheng Yangf08864b2008-05-15 18:23:25 +08006231static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6232{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006233 struct vcpu_vmx *vmx = to_vmx(vcpu);
6234
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006235 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006236 /*
6237 * Tracking the NMI-blocked state in software is built upon
6238 * finding the next open IRQ window. This, in turn, depends on
6239 * well-behaving guests: They have to keep IRQs disabled at
6240 * least as long as the NMI handler runs. Otherwise we may
6241 * cause NMI nesting, maybe breaking the guest. But as this is
6242 * highly unlikely, we can live with the residual risk.
6243 */
6244 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6245 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6246 }
6247
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006248 ++vcpu->stat.nmi_injections;
6249 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006250
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006251 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006252 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006253 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006254 return;
6255 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006256
Sheng Yangf08864b2008-05-15 18:23:25 +08006257 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6258 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006259
6260 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006261}
6262
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006263static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6264{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006265 struct vcpu_vmx *vmx = to_vmx(vcpu);
6266 bool masked;
6267
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006268 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006269 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006270 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006271 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006272 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6273 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6274 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006275}
6276
6277static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6278{
6279 struct vcpu_vmx *vmx = to_vmx(vcpu);
6280
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006281 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006282 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6283 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6284 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6285 }
6286 } else {
6287 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6288 if (masked)
6289 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6290 GUEST_INTR_STATE_NMI);
6291 else
6292 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6293 GUEST_INTR_STATE_NMI);
6294 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006295}
6296
Jan Kiszka2505dc92013-04-14 12:12:47 +02006297static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6298{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006299 if (to_vmx(vcpu)->nested.nested_run_pending)
6300 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006301
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006302 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006303 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6304 return 0;
6305
Jan Kiszka2505dc92013-04-14 12:12:47 +02006306 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6307 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6308 | GUEST_INTR_STATE_NMI));
6309}
6310
Gleb Natapov78646122009-03-23 12:12:11 +02006311static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6312{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006313 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6314 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006315 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6316 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006317}
6318
Izik Eiduscbc94022007-10-25 00:29:55 +02006319static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6320{
6321 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006322
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006323 if (enable_unrestricted_guest)
6324 return 0;
6325
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006326 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6327 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006328 if (ret)
6329 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006330 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006331 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006332}
6333
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006334static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6335{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006336 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006337 return 0;
6338}
6339
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006340static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006341{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006342 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006343 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006344 /*
6345 * Update instruction length as we may reinject the exception
6346 * from user space while in guest debugging mode.
6347 */
6348 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6349 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006350 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006351 return false;
6352 /* fall through */
6353 case DB_VECTOR:
6354 if (vcpu->guest_debug &
6355 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6356 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006357 /* fall through */
6358 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006359 case OF_VECTOR:
6360 case BR_VECTOR:
6361 case UD_VECTOR:
6362 case DF_VECTOR:
6363 case SS_VECTOR:
6364 case GP_VECTOR:
6365 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006366 return true;
6367 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006368 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006369 return false;
6370}
6371
6372static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6373 int vec, u32 err_code)
6374{
6375 /*
6376 * Instruction with address size override prefix opcode 0x67
6377 * Cause the #SS fault with 0 error code in VM86 mode.
6378 */
6379 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6380 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6381 if (vcpu->arch.halt_request) {
6382 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006383 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006384 }
6385 return 1;
6386 }
6387 return 0;
6388 }
6389
6390 /*
6391 * Forward all other exceptions that are valid in real mode.
6392 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6393 * the required debugging infrastructure rework.
6394 */
6395 kvm_queue_exception(vcpu, vec);
6396 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006397}
6398
Andi Kleena0861c02009-06-08 17:37:09 +08006399/*
6400 * Trigger machine check on the host. We assume all the MSRs are already set up
6401 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6402 * We pass a fake environment to the machine check handler because we want
6403 * the guest to be always treated like user space, no matter what context
6404 * it used internally.
6405 */
6406static void kvm_machine_check(void)
6407{
6408#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6409 struct pt_regs regs = {
6410 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6411 .flags = X86_EFLAGS_IF,
6412 };
6413
6414 do_machine_check(&regs, 0);
6415#endif
6416}
6417
Avi Kivity851ba692009-08-24 11:10:17 +03006418static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08006419{
6420 /* already handled by vcpu_run */
6421 return 1;
6422}
6423
Avi Kivity851ba692009-08-24 11:10:17 +03006424static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006425{
Avi Kivity1155f762007-11-22 11:30:47 +02006426 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006427 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006428 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006429 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006430 u32 vect_info;
6431 enum emulation_result er;
6432
Avi Kivity1155f762007-11-22 11:30:47 +02006433 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02006434 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006435
Andi Kleena0861c02009-06-08 17:37:09 +08006436 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03006437 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006438
Jim Mattsonef85b672016-12-12 11:01:37 -08006439 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02006440 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03006441
Wanpeng Li082d06e2018-04-03 16:28:48 -07006442 if (is_invalid_opcode(intr_info))
6443 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006444
Avi Kivity6aa8b732006-12-10 02:21:36 -08006445 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06006446 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006447 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006448
Liran Alon9e869482018-03-12 13:12:51 +02006449 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
6450 WARN_ON_ONCE(!enable_vmware_backdoor);
6451 er = emulate_instruction(vcpu,
6452 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
6453 if (er == EMULATE_USER_EXIT)
6454 return 0;
6455 else if (er != EMULATE_DONE)
6456 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
6457 return 1;
6458 }
6459
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006460 /*
6461 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6462 * MMIO, it is better to report an internal error.
6463 * See the comments in vmx_handle_exit.
6464 */
6465 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6466 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6467 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6468 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006469 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006470 vcpu->run->internal.data[0] = vect_info;
6471 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006472 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006473 return 0;
6474 }
6475
Avi Kivity6aa8b732006-12-10 02:21:36 -08006476 if (is_page_fault(intr_info)) {
6477 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006478 /* EPT won't cause page fault directly */
6479 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02006480 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006481 }
6482
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006483 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006484
6485 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6486 return handle_rmode_exception(vcpu, ex_no, error_code);
6487
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006488 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01006489 case AC_VECTOR:
6490 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6491 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006492 case DB_VECTOR:
6493 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6494 if (!(vcpu->guest_debug &
6495 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01006496 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006497 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07006498 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01006499 skip_emulated_instruction(vcpu);
6500
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006501 kvm_queue_exception(vcpu, DB_VECTOR);
6502 return 1;
6503 }
6504 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6505 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6506 /* fall through */
6507 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006508 /*
6509 * Update instruction length as we may reinject #BP from
6510 * user space while in guest debugging mode. Reading it for
6511 * #DB as well causes no harm, it is not used in that case.
6512 */
6513 vmx->vcpu.arch.event_exit_inst_len =
6514 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006515 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006516 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006517 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6518 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006519 break;
6520 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006521 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6522 kvm_run->ex.exception = ex_no;
6523 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006524 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006525 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006526 return 0;
6527}
6528
Avi Kivity851ba692009-08-24 11:10:17 +03006529static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006530{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006531 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006532 return 1;
6533}
6534
Avi Kivity851ba692009-08-24 11:10:17 +03006535static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006536{
Avi Kivity851ba692009-08-24 11:10:17 +03006537 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006538 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006539 return 0;
6540}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006541
Avi Kivity851ba692009-08-24 11:10:17 +03006542static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006543{
He, Qingbfdaab02007-09-12 14:18:28 +08006544 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08006545 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02006546 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006547
He, Qingbfdaab02007-09-12 14:18:28 +08006548 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006549 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006550
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006551 ++vcpu->stat.io_exits;
6552
Sean Christopherson432baf62018-03-08 08:57:26 -08006553 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01006554 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006555
6556 port = exit_qualification >> 16;
6557 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08006558 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006559
Sean Christophersondca7f122018-03-08 08:57:27 -08006560 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006561}
6562
Ingo Molnar102d8322007-02-19 14:37:47 +02006563static void
6564vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6565{
6566 /*
6567 * Patch in the VMCALL instruction:
6568 */
6569 hypercall[0] = 0x0f;
6570 hypercall[1] = 0x01;
6571 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006572}
6573
Guo Chao0fa06072012-06-28 15:16:19 +08006574/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006575static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6576{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006577 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006578 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6579 unsigned long orig_val = val;
6580
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006581 /*
6582 * We get here when L2 changed cr0 in a way that did not change
6583 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006584 * but did change L0 shadowed bits. So we first calculate the
6585 * effective cr0 value that L1 would like to write into the
6586 * hardware. It consists of the L2-owned bits from the new
6587 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006588 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006589 val = (val & ~vmcs12->cr0_guest_host_mask) |
6590 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6591
David Matlack38991522016-11-29 18:14:08 -08006592 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006593 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006594
6595 if (kvm_set_cr0(vcpu, val))
6596 return 1;
6597 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006598 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006599 } else {
6600 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08006601 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006602 return 1;
David Matlack38991522016-11-29 18:14:08 -08006603
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006604 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006605 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006606}
6607
6608static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6609{
6610 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006611 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6612 unsigned long orig_val = val;
6613
6614 /* analogously to handle_set_cr0 */
6615 val = (val & ~vmcs12->cr4_guest_host_mask) |
6616 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6617 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006618 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006619 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006620 return 0;
6621 } else
6622 return kvm_set_cr4(vcpu, val);
6623}
6624
Paolo Bonzini0367f202016-07-12 10:44:55 +02006625static int handle_desc(struct kvm_vcpu *vcpu)
6626{
6627 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
6628 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6629}
6630
Avi Kivity851ba692009-08-24 11:10:17 +03006631static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006632{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006633 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006634 int cr;
6635 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03006636 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006637 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006638
He, Qingbfdaab02007-09-12 14:18:28 +08006639 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006640 cr = exit_qualification & 15;
6641 reg = (exit_qualification >> 8) & 15;
6642 switch ((exit_qualification >> 4) & 3) {
6643 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03006644 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006645 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006646 switch (cr) {
6647 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006648 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006649 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006650 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006651 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03006652 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006653 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006654 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006655 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006656 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006657 case 8: {
6658 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03006659 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006660 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006661 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006662 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08006663 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006664 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006665 return ret;
6666 /*
6667 * TODO: we might be squashing a
6668 * KVM_GUESTDBG_SINGLESTEP-triggered
6669 * KVM_EXIT_DEBUG here.
6670 */
Avi Kivity851ba692009-08-24 11:10:17 +03006671 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006672 return 0;
6673 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02006674 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006675 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03006676 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006677 WARN_ONCE(1, "Guest should always own CR0.TS");
6678 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02006679 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08006680 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006681 case 1: /*mov from cr*/
6682 switch (cr) {
6683 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006684 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02006685 val = kvm_read_cr3(vcpu);
6686 kvm_register_write(vcpu, reg, val);
6687 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006688 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006689 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006690 val = kvm_get_cr8(vcpu);
6691 kvm_register_write(vcpu, reg, val);
6692 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006693 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006694 }
6695 break;
6696 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02006697 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02006698 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02006699 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006700
Kyle Huey6affcbe2016-11-29 12:40:40 -08006701 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006702 default:
6703 break;
6704 }
Avi Kivity851ba692009-08-24 11:10:17 +03006705 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03006706 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08006707 (int)(exit_qualification >> 4) & 3, cr);
6708 return 0;
6709}
6710
Avi Kivity851ba692009-08-24 11:10:17 +03006711static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006712{
He, Qingbfdaab02007-09-12 14:18:28 +08006713 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006714 int dr, dr7, reg;
6715
6716 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6717 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6718
6719 /* First, if DR does not exist, trigger UD */
6720 if (!kvm_require_dr(vcpu, dr))
6721 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006722
Jan Kiszkaf2483412010-01-20 18:20:20 +01006723 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03006724 if (!kvm_require_cpl(vcpu, 0))
6725 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006726 dr7 = vmcs_readl(GUEST_DR7);
6727 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006728 /*
6729 * As the vm-exit takes precedence over the debug trap, we
6730 * need to emulate the latter, either for the host or the
6731 * guest debugging itself.
6732 */
6733 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03006734 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006735 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02006736 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006737 vcpu->run->debug.arch.exception = DB_VECTOR;
6738 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006739 return 0;
6740 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02006741 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006742 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006743 kvm_queue_exception(vcpu, DB_VECTOR);
6744 return 1;
6745 }
6746 }
6747
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006748 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01006749 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6750 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006751
6752 /*
6753 * No more DR vmexits; force a reload of the debug registers
6754 * and reenter on this instruction. The next vmexit will
6755 * retrieve the full state of the debug registers.
6756 */
6757 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
6758 return 1;
6759 }
6760
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006761 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
6762 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03006763 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006764
6765 if (kvm_get_dr(vcpu, dr, &val))
6766 return 1;
6767 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03006768 } else
Nadav Amit57773922014-06-18 17:19:23 +03006769 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006770 return 1;
6771
Kyle Huey6affcbe2016-11-29 12:40:40 -08006772 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006773}
6774
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01006775static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
6776{
6777 return vcpu->arch.dr6;
6778}
6779
6780static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
6781{
6782}
6783
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006784static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
6785{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006786 get_debugreg(vcpu->arch.db[0], 0);
6787 get_debugreg(vcpu->arch.db[1], 1);
6788 get_debugreg(vcpu->arch.db[2], 2);
6789 get_debugreg(vcpu->arch.db[3], 3);
6790 get_debugreg(vcpu->arch.dr6, 6);
6791 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
6792
6793 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01006794 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006795}
6796
Gleb Natapov020df072010-04-13 10:05:23 +03006797static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
6798{
6799 vmcs_writel(GUEST_DR7, val);
6800}
6801
Avi Kivity851ba692009-08-24 11:10:17 +03006802static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006803{
Kyle Huey6a908b62016-11-29 12:40:37 -08006804 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006805}
6806
Avi Kivity851ba692009-08-24 11:10:17 +03006807static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006808{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006809 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006810 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006811
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006812 msr_info.index = ecx;
6813 msr_info.host_initiated = false;
6814 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02006815 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006816 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006817 return 1;
6818 }
6819
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006820 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006821
Avi Kivity6aa8b732006-12-10 02:21:36 -08006822 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006823 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
6824 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006825 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006826}
6827
Avi Kivity851ba692009-08-24 11:10:17 +03006828static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006829{
Will Auld8fe8ab42012-11-29 12:42:12 -08006830 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006831 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6832 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
6833 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006834
Will Auld8fe8ab42012-11-29 12:42:12 -08006835 msr.data = data;
6836 msr.index = ecx;
6837 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03006838 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02006839 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006840 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006841 return 1;
6842 }
6843
Avi Kivity59200272010-01-25 19:47:02 +02006844 trace_kvm_msr_write(ecx, data);
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_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006849{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01006850 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006851 return 1;
6852}
6853
Avi Kivity851ba692009-08-24 11:10:17 +03006854static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006855{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006856 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6857 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006858
Avi Kivity3842d132010-07-27 12:30:24 +03006859 kvm_make_request(KVM_REQ_EVENT, vcpu);
6860
Jan Kiszkaa26bf122008-09-26 09:30:45 +02006861 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006862 return 1;
6863}
6864
Avi Kivity851ba692009-08-24 11:10:17 +03006865static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006866{
Avi Kivityd3bef152007-06-05 15:53:05 +03006867 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006868}
6869
Avi Kivity851ba692009-08-24 11:10:17 +03006870static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02006871{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03006872 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02006873}
6874
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006875static int handle_invd(struct kvm_vcpu *vcpu)
6876{
Andre Przywara51d8b662010-12-21 11:12:02 +01006877 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006878}
6879
Avi Kivity851ba692009-08-24 11:10:17 +03006880static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03006881{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006882 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006883
6884 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006885 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006886}
6887
Avi Kivityfee84b02011-11-10 14:57:25 +02006888static int handle_rdpmc(struct kvm_vcpu *vcpu)
6889{
6890 int err;
6891
6892 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006893 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02006894}
6895
Avi Kivity851ba692009-08-24 11:10:17 +03006896static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02006897{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006898 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02006899}
6900
Dexuan Cui2acf9232010-06-10 11:27:12 +08006901static int handle_xsetbv(struct kvm_vcpu *vcpu)
6902{
6903 u64 new_bv = kvm_read_edx_eax(vcpu);
6904 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
6905
6906 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006907 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08006908 return 1;
6909}
6910
Wanpeng Lif53cd632014-12-02 19:14:58 +08006911static int handle_xsaves(struct kvm_vcpu *vcpu)
6912{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006913 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08006914 WARN(1, "this should never happen\n");
6915 return 1;
6916}
6917
6918static int handle_xrstors(struct kvm_vcpu *vcpu)
6919{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006920 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08006921 WARN(1, "this should never happen\n");
6922 return 1;
6923}
6924
Avi Kivity851ba692009-08-24 11:10:17 +03006925static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08006926{
Kevin Tian58fbbf22011-08-30 13:56:17 +03006927 if (likely(fasteoi)) {
6928 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6929 int access_type, offset;
6930
6931 access_type = exit_qualification & APIC_ACCESS_TYPE;
6932 offset = exit_qualification & APIC_ACCESS_OFFSET;
6933 /*
6934 * Sane guest uses MOV to write EOI, with written value
6935 * not cared. So make a short-circuit here by avoiding
6936 * heavy instruction emulation.
6937 */
6938 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6939 (offset == APIC_EOI)) {
6940 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006941 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03006942 }
6943 }
Andre Przywara51d8b662010-12-21 11:12:02 +01006944 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08006945}
6946
Yang Zhangc7c9c562013-01-25 10:18:51 +08006947static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6948{
6949 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6950 int vector = exit_qualification & 0xff;
6951
6952 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6953 kvm_apic_set_eoi_accelerated(vcpu, vector);
6954 return 1;
6955}
6956
Yang Zhang83d4c282013-01-25 10:18:49 +08006957static int handle_apic_write(struct kvm_vcpu *vcpu)
6958{
6959 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6960 u32 offset = exit_qualification & 0xfff;
6961
6962 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6963 kvm_apic_write_nodecode(vcpu, offset);
6964 return 1;
6965}
6966
Avi Kivity851ba692009-08-24 11:10:17 +03006967static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02006968{
Jan Kiszka60637aa2008-09-26 09:30:47 +02006969 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02006970 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02006971 bool has_error_code = false;
6972 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02006973 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006974 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006975
6976 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006977 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006978 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02006979
6980 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6981
6982 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006983 if (reason == TASK_SWITCH_GATE && idt_v) {
6984 switch (type) {
6985 case INTR_TYPE_NMI_INTR:
6986 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02006987 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006988 break;
6989 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006990 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006991 kvm_clear_interrupt_queue(vcpu);
6992 break;
6993 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02006994 if (vmx->idt_vectoring_info &
6995 VECTORING_INFO_DELIVER_CODE_MASK) {
6996 has_error_code = true;
6997 error_code =
6998 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6999 }
7000 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007001 case INTR_TYPE_SOFT_EXCEPTION:
7002 kvm_clear_exception_queue(vcpu);
7003 break;
7004 default:
7005 break;
7006 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007007 }
Izik Eidus37817f22008-03-24 23:14:53 +02007008 tss_selector = exit_qualification;
7009
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007010 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7011 type != INTR_TYPE_EXT_INTR &&
7012 type != INTR_TYPE_NMI_INTR))
7013 skip_emulated_instruction(vcpu);
7014
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007015 if (kvm_task_switch(vcpu, tss_selector,
7016 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7017 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007018 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7019 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7020 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007021 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007022 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007023
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007024 /*
7025 * TODO: What about debug traps on tss switch?
7026 * Are we supposed to inject them and update dr6?
7027 */
7028
7029 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007030}
7031
Avi Kivity851ba692009-08-24 11:10:17 +03007032static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007033{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007034 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007035 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007036 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007037
Sheng Yangf9c617f2009-03-25 10:08:52 +08007038 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007039
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007040 /*
7041 * EPT violation happened while executing iret from NMI,
7042 * "blocked by NMI" bit has to be set before next VM entry.
7043 * There are errata that may cause this bit to not be set:
7044 * AAK134, BY25.
7045 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007046 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007047 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007048 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007049 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7050
Sheng Yang14394422008-04-28 12:24:45 +08007051 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007052 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007053
Junaid Shahid27959a42016-12-06 16:46:10 -08007054 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007055 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007056 ? PFERR_USER_MASK : 0;
7057 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007058 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007059 ? PFERR_WRITE_MASK : 0;
7060 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007061 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007062 ? PFERR_FETCH_MASK : 0;
7063 /* ept page table entry is present? */
7064 error_code |= (exit_qualification &
7065 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7066 EPT_VIOLATION_EXECUTABLE))
7067 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007068
Paolo Bonzinieebed242016-11-28 14:39:58 +01007069 error_code |= (exit_qualification & 0x100) != 0 ?
7070 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007071
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007072 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007073 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007074}
7075
Avi Kivity851ba692009-08-24 11:10:17 +03007076static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007077{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007078 gpa_t gpa;
7079
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007080 /*
7081 * A nested guest cannot optimize MMIO vmexits, because we have an
7082 * nGPA here instead of the required GPA.
7083 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007084 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007085 if (!is_guest_mode(vcpu) &&
7086 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007087 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007088 /*
7089 * Doing kvm_skip_emulated_instruction() depends on undefined
7090 * behavior: Intel's manual doesn't mandate
7091 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7092 * occurs and while on real hardware it was observed to be set,
7093 * other hypervisors (namely Hyper-V) don't set it, we end up
7094 * advancing IP with some random value. Disable fast mmio when
7095 * running nested and keep it for real hardware in hope that
7096 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7097 */
7098 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7099 return kvm_skip_emulated_instruction(vcpu);
7100 else
7101 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
7102 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007103 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007104
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007105 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007106}
7107
Avi Kivity851ba692009-08-24 11:10:17 +03007108static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007109{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007110 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007111 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7112 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007113 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007114 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007115
7116 return 1;
7117}
7118
Mohammed Gamal80ced182009-09-01 12:48:18 +02007119static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007120{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007121 struct vcpu_vmx *vmx = to_vmx(vcpu);
7122 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007123 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007124 u32 cpu_exec_ctrl;
7125 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007126 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007127
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007128 /*
7129 * We should never reach the point where we are emulating L2
7130 * due to invalid guest state as that means we incorrectly
7131 * allowed a nested VMEntry with an invalid vmcs12.
7132 */
7133 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7134
Avi Kivity49e9d552010-09-19 14:34:08 +02007135 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7136 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007137
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007138 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007139 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007140 return handle_interrupt_window(&vmx->vcpu);
7141
Radim Krčmář72875d82017-04-26 22:32:19 +02007142 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007143 return 1;
7144
Liran Alon9b8ae632017-11-05 16:56:34 +02007145 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007146
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007147 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007148 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007149 ret = 0;
7150 goto out;
7151 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007152
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007153 if (err != EMULATE_DONE)
7154 goto emulation_error;
7155
7156 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7157 vcpu->arch.exception.pending)
7158 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007159
Gleb Natapov8d76c492013-05-08 18:38:44 +03007160 if (vcpu->arch.halt_request) {
7161 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007162 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007163 goto out;
7164 }
7165
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007166 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007167 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007168 if (need_resched())
7169 schedule();
7170 }
7171
Mohammed Gamal80ced182009-09-01 12:48:18 +02007172out:
7173 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007174
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007175emulation_error:
7176 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7177 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7178 vcpu->run->internal.ndata = 0;
7179 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007180}
7181
7182static void grow_ple_window(struct kvm_vcpu *vcpu)
7183{
7184 struct vcpu_vmx *vmx = to_vmx(vcpu);
7185 int old = vmx->ple_window;
7186
Babu Mogerc8e88712018-03-16 16:37:24 -04007187 vmx->ple_window = __grow_ple_window(old, ple_window,
7188 ple_window_grow,
7189 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007190
7191 if (vmx->ple_window != old)
7192 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007193
7194 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007195}
7196
7197static void shrink_ple_window(struct kvm_vcpu *vcpu)
7198{
7199 struct vcpu_vmx *vmx = to_vmx(vcpu);
7200 int old = vmx->ple_window;
7201
Babu Mogerc8e88712018-03-16 16:37:24 -04007202 vmx->ple_window = __shrink_ple_window(old, ple_window,
7203 ple_window_shrink,
7204 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007205
7206 if (vmx->ple_window != old)
7207 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007208
7209 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007210}
7211
7212/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007213 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7214 */
7215static void wakeup_handler(void)
7216{
7217 struct kvm_vcpu *vcpu;
7218 int cpu = smp_processor_id();
7219
7220 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7221 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7222 blocked_vcpu_list) {
7223 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7224
7225 if (pi_test_on(pi_desc) == 1)
7226 kvm_vcpu_kick(vcpu);
7227 }
7228 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7229}
7230
Peng Haoe01bca22018-04-07 05:47:32 +08007231static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007232{
7233 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7234 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7235 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7236 0ull, VMX_EPT_EXECUTABLE_MASK,
7237 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007238 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007239
7240 ept_set_mmio_spte_mask();
7241 kvm_enable_tdp();
7242}
7243
Tiejun Chenf2c76482014-10-28 10:14:47 +08007244static __init int hardware_setup(void)
7245{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007246 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007247
7248 rdmsrl_safe(MSR_EFER, &host_efer);
7249
7250 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7251 kvm_define_shared_msr(i, vmx_msr_index[i]);
7252
Radim Krčmář23611332016-09-29 22:41:33 +02007253 for (i = 0; i < VMX_BITMAP_NR; i++) {
7254 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7255 if (!vmx_bitmap[i])
7256 goto out;
7257 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007258
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007259 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7260 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7261
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007262 if (setup_vmcs_config(&vmcs_config) < 0) {
7263 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007264 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007265 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007266
7267 if (boot_cpu_has(X86_FEATURE_NX))
7268 kvm_enable_efer_bits(EFER_NX);
7269
Wanpeng Li08d839c2017-03-23 05:30:08 -07007270 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7271 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007272 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007273
Tiejun Chenf2c76482014-10-28 10:14:47 +08007274 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007275 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007276 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007277 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007278 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007279
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007280 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007281 enable_ept_ad_bits = 0;
7282
Wanpeng Li8ad81822017-10-09 15:51:53 -07007283 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007284 enable_unrestricted_guest = 0;
7285
Paolo Bonziniad15a292015-01-30 16:18:49 +01007286 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007287 flexpriority_enabled = 0;
7288
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007289 if (!cpu_has_virtual_nmis())
7290 enable_vnmi = 0;
7291
Paolo Bonziniad15a292015-01-30 16:18:49 +01007292 /*
7293 * set_apic_access_page_addr() is used to reload apic access
7294 * page upon invalidation. No need to do anything if not
7295 * using the APIC_ACCESS_ADDR VMCS field.
7296 */
7297 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007298 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007299
7300 if (!cpu_has_vmx_tpr_shadow())
7301 kvm_x86_ops->update_cr8_intercept = NULL;
7302
7303 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7304 kvm_disable_largepages();
7305
Wanpeng Li0f107682017-09-28 18:06:24 -07007306 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007307 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007308 ple_window = 0;
7309 ple_window_grow = 0;
7310 ple_window_max = 0;
7311 ple_window_shrink = 0;
7312 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007313
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007314 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007315 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007316 kvm_x86_ops->sync_pir_to_irr = NULL;
7317 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007318
Haozhong Zhang64903d62015-10-20 15:39:09 +08007319 if (cpu_has_vmx_tsc_scaling()) {
7320 kvm_has_tsc_control = true;
7321 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7322 kvm_tsc_scaling_ratio_frac_bits = 48;
7323 }
7324
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007325 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7326
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007327 if (enable_ept)
7328 vmx_enable_tdp();
7329 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007330 kvm_disable_tdp();
7331
Kai Huang843e4332015-01-28 10:54:28 +08007332 /*
7333 * Only enable PML when hardware supports PML feature, and both EPT
7334 * and EPT A/D bit features are enabled -- PML depends on them to work.
7335 */
7336 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7337 enable_pml = 0;
7338
7339 if (!enable_pml) {
7340 kvm_x86_ops->slot_enable_log_dirty = NULL;
7341 kvm_x86_ops->slot_disable_log_dirty = NULL;
7342 kvm_x86_ops->flush_log_dirty = NULL;
7343 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7344 }
7345
Yunhong Jiang64672c92016-06-13 14:19:59 -07007346 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7347 u64 vmx_msr;
7348
7349 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7350 cpu_preemption_timer_multi =
7351 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7352 } else {
7353 kvm_x86_ops->set_hv_timer = NULL;
7354 kvm_x86_ops->cancel_hv_timer = NULL;
7355 }
7356
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007357 if (!cpu_has_vmx_shadow_vmcs())
7358 enable_shadow_vmcs = 0;
7359 if (enable_shadow_vmcs)
7360 init_vmcs_shadow_fields();
7361
Feng Wubf9f6ac2015-09-18 22:29:55 +08007362 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007363 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007364
Ashok Rajc45dcc72016-06-22 14:59:56 +08007365 kvm_mce_cap_supported |= MCG_LMCE_P;
7366
Tiejun Chenf2c76482014-10-28 10:14:47 +08007367 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007368
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007369out:
Radim Krčmář23611332016-09-29 22:41:33 +02007370 for (i = 0; i < VMX_BITMAP_NR; i++)
7371 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007372
7373 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007374}
7375
7376static __exit void hardware_unsetup(void)
7377{
Radim Krčmář23611332016-09-29 22:41:33 +02007378 int i;
7379
7380 for (i = 0; i < VMX_BITMAP_NR; i++)
7381 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007382
Tiejun Chenf2c76482014-10-28 10:14:47 +08007383 free_kvm_area();
7384}
7385
Avi Kivity6aa8b732006-12-10 02:21:36 -08007386/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007387 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7388 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7389 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03007390static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007391{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007392 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007393 grow_ple_window(vcpu);
7394
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08007395 /*
7396 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7397 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7398 * never set PAUSE_EXITING and just set PLE if supported,
7399 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7400 */
7401 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007402 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007403}
7404
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007405static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08007406{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007407 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08007408}
7409
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007410static int handle_mwait(struct kvm_vcpu *vcpu)
7411{
7412 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7413 return handle_nop(vcpu);
7414}
7415
Jim Mattson45ec3682017-08-23 16:32:04 -07007416static int handle_invalid_op(struct kvm_vcpu *vcpu)
7417{
7418 kvm_queue_exception(vcpu, UD_VECTOR);
7419 return 1;
7420}
7421
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007422static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7423{
7424 return 1;
7425}
7426
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007427static int handle_monitor(struct kvm_vcpu *vcpu)
7428{
7429 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7430 return handle_nop(vcpu);
7431}
7432
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007433/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007434 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7435 * set the success or error code of an emulated VMX instruction, as specified
7436 * by Vol 2B, VMX Instruction Reference, "Conventions".
7437 */
7438static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7439{
7440 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7441 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7442 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7443}
7444
7445static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7446{
7447 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7448 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7449 X86_EFLAGS_SF | X86_EFLAGS_OF))
7450 | X86_EFLAGS_CF);
7451}
7452
Abel Gordon145c28d2013-04-18 14:36:55 +03007453static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007454 u32 vm_instruction_error)
7455{
7456 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7457 /*
7458 * failValid writes the error number to the current VMCS, which
7459 * can't be done there isn't a current VMCS.
7460 */
7461 nested_vmx_failInvalid(vcpu);
7462 return;
7463 }
7464 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7465 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7466 X86_EFLAGS_SF | X86_EFLAGS_OF))
7467 | X86_EFLAGS_ZF);
7468 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7469 /*
7470 * We don't need to force a shadow sync because
7471 * VM_INSTRUCTION_ERROR is not shadowed
7472 */
7473}
Abel Gordon145c28d2013-04-18 14:36:55 +03007474
Wincy Vanff651cb2014-12-11 08:52:58 +03007475static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7476{
7477 /* TODO: not to reset guest simply here. */
7478 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007479 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007480}
7481
Jan Kiszkaf4124502014-03-07 20:03:13 +01007482static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7483{
7484 struct vcpu_vmx *vmx =
7485 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7486
7487 vmx->nested.preemption_timer_expired = true;
7488 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7489 kvm_vcpu_kick(&vmx->vcpu);
7490
7491 return HRTIMER_NORESTART;
7492}
7493
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007494/*
Bandan Das19677e32014-05-06 02:19:15 -04007495 * Decode the memory-address operand of a vmx instruction, as recorded on an
7496 * exit caused by such an instruction (run by a guest hypervisor).
7497 * On success, returns 0. When the operand is invalid, returns 1 and throws
7498 * #UD or #GP.
7499 */
7500static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7501 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007502 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007503{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007504 gva_t off;
7505 bool exn;
7506 struct kvm_segment s;
7507
Bandan Das19677e32014-05-06 02:19:15 -04007508 /*
7509 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7510 * Execution", on an exit, vmx_instruction_info holds most of the
7511 * addressing components of the operand. Only the displacement part
7512 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7513 * For how an actual address is calculated from all these components,
7514 * refer to Vol. 1, "Operand Addressing".
7515 */
7516 int scaling = vmx_instruction_info & 3;
7517 int addr_size = (vmx_instruction_info >> 7) & 7;
7518 bool is_reg = vmx_instruction_info & (1u << 10);
7519 int seg_reg = (vmx_instruction_info >> 15) & 7;
7520 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7521 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7522 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7523 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7524
7525 if (is_reg) {
7526 kvm_queue_exception(vcpu, UD_VECTOR);
7527 return 1;
7528 }
7529
7530 /* Addr = segment_base + offset */
7531 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007532 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007533 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007534 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007535 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007536 off += kvm_register_read(vcpu, index_reg)<<scaling;
7537 vmx_get_segment(vcpu, &s, seg_reg);
7538 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007539
7540 if (addr_size == 1) /* 32 bit */
7541 *ret &= 0xffffffff;
7542
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007543 /* Checks for #GP/#SS exceptions. */
7544 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007545 if (is_long_mode(vcpu)) {
7546 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7547 * non-canonical form. This is the only check on the memory
7548 * destination for long mode!
7549 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007550 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007551 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007552 /* Protected mode: apply checks for segment validity in the
7553 * following order:
7554 * - segment type check (#GP(0) may be thrown)
7555 * - usability check (#GP(0)/#SS(0))
7556 * - limit check (#GP(0)/#SS(0))
7557 */
7558 if (wr)
7559 /* #GP(0) if the destination operand is located in a
7560 * read-only data segment or any code segment.
7561 */
7562 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7563 else
7564 /* #GP(0) if the source operand is located in an
7565 * execute-only code segment
7566 */
7567 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007568 if (exn) {
7569 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7570 return 1;
7571 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007572 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7573 */
7574 exn = (s.unusable != 0);
7575 /* Protected mode: #GP(0)/#SS(0) if the memory
7576 * operand is outside the segment limit.
7577 */
7578 exn = exn || (off + sizeof(u64) > s.limit);
7579 }
7580 if (exn) {
7581 kvm_queue_exception_e(vcpu,
7582 seg_reg == VCPU_SREG_SS ?
7583 SS_VECTOR : GP_VECTOR,
7584 0);
7585 return 1;
7586 }
7587
Bandan Das19677e32014-05-06 02:19:15 -04007588 return 0;
7589}
7590
Radim Krčmářcbf71272017-05-19 15:48:51 +02007591static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007592{
7593 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04007594 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04007595
7596 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007597 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007598 return 1;
7599
Radim Krčmářcbf71272017-05-19 15:48:51 +02007600 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, vmpointer,
7601 sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007602 kvm_inject_page_fault(vcpu, &e);
7603 return 1;
7604 }
7605
Bandan Das3573e222014-05-06 02:19:16 -04007606 return 0;
7607}
7608
Jim Mattsone29acc52016-11-30 12:03:43 -08007609static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7610{
7611 struct vcpu_vmx *vmx = to_vmx(vcpu);
7612 struct vmcs *shadow_vmcs;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007613 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08007614
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007615 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7616 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06007617 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08007618
7619 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7620 if (!vmx->nested.cached_vmcs12)
7621 goto out_cached_vmcs12;
7622
7623 if (enable_shadow_vmcs) {
7624 shadow_vmcs = alloc_vmcs();
7625 if (!shadow_vmcs)
7626 goto out_shadow_vmcs;
7627 /* mark vmcs as shadow */
7628 shadow_vmcs->revision_id |= (1u << 31);
7629 /* init shadow vmcs */
7630 vmcs_clear(shadow_vmcs);
7631 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7632 }
7633
Jim Mattsone29acc52016-11-30 12:03:43 -08007634 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7635 HRTIMER_MODE_REL_PINNED);
7636 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7637
7638 vmx->nested.vmxon = true;
7639 return 0;
7640
7641out_shadow_vmcs:
7642 kfree(vmx->nested.cached_vmcs12);
7643
7644out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06007645 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08007646
Jim Mattsonde3a0022017-11-27 17:22:25 -06007647out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08007648 return -ENOMEM;
7649}
7650
Bandan Das3573e222014-05-06 02:19:16 -04007651/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007652 * Emulate the VMXON instruction.
7653 * Currently, we just remember that VMX is active, and do not save or even
7654 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7655 * do not currently need to store anything in that guest-allocated memory
7656 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7657 * argument is different from the VMXON pointer (which the spec says they do).
7658 */
7659static int handle_vmon(struct kvm_vcpu *vcpu)
7660{
Jim Mattsone29acc52016-11-30 12:03:43 -08007661 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007662 gpa_t vmptr;
7663 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007664 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007665 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7666 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007667
Jim Mattson70f3aac2017-04-26 08:53:46 -07007668 /*
7669 * The Intel VMX Instruction Reference lists a bunch of bits that are
7670 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
7671 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
7672 * Otherwise, we should fail with #UD. But most faulting conditions
7673 * have already been checked by hardware, prior to the VM-exit for
7674 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
7675 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007676 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07007677 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007678 kvm_queue_exception(vcpu, UD_VECTOR);
7679 return 1;
7680 }
7681
Abel Gordon145c28d2013-04-18 14:36:55 +03007682 if (vmx->nested.vmxon) {
7683 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007684 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03007685 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007686
Haozhong Zhang3b840802016-06-22 14:59:54 +08007687 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007688 != VMXON_NEEDED_FEATURES) {
7689 kvm_inject_gp(vcpu, 0);
7690 return 1;
7691 }
7692
Radim Krčmářcbf71272017-05-19 15:48:51 +02007693 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08007694 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007695
7696 /*
7697 * SDM 3: 24.11.5
7698 * The first 4 bytes of VMXON region contain the supported
7699 * VMCS revision identifier
7700 *
7701 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
7702 * which replaces physical address width with 32
7703 */
7704 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7705 nested_vmx_failInvalid(vcpu);
7706 return kvm_skip_emulated_instruction(vcpu);
7707 }
7708
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02007709 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7710 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02007711 nested_vmx_failInvalid(vcpu);
7712 return kvm_skip_emulated_instruction(vcpu);
7713 }
7714 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
7715 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007716 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007717 nested_vmx_failInvalid(vcpu);
7718 return kvm_skip_emulated_instruction(vcpu);
7719 }
7720 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007721 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007722
7723 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08007724 ret = enter_vmx_operation(vcpu);
7725 if (ret)
7726 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007727
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007728 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007729 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007730}
7731
7732/*
7733 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7734 * for running VMX instructions (except VMXON, whose prerequisites are
7735 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07007736 * Note that many of these exceptions have priority over VM exits, so they
7737 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007738 */
7739static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7740{
Jim Mattson70f3aac2017-04-26 08:53:46 -07007741 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007742 kvm_queue_exception(vcpu, UD_VECTOR);
7743 return 0;
7744 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007745 return 1;
7746}
7747
David Matlack8ca44e82017-08-01 14:00:39 -07007748static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
7749{
7750 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
7751 vmcs_write64(VMCS_LINK_POINTER, -1ull);
7752}
7753
Abel Gordone7953d72013-04-18 14:37:55 +03007754static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7755{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007756 if (vmx->nested.current_vmptr == -1ull)
7757 return;
7758
Abel Gordon012f83c2013-04-18 14:39:25 +03007759 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007760 /* copy to memory all shadowed fields in case
7761 they were modified */
7762 copy_shadow_to_vmcs12(vmx);
7763 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07007764 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03007765 }
Wincy Van705699a2015-02-03 23:58:17 +08007766 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07007767
7768 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02007769 kvm_vcpu_write_guest_page(&vmx->vcpu,
7770 vmx->nested.current_vmptr >> PAGE_SHIFT,
7771 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07007772
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007773 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03007774}
7775
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007776/*
7777 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7778 * just stops using VMX.
7779 */
7780static void free_nested(struct vcpu_vmx *vmx)
7781{
Wanpeng Lib7455822017-11-22 14:04:00 -08007782 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007783 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007784
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007785 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08007786 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07007787 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07007788 vmx->nested.posted_intr_nv = -1;
7789 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007790 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07007791 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007792 vmcs_clear(vmx->vmcs01.shadow_vmcs);
7793 free_vmcs(vmx->vmcs01.shadow_vmcs);
7794 vmx->vmcs01.shadow_vmcs = NULL;
7795 }
David Matlack4f2777b2016-07-13 17:16:37 -07007796 kfree(vmx->nested.cached_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06007797 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007798 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02007799 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007800 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007801 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007802 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02007803 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007804 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007805 }
Wincy Van705699a2015-02-03 23:58:17 +08007806 if (vmx->nested.pi_desc_page) {
7807 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007808 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08007809 vmx->nested.pi_desc_page = NULL;
7810 vmx->nested.pi_desc = NULL;
7811 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007812
Jim Mattsonde3a0022017-11-27 17:22:25 -06007813 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007814}
7815
7816/* Emulate the VMXOFF instruction */
7817static int handle_vmoff(struct kvm_vcpu *vcpu)
7818{
7819 if (!nested_vmx_check_permission(vcpu))
7820 return 1;
7821 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007822 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007823 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007824}
7825
Nadav Har'El27d6c862011-05-25 23:06:59 +03007826/* Emulate the VMCLEAR instruction */
7827static int handle_vmclear(struct kvm_vcpu *vcpu)
7828{
7829 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08007830 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007831 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007832
7833 if (!nested_vmx_check_permission(vcpu))
7834 return 1;
7835
Radim Krčmářcbf71272017-05-19 15:48:51 +02007836 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03007837 return 1;
7838
Radim Krčmářcbf71272017-05-19 15:48:51 +02007839 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7840 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
7841 return kvm_skip_emulated_instruction(vcpu);
7842 }
7843
7844 if (vmptr == vmx->nested.vmxon_ptr) {
7845 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
7846 return kvm_skip_emulated_instruction(vcpu);
7847 }
7848
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007849 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03007850 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007851
Jim Mattson587d7e722017-03-02 12:41:48 -08007852 kvm_vcpu_write_guest(vcpu,
7853 vmptr + offsetof(struct vmcs12, launch_state),
7854 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03007855
Nadav Har'El27d6c862011-05-25 23:06:59 +03007856 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007857 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007858}
7859
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007860static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7861
7862/* Emulate the VMLAUNCH instruction */
7863static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7864{
7865 return nested_vmx_run(vcpu, true);
7866}
7867
7868/* Emulate the VMRESUME instruction */
7869static int handle_vmresume(struct kvm_vcpu *vcpu)
7870{
7871
7872 return nested_vmx_run(vcpu, false);
7873}
7874
Nadav Har'El49f705c2011-05-25 23:08:30 +03007875/*
7876 * Read a vmcs12 field. Since these can have varying lengths and we return
7877 * one type, we chose the biggest type (u64) and zero-extend the return value
7878 * to that size. Note that the caller, handle_vmread, might need to use only
7879 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7880 * 64-bit fields are to be returned).
7881 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007882static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7883 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03007884{
7885 short offset = vmcs_field_to_offset(field);
7886 char *p;
7887
7888 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007889 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007890
7891 p = ((char *)(get_vmcs12(vcpu))) + offset;
7892
Jim Mattsond37f4262017-12-22 12:12:16 -08007893 switch (vmcs_field_width(field)) {
7894 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007895 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007896 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007897 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007898 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007899 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007900 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007901 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007902 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007903 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007904 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007905 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007906 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007907 WARN_ON(1);
7908 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007909 }
7910}
7911
Abel Gordon20b97fe2013-04-18 14:36:25 +03007912
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007913static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7914 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03007915 short offset = vmcs_field_to_offset(field);
7916 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7917 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007918 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007919
Jim Mattsond37f4262017-12-22 12:12:16 -08007920 switch (vmcs_field_width(field)) {
7921 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007922 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007923 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007924 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007925 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007926 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007927 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007928 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007929 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007930 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007931 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007932 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007933 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007934 WARN_ON(1);
7935 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007936 }
7937
7938}
7939
Abel Gordon16f5b902013-04-18 14:38:25 +03007940static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7941{
7942 int i;
7943 unsigned long field;
7944 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007945 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007946 const u16 *fields = shadow_read_write_fields;
Mathias Krausec2bae892013-06-26 20:36:21 +02007947 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03007948
Jan Kiszka282da872014-10-08 18:05:39 +02007949 preempt_disable();
7950
Abel Gordon16f5b902013-04-18 14:38:25 +03007951 vmcs_load(shadow_vmcs);
7952
7953 for (i = 0; i < num_fields; i++) {
7954 field = fields[i];
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007955 field_value = __vmcs_readl(field);
Abel Gordon16f5b902013-04-18 14:38:25 +03007956 vmcs12_write_any(&vmx->vcpu, field, field_value);
7957 }
7958
7959 vmcs_clear(shadow_vmcs);
7960 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02007961
7962 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03007963}
7964
Abel Gordonc3114422013-04-18 14:38:55 +03007965static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7966{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007967 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02007968 shadow_read_write_fields,
7969 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03007970 };
Mathias Krausec2bae892013-06-26 20:36:21 +02007971 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03007972 max_shadow_read_write_fields,
7973 max_shadow_read_only_fields
7974 };
7975 int i, q;
7976 unsigned long field;
7977 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007978 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03007979
7980 vmcs_load(shadow_vmcs);
7981
Mathias Krausec2bae892013-06-26 20:36:21 +02007982 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03007983 for (i = 0; i < max_fields[q]; i++) {
7984 field = fields[q][i];
7985 vmcs12_read_any(&vmx->vcpu, field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007986 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03007987 }
7988 }
7989
7990 vmcs_clear(shadow_vmcs);
7991 vmcs_load(vmx->loaded_vmcs->vmcs);
7992}
7993
Nadav Har'El49f705c2011-05-25 23:08:30 +03007994/*
7995 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7996 * used before) all generate the same failure when it is missing.
7997 */
7998static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
7999{
8000 struct vcpu_vmx *vmx = to_vmx(vcpu);
8001 if (vmx->nested.current_vmptr == -1ull) {
8002 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008003 return 0;
8004 }
8005 return 1;
8006}
8007
8008static int handle_vmread(struct kvm_vcpu *vcpu)
8009{
8010 unsigned long field;
8011 u64 field_value;
8012 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8013 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8014 gva_t gva = 0;
8015
Kyle Hueyeb277562016-11-29 12:40:39 -08008016 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008017 return 1;
8018
Kyle Huey6affcbe2016-11-29 12:40:40 -08008019 if (!nested_vmx_check_vmcs12(vcpu))
8020 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008021
Nadav Har'El49f705c2011-05-25 23:08:30 +03008022 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008023 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008024 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008025 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008026 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008027 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008028 }
8029 /*
8030 * Now copy part of this value to register or memory, as requested.
8031 * Note that the number of bits actually copied is 32 or 64 depending
8032 * on the guest's mode (32 or 64 bit), not on the given field's length.
8033 */
8034 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008035 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008036 field_value);
8037 } else {
8038 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008039 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008040 return 1;
Jim Mattson70f3aac2017-04-26 08:53:46 -07008041 /* _system ok, as hardware has verified cpl=0 */
Nadav Har'El49f705c2011-05-25 23:08:30 +03008042 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
8043 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
8044 }
8045
8046 nested_vmx_succeed(vcpu);
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
8051static int handle_vmwrite(struct kvm_vcpu *vcpu)
8052{
8053 unsigned long field;
8054 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008055 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008056 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8057 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008058
Nadav Har'El49f705c2011-05-25 23:08:30 +03008059 /* The value to write might be 32 or 64 bits, depending on L1's long
8060 * mode, and eventually we need to write that into a field of several
8061 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008062 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008063 * bits into the vmcs12 field.
8064 */
8065 u64 field_value = 0;
8066 struct x86_exception e;
8067
Kyle Hueyeb277562016-11-29 12:40:39 -08008068 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008069 return 1;
8070
Kyle Huey6affcbe2016-11-29 12:40:40 -08008071 if (!nested_vmx_check_vmcs12(vcpu))
8072 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008073
Nadav Har'El49f705c2011-05-25 23:08:30 +03008074 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008075 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008076 (((vmx_instruction_info) >> 3) & 0xf));
8077 else {
8078 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008079 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008080 return 1;
8081 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
Nadav Amit27e6fb52014-06-18 17:19:26 +03008082 &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008083 kvm_inject_page_fault(vcpu, &e);
8084 return 1;
8085 }
8086 }
8087
8088
Nadav Amit27e6fb52014-06-18 17:19:26 +03008089 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008090 if (vmcs_field_readonly(field)) {
8091 nested_vmx_failValid(vcpu,
8092 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008093 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008094 }
8095
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008096 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008097 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008098 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008099 }
8100
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008101 switch (field) {
8102#define SHADOW_FIELD_RW(x) case x:
8103#include "vmx_shadow_fields.h"
8104 /*
8105 * The fields that can be updated by L1 without a vmexit are
8106 * always updated in the vmcs02, the others go down the slow
8107 * path of prepare_vmcs02.
8108 */
8109 break;
8110 default:
8111 vmx->nested.dirty_vmcs12 = true;
8112 break;
8113 }
8114
Nadav Har'El49f705c2011-05-25 23:08:30 +03008115 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008116 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008117}
8118
Jim Mattsona8bc2842016-11-30 12:03:44 -08008119static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8120{
8121 vmx->nested.current_vmptr = vmptr;
8122 if (enable_shadow_vmcs) {
8123 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8124 SECONDARY_EXEC_SHADOW_VMCS);
8125 vmcs_write64(VMCS_LINK_POINTER,
8126 __pa(vmx->vmcs01.shadow_vmcs));
8127 vmx->nested.sync_shadow_vmcs = true;
8128 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008129 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008130}
8131
Nadav Har'El63846662011-05-25 23:07:29 +03008132/* Emulate the VMPTRLD instruction */
8133static int handle_vmptrld(struct kvm_vcpu *vcpu)
8134{
8135 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008136 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008137
8138 if (!nested_vmx_check_permission(vcpu))
8139 return 1;
8140
Radim Krčmářcbf71272017-05-19 15:48:51 +02008141 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008142 return 1;
8143
Radim Krčmářcbf71272017-05-19 15:48:51 +02008144 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8145 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8146 return kvm_skip_emulated_instruction(vcpu);
8147 }
8148
8149 if (vmptr == vmx->nested.vmxon_ptr) {
8150 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8151 return kvm_skip_emulated_instruction(vcpu);
8152 }
8153
Nadav Har'El63846662011-05-25 23:07:29 +03008154 if (vmx->nested.current_vmptr != vmptr) {
8155 struct vmcs12 *new_vmcs12;
8156 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008157 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8158 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008159 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008160 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008161 }
8162 new_vmcs12 = kmap(page);
8163 if (new_vmcs12->revision_id != VMCS12_REVISION) {
8164 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008165 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008166 nested_vmx_failValid(vcpu,
8167 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008168 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008169 }
Nadav Har'El63846662011-05-25 23:07:29 +03008170
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008171 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008172 /*
8173 * Load VMCS12 from guest memory since it is not already
8174 * cached.
8175 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008176 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8177 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008178 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008179
Jim Mattsona8bc2842016-11-30 12:03:44 -08008180 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008181 }
8182
8183 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008184 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008185}
8186
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008187/* Emulate the VMPTRST instruction */
8188static int handle_vmptrst(struct kvm_vcpu *vcpu)
8189{
8190 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8191 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8192 gva_t vmcs_gva;
8193 struct x86_exception e;
8194
8195 if (!nested_vmx_check_permission(vcpu))
8196 return 1;
8197
8198 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008199 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008200 return 1;
Jim Mattson70f3aac2017-04-26 08:53:46 -07008201 /* ok to use *_system, as hardware has verified cpl=0 */
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008202 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
8203 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8204 sizeof(u64), &e)) {
8205 kvm_inject_page_fault(vcpu, &e);
8206 return 1;
8207 }
8208 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008209 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008210}
8211
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008212/* Emulate the INVEPT instruction */
8213static int handle_invept(struct kvm_vcpu *vcpu)
8214{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008215 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008216 u32 vmx_instruction_info, types;
8217 unsigned long type;
8218 gva_t gva;
8219 struct x86_exception e;
8220 struct {
8221 u64 eptp, gpa;
8222 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008223
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008224 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008225 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008226 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008227 kvm_queue_exception(vcpu, UD_VECTOR);
8228 return 1;
8229 }
8230
8231 if (!nested_vmx_check_permission(vcpu))
8232 return 1;
8233
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008234 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008235 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008236
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008237 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008238
Jim Mattson85c856b2016-10-26 08:38:38 -07008239 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008240 nested_vmx_failValid(vcpu,
8241 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008242 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008243 }
8244
8245 /* According to the Intel VMX instruction reference, the memory
8246 * operand is read even if it isn't needed (e.g., for type==global)
8247 */
8248 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008249 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008250 return 1;
8251 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
8252 sizeof(operand), &e)) {
8253 kvm_inject_page_fault(vcpu, &e);
8254 return 1;
8255 }
8256
8257 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008258 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008259 /*
8260 * TODO: track mappings and invalidate
8261 * single context requests appropriately
8262 */
8263 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008264 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008265 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008266 nested_vmx_succeed(vcpu);
8267 break;
8268 default:
8269 BUG_ON(1);
8270 break;
8271 }
8272
Kyle Huey6affcbe2016-11-29 12:40:40 -08008273 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008274}
8275
Petr Matouseka642fc32014-09-23 20:22:30 +02008276static int handle_invvpid(struct kvm_vcpu *vcpu)
8277{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008278 struct vcpu_vmx *vmx = to_vmx(vcpu);
8279 u32 vmx_instruction_info;
8280 unsigned long type, types;
8281 gva_t gva;
8282 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008283 struct {
8284 u64 vpid;
8285 u64 gla;
8286 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008287
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008288 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008289 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008290 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008291 kvm_queue_exception(vcpu, UD_VECTOR);
8292 return 1;
8293 }
8294
8295 if (!nested_vmx_check_permission(vcpu))
8296 return 1;
8297
8298 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8299 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8300
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008301 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008302 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008303
Jim Mattson85c856b2016-10-26 08:38:38 -07008304 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008305 nested_vmx_failValid(vcpu,
8306 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008307 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008308 }
8309
8310 /* according to the intel vmx instruction reference, the memory
8311 * operand is read even if it isn't needed (e.g., for type==global)
8312 */
8313 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8314 vmx_instruction_info, false, &gva))
8315 return 1;
Jim Mattson40352602017-06-28 09:37:37 -07008316 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
8317 sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008318 kvm_inject_page_fault(vcpu, &e);
8319 return 1;
8320 }
Jim Mattson40352602017-06-28 09:37:37 -07008321 if (operand.vpid >> 16) {
8322 nested_vmx_failValid(vcpu,
8323 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8324 return kvm_skip_emulated_instruction(vcpu);
8325 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008326
8327 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008328 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Yu Zhangfd8cb432017-08-24 20:27:56 +08008329 if (is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07008330 nested_vmx_failValid(vcpu,
8331 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8332 return kvm_skip_emulated_instruction(vcpu);
8333 }
8334 /* fall through */
Paolo Bonzinief697a72016-03-18 16:58:38 +01008335 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008336 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07008337 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008338 nested_vmx_failValid(vcpu,
8339 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008340 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008341 }
8342 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008343 case VMX_VPID_EXTENT_ALL_CONTEXT:
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008344 break;
8345 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008346 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008347 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008348 }
8349
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08008350 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008351 nested_vmx_succeed(vcpu);
8352
Kyle Huey6affcbe2016-11-29 12:40:40 -08008353 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02008354}
8355
Kai Huang843e4332015-01-28 10:54:28 +08008356static int handle_pml_full(struct kvm_vcpu *vcpu)
8357{
8358 unsigned long exit_qualification;
8359
8360 trace_kvm_pml_full(vcpu->vcpu_id);
8361
8362 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8363
8364 /*
8365 * PML buffer FULL happened while executing iret from NMI,
8366 * "blocked by NMI" bit has to be set before next VM entry.
8367 */
8368 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008369 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08008370 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8371 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8372 GUEST_INTR_STATE_NMI);
8373
8374 /*
8375 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8376 * here.., and there's no userspace involvement needed for PML.
8377 */
8378 return 1;
8379}
8380
Yunhong Jiang64672c92016-06-13 14:19:59 -07008381static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8382{
8383 kvm_lapic_expired_hv_timer(vcpu);
8384 return 1;
8385}
8386
Bandan Das41ab9372017-08-03 15:54:43 -04008387static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8388{
8389 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04008390 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8391
8392 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008393 switch (address & VMX_EPTP_MT_MASK) {
8394 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008395 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008396 return false;
8397 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02008398 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008399 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008400 return false;
8401 break;
8402 default:
8403 return false;
8404 }
8405
David Hildenbrandbb97a012017-08-10 23:15:28 +02008406 /* only 4 levels page-walk length are valid */
8407 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04008408 return false;
8409
8410 /* Reserved bits should not be set */
8411 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8412 return false;
8413
8414 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008415 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008416 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008417 return false;
8418 }
8419
8420 return true;
8421}
8422
8423static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8424 struct vmcs12 *vmcs12)
8425{
8426 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8427 u64 address;
8428 bool accessed_dirty;
8429 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8430
8431 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8432 !nested_cpu_has_ept(vmcs12))
8433 return 1;
8434
8435 if (index >= VMFUNC_EPTP_ENTRIES)
8436 return 1;
8437
8438
8439 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8440 &address, index * 8, 8))
8441 return 1;
8442
David Hildenbrandbb97a012017-08-10 23:15:28 +02008443 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04008444
8445 /*
8446 * If the (L2) guest does a vmfunc to the currently
8447 * active ept pointer, we don't have to do anything else
8448 */
8449 if (vmcs12->ept_pointer != address) {
8450 if (!valid_ept_address(vcpu, address))
8451 return 1;
8452
8453 kvm_mmu_unload(vcpu);
8454 mmu->ept_ad = accessed_dirty;
8455 mmu->base_role.ad_disabled = !accessed_dirty;
8456 vmcs12->ept_pointer = address;
8457 /*
8458 * TODO: Check what's the correct approach in case
8459 * mmu reload fails. Currently, we just let the next
8460 * reload potentially fail
8461 */
8462 kvm_mmu_reload(vcpu);
8463 }
8464
8465 return 0;
8466}
8467
Bandan Das2a499e42017-08-03 15:54:41 -04008468static int handle_vmfunc(struct kvm_vcpu *vcpu)
8469{
Bandan Das27c42a12017-08-03 15:54:42 -04008470 struct vcpu_vmx *vmx = to_vmx(vcpu);
8471 struct vmcs12 *vmcs12;
8472 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8473
8474 /*
8475 * VMFUNC is only supported for nested guests, but we always enable the
8476 * secondary control for simplicity; for non-nested mode, fake that we
8477 * didn't by injecting #UD.
8478 */
8479 if (!is_guest_mode(vcpu)) {
8480 kvm_queue_exception(vcpu, UD_VECTOR);
8481 return 1;
8482 }
8483
8484 vmcs12 = get_vmcs12(vcpu);
8485 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8486 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04008487
8488 switch (function) {
8489 case 0:
8490 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8491 goto fail;
8492 break;
8493 default:
8494 goto fail;
8495 }
8496 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04008497
8498fail:
8499 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8500 vmcs_read32(VM_EXIT_INTR_INFO),
8501 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04008502 return 1;
8503}
8504
Nadav Har'El0140cae2011-05-25 23:06:28 +03008505/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08008506 * The exit handlers return 1 if the exit was handled fully and guest execution
8507 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8508 * to be done to userspace and return 0.
8509 */
Mathias Krause772e0312012-08-30 01:30:19 +02008510static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08008511 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8512 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08008513 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08008514 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008515 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008516 [EXIT_REASON_CR_ACCESS] = handle_cr,
8517 [EXIT_REASON_DR_ACCESS] = handle_dr,
8518 [EXIT_REASON_CPUID] = handle_cpuid,
8519 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8520 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8521 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8522 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02008523 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03008524 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02008525 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02008526 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03008527 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008528 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03008529 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008530 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008531 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008532 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008533 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008534 [EXIT_REASON_VMOFF] = handle_vmoff,
8535 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08008536 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8537 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08008538 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008539 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02008540 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08008541 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02008542 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08008543 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02008544 [EXIT_REASON_GDTR_IDTR] = handle_desc,
8545 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03008546 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8547 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008548 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008549 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008550 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008551 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008552 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02008553 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07008554 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07008555 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08008556 [EXIT_REASON_XSAVES] = handle_xsaves,
8557 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08008558 [EXIT_REASON_PML_FULL] = handle_pml_full,
Bandan Das2a499e42017-08-03 15:54:41 -04008559 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07008560 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008561};
8562
8563static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04008564 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008565
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008566static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8567 struct vmcs12 *vmcs12)
8568{
8569 unsigned long exit_qualification;
8570 gpa_t bitmap, last_bitmap;
8571 unsigned int port;
8572 int size;
8573 u8 b;
8574
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008575 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05008576 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008577
8578 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8579
8580 port = exit_qualification >> 16;
8581 size = (exit_qualification & 7) + 1;
8582
8583 last_bitmap = (gpa_t)-1;
8584 b = -1;
8585
8586 while (size > 0) {
8587 if (port < 0x8000)
8588 bitmap = vmcs12->io_bitmap_a;
8589 else if (port < 0x10000)
8590 bitmap = vmcs12->io_bitmap_b;
8591 else
Joe Perches1d804d02015-03-30 16:46:09 -07008592 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008593 bitmap += (port & 0x7fff) / 8;
8594
8595 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008596 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008597 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008598 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07008599 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008600
8601 port++;
8602 size--;
8603 last_bitmap = bitmap;
8604 }
8605
Joe Perches1d804d02015-03-30 16:46:09 -07008606 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008607}
8608
Nadav Har'El644d7112011-05-25 23:12:35 +03008609/*
8610 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8611 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8612 * disinterest in the current event (read or write a specific MSR) by using an
8613 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8614 */
8615static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8616 struct vmcs12 *vmcs12, u32 exit_reason)
8617{
8618 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8619 gpa_t bitmap;
8620
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01008621 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07008622 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008623
8624 /*
8625 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8626 * for the four combinations of read/write and low/high MSR numbers.
8627 * First we need to figure out which of the four to use:
8628 */
8629 bitmap = vmcs12->msr_bitmap;
8630 if (exit_reason == EXIT_REASON_MSR_WRITE)
8631 bitmap += 2048;
8632 if (msr_index >= 0xc0000000) {
8633 msr_index -= 0xc0000000;
8634 bitmap += 1024;
8635 }
8636
8637 /* Then read the msr_index'th bit from this bitmap: */
8638 if (msr_index < 1024*8) {
8639 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008640 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008641 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008642 return 1 & (b >> (msr_index & 7));
8643 } else
Joe Perches1d804d02015-03-30 16:46:09 -07008644 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03008645}
8646
8647/*
8648 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8649 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8650 * intercept (via guest_host_mask etc.) the current event.
8651 */
8652static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8653 struct vmcs12 *vmcs12)
8654{
8655 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8656 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008657 int reg;
8658 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03008659
8660 switch ((exit_qualification >> 4) & 3) {
8661 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008662 reg = (exit_qualification >> 8) & 15;
8663 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03008664 switch (cr) {
8665 case 0:
8666 if (vmcs12->cr0_guest_host_mask &
8667 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008668 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008669 break;
8670 case 3:
8671 if ((vmcs12->cr3_target_count >= 1 &&
8672 vmcs12->cr3_target_value0 == val) ||
8673 (vmcs12->cr3_target_count >= 2 &&
8674 vmcs12->cr3_target_value1 == val) ||
8675 (vmcs12->cr3_target_count >= 3 &&
8676 vmcs12->cr3_target_value2 == val) ||
8677 (vmcs12->cr3_target_count >= 4 &&
8678 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07008679 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008680 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008681 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008682 break;
8683 case 4:
8684 if (vmcs12->cr4_guest_host_mask &
8685 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07008686 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008687 break;
8688 case 8:
8689 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008690 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008691 break;
8692 }
8693 break;
8694 case 2: /* clts */
8695 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
8696 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008697 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008698 break;
8699 case 1: /* mov from cr */
8700 switch (cr) {
8701 case 3:
8702 if (vmcs12->cpu_based_vm_exec_control &
8703 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008704 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008705 break;
8706 case 8:
8707 if (vmcs12->cpu_based_vm_exec_control &
8708 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008709 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008710 break;
8711 }
8712 break;
8713 case 3: /* lmsw */
8714 /*
8715 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
8716 * cr0. Other attempted changes are ignored, with no exit.
8717 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008718 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03008719 if (vmcs12->cr0_guest_host_mask & 0xe &
8720 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008721 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008722 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
8723 !(vmcs12->cr0_read_shadow & 0x1) &&
8724 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07008725 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008726 break;
8727 }
Joe Perches1d804d02015-03-30 16:46:09 -07008728 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008729}
8730
8731/*
8732 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8733 * should handle it ourselves in L0 (and then continue L2). Only call this
8734 * when in is_guest_mode (L2).
8735 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02008736static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03008737{
Nadav Har'El644d7112011-05-25 23:12:35 +03008738 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8739 struct vcpu_vmx *vmx = to_vmx(vcpu);
8740 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8741
Jim Mattson4f350c62017-09-14 16:31:44 -07008742 if (vmx->nested.nested_run_pending)
8743 return false;
8744
8745 if (unlikely(vmx->fail)) {
8746 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8747 vmcs_read32(VM_INSTRUCTION_ERROR));
8748 return true;
8749 }
Jan Kiszka542060e2014-01-04 18:47:21 +01008750
David Matlackc9f04402017-08-01 14:00:40 -07008751 /*
8752 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06008753 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
8754 * Page). The CPU may write to these pages via their host
8755 * physical address while L2 is running, bypassing any
8756 * address-translation-based dirty tracking (e.g. EPT write
8757 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07008758 *
8759 * Mark them dirty on every exit from L2 to prevent them from
8760 * getting out of sync with dirty tracking.
8761 */
8762 nested_mark_vmcs12_pages_dirty(vcpu);
8763
Jim Mattson4f350c62017-09-14 16:31:44 -07008764 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
8765 vmcs_readl(EXIT_QUALIFICATION),
8766 vmx->idt_vectoring_info,
8767 intr_info,
8768 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8769 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03008770
8771 switch (exit_reason) {
8772 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08008773 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07008774 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008775 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07008776 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01008777 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01008778 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008779 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01008780 else if (is_debug(intr_info) &&
8781 vcpu->guest_debug &
8782 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8783 return false;
8784 else if (is_breakpoint(intr_info) &&
8785 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8786 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008787 return vmcs12->exception_bitmap &
8788 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8789 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07008790 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008791 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07008792 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008793 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008794 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008795 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008796 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008797 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07008798 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008799 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07008800 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008801 case EXIT_REASON_HLT:
8802 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8803 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07008804 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008805 case EXIT_REASON_INVLPG:
8806 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8807 case EXIT_REASON_RDPMC:
8808 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02008809 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02008810 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02008811 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02008812 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01008813 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03008814 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8815 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8816 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8817 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8818 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8819 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02008820 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03008821 /*
8822 * VMX instructions trap unconditionally. This allows L1 to
8823 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8824 */
Joe Perches1d804d02015-03-30 16:46:09 -07008825 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008826 case EXIT_REASON_CR_ACCESS:
8827 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8828 case EXIT_REASON_DR_ACCESS:
8829 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8830 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008831 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02008832 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
8833 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03008834 case EXIT_REASON_MSR_READ:
8835 case EXIT_REASON_MSR_WRITE:
8836 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8837 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07008838 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008839 case EXIT_REASON_MWAIT_INSTRUCTION:
8840 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008841 case EXIT_REASON_MONITOR_TRAP_FLAG:
8842 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03008843 case EXIT_REASON_MONITOR_INSTRUCTION:
8844 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8845 case EXIT_REASON_PAUSE_INSTRUCTION:
8846 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8847 nested_cpu_has2(vmcs12,
8848 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8849 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07008850 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008851 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008852 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03008853 case EXIT_REASON_APIC_ACCESS:
8854 return nested_cpu_has2(vmcs12,
8855 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Wincy Van82f0dd42015-02-03 23:57:18 +08008856 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08008857 case EXIT_REASON_EOI_INDUCED:
8858 /* apic_write and eoi_induced should exit unconditionally. */
Joe Perches1d804d02015-03-30 16:46:09 -07008859 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008860 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008861 /*
8862 * L0 always deals with the EPT violation. If nested EPT is
8863 * used, and the nested mmu code discovers that the address is
8864 * missing in the guest EPT table (EPT12), the EPT violation
8865 * will be injected with nested_ept_inject_page_fault()
8866 */
Joe Perches1d804d02015-03-30 16:46:09 -07008867 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008868 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008869 /*
8870 * L2 never uses directly L1's EPT, but rather L0's own EPT
8871 * table (shadow on EPT) or a merged EPT table that L0 built
8872 * (EPT on EPT). So any problems with the structure of the
8873 * table is L0's fault.
8874 */
Joe Perches1d804d02015-03-30 16:46:09 -07008875 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02008876 case EXIT_REASON_INVPCID:
8877 return
8878 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
8879 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008880 case EXIT_REASON_WBINVD:
8881 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8882 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07008883 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08008884 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8885 /*
8886 * This should never happen, since it is not possible to
8887 * set XSS to a non-zero value---neither in L1 nor in L2.
8888 * If if it were, XSS would have to be checked against
8889 * the XSS exit bitmap in vmcs12.
8890 */
8891 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08008892 case EXIT_REASON_PREEMPTION_TIMER:
8893 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02008894 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04008895 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02008896 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04008897 case EXIT_REASON_VMFUNC:
8898 /* VM functions are emulated through L2->L0 vmexits. */
8899 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008900 default:
Joe Perches1d804d02015-03-30 16:46:09 -07008901 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008902 }
8903}
8904
Paolo Bonzini7313c692017-07-27 10:31:25 +02008905static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
8906{
8907 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8908
8909 /*
8910 * At this point, the exit interruption info in exit_intr_info
8911 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
8912 * we need to query the in-kernel LAPIC.
8913 */
8914 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
8915 if ((exit_intr_info &
8916 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
8917 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
8918 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8919 vmcs12->vm_exit_intr_error_code =
8920 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
8921 }
8922
8923 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
8924 vmcs_readl(EXIT_QUALIFICATION));
8925 return 1;
8926}
8927
Avi Kivity586f9602010-11-18 13:09:54 +02008928static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8929{
8930 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8931 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8932}
8933
Kai Huanga3eaa862015-11-04 13:46:05 +08008934static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08008935{
Kai Huanga3eaa862015-11-04 13:46:05 +08008936 if (vmx->pml_pg) {
8937 __free_page(vmx->pml_pg);
8938 vmx->pml_pg = NULL;
8939 }
Kai Huang843e4332015-01-28 10:54:28 +08008940}
8941
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008942static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08008943{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008944 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008945 u64 *pml_buf;
8946 u16 pml_idx;
8947
8948 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8949
8950 /* Do nothing if PML buffer is empty */
8951 if (pml_idx == (PML_ENTITY_NUM - 1))
8952 return;
8953
8954 /* PML index always points to next available PML buffer entity */
8955 if (pml_idx >= PML_ENTITY_NUM)
8956 pml_idx = 0;
8957 else
8958 pml_idx++;
8959
8960 pml_buf = page_address(vmx->pml_pg);
8961 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8962 u64 gpa;
8963
8964 gpa = pml_buf[pml_idx];
8965 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008966 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08008967 }
8968
8969 /* reset PML index */
8970 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8971}
8972
8973/*
8974 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8975 * Called before reporting dirty_bitmap to userspace.
8976 */
8977static void kvm_flush_pml_buffers(struct kvm *kvm)
8978{
8979 int i;
8980 struct kvm_vcpu *vcpu;
8981 /*
8982 * We only need to kick vcpu out of guest mode here, as PML buffer
8983 * is flushed at beginning of all VMEXITs, and it's obvious that only
8984 * vcpus running in guest are possible to have unflushed GPAs in PML
8985 * buffer.
8986 */
8987 kvm_for_each_vcpu(i, vcpu, kvm)
8988 kvm_vcpu_kick(vcpu);
8989}
8990
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008991static void vmx_dump_sel(char *name, uint32_t sel)
8992{
8993 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05008994 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008995 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8996 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
8997 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
8998}
8999
9000static void vmx_dump_dtsel(char *name, uint32_t limit)
9001{
9002 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9003 name, vmcs_read32(limit),
9004 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9005}
9006
9007static void dump_vmcs(void)
9008{
9009 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9010 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9011 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9012 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9013 u32 secondary_exec_control = 0;
9014 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009015 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009016 int i, n;
9017
9018 if (cpu_has_secondary_exec_ctrls())
9019 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9020
9021 pr_err("*** Guest State ***\n");
9022 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9023 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9024 vmcs_readl(CR0_GUEST_HOST_MASK));
9025 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9026 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9027 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9028 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9029 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9030 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009031 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9032 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9033 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9034 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009035 }
9036 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9037 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9038 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9039 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9040 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9041 vmcs_readl(GUEST_SYSENTER_ESP),
9042 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9043 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9044 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9045 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9046 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9047 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9048 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9049 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9050 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9051 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9052 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9053 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9054 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009055 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9056 efer, vmcs_read64(GUEST_IA32_PAT));
9057 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9058 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009059 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009060 if (cpu_has_load_perf_global_ctrl &&
9061 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009062 pr_err("PerfGlobCtl = 0x%016llx\n",
9063 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009064 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009065 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009066 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9067 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9068 vmcs_read32(GUEST_ACTIVITY_STATE));
9069 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9070 pr_err("InterruptStatus = %04x\n",
9071 vmcs_read16(GUEST_INTR_STATUS));
9072
9073 pr_err("*** Host State ***\n");
9074 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9075 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9076 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9077 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9078 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9079 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9080 vmcs_read16(HOST_TR_SELECTOR));
9081 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9082 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9083 vmcs_readl(HOST_TR_BASE));
9084 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9085 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9086 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9087 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9088 vmcs_readl(HOST_CR4));
9089 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9090 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9091 vmcs_read32(HOST_IA32_SYSENTER_CS),
9092 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9093 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009094 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9095 vmcs_read64(HOST_IA32_EFER),
9096 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009097 if (cpu_has_load_perf_global_ctrl &&
9098 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009099 pr_err("PerfGlobCtl = 0x%016llx\n",
9100 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009101
9102 pr_err("*** Control State ***\n");
9103 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9104 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9105 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9106 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9107 vmcs_read32(EXCEPTION_BITMAP),
9108 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9109 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9110 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9111 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9112 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9113 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9114 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9115 vmcs_read32(VM_EXIT_INTR_INFO),
9116 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9117 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9118 pr_err(" reason=%08x qualification=%016lx\n",
9119 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9120 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9121 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9122 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009123 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009124 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009125 pr_err("TSC Multiplier = 0x%016llx\n",
9126 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009127 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9128 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9129 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9130 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9131 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009132 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009133 n = vmcs_read32(CR3_TARGET_COUNT);
9134 for (i = 0; i + 1 < n; i += 4)
9135 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9136 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9137 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9138 if (i < n)
9139 pr_err("CR3 target%u=%016lx\n",
9140 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9141 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9142 pr_err("PLE Gap=%08x Window=%08x\n",
9143 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9144 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9145 pr_err("Virtual processor ID = 0x%04x\n",
9146 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9147}
9148
Avi Kivity6aa8b732006-12-10 02:21:36 -08009149/*
9150 * The guest has exited. See if we can fix it or if we need userspace
9151 * assistance.
9152 */
Avi Kivity851ba692009-08-24 11:10:17 +03009153static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009154{
Avi Kivity29bd8a72007-09-10 17:27:03 +03009155 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08009156 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02009157 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03009158
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01009159 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
9160
Kai Huang843e4332015-01-28 10:54:28 +08009161 /*
9162 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
9163 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
9164 * querying dirty_bitmap, we only need to kick all vcpus out of guest
9165 * mode as if vcpus is in root mode, the PML buffer must has been
9166 * flushed already.
9167 */
9168 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009169 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009170
Mohammed Gamal80ced182009-09-01 12:48:18 +02009171 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02009172 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02009173 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01009174
Paolo Bonzini7313c692017-07-27 10:31:25 +02009175 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
9176 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03009177
Mohammed Gamal51207022010-05-31 22:40:54 +03009178 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009179 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03009180 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9181 vcpu->run->fail_entry.hardware_entry_failure_reason
9182 = exit_reason;
9183 return 0;
9184 }
9185
Avi Kivity29bd8a72007-09-10 17:27:03 +03009186 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009187 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9188 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03009189 = vmcs_read32(VM_INSTRUCTION_ERROR);
9190 return 0;
9191 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009192
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009193 /*
9194 * Note:
9195 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9196 * delivery event since it indicates guest is accessing MMIO.
9197 * The vm-exit can be triggered again after return to guest that
9198 * will cause infinite loop.
9199 */
Mike Dayd77c26f2007-10-08 09:02:08 -04009200 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08009201 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02009202 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00009203 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009204 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9205 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9206 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009207 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009208 vcpu->run->internal.data[0] = vectoring_info;
9209 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009210 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9211 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9212 vcpu->run->internal.ndata++;
9213 vcpu->run->internal.data[3] =
9214 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9215 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009216 return 0;
9217 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009218
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009219 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009220 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9221 if (vmx_interrupt_allowed(vcpu)) {
9222 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9223 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9224 vcpu->arch.nmi_pending) {
9225 /*
9226 * This CPU don't support us in finding the end of an
9227 * NMI-blocked window if the guest runs with IRQs
9228 * disabled. So we pull the trigger after 1 s of
9229 * futile waiting, but inform the user about this.
9230 */
9231 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9232 "state on VCPU %d after 1 s timeout\n",
9233 __func__, vcpu->vcpu_id);
9234 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9235 }
9236 }
9237
Avi Kivity6aa8b732006-12-10 02:21:36 -08009238 if (exit_reason < kvm_vmx_max_exit_handlers
9239 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03009240 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009241 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01009242 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9243 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03009244 kvm_queue_exception(vcpu, UD_VECTOR);
9245 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009246 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009247}
9248
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009249static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009250{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009251 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9252
9253 if (is_guest_mode(vcpu) &&
9254 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9255 return;
9256
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009257 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009258 vmcs_write32(TPR_THRESHOLD, 0);
9259 return;
9260 }
9261
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009262 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009263}
9264
Yang Zhang8d146952013-01-25 10:18:50 +08009265static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
9266{
9267 u32 sec_exec_control;
9268
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009269 /* Postpone execution until vmcs01 is the current VMCS. */
9270 if (is_guest_mode(vcpu)) {
9271 to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
9272 return;
9273 }
9274
Wanpeng Lif6e90f92016-09-22 07:43:25 +08009275 if (!cpu_has_vmx_virtualize_x2apic_mode())
Yang Zhang8d146952013-01-25 10:18:50 +08009276 return;
9277
Paolo Bonzini35754c92015-07-29 12:05:37 +02009278 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08009279 return;
9280
9281 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9282
9283 if (set) {
9284 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9285 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9286 } else {
9287 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9288 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Junaid Shahida468f2d2018-04-26 13:09:50 -07009289 vmx_flush_tlb(vcpu, true);
Yang Zhang8d146952013-01-25 10:18:50 +08009290 }
9291 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9292
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009293 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009294}
9295
Tang Chen38b99172014-09-24 15:57:54 +08009296static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9297{
9298 struct vcpu_vmx *vmx = to_vmx(vcpu);
9299
9300 /*
9301 * Currently we do not handle the nested case where L2 has an
9302 * APIC access page of its own; that page is still pinned.
9303 * Hence, we skip the case where the VCPU is in guest mode _and_
9304 * L1 prepared an APIC access page for L2.
9305 *
9306 * For the case where L1 and L2 share the same APIC access page
9307 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
9308 * in the vmcs12), this function will only update either the vmcs01
9309 * or the vmcs02. If the former, the vmcs02 will be updated by
9310 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
9311 * the next L2->L1 exit.
9312 */
9313 if (!is_guest_mode(vcpu) ||
David Matlack4f2777b2016-07-13 17:16:37 -07009314 !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009315 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Tang Chen38b99172014-09-24 15:57:54 +08009316 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -07009317 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009318 }
Tang Chen38b99172014-09-24 15:57:54 +08009319}
9320
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009321static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009322{
9323 u16 status;
9324 u8 old;
9325
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009326 if (max_isr == -1)
9327 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009328
9329 status = vmcs_read16(GUEST_INTR_STATUS);
9330 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009331 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08009332 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009333 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009334 vmcs_write16(GUEST_INTR_STATUS, status);
9335 }
9336}
9337
9338static void vmx_set_rvi(int vector)
9339{
9340 u16 status;
9341 u8 old;
9342
Wei Wang4114c272014-11-05 10:53:43 +08009343 if (vector == -1)
9344 vector = 0;
9345
Yang Zhangc7c9c562013-01-25 10:18:51 +08009346 status = vmcs_read16(GUEST_INTR_STATUS);
9347 old = (u8)status & 0xff;
9348 if ((u8)vector != old) {
9349 status &= ~0xff;
9350 status |= (u8)vector;
9351 vmcs_write16(GUEST_INTR_STATUS, status);
9352 }
9353}
9354
9355static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9356{
Liran Alon851c1a182017-12-24 18:12:56 +02009357 /*
9358 * When running L2, updating RVI is only relevant when
9359 * vmcs12 virtual-interrupt-delivery enabled.
9360 * However, it can be enabled only when L1 also
9361 * intercepts external-interrupts and in that case
9362 * we should not update vmcs02 RVI but instead intercept
9363 * interrupt. Therefore, do nothing when running L2.
9364 */
9365 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08009366 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009367}
9368
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009369static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009370{
9371 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009372 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02009373 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009374
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009375 WARN_ON(!vcpu->arch.apicv_active);
9376 if (pi_test_on(&vmx->pi_desc)) {
9377 pi_clear_on(&vmx->pi_desc);
9378 /*
9379 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9380 * But on x86 this is just a compiler barrier anyway.
9381 */
9382 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02009383 max_irr_updated =
9384 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
9385
9386 /*
9387 * If we are running L2 and L1 has a new pending interrupt
9388 * which can be injected, we should re-evaluate
9389 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02009390 * If L1 intercepts external-interrupts, we should
9391 * exit from L2 to L1. Otherwise, interrupt should be
9392 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02009393 */
Liran Alon851c1a182017-12-24 18:12:56 +02009394 if (is_guest_mode(vcpu) && max_irr_updated) {
9395 if (nested_exit_on_intr(vcpu))
9396 kvm_vcpu_exiting_guest_mode(vcpu);
9397 else
9398 kvm_make_request(KVM_REQ_EVENT, vcpu);
9399 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009400 } else {
9401 max_irr = kvm_lapic_find_highest_irr(vcpu);
9402 }
9403 vmx_hwapic_irr_update(vcpu, max_irr);
9404 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009405}
9406
Andrey Smetanin63086302015-11-10 15:36:32 +03009407static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009408{
Andrey Smetanind62caab2015-11-10 15:36:33 +03009409 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08009410 return;
9411
Yang Zhangc7c9c562013-01-25 10:18:51 +08009412 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9413 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9414 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9415 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9416}
9417
Paolo Bonzini967235d2016-12-19 14:03:45 +01009418static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9419{
9420 struct vcpu_vmx *vmx = to_vmx(vcpu);
9421
9422 pi_clear_on(&vmx->pi_desc);
9423 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9424}
9425
Avi Kivity51aa01d2010-07-20 14:31:20 +03009426static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03009427{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009428 u32 exit_intr_info = 0;
9429 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02009430
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009431 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9432 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02009433 return;
9434
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009435 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9436 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9437 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08009438
Wanpeng Li1261bfa2017-07-13 18:30:40 -07009439 /* if exit due to PF check for async PF */
9440 if (is_page_fault(exit_intr_info))
9441 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9442
Andi Kleena0861c02009-06-08 17:37:09 +08009443 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009444 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9445 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08009446 kvm_machine_check();
9447
Gleb Natapov20f65982009-05-11 13:35:55 +03009448 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08009449 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -07009450 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03009451 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -07009452 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009453 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03009454}
Gleb Natapov20f65982009-05-11 13:35:55 +03009455
Yang Zhanga547c6d2013-04-11 19:25:10 +08009456static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9457{
9458 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9459
Yang Zhanga547c6d2013-04-11 19:25:10 +08009460 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9461 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9462 unsigned int vector;
9463 unsigned long entry;
9464 gate_desc *desc;
9465 struct vcpu_vmx *vmx = to_vmx(vcpu);
9466#ifdef CONFIG_X86_64
9467 unsigned long tmp;
9468#endif
9469
9470 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9471 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02009472 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009473 asm volatile(
9474#ifdef CONFIG_X86_64
9475 "mov %%" _ASM_SP ", %[sp]\n\t"
9476 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
9477 "push $%c[ss]\n\t"
9478 "push %[sp]\n\t"
9479#endif
9480 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08009481 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009482 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08009483 :
9484#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06009485 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009486#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -05009487 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +08009488 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009489 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009490 [ss]"i"(__KERNEL_DS),
9491 [cs]"i"(__KERNEL_CS)
9492 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02009493 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08009494}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009495STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009496
Tom Lendackybc226f02018-05-10 22:06:39 +02009497static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009498{
Tom Lendackybc226f02018-05-10 22:06:39 +02009499 switch (index) {
9500 case MSR_IA32_SMBASE:
9501 /*
9502 * We cannot do SMM unless we can run the guest in big
9503 * real mode.
9504 */
9505 return enable_unrestricted_guest || emulate_invalid_guest_state;
9506 case MSR_AMD64_VIRT_SPEC_CTRL:
9507 /* This is AMD only. */
9508 return false;
9509 default:
9510 return true;
9511 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009512}
9513
Liu, Jinsongda8999d2014-02-24 10:55:46 +00009514static bool vmx_mpx_supported(void)
9515{
9516 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
9517 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
9518}
9519
Wanpeng Li55412b22014-12-02 19:21:30 +08009520static bool vmx_xsaves_supported(void)
9521{
9522 return vmcs_config.cpu_based_2nd_exec_ctrl &
9523 SECONDARY_EXEC_XSAVES;
9524}
9525
Avi Kivity51aa01d2010-07-20 14:31:20 +03009526static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9527{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02009528 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03009529 bool unblock_nmi;
9530 u8 vector;
9531 bool idtv_info_valid;
9532
9533 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03009534
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009535 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009536 if (vmx->loaded_vmcs->nmi_known_unmasked)
9537 return;
9538 /*
9539 * Can't use vmx->exit_intr_info since we're not sure what
9540 * the exit reason is.
9541 */
9542 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9543 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9544 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9545 /*
9546 * SDM 3: 27.7.1.2 (September 2008)
9547 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9548 * a guest IRET fault.
9549 * SDM 3: 23.2.2 (September 2008)
9550 * Bit 12 is undefined in any of the following cases:
9551 * If the VM exit sets the valid bit in the IDT-vectoring
9552 * information field.
9553 * If the VM exit is due to a double fault.
9554 */
9555 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9556 vector != DF_VECTOR && !idtv_info_valid)
9557 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9558 GUEST_INTR_STATE_NMI);
9559 else
9560 vmx->loaded_vmcs->nmi_known_unmasked =
9561 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9562 & GUEST_INTR_STATE_NMI);
9563 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
9564 vmx->loaded_vmcs->vnmi_blocked_time +=
9565 ktime_to_ns(ktime_sub(ktime_get(),
9566 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03009567}
9568
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009569static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03009570 u32 idt_vectoring_info,
9571 int instr_len_field,
9572 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03009573{
Avi Kivity51aa01d2010-07-20 14:31:20 +03009574 u8 vector;
9575 int type;
9576 bool idtv_info_valid;
9577
9578 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03009579
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009580 vcpu->arch.nmi_injected = false;
9581 kvm_clear_exception_queue(vcpu);
9582 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009583
9584 if (!idtv_info_valid)
9585 return;
9586
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009587 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03009588
Avi Kivity668f6122008-07-02 09:28:55 +03009589 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
9590 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009591
Gleb Natapov64a7ec02009-03-30 16:03:29 +03009592 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03009593 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009594 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03009595 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009596 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03009597 * Clear bit "block by NMI" before VM entry if a NMI
9598 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03009599 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009600 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009601 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009602 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009603 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009604 /* fall through */
9605 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03009606 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03009607 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03009608 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03009609 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03009610 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009611 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009612 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009613 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009614 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03009615 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009616 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009617 break;
9618 default:
9619 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03009620 }
Avi Kivitycf393f72008-07-01 16:20:21 +03009621}
9622
Avi Kivity83422e12010-07-20 14:43:23 +03009623static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
9624{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009625 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03009626 VM_EXIT_INSTRUCTION_LEN,
9627 IDT_VECTORING_ERROR_CODE);
9628}
9629
Avi Kivityb463a6f2010-07-20 15:06:17 +03009630static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
9631{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009632 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03009633 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9634 VM_ENTRY_INSTRUCTION_LEN,
9635 VM_ENTRY_EXCEPTION_ERROR_CODE);
9636
9637 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
9638}
9639
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009640static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
9641{
9642 int i, nr_msrs;
9643 struct perf_guest_switch_msr *msrs;
9644
9645 msrs = perf_guest_get_msrs(&nr_msrs);
9646
9647 if (!msrs)
9648 return;
9649
9650 for (i = 0; i < nr_msrs; i++)
9651 if (msrs[i].host == msrs[i].guest)
9652 clear_atomic_switch_msr(vmx, msrs[i].msr);
9653 else
9654 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
9655 msrs[i].host);
9656}
9657
Jiang Biao33365e72016-11-03 15:03:37 +08009658static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07009659{
9660 struct vcpu_vmx *vmx = to_vmx(vcpu);
9661 u64 tscl;
9662 u32 delta_tsc;
9663
9664 if (vmx->hv_deadline_tsc == -1)
9665 return;
9666
9667 tscl = rdtsc();
9668 if (vmx->hv_deadline_tsc > tscl)
9669 /* sure to be 32 bit only because checked on set_hv_timer */
9670 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
9671 cpu_preemption_timer_multi);
9672 else
9673 delta_tsc = 0;
9674
9675 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
9676}
9677
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08009678static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009679{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009680 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009681 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +02009682
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009683 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009684 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009685 vmx->loaded_vmcs->soft_vnmi_blocked))
9686 vmx->loaded_vmcs->entry_time = ktime_get();
9687
Avi Kivity104f2262010-11-18 13:12:52 +02009688 /* Don't enter VMX if guest state is invalid, let the exit handler
9689 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02009690 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02009691 return;
9692
Radim Krčmářa7653ec2014-08-21 18:08:07 +02009693 if (vmx->ple_window_dirty) {
9694 vmx->ple_window_dirty = false;
9695 vmcs_write32(PLE_WINDOW, vmx->ple_window);
9696 }
9697
Abel Gordon012f83c2013-04-18 14:39:25 +03009698 if (vmx->nested.sync_shadow_vmcs) {
9699 copy_vmcs12_to_shadow(vmx);
9700 vmx->nested.sync_shadow_vmcs = false;
9701 }
9702
Avi Kivity104f2262010-11-18 13:12:52 +02009703 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
9704 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
9705 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
9706 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
9707
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009708 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +02009709 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009710 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +02009711 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009712 }
9713
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07009714 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +02009715 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009716 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +02009717 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009718 }
9719
Avi Kivity104f2262010-11-18 13:12:52 +02009720 /* When single-stepping over STI and MOV SS, we must clear the
9721 * corresponding interruptibility bits in the guest state. Otherwise
9722 * vmentry fails as it then expects bit 14 (BS) in pending debug
9723 * exceptions being set, but that's not correct for the guest debugging
9724 * case. */
9725 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9726 vmx_set_interrupt_shadow(vcpu, 0);
9727
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02009728 if (static_cpu_has(X86_FEATURE_PKU) &&
9729 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
9730 vcpu->arch.pkru != vmx->host_pkru)
9731 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009732
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009733 atomic_switch_perf_msrs(vmx);
9734
Yunhong Jiang64672c92016-06-13 14:19:59 -07009735 vmx_arm_hv_timer(vcpu);
9736
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009737 /*
9738 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
9739 * it's non-zero. Since vmentry is serialising on affected CPUs, there
9740 * is no need to worry about the conditional branch over the wrmsr
9741 * being speculatively taken.
9742 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +02009743 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009744
Nadav Har'Eld462b812011-05-24 15:26:10 +03009745 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009746
9747 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
9748 (unsigned long)&current_evmcs->host_rsp : 0;
9749
Avi Kivity104f2262010-11-18 13:12:52 +02009750 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08009751 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009752 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
9753 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
9754 "push %%" _ASM_CX " \n\t"
9755 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03009756 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009757 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009758 /* Avoid VMWRITE when Enlightened VMCS is in use */
9759 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
9760 "jz 2f \n\t"
9761 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
9762 "jmp 1f \n\t"
9763 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009764 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03009765 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009766 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009767 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
9768 "mov %%cr2, %%" _ASM_DX " \n\t"
9769 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009770 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009771 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009772 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009773 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02009774 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009775 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009776 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
9777 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
9778 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
9779 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
9780 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
9781 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009782#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009783 "mov %c[r8](%0), %%r8 \n\t"
9784 "mov %c[r9](%0), %%r9 \n\t"
9785 "mov %c[r10](%0), %%r10 \n\t"
9786 "mov %c[r11](%0), %%r11 \n\t"
9787 "mov %c[r12](%0), %%r12 \n\t"
9788 "mov %c[r13](%0), %%r13 \n\t"
9789 "mov %c[r14](%0), %%r14 \n\t"
9790 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009791#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009792 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03009793
Avi Kivity6aa8b732006-12-10 02:21:36 -08009794 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03009795 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009796 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009797 "jmp 2f \n\t"
9798 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
9799 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08009800 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009801 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02009802 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -08009803 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009804 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
9805 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
9806 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
9807 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
9808 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
9809 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
9810 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009811#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009812 "mov %%r8, %c[r8](%0) \n\t"
9813 "mov %%r9, %c[r9](%0) \n\t"
9814 "mov %%r10, %c[r10](%0) \n\t"
9815 "mov %%r11, %c[r11](%0) \n\t"
9816 "mov %%r12, %c[r12](%0) \n\t"
9817 "mov %%r13, %c[r13](%0) \n\t"
9818 "mov %%r14, %c[r14](%0) \n\t"
9819 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -08009820 "xor %%r8d, %%r8d \n\t"
9821 "xor %%r9d, %%r9d \n\t"
9822 "xor %%r10d, %%r10d \n\t"
9823 "xor %%r11d, %%r11d \n\t"
9824 "xor %%r12d, %%r12d \n\t"
9825 "xor %%r13d, %%r13d \n\t"
9826 "xor %%r14d, %%r14d \n\t"
9827 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009828#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009829 "mov %%cr2, %%" _ASM_AX " \n\t"
9830 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03009831
Jim Mattson0cb5b302018-01-03 14:31:38 -08009832 "xor %%eax, %%eax \n\t"
9833 "xor %%ebx, %%ebx \n\t"
9834 "xor %%esi, %%esi \n\t"
9835 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009836 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009837 ".pushsection .rodata \n\t"
9838 ".global vmx_return \n\t"
9839 "vmx_return: " _ASM_PTR " 2b \n\t"
9840 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009841 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +03009842 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02009843 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +03009844 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009845 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
9846 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
9847 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
9848 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
9849 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
9850 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
9851 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009852#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009853 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
9854 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
9855 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
9856 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
9857 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
9858 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
9859 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
9860 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08009861#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02009862 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
9863 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02009864 : "cc", "memory"
9865#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009866 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009867 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009868#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009869 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009870#endif
9871 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08009872
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009873 /*
9874 * We do not use IBRS in the kernel. If this vCPU has used the
9875 * SPEC_CTRL MSR it may have left it on; save the value and
9876 * turn it off. This is much more efficient than blindly adding
9877 * it to the atomic save/restore list. Especially as the former
9878 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
9879 *
9880 * For non-nested case:
9881 * If the L01 MSR bitmap does not intercept the MSR, then we need to
9882 * save it.
9883 *
9884 * For nested case:
9885 * If the L02 MSR bitmap does not intercept the MSR, then we need to
9886 * save it.
9887 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +01009888 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +01009889 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009890
Thomas Gleixnerccbcd262018-05-09 23:01:01 +02009891 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009892
David Woodhouse117cc7a2018-01-12 11:11:27 +00009893 /* Eliminate branch target predictions from guest mode */
9894 vmexit_fill_RSB();
9895
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009896 /* All fields are clean at this point */
9897 if (static_branch_unlikely(&enable_evmcs))
9898 current_evmcs->hv_clean_fields |=
9899 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
9900
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009901 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -08009902 if (vmx->host_debugctlmsr)
9903 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009904
Avi Kivityaa67f602012-08-01 16:48:03 +03009905#ifndef CONFIG_X86_64
9906 /*
9907 * The sysexit path does not restore ds/es, so we must set them to
9908 * a reasonable value ourselves.
9909 *
9910 * We can't defer this to vmx_load_host_state() since that function
9911 * may be executed in interrupt context, which saves and restore segments
9912 * around it, nullifying its effect.
9913 */
9914 loadsegment(ds, __USER_DS);
9915 loadsegment(es, __USER_DS);
9916#endif
9917
Avi Kivity6de4f3a2009-05-31 22:58:47 +03009918 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02009919 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009920 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03009921 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009922 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009923 vcpu->arch.regs_dirty = 0;
9924
Gleb Natapove0b890d2013-09-25 12:51:33 +03009925 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009926 * eager fpu is enabled if PKEY is supported and CR4 is switched
9927 * back on host, so it is safe to read guest PKRU from current
9928 * XSAVE.
9929 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02009930 if (static_cpu_has(X86_FEATURE_PKU) &&
9931 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
9932 vcpu->arch.pkru = __read_pkru();
9933 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009934 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009935 }
9936
Gleb Natapove0b890d2013-09-25 12:51:33 +03009937 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -07009938 vmx->idt_vectoring_info = 0;
9939
9940 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
9941 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9942 return;
9943
9944 vmx->loaded_vmcs->launched = 1;
9945 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +03009946
Avi Kivity51aa01d2010-07-20 14:31:20 +03009947 vmx_complete_atomic_exit(vmx);
9948 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03009949 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009950}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009951STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009952
Sean Christopherson434a1e92018-03-20 12:17:18 -07009953static struct kvm *vmx_vm_alloc(void)
9954{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07009955 struct kvm_vmx *kvm_vmx = kzalloc(sizeof(struct kvm_vmx), GFP_KERNEL);
9956 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -07009957}
9958
9959static void vmx_vm_free(struct kvm *kvm)
9960{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07009961 kfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -07009962}
9963
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009964static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009965{
9966 struct vcpu_vmx *vmx = to_vmx(vcpu);
9967 int cpu;
9968
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009969 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009970 return;
9971
9972 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009973 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009974 vmx_vcpu_put(vcpu);
9975 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009976 put_cpu();
9977}
9978
Jim Mattson2f1fe812016-07-08 15:36:06 -07009979/*
9980 * Ensure that the current vmcs of the logical processor is the
9981 * vmcs01 of the vcpu before calling free_nested().
9982 */
9983static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9984{
9985 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009986
Christoffer Dallec7660c2017-12-04 21:35:23 +01009987 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009988 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009989 free_nested(vmx);
9990 vcpu_put(vcpu);
9991}
9992
Avi Kivity6aa8b732006-12-10 02:21:36 -08009993static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9994{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009995 struct vcpu_vmx *vmx = to_vmx(vcpu);
9996
Kai Huang843e4332015-01-28 10:54:28 +08009997 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08009998 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08009999 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010000 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010001 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010002 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010003 kfree(vmx->guest_msrs);
10004 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010005 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010006}
10007
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010008static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010009{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010010 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010011 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010012 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010013 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010014
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010015 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010016 return ERR_PTR(-ENOMEM);
10017
Wanpeng Li991e7a02015-09-16 17:30:05 +080010018 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010019
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010020 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10021 if (err)
10022 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010023
Peter Feiner4e595162016-07-07 14:49:58 -070010024 err = -ENOMEM;
10025
10026 /*
10027 * If PML is turned on, failure on enabling PML just results in failure
10028 * of creating the vcpu, therefore we can simplify PML logic (by
10029 * avoiding dealing with cases, such as enabling PML partially on vcpus
10030 * for the guest, etc.
10031 */
10032 if (enable_pml) {
10033 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10034 if (!vmx->pml_pg)
10035 goto uninit_vcpu;
10036 }
10037
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010038 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020010039 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10040 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030010041
Peter Feiner4e595162016-07-07 14:49:58 -070010042 if (!vmx->guest_msrs)
10043 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010044
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010045 err = alloc_loaded_vmcs(&vmx->vmcs01);
10046 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010047 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010048
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010049 msr_bitmap = vmx->vmcs01.msr_bitmap;
10050 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10051 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10052 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10053 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10054 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10055 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10056 vmx->msr_bitmap_mode = 0;
10057
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010058 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030010059 cpu = get_cpu();
10060 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100010061 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020010062 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010063 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030010064 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020010065 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020010066 err = alloc_apic_access_page(kvm);
10067 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020010068 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020010069 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080010070
Sean Christophersone90008d2018-03-05 12:04:37 -080010071 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080010072 err = init_rmode_identity_map(kvm);
10073 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020010074 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080010075 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080010076
Wanpeng Li5c614b32015-10-13 09:18:36 -070010077 if (nested) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010078 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
10079 kvm_vcpu_apicv_active(&vmx->vcpu));
Wanpeng Li5c614b32015-10-13 09:18:36 -070010080 vmx->nested.vpid02 = allocate_vpid();
10081 }
Wincy Vanb9c237b2015-02-03 23:56:30 +080010082
Wincy Van705699a2015-02-03 23:58:17 +080010083 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010084 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010085
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010086 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
10087
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020010088 /*
10089 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
10090 * or POSTED_INTR_WAKEUP_VECTOR.
10091 */
10092 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
10093 vmx->pi_desc.sn = 1;
10094
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010095 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010096
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010097free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -070010098 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080010099 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010100free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010101 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070010102free_pml:
10103 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010104uninit_vcpu:
10105 kvm_vcpu_uninit(&vmx->vcpu);
10106free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080010107 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100010108 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010109 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010110}
10111
Wanpeng Lib31c1142018-03-12 04:53:04 -070010112static int vmx_vm_init(struct kvm *kvm)
10113{
10114 if (!ple_gap)
10115 kvm->arch.pause_in_guest = true;
10116 return 0;
10117}
10118
Yang, Sheng002c7f72007-07-31 14:23:01 +030010119static void __init vmx_check_processor_compat(void *rtn)
10120{
10121 struct vmcs_config vmcs_conf;
10122
10123 *(int *)rtn = 0;
10124 if (setup_vmcs_config(&vmcs_conf) < 0)
10125 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010010126 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030010127 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
10128 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
10129 smp_processor_id());
10130 *(int *)rtn = -EIO;
10131 }
10132}
10133
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010134static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080010135{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010136 u8 cache;
10137 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010138
Sheng Yang522c68c2009-04-27 20:35:43 +080010139 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020010140 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080010141 * 2. EPT with VT-d:
10142 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020010143 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080010144 * b. VT-d with snooping control feature: snooping control feature of
10145 * VT-d engine can guarantee the cache correctness. Just set it
10146 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080010147 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080010148 * consistent with host MTRR
10149 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020010150 if (is_mmio) {
10151 cache = MTRR_TYPE_UNCACHABLE;
10152 goto exit;
10153 }
10154
10155 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010156 ipat = VMX_EPT_IPAT_BIT;
10157 cache = MTRR_TYPE_WRBACK;
10158 goto exit;
10159 }
10160
10161 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
10162 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020010163 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080010164 cache = MTRR_TYPE_WRBACK;
10165 else
10166 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010167 goto exit;
10168 }
10169
Xiao Guangrongff536042015-06-15 16:55:22 +080010170 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010171
10172exit:
10173 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080010174}
10175
Sheng Yang17cc3932010-01-05 19:02:27 +080010176static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020010177{
Sheng Yang878403b2010-01-05 19:02:29 +080010178 if (enable_ept && !cpu_has_vmx_ept_1g_page())
10179 return PT_DIRECTORY_LEVEL;
10180 else
10181 /* For shadow and EPT supported 1GB page */
10182 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020010183}
10184
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010185static void vmcs_set_secondary_exec_control(u32 new_ctl)
10186{
10187 /*
10188 * These bits in the secondary execution controls field
10189 * are dynamic, the others are mostly based on the hypervisor
10190 * architecture and the guest's CPUID. Do not touch the
10191 * dynamic bits.
10192 */
10193 u32 mask =
10194 SECONDARY_EXEC_SHADOW_VMCS |
10195 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020010196 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10197 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010198
10199 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10200
10201 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10202 (new_ctl & ~mask) | (cur_ctl & mask));
10203}
10204
David Matlack8322ebb2016-11-29 18:14:09 -080010205/*
10206 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10207 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10208 */
10209static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10210{
10211 struct vcpu_vmx *vmx = to_vmx(vcpu);
10212 struct kvm_cpuid_entry2 *entry;
10213
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010214 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
10215 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080010216
10217#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10218 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010219 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080010220} while (0)
10221
10222 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10223 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10224 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10225 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10226 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10227 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10228 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10229 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10230 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10231 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10232 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10233 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10234 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10235 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10236 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10237
10238 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10239 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10240 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10241 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10242 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010010243 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080010244
10245#undef cr4_fixed1_update
10246}
10247
Sheng Yang0e851882009-12-18 16:48:46 +080010248static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10249{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010250 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010251
Paolo Bonzini80154d72017-08-24 13:55:35 +020010252 if (cpu_has_secondary_exec_ctrls()) {
10253 vmx_compute_secondary_exec_control(vmx);
10254 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010255 }
Mao, Junjiead756a12012-07-02 01:18:48 +000010256
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010257 if (nested_vmx_allowed(vcpu))
10258 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10259 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10260 else
10261 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10262 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080010263
10264 if (nested_vmx_allowed(vcpu))
10265 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +080010266}
10267
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010268static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10269{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030010270 if (func == 1 && nested)
10271 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010272}
10273
Yang Zhang25d92082013-08-06 12:00:32 +030010274static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10275 struct x86_exception *fault)
10276{
Jan Kiszka533558b2014-01-04 18:47:20 +010010277 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040010278 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010279 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010280 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030010281
Bandan Dasc5f983f2017-05-05 15:25:14 -040010282 if (vmx->nested.pml_full) {
10283 exit_reason = EXIT_REASON_PML_FULL;
10284 vmx->nested.pml_full = false;
10285 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10286 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010010287 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030010288 else
Jan Kiszka533558b2014-01-04 18:47:20 +010010289 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010290
10291 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030010292 vmcs12->guest_physical_address = fault->address;
10293}
10294
Peter Feiner995f00a2017-06-30 17:26:32 -070010295static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10296{
David Hildenbrandbb97a012017-08-10 23:15:28 +020010297 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070010298}
10299
Nadav Har'El155a97a2013-08-05 11:07:16 +030010300/* Callbacks for nested_ept_init_mmu_context: */
10301
10302static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10303{
10304 /* return the page table to be shadowed - in our case, EPT12 */
10305 return get_vmcs12(vcpu)->ept_pointer;
10306}
10307
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010308static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030010309{
Paolo Bonziniad896af2013-10-02 16:56:14 +020010310 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020010311 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010312 return 1;
10313
10314 kvm_mmu_unload(vcpu);
Paolo Bonziniad896af2013-10-02 16:56:14 +020010315 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010316 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010317 VMX_EPT_EXECUTE_ONLY_BIT,
David Hildenbranda057e0e2017-08-10 23:36:54 +020010318 nested_ept_ad_enabled(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030010319 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10320 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10321 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10322
10323 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010324 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030010325}
10326
10327static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10328{
10329 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10330}
10331
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030010332static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10333 u16 error_code)
10334{
10335 bool inequality, bit;
10336
10337 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10338 inequality =
10339 (error_code & vmcs12->page_fault_error_code_mask) !=
10340 vmcs12->page_fault_error_code_match;
10341 return inequality ^ bit;
10342}
10343
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010344static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10345 struct x86_exception *fault)
10346{
10347 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10348
10349 WARN_ON(!is_guest_mode(vcpu));
10350
Wanpeng Li305d0ab2017-09-28 18:16:44 -070010351 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10352 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020010353 vmcs12->vm_exit_intr_error_code = fault->error_code;
10354 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10355 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10356 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10357 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010358 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010359 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010360 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010361}
10362
Paolo Bonzinic9923842017-12-13 14:16:30 +010010363static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10364 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010365
10366static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010367 struct vmcs12 *vmcs12)
10368{
10369 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010370 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010371 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010372
10373 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010374 /*
10375 * Translate L1 physical address to host physical
10376 * address for vmcs02. Keep the page pinned, so this
10377 * physical address remains valid. We keep a reference
10378 * to it so we can release it later.
10379 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010380 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010381 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010382 vmx->nested.apic_access_page = NULL;
10383 }
10384 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010385 /*
10386 * If translation failed, no matter: This feature asks
10387 * to exit when accessing the given address, and if it
10388 * can never be accessed, this feature won't do
10389 * anything anyway.
10390 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010391 if (!is_error_page(page)) {
10392 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010393 hpa = page_to_phys(vmx->nested.apic_access_page);
10394 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10395 } else {
10396 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
10397 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10398 }
10399 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
10400 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
10401 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
10402 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10403 kvm_vcpu_reload_apic_access_page(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010404 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010405
10406 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010407 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010408 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010409 vmx->nested.virtual_apic_page = NULL;
10410 }
10411 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010412
10413 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010414 * If translation failed, VM entry will fail because
10415 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10416 * Failing the vm entry is _not_ what the processor
10417 * does but it's basically the only possibility we
10418 * have. We could still enter the guest if CR8 load
10419 * exits are enabled, CR8 store exits are enabled, and
10420 * virtualize APIC access is disabled; in this case
10421 * the processor would never use the TPR shadow and we
10422 * could simply clear the bit from the execution
10423 * control. But such a configuration is useless, so
10424 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010425 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010426 if (!is_error_page(page)) {
10427 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010428 hpa = page_to_phys(vmx->nested.virtual_apic_page);
10429 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
10430 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010431 }
10432
Wincy Van705699a2015-02-03 23:58:17 +080010433 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010434 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
10435 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010436 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010437 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080010438 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010439 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
10440 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010441 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010442 vmx->nested.pi_desc_page = page;
10443 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080010444 vmx->nested.pi_desc =
10445 (struct pi_desc *)((void *)vmx->nested.pi_desc +
10446 (unsigned long)(vmcs12->posted_intr_desc_addr &
10447 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010448 vmcs_write64(POSTED_INTR_DESC_ADDR,
10449 page_to_phys(vmx->nested.pi_desc_page) +
10450 (unsigned long)(vmcs12->posted_intr_desc_addr &
10451 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080010452 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080010453 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000010454 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
10455 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010456 else
10457 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
10458 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010459}
10460
Jan Kiszkaf4124502014-03-07 20:03:13 +010010461static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
10462{
10463 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
10464 struct vcpu_vmx *vmx = to_vmx(vcpu);
10465
10466 if (vcpu->arch.virtual_tsc_khz == 0)
10467 return;
10468
10469 /* Make sure short timeouts reliably trigger an immediate vmexit.
10470 * hrtimer_start does not guarantee this. */
10471 if (preemption_timeout <= 1) {
10472 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
10473 return;
10474 }
10475
10476 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10477 preemption_timeout *= 1000000;
10478 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
10479 hrtimer_start(&vmx->nested.preemption_timer,
10480 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
10481}
10482
Jim Mattson56a20512017-07-06 16:33:06 -070010483static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
10484 struct vmcs12 *vmcs12)
10485{
10486 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
10487 return 0;
10488
10489 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
10490 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
10491 return -EINVAL;
10492
10493 return 0;
10494}
10495
Wincy Van3af18d92015-02-03 23:49:31 +080010496static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
10497 struct vmcs12 *vmcs12)
10498{
Wincy Van3af18d92015-02-03 23:49:31 +080010499 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10500 return 0;
10501
Jim Mattson5fa99cb2017-07-06 16:33:07 -070010502 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080010503 return -EINVAL;
10504
10505 return 0;
10506}
10507
Jim Mattson712b12d2017-08-24 13:24:47 -070010508static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
10509 struct vmcs12 *vmcs12)
10510{
10511 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10512 return 0;
10513
10514 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
10515 return -EINVAL;
10516
10517 return 0;
10518}
10519
Wincy Van3af18d92015-02-03 23:49:31 +080010520/*
10521 * Merge L0's and L1's MSR bitmap, return false to indicate that
10522 * we do not use the hardware.
10523 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010010524static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10525 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080010526{
Wincy Van82f0dd42015-02-03 23:57:18 +080010527 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080010528 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020010529 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010530 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010010531 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010532 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010010533 *
10534 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
10535 * ensures that we do not accidentally generate an L02 MSR bitmap
10536 * from the L12 MSR bitmap that is too permissive.
10537 * 2. That L1 or L2s have actually used the MSR. This avoids
10538 * unnecessarily merging of the bitmap if the MSR is unused. This
10539 * works properly because we only update the L01 MSR bitmap lazily.
10540 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
10541 * updated to reflect this when L1 (or its L2s) actually write to
10542 * the MSR.
10543 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000010544 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
10545 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080010546
Paolo Bonzinic9923842017-12-13 14:16:30 +010010547 /* Nothing to do if the MSR bitmap is not in use. */
10548 if (!cpu_has_vmx_msr_bitmap() ||
10549 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10550 return false;
10551
Ashok Raj15d45072018-02-01 22:59:43 +010010552 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010553 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080010554 return false;
10555
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010556 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
10557 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080010558 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010010559
Radim Krčmářd048c092016-08-08 20:16:22 +020010560 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010010561 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
10562 /*
10563 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
10564 * just lets the processor take the value from the virtual-APIC page;
10565 * take those 256 bits directly from the L1 bitmap.
10566 */
10567 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10568 unsigned word = msr / BITS_PER_LONG;
10569 msr_bitmap_l0[word] = msr_bitmap_l1[word];
10570 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080010571 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010010572 } else {
10573 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10574 unsigned word = msr / BITS_PER_LONG;
10575 msr_bitmap_l0[word] = ~0;
10576 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
10577 }
10578 }
10579
10580 nested_vmx_disable_intercept_for_msr(
10581 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010582 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010583 MSR_TYPE_W);
10584
10585 if (nested_cpu_has_vid(vmcs12)) {
10586 nested_vmx_disable_intercept_for_msr(
10587 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010588 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010589 MSR_TYPE_W);
10590 nested_vmx_disable_intercept_for_msr(
10591 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010592 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010593 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080010594 }
Ashok Raj15d45072018-02-01 22:59:43 +010010595
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010596 if (spec_ctrl)
10597 nested_vmx_disable_intercept_for_msr(
10598 msr_bitmap_l1, msr_bitmap_l0,
10599 MSR_IA32_SPEC_CTRL,
10600 MSR_TYPE_R | MSR_TYPE_W);
10601
Ashok Raj15d45072018-02-01 22:59:43 +010010602 if (pred_cmd)
10603 nested_vmx_disable_intercept_for_msr(
10604 msr_bitmap_l1, msr_bitmap_l0,
10605 MSR_IA32_PRED_CMD,
10606 MSR_TYPE_W);
10607
Wincy Vanf2b93282015-02-03 23:56:03 +080010608 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010609 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080010610
10611 return true;
10612}
10613
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040010614static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
10615 struct vmcs12 *vmcs12)
10616{
10617 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
10618 !page_address_valid(vcpu, vmcs12->apic_access_addr))
10619 return -EINVAL;
10620 else
10621 return 0;
10622}
10623
Wincy Vanf2b93282015-02-03 23:56:03 +080010624static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
10625 struct vmcs12 *vmcs12)
10626{
Wincy Van82f0dd42015-02-03 23:57:18 +080010627 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080010628 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080010629 !nested_cpu_has_vid(vmcs12) &&
10630 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080010631 return 0;
10632
10633 /*
10634 * If virtualize x2apic mode is enabled,
10635 * virtualize apic access must be disabled.
10636 */
Wincy Van82f0dd42015-02-03 23:57:18 +080010637 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10638 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080010639 return -EINVAL;
10640
Wincy Van608406e2015-02-03 23:57:51 +080010641 /*
10642 * If virtual interrupt delivery is enabled,
10643 * we must exit on external interrupts.
10644 */
10645 if (nested_cpu_has_vid(vmcs12) &&
10646 !nested_exit_on_intr(vcpu))
10647 return -EINVAL;
10648
Wincy Van705699a2015-02-03 23:58:17 +080010649 /*
10650 * bits 15:8 should be zero in posted_intr_nv,
10651 * the descriptor address has been already checked
10652 * in nested_get_vmcs12_pages.
10653 */
10654 if (nested_cpu_has_posted_intr(vmcs12) &&
10655 (!nested_cpu_has_vid(vmcs12) ||
10656 !nested_exit_intr_ack_set(vcpu) ||
10657 vmcs12->posted_intr_nv & 0xff00))
10658 return -EINVAL;
10659
Wincy Vanf2b93282015-02-03 23:56:03 +080010660 /* tpr shadow is needed by all apicv features. */
10661 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10662 return -EINVAL;
10663
10664 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080010665}
10666
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010667static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
10668 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010669 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030010670{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010671 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010672 u64 count, addr;
10673
10674 if (vmcs12_read_any(vcpu, count_field, &count) ||
10675 vmcs12_read_any(vcpu, addr_field, &addr)) {
10676 WARN_ON(1);
10677 return -EINVAL;
10678 }
10679 if (count == 0)
10680 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010681 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010682 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
10683 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010684 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010685 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
10686 addr_field, maxphyaddr, count, addr);
10687 return -EINVAL;
10688 }
10689 return 0;
10690}
10691
10692static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
10693 struct vmcs12 *vmcs12)
10694{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010695 if (vmcs12->vm_exit_msr_load_count == 0 &&
10696 vmcs12->vm_exit_msr_store_count == 0 &&
10697 vmcs12->vm_entry_msr_load_count == 0)
10698 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010699 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010700 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010701 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010702 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010703 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010704 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030010705 return -EINVAL;
10706 return 0;
10707}
10708
Bandan Dasc5f983f2017-05-05 15:25:14 -040010709static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
10710 struct vmcs12 *vmcs12)
10711{
10712 u64 address = vmcs12->pml_address;
10713 int maxphyaddr = cpuid_maxphyaddr(vcpu);
10714
10715 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
10716 if (!nested_cpu_has_ept(vmcs12) ||
10717 !IS_ALIGNED(address, 4096) ||
10718 address >> maxphyaddr)
10719 return -EINVAL;
10720 }
10721
10722 return 0;
10723}
10724
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010725static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
10726 struct vmx_msr_entry *e)
10727{
10728 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020010729 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010730 return -EINVAL;
10731 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
10732 e->index == MSR_IA32_UCODE_REV)
10733 return -EINVAL;
10734 if (e->reserved != 0)
10735 return -EINVAL;
10736 return 0;
10737}
10738
10739static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
10740 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030010741{
10742 if (e->index == MSR_FS_BASE ||
10743 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010744 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
10745 nested_vmx_msr_check_common(vcpu, e))
10746 return -EINVAL;
10747 return 0;
10748}
10749
10750static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
10751 struct vmx_msr_entry *e)
10752{
10753 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
10754 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030010755 return -EINVAL;
10756 return 0;
10757}
10758
10759/*
10760 * Load guest's/host's msr at nested entry/exit.
10761 * return 0 for success, entry index for failure.
10762 */
10763static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10764{
10765 u32 i;
10766 struct vmx_msr_entry e;
10767 struct msr_data msr;
10768
10769 msr.host_initiated = false;
10770 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010771 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
10772 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010773 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010774 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10775 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010776 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010777 }
10778 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010779 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010780 "%s check failed (%u, 0x%x, 0x%x)\n",
10781 __func__, i, e.index, e.reserved);
10782 goto fail;
10783 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010784 msr.index = e.index;
10785 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010786 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010787 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010788 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10789 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030010790 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010791 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010792 }
10793 return 0;
10794fail:
10795 return i + 1;
10796}
10797
10798static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10799{
10800 u32 i;
10801 struct vmx_msr_entry e;
10802
10803 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010804 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010805 if (kvm_vcpu_read_guest(vcpu,
10806 gpa + i * sizeof(e),
10807 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010808 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010809 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10810 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010811 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010812 }
10813 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010814 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010815 "%s check failed (%u, 0x%x, 0x%x)\n",
10816 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030010817 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010818 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010819 msr_info.host_initiated = false;
10820 msr_info.index = e.index;
10821 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010822 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010823 "%s cannot read MSR (%u, 0x%x)\n",
10824 __func__, i, e.index);
10825 return -EINVAL;
10826 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010827 if (kvm_vcpu_write_guest(vcpu,
10828 gpa + i * sizeof(e) +
10829 offsetof(struct vmx_msr_entry, value),
10830 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010831 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010832 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010833 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010834 return -EINVAL;
10835 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010836 }
10837 return 0;
10838}
10839
Ladi Prosek1dc35da2016-11-30 16:03:11 +010010840static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
10841{
10842 unsigned long invalid_mask;
10843
10844 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
10845 return (val & invalid_mask) == 0;
10846}
10847
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010848/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010849 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
10850 * emulating VM entry into a guest with EPT enabled.
10851 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10852 * is assigned to entry_failure_code on failure.
10853 */
10854static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080010855 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010856{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010857 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010010858 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010859 *entry_failure_code = ENTRY_FAIL_DEFAULT;
10860 return 1;
10861 }
10862
10863 /*
10864 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
10865 * must not be dereferenced.
10866 */
10867 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
10868 !nested_ept) {
10869 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
10870 *entry_failure_code = ENTRY_FAIL_PDPTE;
10871 return 1;
10872 }
10873 }
10874
10875 vcpu->arch.cr3 = cr3;
10876 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
10877 }
10878
10879 kvm_mmu_reset_context(vcpu);
10880 return 0;
10881}
10882
Paolo Bonzini74a497f2017-12-20 13:55:39 +010010883static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10884 bool from_vmentry)
10885{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010010886 struct vcpu_vmx *vmx = to_vmx(vcpu);
10887
10888 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
10889 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
10890 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
10891 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
10892 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
10893 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
10894 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
10895 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
10896 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
10897 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
10898 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
10899 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
10900 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
10901 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
10902 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
10903 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
10904 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
10905 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
10906 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
10907 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
10908 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
10909 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
10910 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
10911 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
10912 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
10913 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
10914 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
10915 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
10916 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
10917 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
10918 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010010919
10920 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
10921 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
10922 vmcs12->guest_pending_dbg_exceptions);
10923 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
10924 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
10925
10926 if (nested_cpu_has_xsaves(vmcs12))
10927 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
10928 vmcs_write64(VMCS_LINK_POINTER, -1ull);
10929
10930 if (cpu_has_vmx_posted_intr())
10931 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
10932
10933 /*
10934 * Whether page-faults are trapped is determined by a combination of
10935 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
10936 * If enable_ept, L0 doesn't care about page faults and we should
10937 * set all of these to L1's desires. However, if !enable_ept, L0 does
10938 * care about (at least some) page faults, and because it is not easy
10939 * (if at all possible?) to merge L0 and L1's desires, we simply ask
10940 * to exit on each and every L2 page fault. This is done by setting
10941 * MASK=MATCH=0 and (see below) EB.PF=1.
10942 * Note that below we don't need special code to set EB.PF beyond the
10943 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
10944 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
10945 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
10946 */
10947 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
10948 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
10949 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
10950 enable_ept ? vmcs12->page_fault_error_code_match : 0);
10951
10952 /* All VMFUNCs are currently emulated through L0 vmexits. */
10953 if (cpu_has_vmx_vmfunc())
10954 vmcs_write64(VM_FUNCTION_CONTROL, 0);
10955
10956 if (cpu_has_vmx_apicv()) {
10957 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
10958 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
10959 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
10960 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
10961 }
10962
10963 /*
10964 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
10965 * Some constant fields are set here by vmx_set_constant_host_state().
10966 * Other fields are different per CPU, and will be set later when
10967 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
10968 */
10969 vmx_set_constant_host_state(vmx);
10970
10971 /*
10972 * Set the MSR load/store lists to match L0's settings.
10973 */
10974 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
10975 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10976 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
10977 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10978 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
10979
10980 set_cr4_guest_host_mask(vmx);
10981
10982 if (vmx_mpx_supported())
10983 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
10984
10985 if (enable_vpid) {
10986 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
10987 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
10988 else
10989 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
10990 }
10991
10992 /*
10993 * L1 may access the L2's PDPTR, so save them to construct vmcs12
10994 */
10995 if (enable_ept) {
10996 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
10997 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
10998 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
10999 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
11000 }
Radim Krčmář80132f42018-02-02 18:26:58 +010011001
11002 if (cpu_has_vmx_msr_bitmap())
11003 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011004}
11005
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011006/*
11007 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
11008 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080011009 * 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 +030011010 * guest in a way that will both be appropriate to L1's requests, and our
11011 * needs. In addition to modifying the active vmcs (which is vmcs02), this
11012 * function also has additional necessary side-effects, like setting various
11013 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010011014 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11015 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011016 */
Ladi Prosekee146c12016-11-30 16:03:09 +010011017static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011018 bool from_vmentry, u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011019{
11020 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040011021 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011022
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011023 if (vmx->nested.dirty_vmcs12) {
11024 prepare_vmcs02_full(vcpu, vmcs12, from_vmentry);
11025 vmx->nested.dirty_vmcs12 = false;
11026 }
11027
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011028 /*
11029 * First, the fields that are shadowed. This must be kept in sync
11030 * with vmx_shadow_fields.h.
11031 */
11032
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011033 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011034 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011035 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011036 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
11037 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011038
11039 /*
11040 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
11041 * HOST_FS_BASE, HOST_GS_BASE.
11042 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011043
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011044 if (from_vmentry &&
11045 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020011046 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
11047 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
11048 } else {
11049 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
11050 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
11051 }
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011052 if (from_vmentry) {
11053 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
11054 vmcs12->vm_entry_intr_info_field);
11055 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
11056 vmcs12->vm_entry_exception_error_code);
11057 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
11058 vmcs12->vm_entry_instruction_len);
11059 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
11060 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070011061 vmx->loaded_vmcs->nmi_known_unmasked =
11062 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011063 } else {
11064 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
11065 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030011066 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011067
Jan Kiszkaf4124502014-03-07 20:03:13 +010011068 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080011069
Paolo Bonzini93140062016-07-06 13:23:51 +020011070 /* Preemption timer setting is only taken from vmcs01. */
11071 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11072 exec_control |= vmcs_config.pin_based_exec_ctrl;
11073 if (vmx->hv_deadline_tsc == -1)
11074 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11075
11076 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080011077 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011078 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
11079 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011080 } else {
Wincy Van705699a2015-02-03 23:58:17 +080011081 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011082 }
Wincy Van705699a2015-02-03 23:58:17 +080011083
Jan Kiszkaf4124502014-03-07 20:03:13 +010011084 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011085
Jan Kiszkaf4124502014-03-07 20:03:13 +010011086 vmx->nested.preemption_timer_expired = false;
11087 if (nested_cpu_has_preemption_timer(vmcs12))
11088 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010011089
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011090 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020011091 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080011092
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011093 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011094 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020011095 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010011096 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020011097 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011098 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040011099 SECONDARY_EXEC_APIC_REGISTER_VIRT |
11100 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011101 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040011102 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
11103 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
11104 ~SECONDARY_EXEC_ENABLE_PML;
11105 exec_control |= vmcs12_exec_ctrl;
11106 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011107
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011108 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080011109 vmcs_write16(GUEST_INTR_STATUS,
11110 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080011111
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011112 /*
11113 * Write an illegal value to APIC_ACCESS_ADDR. Later,
11114 * nested_get_vmcs12_pages will either fix it up or
11115 * remove the VM execution control.
11116 */
11117 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
11118 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
11119
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011120 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
11121 }
11122
Jim Mattson83bafef2016-10-04 10:48:38 -070011123 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011124 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
11125 * entry, but only if the current (host) sp changed from the value
11126 * we wrote last (vmx->host_rsp). This cache is no longer relevant
11127 * if we switch vmcs, and rather than hold a separate cache per vmcs,
11128 * here we just force the write to happen on entry.
11129 */
11130 vmx->host_rsp = 0;
11131
11132 exec_control = vmx_exec_control(vmx); /* L0's desires */
11133 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
11134 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
11135 exec_control &= ~CPU_BASED_TPR_SHADOW;
11136 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011137
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011138 /*
11139 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
11140 * nested_get_vmcs12_pages can't fix it up, the illegal value
11141 * will result in a VM entry failure.
11142 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011143 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011144 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011145 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070011146 } else {
11147#ifdef CONFIG_X86_64
11148 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
11149 CPU_BASED_CR8_STORE_EXITING;
11150#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011151 }
11152
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011153 /*
Quan Xu8eb73e22017-12-12 16:44:21 +080011154 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
11155 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011156 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011157 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
11158 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
11159
11160 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
11161
11162 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
11163 * bitwise-or of what L1 wants to trap for L2, and what we want to
11164 * trap. Note that CR0.TS also needs updating - we do this later.
11165 */
11166 update_exception_bitmap(vcpu);
11167 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
11168 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11169
Nadav Har'El8049d652013-08-05 11:07:06 +030011170 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
11171 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
11172 * bits are further modified by vmx_set_efer() below.
11173 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010011174 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030011175
11176 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
11177 * emulated by vmx_set_efer(), below.
11178 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020011179 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030011180 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
11181 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011182 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
11183
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011184 if (from_vmentry &&
11185 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011186 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011187 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011188 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011189 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011190 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011191
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011192 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11193
Peter Feinerc95ba922016-08-17 09:36:47 -070011194 if (kvm_has_tsc_control)
11195 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011196
11197 if (enable_vpid) {
11198 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070011199 * There is no direct mapping between vpid02 and vpid12, the
11200 * vpid02 is per-vCPU for L0 and reused while the value of
11201 * vpid12 is changed w/ one invvpid during nested vmentry.
11202 * The vpid12 is allocated by L1 for L2, so it will not
11203 * influence global bitmap(for vpid01 and vpid02 allocation)
11204 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011205 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070011206 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070011207 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
11208 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011209 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011210 }
11211 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011212 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011213 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011214 }
11215
Ladi Prosek1fb883b2017-04-04 14:18:53 +020011216 if (enable_pml) {
11217 /*
11218 * Conceptually we want to copy the PML address and index from
11219 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11220 * since we always flush the log on each vmexit, this happens
11221 * to be equivalent to simply resetting the fields in vmcs02.
11222 */
11223 ASSERT(vmx->pml_pg);
11224 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11225 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11226 }
11227
Nadav Har'El155a97a2013-08-05 11:07:16 +030011228 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011229 if (nested_ept_init_mmu_context(vcpu)) {
11230 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11231 return 1;
11232 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011233 } else if (nested_cpu_has2(vmcs12,
11234 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070011235 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011236 }
11237
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011238 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011239 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11240 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011241 * The CR0_READ_SHADOW is what L2 should have expected to read given
11242 * the specifications by L1; It's not enough to take
11243 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11244 * have more bits than L1 expected.
11245 */
11246 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11247 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11248
11249 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11250 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11251
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011252 if (from_vmentry &&
11253 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080011254 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11255 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11256 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11257 else
11258 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11259 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11260 vmx_set_efer(vcpu, vcpu->arch.efer);
11261
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011262 /*
11263 * Guest state is invalid and unrestricted guest is disabled,
11264 * which means L1 attempted VMEntry to L2 with invalid state.
11265 * Fail the VMEntry.
11266 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010011267 if (vmx->emulation_required) {
11268 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011269 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010011270 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011271
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011272 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010011273 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011274 entry_failure_code))
11275 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010011276
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011277 if (!enable_ept)
11278 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11279
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011280 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11281 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010011282 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011283}
11284
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011285static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11286{
11287 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11288 nested_cpu_has_virtual_nmis(vmcs12))
11289 return -EINVAL;
11290
11291 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11292 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
11293 return -EINVAL;
11294
11295 return 0;
11296}
11297
Jim Mattsonca0bde22016-11-30 12:03:46 -080011298static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11299{
11300 struct vcpu_vmx *vmx = to_vmx(vcpu);
11301
11302 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
11303 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11304 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11305
Jim Mattson56a20512017-07-06 16:33:06 -070011306 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11307 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11308
Jim Mattsonca0bde22016-11-30 12:03:46 -080011309 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11310 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11311
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011312 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
11313 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11314
Jim Mattson712b12d2017-08-24 13:24:47 -070011315 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11316 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11317
Jim Mattsonca0bde22016-11-30 12:03:46 -080011318 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
11319 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11320
11321 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11322 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11323
Bandan Dasc5f983f2017-05-05 15:25:14 -040011324 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
11325 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11326
Jim Mattsonca0bde22016-11-30 12:03:46 -080011327 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011328 vmx->nested.msrs.procbased_ctls_low,
11329 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070011330 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
11331 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011332 vmx->nested.msrs.secondary_ctls_low,
11333 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011334 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011335 vmx->nested.msrs.pinbased_ctls_low,
11336 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011337 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011338 vmx->nested.msrs.exit_ctls_low,
11339 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011340 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011341 vmx->nested.msrs.entry_ctls_low,
11342 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011343 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11344
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011345 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011346 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11347
Bandan Das41ab9372017-08-03 15:54:43 -040011348 if (nested_cpu_has_vmfunc(vmcs12)) {
11349 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011350 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040011351 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11352
11353 if (nested_cpu_has_eptp_switching(vmcs12)) {
11354 if (!nested_cpu_has_ept(vmcs12) ||
11355 !page_address_valid(vcpu, vmcs12->eptp_list_address))
11356 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11357 }
11358 }
Bandan Das27c42a12017-08-03 15:54:42 -040011359
Jim Mattsonc7c2c702017-05-05 11:28:09 -070011360 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
11361 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11362
Jim Mattsonca0bde22016-11-30 12:03:46 -080011363 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
11364 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
11365 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
11366 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11367
11368 return 0;
11369}
11370
11371static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11372 u32 *exit_qual)
11373{
11374 bool ia32e;
11375
11376 *exit_qual = ENTRY_FAIL_DEFAULT;
11377
11378 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
11379 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
11380 return 1;
11381
11382 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
11383 vmcs12->vmcs_link_pointer != -1ull) {
11384 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
11385 return 1;
11386 }
11387
11388 /*
11389 * If the load IA32_EFER VM-entry control is 1, the following checks
11390 * are performed on the field for the IA32_EFER MSR:
11391 * - Bits reserved in the IA32_EFER MSR must be 0.
11392 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
11393 * the IA-32e mode guest VM-exit control. It must also be identical
11394 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
11395 * CR0.PG) is 1.
11396 */
11397 if (to_vmx(vcpu)->nested.nested_run_pending &&
11398 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
11399 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
11400 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
11401 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
11402 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
11403 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
11404 return 1;
11405 }
11406
11407 /*
11408 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
11409 * IA32_EFER MSR must be 0 in the field for that register. In addition,
11410 * the values of the LMA and LME bits in the field must each be that of
11411 * the host address-space size VM-exit control.
11412 */
11413 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
11414 ia32e = (vmcs12->vm_exit_controls &
11415 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
11416 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
11417 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
11418 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
11419 return 1;
11420 }
11421
Wanpeng Lif1b026a2017-11-05 16:54:48 -080011422 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
11423 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
11424 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
11425 return 1;
11426
Jim Mattsonca0bde22016-11-30 12:03:46 -080011427 return 0;
11428}
11429
Jim Mattson858e25c2016-11-30 12:03:47 -080011430static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry)
11431{
11432 struct vcpu_vmx *vmx = to_vmx(vcpu);
11433 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080011434 u32 msr_entry_idx;
11435 u32 exit_qual;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011436 int r;
Jim Mattson858e25c2016-11-30 12:03:47 -080011437
Jim Mattson858e25c2016-11-30 12:03:47 -080011438 enter_guest_mode(vcpu);
11439
11440 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
11441 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11442
Jim Mattsonde3a0022017-11-27 17:22:25 -060011443 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080011444 vmx_segment_cache_clear(vmx);
11445
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011446 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11447 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
11448
11449 r = EXIT_REASON_INVALID_STATE;
11450 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &exit_qual))
11451 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080011452
11453 nested_get_vmcs12_pages(vcpu, vmcs12);
11454
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011455 r = EXIT_REASON_MSR_LOAD_FAIL;
Jim Mattson858e25c2016-11-30 12:03:47 -080011456 msr_entry_idx = nested_vmx_load_msr(vcpu,
11457 vmcs12->vm_entry_msr_load_addr,
11458 vmcs12->vm_entry_msr_load_count);
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011459 if (msr_entry_idx)
11460 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080011461
Jim Mattson858e25c2016-11-30 12:03:47 -080011462 /*
11463 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
11464 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
11465 * returned as far as L1 is concerned. It will only return (and set
11466 * the success flag) when L2 exits (see nested_vmx_vmexit()).
11467 */
11468 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011469
11470fail:
11471 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11472 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
11473 leave_guest_mode(vcpu);
11474 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
11475 nested_vmx_entry_failure(vcpu, vmcs12, r, exit_qual);
11476 return 1;
Jim Mattson858e25c2016-11-30 12:03:47 -080011477}
11478
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011479/*
11480 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
11481 * for running an L2 nested guest.
11482 */
11483static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
11484{
11485 struct vmcs12 *vmcs12;
11486 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011487 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080011488 u32 exit_qual;
11489 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011490
Kyle Hueyeb277562016-11-29 12:40:39 -080011491 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011492 return 1;
11493
Kyle Hueyeb277562016-11-29 12:40:39 -080011494 if (!nested_vmx_check_vmcs12(vcpu))
11495 goto out;
11496
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011497 vmcs12 = get_vmcs12(vcpu);
11498
Abel Gordon012f83c2013-04-18 14:39:25 +030011499 if (enable_shadow_vmcs)
11500 copy_shadow_to_vmcs12(vmx);
11501
Nadav Har'El7c177932011-05-25 23:12:04 +030011502 /*
11503 * The nested entry process starts with enforcing various prerequisites
11504 * on vmcs12 as required by the Intel SDM, and act appropriately when
11505 * they fail: As the SDM explains, some conditions should cause the
11506 * instruction to fail, while others will cause the instruction to seem
11507 * to succeed, but return an EXIT_REASON_INVALID_STATE.
11508 * To speed up the normal (success) code path, we should avoid checking
11509 * for misconfigurations which will anyway be caught by the processor
11510 * when using the merged vmcs02.
11511 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011512 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
11513 nested_vmx_failValid(vcpu,
11514 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
11515 goto out;
11516 }
11517
Nadav Har'El7c177932011-05-25 23:12:04 +030011518 if (vmcs12->launch_state == launch) {
11519 nested_vmx_failValid(vcpu,
11520 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
11521 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080011522 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030011523 }
11524
Jim Mattsonca0bde22016-11-30 12:03:46 -080011525 ret = check_vmentry_prereqs(vcpu, vmcs12);
11526 if (ret) {
11527 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080011528 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020011529 }
11530
Nadav Har'El7c177932011-05-25 23:12:04 +030011531 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080011532 * After this point, the trap flag no longer triggers a singlestep trap
11533 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
11534 * This is not 100% correct; for performance reasons, we delegate most
11535 * of the checks on host state to the processor. If those fail,
11536 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020011537 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080011538 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020011539
Jim Mattsonca0bde22016-11-30 12:03:46 -080011540 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
11541 if (ret) {
11542 nested_vmx_entry_failure(vcpu, vmcs12,
11543 EXIT_REASON_INVALID_STATE, exit_qual);
11544 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020011545 }
11546
11547 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030011548 * We're finally done with prerequisite checking, and can start with
11549 * the nested entry.
11550 */
11551
Jim Mattson858e25c2016-11-30 12:03:47 -080011552 ret = enter_vmx_non_root_mode(vcpu, true);
11553 if (ret)
11554 return ret;
Wincy Vanff651cb2014-12-11 08:52:58 +030011555
Chao Gao135a06c2018-02-11 10:06:30 +080011556 /*
11557 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
11558 * by event injection, halt vcpu.
11559 */
11560 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
11561 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK))
Joel Schopp5cb56052015-03-02 13:43:31 -060011562 return kvm_vcpu_halt(vcpu);
Jan Kiszka6dfacad2013-12-04 08:58:54 +010011563
Jan Kiszka7af40ad32014-01-04 18:47:23 +010011564 vmx->nested.nested_run_pending = 1;
11565
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011566 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080011567
11568out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080011569 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011570}
11571
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011572/*
11573 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
11574 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
11575 * This function returns the new value we should put in vmcs12.guest_cr0.
11576 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
11577 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
11578 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
11579 * didn't trap the bit, because if L1 did, so would L0).
11580 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
11581 * been modified by L2, and L1 knows it. So just leave the old value of
11582 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
11583 * isn't relevant, because if L0 traps this bit it can set it to anything.
11584 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
11585 * changed these bits, and therefore they need to be updated, but L0
11586 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
11587 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
11588 */
11589static inline unsigned long
11590vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11591{
11592 return
11593 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
11594 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
11595 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
11596 vcpu->arch.cr0_guest_owned_bits));
11597}
11598
11599static inline unsigned long
11600vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11601{
11602 return
11603 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
11604 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
11605 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
11606 vcpu->arch.cr4_guest_owned_bits));
11607}
11608
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011609static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
11610 struct vmcs12 *vmcs12)
11611{
11612 u32 idt_vectoring;
11613 unsigned int nr;
11614
Wanpeng Li664f8e22017-08-24 03:35:09 -070011615 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011616 nr = vcpu->arch.exception.nr;
11617 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11618
11619 if (kvm_exception_is_soft(nr)) {
11620 vmcs12->vm_exit_instruction_len =
11621 vcpu->arch.event_exit_inst_len;
11622 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
11623 } else
11624 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
11625
11626 if (vcpu->arch.exception.has_error_code) {
11627 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
11628 vmcs12->idt_vectoring_error_code =
11629 vcpu->arch.exception.error_code;
11630 }
11631
11632 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010011633 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011634 vmcs12->idt_vectoring_info_field =
11635 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030011636 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011637 nr = vcpu->arch.interrupt.nr;
11638 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11639
11640 if (vcpu->arch.interrupt.soft) {
11641 idt_vectoring |= INTR_TYPE_SOFT_INTR;
11642 vmcs12->vm_entry_instruction_len =
11643 vcpu->arch.event_exit_inst_len;
11644 } else
11645 idt_vectoring |= INTR_TYPE_EXT_INTR;
11646
11647 vmcs12->idt_vectoring_info_field = idt_vectoring;
11648 }
11649}
11650
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011651static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
11652{
11653 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011654 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020011655 bool block_nested_events =
11656 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080011657
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011658 if (vcpu->arch.exception.pending &&
11659 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020011660 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011661 return -EBUSY;
11662 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011663 return 0;
11664 }
11665
Jan Kiszkaf4124502014-03-07 20:03:13 +010011666 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
11667 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020011668 if (block_nested_events)
Jan Kiszkaf4124502014-03-07 20:03:13 +010011669 return -EBUSY;
11670 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
11671 return 0;
11672 }
11673
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011674 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020011675 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011676 return -EBUSY;
11677 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11678 NMI_VECTOR | INTR_TYPE_NMI_INTR |
11679 INTR_INFO_VALID_MASK, 0);
11680 /*
11681 * The NMI-triggered VM exit counts as injection:
11682 * clear this one and block further NMIs.
11683 */
11684 vcpu->arch.nmi_pending = 0;
11685 vmx_set_nmi_mask(vcpu, true);
11686 return 0;
11687 }
11688
11689 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
11690 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020011691 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011692 return -EBUSY;
11693 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080011694 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011695 }
11696
David Hildenbrand6342c502017-01-25 11:58:58 +010011697 vmx_complete_nested_posted_interrupt(vcpu);
11698 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011699}
11700
Jan Kiszkaf4124502014-03-07 20:03:13 +010011701static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
11702{
11703 ktime_t remaining =
11704 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
11705 u64 value;
11706
11707 if (ktime_to_ns(remaining) <= 0)
11708 return 0;
11709
11710 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
11711 do_div(value, 1000000);
11712 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11713}
11714
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011715/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011716 * Update the guest state fields of vmcs12 to reflect changes that
11717 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
11718 * VM-entry controls is also updated, since this is really a guest
11719 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011720 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011721static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011722{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011723 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
11724 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
11725
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011726 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
11727 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
11728 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
11729
11730 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
11731 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
11732 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
11733 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
11734 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
11735 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
11736 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
11737 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
11738 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
11739 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
11740 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
11741 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
11742 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
11743 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
11744 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
11745 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
11746 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
11747 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
11748 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
11749 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
11750 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
11751 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
11752 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
11753 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
11754 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
11755 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
11756 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
11757 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
11758 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
11759 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
11760 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
11761 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
11762 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
11763 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
11764 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
11765 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
11766
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011767 vmcs12->guest_interruptibility_info =
11768 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
11769 vmcs12->guest_pending_dbg_exceptions =
11770 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010011771 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
11772 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
11773 else
11774 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011775
Jan Kiszkaf4124502014-03-07 20:03:13 +010011776 if (nested_cpu_has_preemption_timer(vmcs12)) {
11777 if (vmcs12->vm_exit_controls &
11778 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
11779 vmcs12->vmx_preemption_timer_value =
11780 vmx_get_preemption_timer_value(vcpu);
11781 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
11782 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080011783
Nadav Har'El3633cfc2013-08-05 11:07:07 +030011784 /*
11785 * In some cases (usually, nested EPT), L2 is allowed to change its
11786 * own CR3 without exiting. If it has changed it, we must keep it.
11787 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
11788 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
11789 *
11790 * Additionally, restore L2's PDPTR to vmcs12.
11791 */
11792 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010011793 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030011794 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
11795 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
11796 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
11797 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
11798 }
11799
Jim Mattsond281e132017-06-01 12:44:46 -070011800 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030011801
Wincy Van608406e2015-02-03 23:57:51 +080011802 if (nested_cpu_has_vid(vmcs12))
11803 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
11804
Jan Kiszkac18911a2013-03-13 16:06:41 +010011805 vmcs12->vm_entry_controls =
11806 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020011807 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010011808
Jan Kiszka2996fca2014-06-16 13:59:43 +020011809 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
11810 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
11811 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11812 }
11813
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011814 /* TODO: These cannot have changed unless we have MSR bitmaps and
11815 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020011816 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011817 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020011818 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
11819 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011820 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
11821 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
11822 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010011823 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011824 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011825}
11826
11827/*
11828 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
11829 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
11830 * and this function updates it to reflect the changes to the guest state while
11831 * L2 was running (and perhaps made some exits which were handled directly by L0
11832 * without going back to L1), and to reflect the exit reason.
11833 * Note that we do not have to copy here all VMCS fields, just those that
11834 * could have changed by the L2 guest or the exit - i.e., the guest-state and
11835 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
11836 * which already writes to vmcs12 directly.
11837 */
11838static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11839 u32 exit_reason, u32 exit_intr_info,
11840 unsigned long exit_qualification)
11841{
11842 /* update guest state fields: */
11843 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011844
11845 /* update exit information fields: */
11846
Jan Kiszka533558b2014-01-04 18:47:20 +010011847 vmcs12->vm_exit_reason = exit_reason;
11848 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010011849 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020011850
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011851 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011852 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
11853 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
11854
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011855 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070011856 vmcs12->launch_state = 1;
11857
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011858 /* vm_entry_intr_info_field is cleared on exit. Emulate this
11859 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011860 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011861
11862 /*
11863 * Transfer the event that L0 or L1 may wanted to inject into
11864 * L2 to IDT_VECTORING_INFO_FIELD.
11865 */
11866 vmcs12_save_pending_event(vcpu, vmcs12);
11867 }
11868
11869 /*
11870 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
11871 * preserved above and would only end up incorrectly in L1.
11872 */
11873 vcpu->arch.nmi_injected = false;
11874 kvm_clear_exception_queue(vcpu);
11875 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011876}
11877
Wanpeng Li5af41572017-11-05 16:54:49 -080011878static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
11879 struct vmcs12 *vmcs12)
11880{
11881 u32 entry_failure_code;
11882
11883 nested_ept_uninit_mmu_context(vcpu);
11884
11885 /*
11886 * Only PDPTE load can fail as the value of cr3 was checked on entry and
11887 * couldn't have changed.
11888 */
11889 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
11890 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
11891
11892 if (!enable_ept)
11893 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
11894}
11895
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011896/*
11897 * A part of what we need to when the nested L2 guest exits and we want to
11898 * run its L1 parent, is to reset L1's guest state to the host state specified
11899 * in vmcs12.
11900 * This function is to be called not only on normal nested exit, but also on
11901 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
11902 * Failures During or After Loading Guest State").
11903 * This function should be called when the active VMCS is L1's (vmcs01).
11904 */
Jan Kiszka733568f2013-02-23 15:07:47 +010011905static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
11906 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011907{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011908 struct kvm_segment seg;
11909
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011910 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
11911 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020011912 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011913 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11914 else
11915 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11916 vmx_set_efer(vcpu, vcpu->arch.efer);
11917
11918 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
11919 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070011920 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011921 /*
11922 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011923 * actually changed, because vmx_set_cr0 refers to efer set above.
11924 *
11925 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
11926 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011927 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011928 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020011929 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011930
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011931 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011932 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080011933 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011934
Wanpeng Li5af41572017-11-05 16:54:49 -080011935 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011936
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011937 if (enable_vpid) {
11938 /*
11939 * Trivially support vpid by letting L2s share their parent
11940 * L1's vpid. TODO: move to a more elaborate solution, giving
11941 * each L2 its own vpid and exposing the vpid feature to L1.
11942 */
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011943 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011944 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011945
11946 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
11947 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
11948 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
11949 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
11950 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020011951 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
11952 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011953
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011954 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
11955 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
11956 vmcs_write64(GUEST_BNDCFGS, 0);
11957
Jan Kiszka44811c02013-08-04 17:17:27 +020011958 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011959 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011960 vcpu->arch.pat = vmcs12->host_ia32_pat;
11961 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011962 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
11963 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
11964 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011965
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011966 /* Set L1 segment info according to Intel SDM
11967 27.5.2 Loading Host Segment and Descriptor-Table Registers */
11968 seg = (struct kvm_segment) {
11969 .base = 0,
11970 .limit = 0xFFFFFFFF,
11971 .selector = vmcs12->host_cs_selector,
11972 .type = 11,
11973 .present = 1,
11974 .s = 1,
11975 .g = 1
11976 };
11977 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
11978 seg.l = 1;
11979 else
11980 seg.db = 1;
11981 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
11982 seg = (struct kvm_segment) {
11983 .base = 0,
11984 .limit = 0xFFFFFFFF,
11985 .type = 3,
11986 .present = 1,
11987 .s = 1,
11988 .db = 1,
11989 .g = 1
11990 };
11991 seg.selector = vmcs12->host_ds_selector;
11992 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
11993 seg.selector = vmcs12->host_es_selector;
11994 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
11995 seg.selector = vmcs12->host_ss_selector;
11996 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
11997 seg.selector = vmcs12->host_fs_selector;
11998 seg.base = vmcs12->host_fs_base;
11999 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
12000 seg.selector = vmcs12->host_gs_selector;
12001 seg.base = vmcs12->host_gs_base;
12002 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
12003 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030012004 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012005 .limit = 0x67,
12006 .selector = vmcs12->host_tr_selector,
12007 .type = 11,
12008 .present = 1
12009 };
12010 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
12011
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012012 kvm_set_dr(vcpu, 7, 0x400);
12013 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030012014
Wincy Van3af18d92015-02-03 23:49:31 +080012015 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010012016 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080012017
Wincy Vanff651cb2014-12-11 08:52:58 +030012018 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
12019 vmcs12->vm_exit_msr_load_count))
12020 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012021}
12022
12023/*
12024 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
12025 * and modify vmcs12 to make it see what it would expect to see there if
12026 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
12027 */
Jan Kiszka533558b2014-01-04 18:47:20 +010012028static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
12029 u32 exit_intr_info,
12030 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012031{
12032 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012033 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12034
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012035 /* trying to cancel vmlaunch/vmresume is a bug */
12036 WARN_ON_ONCE(vmx->nested.nested_run_pending);
12037
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012038 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070012039 * The only expected VM-instruction error is "VM entry with
12040 * invalid control field(s)." Anything else indicates a
12041 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012042 */
Jim Mattson4f350c62017-09-14 16:31:44 -070012043 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
12044 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
12045
12046 leave_guest_mode(vcpu);
12047
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012048 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12049 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12050
Jim Mattson4f350c62017-09-14 16:31:44 -070012051 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012052 if (exit_reason == -1)
12053 sync_vmcs12(vcpu, vmcs12);
12054 else
12055 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
12056 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070012057
12058 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
12059 vmcs12->vm_exit_msr_store_count))
12060 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040012061 }
12062
Jim Mattson4f350c62017-09-14 16:31:44 -070012063 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020012064 vm_entry_controls_reset_shadow(vmx);
12065 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010012066 vmx_segment_cache_clear(vmx);
12067
Paolo Bonzini93140062016-07-06 13:23:51 +020012068 /* Update any VMCS fields that might have changed while L2 ran */
Jim Mattson83bafef2016-10-04 10:48:38 -070012069 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
12070 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010012071 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini93140062016-07-06 13:23:51 +020012072 if (vmx->hv_deadline_tsc == -1)
12073 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12074 PIN_BASED_VMX_PREEMPTION_TIMER);
12075 else
12076 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12077 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070012078 if (kvm_has_tsc_control)
12079 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012080
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020012081 if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
12082 vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
12083 vmx_set_virtual_x2apic_mode(vcpu,
12084 vcpu->arch.apic_base & X2APIC_ENABLE);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012085 } else if (!nested_cpu_has_ept(vmcs12) &&
12086 nested_cpu_has2(vmcs12,
12087 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070012088 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020012089 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012090
12091 /* This is needed for same reason as it was needed in prepare_vmcs02 */
12092 vmx->host_rsp = 0;
12093
12094 /* Unpin physical memory we referred to in vmcs02 */
12095 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012096 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012097 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012098 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012099 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012100 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012101 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012102 }
Wincy Van705699a2015-02-03 23:58:17 +080012103 if (vmx->nested.pi_desc_page) {
12104 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012105 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080012106 vmx->nested.pi_desc_page = NULL;
12107 vmx->nested.pi_desc = NULL;
12108 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012109
12110 /*
Tang Chen38b99172014-09-24 15:57:54 +080012111 * We are now running in L2, mmu_notifier will force to reload the
12112 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
12113 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080012114 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080012115
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012116 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030012117 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012118
12119 /* in case we halted in L2 */
12120 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070012121
12122 if (likely(!vmx->fail)) {
12123 /*
12124 * TODO: SDM says that with acknowledge interrupt on
12125 * exit, bit 31 of the VM-exit interrupt information
12126 * (valid interrupt) is always set to 1 on
12127 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
12128 * need kvm_cpu_has_interrupt(). See the commit
12129 * message for details.
12130 */
12131 if (nested_exit_intr_ack_set(vcpu) &&
12132 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
12133 kvm_cpu_has_interrupt(vcpu)) {
12134 int irq = kvm_cpu_get_interrupt(vcpu);
12135 WARN_ON(irq < 0);
12136 vmcs12->vm_exit_intr_info = irq |
12137 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
12138 }
12139
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012140 if (exit_reason != -1)
12141 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
12142 vmcs12->exit_qualification,
12143 vmcs12->idt_vectoring_info_field,
12144 vmcs12->vm_exit_intr_info,
12145 vmcs12->vm_exit_intr_error_code,
12146 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070012147
12148 load_vmcs12_host_state(vcpu, vmcs12);
12149
12150 return;
12151 }
12152
12153 /*
12154 * After an early L2 VM-entry failure, we're now back
12155 * in L1 which thinks it just finished a VMLAUNCH or
12156 * VMRESUME instruction, so we need to set the failure
12157 * flag and the VM-instruction error field of the VMCS
12158 * accordingly.
12159 */
12160 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080012161
12162 load_vmcs12_mmu_host_state(vcpu, vmcs12);
12163
Jim Mattson4f350c62017-09-14 16:31:44 -070012164 /*
12165 * The emulated instruction was already skipped in
12166 * nested_vmx_run, but the updated RIP was never
12167 * written back to the vmcs01.
12168 */
12169 skip_emulated_instruction(vcpu);
12170 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012171}
12172
Nadav Har'El7c177932011-05-25 23:12:04 +030012173/*
Jan Kiszka42124922014-01-04 18:47:19 +010012174 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
12175 */
12176static void vmx_leave_nested(struct kvm_vcpu *vcpu)
12177{
Wanpeng Li2f707d92017-03-06 04:03:28 -080012178 if (is_guest_mode(vcpu)) {
12179 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010012180 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080012181 }
Jan Kiszka42124922014-01-04 18:47:19 +010012182 free_nested(to_vmx(vcpu));
12183}
12184
12185/*
Nadav Har'El7c177932011-05-25 23:12:04 +030012186 * L1's failure to enter L2 is a subset of a normal exit, as explained in
12187 * 23.7 "VM-entry failures during or after loading guest state" (this also
12188 * lists the acceptable exit-reason and exit-qualification parameters).
12189 * It should only be called before L2 actually succeeded to run, and when
12190 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
12191 */
12192static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
12193 struct vmcs12 *vmcs12,
12194 u32 reason, unsigned long qualification)
12195{
12196 load_vmcs12_host_state(vcpu, vmcs12);
12197 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
12198 vmcs12->exit_qualification = qualification;
12199 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030012200 if (enable_shadow_vmcs)
12201 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030012202}
12203
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012204static int vmx_check_intercept(struct kvm_vcpu *vcpu,
12205 struct x86_instruction_info *info,
12206 enum x86_intercept_stage stage)
12207{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020012208 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12209 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
12210
12211 /*
12212 * RDPID causes #UD if disabled through secondary execution controls.
12213 * Because it is marked as EmulateOnUD, we need to intercept it here.
12214 */
12215 if (info->intercept == x86_intercept_rdtscp &&
12216 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
12217 ctxt->exception.vector = UD_VECTOR;
12218 ctxt->exception.error_code_valid = false;
12219 return X86EMUL_PROPAGATE_FAULT;
12220 }
12221
12222 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012223 return X86EMUL_CONTINUE;
12224}
12225
Yunhong Jiang64672c92016-06-13 14:19:59 -070012226#ifdef CONFIG_X86_64
12227/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
12228static inline int u64_shl_div_u64(u64 a, unsigned int shift,
12229 u64 divisor, u64 *result)
12230{
12231 u64 low = a << shift, high = a >> (64 - shift);
12232
12233 /* To avoid the overflow on divq */
12234 if (high >= divisor)
12235 return 1;
12236
12237 /* Low hold the result, high hold rem which is discarded */
12238 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
12239 "rm" (divisor), "0" (low), "1" (high));
12240 *result = low;
12241
12242 return 0;
12243}
12244
12245static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
12246{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012247 struct vcpu_vmx *vmx;
12248 u64 tscl, guest_tscl, delta_tsc;
12249
12250 if (kvm_mwait_in_guest(vcpu->kvm))
12251 return -EOPNOTSUPP;
12252
12253 vmx = to_vmx(vcpu);
12254 tscl = rdtsc();
12255 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
12256 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012257
12258 /* Convert to host delta tsc if tsc scaling is enabled */
12259 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
12260 u64_shl_div_u64(delta_tsc,
12261 kvm_tsc_scaling_ratio_frac_bits,
12262 vcpu->arch.tsc_scaling_ratio,
12263 &delta_tsc))
12264 return -ERANGE;
12265
12266 /*
12267 * If the delta tsc can't fit in the 32 bit after the multi shift,
12268 * we can't use the preemption timer.
12269 * It's possible that it fits on later vmentries, but checking
12270 * on every vmentry is costly so we just use an hrtimer.
12271 */
12272 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
12273 return -ERANGE;
12274
12275 vmx->hv_deadline_tsc = tscl + delta_tsc;
12276 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12277 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070012278
12279 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012280}
12281
12282static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
12283{
12284 struct vcpu_vmx *vmx = to_vmx(vcpu);
12285 vmx->hv_deadline_tsc = -1;
12286 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12287 PIN_BASED_VMX_PREEMPTION_TIMER);
12288}
12289#endif
12290
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012291static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012292{
Wanpeng Lib31c1142018-03-12 04:53:04 -070012293 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020012294 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012295}
12296
Kai Huang843e4332015-01-28 10:54:28 +080012297static void vmx_slot_enable_log_dirty(struct kvm *kvm,
12298 struct kvm_memory_slot *slot)
12299{
12300 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
12301 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
12302}
12303
12304static void vmx_slot_disable_log_dirty(struct kvm *kvm,
12305 struct kvm_memory_slot *slot)
12306{
12307 kvm_mmu_slot_set_dirty(kvm, slot);
12308}
12309
12310static void vmx_flush_log_dirty(struct kvm *kvm)
12311{
12312 kvm_flush_pml_buffers(kvm);
12313}
12314
Bandan Dasc5f983f2017-05-05 15:25:14 -040012315static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
12316{
12317 struct vmcs12 *vmcs12;
12318 struct vcpu_vmx *vmx = to_vmx(vcpu);
12319 gpa_t gpa;
12320 struct page *page = NULL;
12321 u64 *pml_address;
12322
12323 if (is_guest_mode(vcpu)) {
12324 WARN_ON_ONCE(vmx->nested.pml_full);
12325
12326 /*
12327 * Check if PML is enabled for the nested guest.
12328 * Whether eptp bit 6 is set is already checked
12329 * as part of A/D emulation.
12330 */
12331 vmcs12 = get_vmcs12(vcpu);
12332 if (!nested_cpu_has_pml(vmcs12))
12333 return 0;
12334
Dan Carpenter47698862017-05-10 22:43:17 +030012335 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040012336 vmx->nested.pml_full = true;
12337 return 1;
12338 }
12339
12340 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
12341
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020012342 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
12343 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040012344 return 0;
12345
12346 pml_address = kmap(page);
12347 pml_address[vmcs12->guest_pml_index--] = gpa;
12348 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012349 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040012350 }
12351
12352 return 0;
12353}
12354
Kai Huang843e4332015-01-28 10:54:28 +080012355static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
12356 struct kvm_memory_slot *memslot,
12357 gfn_t offset, unsigned long mask)
12358{
12359 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
12360}
12361
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012362static void __pi_post_block(struct kvm_vcpu *vcpu)
12363{
12364 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12365 struct pi_desc old, new;
12366 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012367
12368 do {
12369 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012370 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
12371 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012372
12373 dest = cpu_physical_id(vcpu->cpu);
12374
12375 if (x2apic_enabled())
12376 new.ndst = dest;
12377 else
12378 new.ndst = (dest << 8) & 0xFF00;
12379
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012380 /* set 'NV' to 'notification vector' */
12381 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012382 } while (cmpxchg64(&pi_desc->control, old.control,
12383 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012384
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012385 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
12386 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012387 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012388 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012389 vcpu->pre_pcpu = -1;
12390 }
12391}
12392
Feng Wuefc64402015-09-18 22:29:51 +080012393/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080012394 * This routine does the following things for vCPU which is going
12395 * to be blocked if VT-d PI is enabled.
12396 * - Store the vCPU to the wakeup list, so when interrupts happen
12397 * we can find the right vCPU to wake up.
12398 * - Change the Posted-interrupt descriptor as below:
12399 * 'NDST' <-- vcpu->pre_pcpu
12400 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
12401 * - If 'ON' is set during this process, which means at least one
12402 * interrupt is posted for this vCPU, we cannot block it, in
12403 * this case, return 1, otherwise, return 0.
12404 *
12405 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070012406static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012407{
Feng Wubf9f6ac2015-09-18 22:29:55 +080012408 unsigned int dest;
12409 struct pi_desc old, new;
12410 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12411
12412 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012413 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12414 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080012415 return 0;
12416
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012417 WARN_ON(irqs_disabled());
12418 local_irq_disable();
12419 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
12420 vcpu->pre_pcpu = vcpu->cpu;
12421 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12422 list_add_tail(&vcpu->blocked_vcpu_list,
12423 &per_cpu(blocked_vcpu_on_cpu,
12424 vcpu->pre_pcpu));
12425 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12426 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080012427
12428 do {
12429 old.control = new.control = pi_desc->control;
12430
Feng Wubf9f6ac2015-09-18 22:29:55 +080012431 WARN((pi_desc->sn == 1),
12432 "Warning: SN field of posted-interrupts "
12433 "is set before blocking\n");
12434
12435 /*
12436 * Since vCPU can be preempted during this process,
12437 * vcpu->cpu could be different with pre_pcpu, we
12438 * need to set pre_pcpu as the destination of wakeup
12439 * notification event, then we can find the right vCPU
12440 * to wakeup in wakeup handler if interrupts happen
12441 * when the vCPU is in blocked state.
12442 */
12443 dest = cpu_physical_id(vcpu->pre_pcpu);
12444
12445 if (x2apic_enabled())
12446 new.ndst = dest;
12447 else
12448 new.ndst = (dest << 8) & 0xFF00;
12449
12450 /* set 'NV' to 'wakeup vector' */
12451 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012452 } while (cmpxchg64(&pi_desc->control, old.control,
12453 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012454
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012455 /* We should not block the vCPU if an interrupt is posted for it. */
12456 if (pi_test_on(pi_desc) == 1)
12457 __pi_post_block(vcpu);
12458
12459 local_irq_enable();
12460 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012461}
12462
Yunhong Jiangbc225122016-06-13 14:19:58 -070012463static int vmx_pre_block(struct kvm_vcpu *vcpu)
12464{
12465 if (pi_pre_block(vcpu))
12466 return 1;
12467
Yunhong Jiang64672c92016-06-13 14:19:59 -070012468 if (kvm_lapic_hv_timer_in_use(vcpu))
12469 kvm_lapic_switch_to_sw_timer(vcpu);
12470
Yunhong Jiangbc225122016-06-13 14:19:58 -070012471 return 0;
12472}
12473
12474static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012475{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012476 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012477 return;
12478
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012479 WARN_ON(irqs_disabled());
12480 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012481 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012482 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080012483}
12484
Yunhong Jiangbc225122016-06-13 14:19:58 -070012485static void vmx_post_block(struct kvm_vcpu *vcpu)
12486{
Yunhong Jiang64672c92016-06-13 14:19:59 -070012487 if (kvm_x86_ops->set_hv_timer)
12488 kvm_lapic_switch_to_hv_timer(vcpu);
12489
Yunhong Jiangbc225122016-06-13 14:19:58 -070012490 pi_post_block(vcpu);
12491}
12492
Feng Wubf9f6ac2015-09-18 22:29:55 +080012493/*
Feng Wuefc64402015-09-18 22:29:51 +080012494 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
12495 *
12496 * @kvm: kvm
12497 * @host_irq: host irq of the interrupt
12498 * @guest_irq: gsi of the interrupt
12499 * @set: set or unset PI
12500 * returns 0 on success, < 0 on failure
12501 */
12502static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
12503 uint32_t guest_irq, bool set)
12504{
12505 struct kvm_kernel_irq_routing_entry *e;
12506 struct kvm_irq_routing_table *irq_rt;
12507 struct kvm_lapic_irq irq;
12508 struct kvm_vcpu *vcpu;
12509 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012510 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080012511
12512 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012513 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12514 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080012515 return 0;
12516
12517 idx = srcu_read_lock(&kvm->irq_srcu);
12518 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012519 if (guest_irq >= irq_rt->nr_rt_entries ||
12520 hlist_empty(&irq_rt->map[guest_irq])) {
12521 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
12522 guest_irq, irq_rt->nr_rt_entries);
12523 goto out;
12524 }
Feng Wuefc64402015-09-18 22:29:51 +080012525
12526 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
12527 if (e->type != KVM_IRQ_ROUTING_MSI)
12528 continue;
12529 /*
12530 * VT-d PI cannot support posting multicast/broadcast
12531 * interrupts to a vCPU, we still use interrupt remapping
12532 * for these kind of interrupts.
12533 *
12534 * For lowest-priority interrupts, we only support
12535 * those with single CPU as the destination, e.g. user
12536 * configures the interrupts via /proc/irq or uses
12537 * irqbalance to make the interrupts single-CPU.
12538 *
12539 * We will support full lowest-priority interrupt later.
12540 */
12541
Radim Krčmář371313132016-07-12 22:09:27 +020012542 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080012543 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
12544 /*
12545 * Make sure the IRTE is in remapped mode if
12546 * we don't handle it in posted mode.
12547 */
12548 ret = irq_set_vcpu_affinity(host_irq, NULL);
12549 if (ret < 0) {
12550 printk(KERN_INFO
12551 "failed to back to remapped mode, irq: %u\n",
12552 host_irq);
12553 goto out;
12554 }
12555
Feng Wuefc64402015-09-18 22:29:51 +080012556 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080012557 }
Feng Wuefc64402015-09-18 22:29:51 +080012558
12559 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
12560 vcpu_info.vector = irq.vector;
12561
hu huajun2698d822018-04-11 15:16:40 +080012562 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080012563 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
12564
12565 if (set)
12566 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080012567 else
Feng Wuefc64402015-09-18 22:29:51 +080012568 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080012569
12570 if (ret < 0) {
12571 printk(KERN_INFO "%s: failed to update PI IRTE\n",
12572 __func__);
12573 goto out;
12574 }
12575 }
12576
12577 ret = 0;
12578out:
12579 srcu_read_unlock(&kvm->irq_srcu, idx);
12580 return ret;
12581}
12582
Ashok Rajc45dcc72016-06-22 14:59:56 +080012583static void vmx_setup_mce(struct kvm_vcpu *vcpu)
12584{
12585 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
12586 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
12587 FEATURE_CONTROL_LMCE;
12588 else
12589 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
12590 ~FEATURE_CONTROL_LMCE;
12591}
12592
Ladi Prosek72d7b372017-10-11 16:54:41 +020012593static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
12594{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012595 /* we need a nested vmexit to enter SMM, postpone if run is pending */
12596 if (to_vmx(vcpu)->nested.nested_run_pending)
12597 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020012598 return 1;
12599}
12600
Ladi Prosek0234bf82017-10-11 16:54:40 +020012601static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
12602{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012603 struct vcpu_vmx *vmx = to_vmx(vcpu);
12604
12605 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
12606 if (vmx->nested.smm.guest_mode)
12607 nested_vmx_vmexit(vcpu, -1, 0, 0);
12608
12609 vmx->nested.smm.vmxon = vmx->nested.vmxon;
12610 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070012611 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020012612 return 0;
12613}
12614
12615static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
12616{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012617 struct vcpu_vmx *vmx = to_vmx(vcpu);
12618 int ret;
12619
12620 if (vmx->nested.smm.vmxon) {
12621 vmx->nested.vmxon = true;
12622 vmx->nested.smm.vmxon = false;
12623 }
12624
12625 if (vmx->nested.smm.guest_mode) {
12626 vcpu->arch.hflags &= ~HF_SMM_MASK;
12627 ret = enter_vmx_non_root_mode(vcpu, false);
12628 vcpu->arch.hflags |= HF_SMM_MASK;
12629 if (ret)
12630 return ret;
12631
12632 vmx->nested.smm.guest_mode = false;
12633 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020012634 return 0;
12635}
12636
Ladi Prosekcc3d9672017-10-17 16:02:39 +020012637static int enable_smi_window(struct kvm_vcpu *vcpu)
12638{
12639 return 0;
12640}
12641
Kees Cook404f6aa2016-08-08 16:29:06 -070012642static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080012643 .cpu_has_kvm_support = cpu_has_kvm_support,
12644 .disabled_by_bios = vmx_disabled_by_bios,
12645 .hardware_setup = hardware_setup,
12646 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030012647 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012648 .hardware_enable = hardware_enable,
12649 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080012650 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020012651 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012652
Wanpeng Lib31c1142018-03-12 04:53:04 -070012653 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070012654 .vm_alloc = vmx_vm_alloc,
12655 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070012656
Avi Kivity6aa8b732006-12-10 02:21:36 -080012657 .vcpu_create = vmx_create_vcpu,
12658 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030012659 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012660
Avi Kivity04d2cc72007-09-10 18:10:54 +030012661 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012662 .vcpu_load = vmx_vcpu_load,
12663 .vcpu_put = vmx_vcpu_put,
12664
Paolo Bonzinia96036b2015-11-10 11:55:36 +010012665 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060012666 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012667 .get_msr = vmx_get_msr,
12668 .set_msr = vmx_set_msr,
12669 .get_segment_base = vmx_get_segment_base,
12670 .get_segment = vmx_get_segment,
12671 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020012672 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012673 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020012674 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020012675 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030012676 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012677 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012678 .set_cr3 = vmx_set_cr3,
12679 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012680 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012681 .get_idt = vmx_get_idt,
12682 .set_idt = vmx_set_idt,
12683 .get_gdt = vmx_get_gdt,
12684 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010012685 .get_dr6 = vmx_get_dr6,
12686 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030012687 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010012688 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030012689 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012690 .get_rflags = vmx_get_rflags,
12691 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080012692
Avi Kivity6aa8b732006-12-10 02:21:36 -080012693 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012694
Avi Kivity6aa8b732006-12-10 02:21:36 -080012695 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020012696 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012697 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040012698 .set_interrupt_shadow = vmx_set_interrupt_shadow,
12699 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020012700 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030012701 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012702 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020012703 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030012704 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020012705 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012706 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010012707 .get_nmi_mask = vmx_get_nmi_mask,
12708 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012709 .enable_nmi_window = enable_nmi_window,
12710 .enable_irq_window = enable_irq_window,
12711 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +080012712 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080012713 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030012714 .get_enable_apicv = vmx_get_enable_apicv,
12715 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080012716 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010012717 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080012718 .hwapic_irr_update = vmx_hwapic_irr_update,
12719 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080012720 .sync_pir_to_irr = vmx_sync_pir_to_irr,
12721 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012722
Izik Eiduscbc94022007-10-25 00:29:55 +020012723 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070012724 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080012725 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080012726 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030012727
Avi Kivity586f9602010-11-18 13:09:54 +020012728 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020012729
Sheng Yang17cc3932010-01-05 19:02:27 +080012730 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080012731
12732 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080012733
12734 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000012735 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020012736
12737 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080012738
12739 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100012740
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012741 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100012742 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020012743
12744 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012745
12746 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080012747 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000012748 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080012749 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020012750 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012751
12752 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012753
12754 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080012755
12756 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
12757 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
12758 .flush_log_dirty = vmx_flush_log_dirty,
12759 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040012760 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020012761
Feng Wubf9f6ac2015-09-18 22:29:55 +080012762 .pre_block = vmx_pre_block,
12763 .post_block = vmx_post_block,
12764
Wei Huang25462f72015-06-19 15:45:05 +020012765 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080012766
12767 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070012768
12769#ifdef CONFIG_X86_64
12770 .set_hv_timer = vmx_set_hv_timer,
12771 .cancel_hv_timer = vmx_cancel_hv_timer,
12772#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080012773
12774 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020012775
Ladi Prosek72d7b372017-10-11 16:54:41 +020012776 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020012777 .pre_enter_smm = vmx_pre_enter_smm,
12778 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020012779 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012780};
12781
12782static int __init vmx_init(void)
12783{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010012784 int r;
12785
12786#if IS_ENABLED(CONFIG_HYPERV)
12787 /*
12788 * Enlightened VMCS usage should be recommended and the host needs
12789 * to support eVMCS v1 or above. We can also disable eVMCS support
12790 * with module parameter.
12791 */
12792 if (enlightened_vmcs &&
12793 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
12794 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
12795 KVM_EVMCS_VERSION) {
12796 int cpu;
12797
12798 /* Check that we have assist pages on all online CPUs */
12799 for_each_online_cpu(cpu) {
12800 if (!hv_get_vp_assist_page(cpu)) {
12801 enlightened_vmcs = false;
12802 break;
12803 }
12804 }
12805
12806 if (enlightened_vmcs) {
12807 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
12808 static_branch_enable(&enable_evmcs);
12809 }
12810 } else {
12811 enlightened_vmcs = false;
12812 }
12813#endif
12814
12815 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Tiejun Chen34a1cd62014-10-28 10:14:48 +080012816 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030012817 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080012818 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080012819
Dave Young2965faa2015-09-09 15:38:55 -070012820#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080012821 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
12822 crash_vmclear_local_loaded_vmcss);
12823#endif
12824
He, Qingfdef3ad2007-04-30 09:45:24 +030012825 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080012826}
12827
12828static void __exit vmx_exit(void)
12829{
Dave Young2965faa2015-09-09 15:38:55 -070012830#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053012831 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080012832 synchronize_rcu();
12833#endif
12834
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080012835 kvm_exit();
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010012836
12837#if IS_ENABLED(CONFIG_HYPERV)
12838 if (static_branch_unlikely(&enable_evmcs)) {
12839 int cpu;
12840 struct hv_vp_assist_page *vp_ap;
12841 /*
12842 * Reset everything to support using non-enlightened VMCS
12843 * access later (e.g. when we reload the module with
12844 * enlightened_vmcs=0)
12845 */
12846 for_each_online_cpu(cpu) {
12847 vp_ap = hv_get_vp_assist_page(cpu);
12848
12849 if (!vp_ap)
12850 continue;
12851
12852 vp_ap->current_nested_vmcs = 0;
12853 vp_ap->enlighten_vmentry = 0;
12854 }
12855
12856 static_branch_disable(&enable_evmcs);
12857 }
12858#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080012859}
12860
12861module_init(vmx_init)
12862module_exit(vmx_exit)