blob: 0c57fb4df2d1dbf0994e2239bb90927c9068e35d [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
Sheng Yang04547152009-04-01 15:52:31 +08001497static inline bool report_flexpriority(void)
1498{
1499 return flexpriority_enabled;
1500}
1501
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001502static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1503{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001504 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001505}
1506
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001507static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1508{
1509 return vmcs12->cpu_based_vm_exec_control & bit;
1510}
1511
1512static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1513{
1514 return (vmcs12->cpu_based_vm_exec_control &
1515 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1516 (vmcs12->secondary_vm_exec_control & bit);
1517}
1518
Jan Kiszkaf4124502014-03-07 20:03:13 +01001519static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1520{
1521 return vmcs12->pin_based_vm_exec_control &
1522 PIN_BASED_VMX_PREEMPTION_TIMER;
1523}
1524
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001525static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1526{
1527 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1528}
1529
1530static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1531{
1532 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1533}
1534
Nadav Har'El155a97a2013-08-05 11:07:16 +03001535static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1536{
1537 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1538}
1539
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001540static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1541{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001542 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001543}
1544
Bandan Dasc5f983f2017-05-05 15:25:14 -04001545static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1546{
1547 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1548}
1549
Wincy Vanf2b93282015-02-03 23:56:03 +08001550static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1551{
1552 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1553}
1554
Wanpeng Li5c614b32015-10-13 09:18:36 -07001555static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1556{
1557 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1558}
1559
Wincy Van82f0dd42015-02-03 23:57:18 +08001560static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1561{
1562 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1563}
1564
Wincy Van608406e2015-02-03 23:57:51 +08001565static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1566{
1567 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1568}
1569
Wincy Van705699a2015-02-03 23:58:17 +08001570static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1571{
1572 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1573}
1574
Bandan Das27c42a12017-08-03 15:54:42 -04001575static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1576{
1577 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1578}
1579
Bandan Das41ab9372017-08-03 15:54:43 -04001580static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1581{
1582 return nested_cpu_has_vmfunc(vmcs12) &&
1583 (vmcs12->vm_function_control &
1584 VMX_VMFUNC_EPTP_SWITCHING);
1585}
1586
Jim Mattsonef85b672016-12-12 11:01:37 -08001587static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001588{
1589 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08001590 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001591}
1592
Jan Kiszka533558b2014-01-04 18:47:20 +01001593static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1594 u32 exit_intr_info,
1595 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001596static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1597 struct vmcs12 *vmcs12,
1598 u32 reason, unsigned long qualification);
1599
Rusty Russell8b9cf982007-07-30 16:31:43 +10001600static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001601{
1602 int i;
1603
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001604 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001605 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001606 return i;
1607 return -1;
1608}
1609
Sheng Yang2384d2b2008-01-17 15:14:33 +08001610static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1611{
1612 struct {
1613 u64 vpid : 16;
1614 u64 rsvd : 48;
1615 u64 gva;
1616 } operand = { vpid, 0, gva };
1617
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001618 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001619 /* CF==1 or ZF==1 --> rc = -1 */
1620 "; ja 1f ; ud2 ; 1:"
1621 : : "a"(&operand), "c"(ext) : "cc", "memory");
1622}
1623
Sheng Yang14394422008-04-28 12:24:45 +08001624static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1625{
1626 struct {
1627 u64 eptp, gpa;
1628 } operand = {eptp, gpa};
1629
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001630 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001631 /* CF==1 or ZF==1 --> rc = -1 */
1632 "; ja 1f ; ud2 ; 1:\n"
1633 : : "a" (&operand), "c" (ext) : "cc", "memory");
1634}
1635
Avi Kivity26bb0982009-09-07 11:14:12 +03001636static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001637{
1638 int i;
1639
Rusty Russell8b9cf982007-07-30 16:31:43 +10001640 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001641 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001642 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001643 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001644}
1645
Avi Kivity6aa8b732006-12-10 02:21:36 -08001646static void vmcs_clear(struct vmcs *vmcs)
1647{
1648 u64 phys_addr = __pa(vmcs);
1649 u8 error;
1650
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001651 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001652 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001653 : "cc", "memory");
1654 if (error)
1655 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1656 vmcs, phys_addr);
1657}
1658
Nadav Har'Eld462b812011-05-24 15:26:10 +03001659static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1660{
1661 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001662 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1663 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001664 loaded_vmcs->cpu = -1;
1665 loaded_vmcs->launched = 0;
1666}
1667
Dongxiao Xu7725b892010-05-11 18:29:38 +08001668static void vmcs_load(struct vmcs *vmcs)
1669{
1670 u64 phys_addr = __pa(vmcs);
1671 u8 error;
1672
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001673 if (static_branch_unlikely(&enable_evmcs))
1674 return evmcs_load(phys_addr);
1675
Dongxiao Xu7725b892010-05-11 18:29:38 +08001676 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001677 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001678 : "cc", "memory");
1679 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001680 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001681 vmcs, phys_addr);
1682}
1683
Dave Young2965faa2015-09-09 15:38:55 -07001684#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001685/*
1686 * This bitmap is used to indicate whether the vmclear
1687 * operation is enabled on all cpus. All disabled by
1688 * default.
1689 */
1690static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1691
1692static inline void crash_enable_local_vmclear(int cpu)
1693{
1694 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1695}
1696
1697static inline void crash_disable_local_vmclear(int cpu)
1698{
1699 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1700}
1701
1702static inline int crash_local_vmclear_enabled(int cpu)
1703{
1704 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1705}
1706
1707static void crash_vmclear_local_loaded_vmcss(void)
1708{
1709 int cpu = raw_smp_processor_id();
1710 struct loaded_vmcs *v;
1711
1712 if (!crash_local_vmclear_enabled(cpu))
1713 return;
1714
1715 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1716 loaded_vmcss_on_cpu_link)
1717 vmcs_clear(v->vmcs);
1718}
1719#else
1720static inline void crash_enable_local_vmclear(int cpu) { }
1721static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001722#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001723
Nadav Har'Eld462b812011-05-24 15:26:10 +03001724static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001725{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001726 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001727 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001728
Nadav Har'Eld462b812011-05-24 15:26:10 +03001729 if (loaded_vmcs->cpu != cpu)
1730 return; /* vcpu migration can race with cpu offline */
1731 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001732 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001733 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001734 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001735
1736 /*
1737 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1738 * is before setting loaded_vmcs->vcpu to -1 which is done in
1739 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1740 * then adds the vmcs into percpu list before it is deleted.
1741 */
1742 smp_wmb();
1743
Nadav Har'Eld462b812011-05-24 15:26:10 +03001744 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001745 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001746}
1747
Nadav Har'Eld462b812011-05-24 15:26:10 +03001748static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001749{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001750 int cpu = loaded_vmcs->cpu;
1751
1752 if (cpu != -1)
1753 smp_call_function_single(cpu,
1754 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001755}
1756
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001757static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001758{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001759 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001760 return;
1761
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001762 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001763 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001764}
1765
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001766static inline void vpid_sync_vcpu_global(void)
1767{
1768 if (cpu_has_vmx_invvpid_global())
1769 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1770}
1771
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001772static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001773{
1774 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001775 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001776 else
1777 vpid_sync_vcpu_global();
1778}
1779
Sheng Yang14394422008-04-28 12:24:45 +08001780static inline void ept_sync_global(void)
1781{
David Hildenbrandf5f51582017-08-24 20:51:30 +02001782 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08001783}
1784
1785static inline void ept_sync_context(u64 eptp)
1786{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02001787 if (cpu_has_vmx_invept_context())
1788 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1789 else
1790 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08001791}
1792
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001793static __always_inline void vmcs_check16(unsigned long field)
1794{
1795 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1796 "16-bit accessor invalid for 64-bit field");
1797 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1798 "16-bit accessor invalid for 64-bit high field");
1799 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1800 "16-bit accessor invalid for 32-bit high field");
1801 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1802 "16-bit accessor invalid for natural width field");
1803}
1804
1805static __always_inline void vmcs_check32(unsigned long field)
1806{
1807 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1808 "32-bit accessor invalid for 16-bit field");
1809 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1810 "32-bit accessor invalid for natural width field");
1811}
1812
1813static __always_inline void vmcs_check64(unsigned long field)
1814{
1815 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1816 "64-bit accessor invalid for 16-bit field");
1817 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1818 "64-bit accessor invalid for 64-bit high field");
1819 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1820 "64-bit accessor invalid for 32-bit field");
1821 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1822 "64-bit accessor invalid for natural width field");
1823}
1824
1825static __always_inline void vmcs_checkl(unsigned long field)
1826{
1827 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1828 "Natural width accessor invalid for 16-bit field");
1829 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1830 "Natural width accessor invalid for 64-bit field");
1831 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1832 "Natural width accessor invalid for 64-bit high field");
1833 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1834 "Natural width accessor invalid for 32-bit field");
1835}
1836
1837static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001838{
Avi Kivity5e520e62011-05-15 10:13:12 -04001839 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001840
Avi Kivity5e520e62011-05-15 10:13:12 -04001841 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1842 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001843 return value;
1844}
1845
Avi Kivity96304212011-05-15 10:13:13 -04001846static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001847{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001848 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001849 if (static_branch_unlikely(&enable_evmcs))
1850 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001851 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001852}
1853
Avi Kivity96304212011-05-15 10:13:13 -04001854static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001855{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001856 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001857 if (static_branch_unlikely(&enable_evmcs))
1858 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001859 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001860}
1861
Avi Kivity96304212011-05-15 10:13:13 -04001862static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001863{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001864 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001865 if (static_branch_unlikely(&enable_evmcs))
1866 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001867#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001868 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001869#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001870 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001871#endif
1872}
1873
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001874static __always_inline unsigned long vmcs_readl(unsigned long field)
1875{
1876 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001877 if (static_branch_unlikely(&enable_evmcs))
1878 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001879 return __vmcs_readl(field);
1880}
1881
Avi Kivitye52de1b2007-01-05 16:36:56 -08001882static noinline void vmwrite_error(unsigned long field, unsigned long value)
1883{
1884 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1885 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1886 dump_stack();
1887}
1888
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001889static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001890{
1891 u8 error;
1892
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001893 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04001894 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08001895 if (unlikely(error))
1896 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001897}
1898
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001899static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001900{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001901 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001902 if (static_branch_unlikely(&enable_evmcs))
1903 return evmcs_write16(field, value);
1904
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001905 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001906}
1907
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001908static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001909{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001910 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001911 if (static_branch_unlikely(&enable_evmcs))
1912 return evmcs_write32(field, value);
1913
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001914 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001915}
1916
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001917static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001918{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001919 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001920 if (static_branch_unlikely(&enable_evmcs))
1921 return evmcs_write64(field, value);
1922
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001923 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03001924#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001925 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001926 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001927#endif
1928}
1929
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001930static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001931{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001932 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001933 if (static_branch_unlikely(&enable_evmcs))
1934 return evmcs_write64(field, value);
1935
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001936 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001937}
1938
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001939static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001940{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001941 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1942 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001943 if (static_branch_unlikely(&enable_evmcs))
1944 return evmcs_write32(field, evmcs_read32(field) & ~mask);
1945
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001946 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
1947}
1948
1949static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
1950{
1951 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1952 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001953 if (static_branch_unlikely(&enable_evmcs))
1954 return evmcs_write32(field, evmcs_read32(field) | mask);
1955
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001956 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001957}
1958
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001959static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
1960{
1961 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
1962}
1963
Gleb Natapov2961e8762013-11-25 15:37:13 +02001964static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1965{
1966 vmcs_write32(VM_ENTRY_CONTROLS, val);
1967 vmx->vm_entry_controls_shadow = val;
1968}
1969
1970static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1971{
1972 if (vmx->vm_entry_controls_shadow != val)
1973 vm_entry_controls_init(vmx, val);
1974}
1975
1976static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1977{
1978 return vmx->vm_entry_controls_shadow;
1979}
1980
1981
1982static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1983{
1984 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
1985}
1986
1987static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1988{
1989 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
1990}
1991
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001992static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
1993{
1994 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
1995}
1996
Gleb Natapov2961e8762013-11-25 15:37:13 +02001997static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
1998{
1999 vmcs_write32(VM_EXIT_CONTROLS, val);
2000 vmx->vm_exit_controls_shadow = val;
2001}
2002
2003static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2004{
2005 if (vmx->vm_exit_controls_shadow != val)
2006 vm_exit_controls_init(vmx, val);
2007}
2008
2009static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2010{
2011 return vmx->vm_exit_controls_shadow;
2012}
2013
2014
2015static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2016{
2017 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2018}
2019
2020static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2021{
2022 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2023}
2024
Avi Kivity2fb92db2011-04-27 19:42:18 +03002025static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2026{
2027 vmx->segment_cache.bitmask = 0;
2028}
2029
2030static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2031 unsigned field)
2032{
2033 bool ret;
2034 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2035
2036 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2037 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2038 vmx->segment_cache.bitmask = 0;
2039 }
2040 ret = vmx->segment_cache.bitmask & mask;
2041 vmx->segment_cache.bitmask |= mask;
2042 return ret;
2043}
2044
2045static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2046{
2047 u16 *p = &vmx->segment_cache.seg[seg].selector;
2048
2049 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2050 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2051 return *p;
2052}
2053
2054static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2055{
2056 ulong *p = &vmx->segment_cache.seg[seg].base;
2057
2058 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2059 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2060 return *p;
2061}
2062
2063static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2064{
2065 u32 *p = &vmx->segment_cache.seg[seg].limit;
2066
2067 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2068 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2069 return *p;
2070}
2071
2072static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2073{
2074 u32 *p = &vmx->segment_cache.seg[seg].ar;
2075
2076 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2077 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2078 return *p;
2079}
2080
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002081static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2082{
2083 u32 eb;
2084
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002085 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002086 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002087 /*
2088 * Guest access to VMware backdoor ports could legitimately
2089 * trigger #GP because of TSS I/O permission bitmap.
2090 * We intercept those #GP and allow access to them anyway
2091 * as VMware does.
2092 */
2093 if (enable_vmware_backdoor)
2094 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002095 if ((vcpu->guest_debug &
2096 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2097 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2098 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002099 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002100 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002101 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002102 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002103
2104 /* When we are running a nested L2 guest and L1 specified for it a
2105 * certain exception bitmap, we must trap the same exceptions and pass
2106 * them to L1. When running L2, we will only handle the exceptions
2107 * specified above if L1 did not want them.
2108 */
2109 if (is_guest_mode(vcpu))
2110 eb |= get_vmcs12(vcpu)->exception_bitmap;
2111
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002112 vmcs_write32(EXCEPTION_BITMAP, eb);
2113}
2114
Ashok Raj15d45072018-02-01 22:59:43 +01002115/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002116 * Check if MSR is intercepted for currently loaded MSR bitmap.
2117 */
2118static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2119{
2120 unsigned long *msr_bitmap;
2121 int f = sizeof(unsigned long);
2122
2123 if (!cpu_has_vmx_msr_bitmap())
2124 return true;
2125
2126 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2127
2128 if (msr <= 0x1fff) {
2129 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2130 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2131 msr &= 0x1fff;
2132 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2133 }
2134
2135 return true;
2136}
2137
2138/*
Ashok Raj15d45072018-02-01 22:59:43 +01002139 * Check if MSR is intercepted for L01 MSR bitmap.
2140 */
2141static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2142{
2143 unsigned long *msr_bitmap;
2144 int f = sizeof(unsigned long);
2145
2146 if (!cpu_has_vmx_msr_bitmap())
2147 return true;
2148
2149 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2150
2151 if (msr <= 0x1fff) {
2152 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2153 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2154 msr &= 0x1fff;
2155 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2156 }
2157
2158 return true;
2159}
2160
Gleb Natapov2961e8762013-11-25 15:37:13 +02002161static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2162 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002163{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002164 vm_entry_controls_clearbit(vmx, entry);
2165 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002166}
2167
Avi Kivity61d2ef22010-04-28 16:40:38 +03002168static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2169{
2170 unsigned i;
2171 struct msr_autoload *m = &vmx->msr_autoload;
2172
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002173 switch (msr) {
2174 case MSR_EFER:
2175 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002176 clear_atomic_switch_msr_special(vmx,
2177 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002178 VM_EXIT_LOAD_IA32_EFER);
2179 return;
2180 }
2181 break;
2182 case MSR_CORE_PERF_GLOBAL_CTRL:
2183 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002184 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002185 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2186 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2187 return;
2188 }
2189 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002190 }
2191
Avi Kivity61d2ef22010-04-28 16:40:38 +03002192 for (i = 0; i < m->nr; ++i)
2193 if (m->guest[i].index == msr)
2194 break;
2195
2196 if (i == m->nr)
2197 return;
2198 --m->nr;
2199 m->guest[i] = m->guest[m->nr];
2200 m->host[i] = m->host[m->nr];
2201 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2202 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2203}
2204
Gleb Natapov2961e8762013-11-25 15:37:13 +02002205static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2206 unsigned long entry, unsigned long exit,
2207 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2208 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002209{
2210 vmcs_write64(guest_val_vmcs, guest_val);
2211 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002212 vm_entry_controls_setbit(vmx, entry);
2213 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002214}
2215
Avi Kivity61d2ef22010-04-28 16:40:38 +03002216static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
2217 u64 guest_val, u64 host_val)
2218{
2219 unsigned i;
2220 struct msr_autoload *m = &vmx->msr_autoload;
2221
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002222 switch (msr) {
2223 case MSR_EFER:
2224 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002225 add_atomic_switch_msr_special(vmx,
2226 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002227 VM_EXIT_LOAD_IA32_EFER,
2228 GUEST_IA32_EFER,
2229 HOST_IA32_EFER,
2230 guest_val, host_val);
2231 return;
2232 }
2233 break;
2234 case MSR_CORE_PERF_GLOBAL_CTRL:
2235 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002236 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002237 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2238 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2239 GUEST_IA32_PERF_GLOBAL_CTRL,
2240 HOST_IA32_PERF_GLOBAL_CTRL,
2241 guest_val, host_val);
2242 return;
2243 }
2244 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002245 case MSR_IA32_PEBS_ENABLE:
2246 /* PEBS needs a quiescent period after being disabled (to write
2247 * a record). Disabling PEBS through VMX MSR swapping doesn't
2248 * provide that period, so a CPU could write host's record into
2249 * guest's memory.
2250 */
2251 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002252 }
2253
Avi Kivity61d2ef22010-04-28 16:40:38 +03002254 for (i = 0; i < m->nr; ++i)
2255 if (m->guest[i].index == msr)
2256 break;
2257
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002258 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002259 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002260 "Can't add msr %x\n", msr);
2261 return;
2262 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03002263 ++m->nr;
2264 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2265 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2266 }
2267
2268 m->guest[i].index = msr;
2269 m->guest[i].value = guest_val;
2270 m->host[i].index = msr;
2271 m->host[i].value = host_val;
2272}
2273
Avi Kivity92c0d902009-10-29 11:00:16 +02002274static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002275{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002276 u64 guest_efer = vmx->vcpu.arch.efer;
2277 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002278
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002279 if (!enable_ept) {
2280 /*
2281 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2282 * host CPUID is more efficient than testing guest CPUID
2283 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2284 */
2285 if (boot_cpu_has(X86_FEATURE_SMEP))
2286 guest_efer |= EFER_NX;
2287 else if (!(guest_efer & EFER_NX))
2288 ignore_bits |= EFER_NX;
2289 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002290
Avi Kivity51c6cf62007-08-29 03:48:05 +03002291 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002292 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002293 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002294 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002295#ifdef CONFIG_X86_64
2296 ignore_bits |= EFER_LMA | EFER_LME;
2297 /* SCE is meaningful only in long mode on Intel */
2298 if (guest_efer & EFER_LMA)
2299 ignore_bits &= ~(u64)EFER_SCE;
2300#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002301
2302 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002303
2304 /*
2305 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2306 * On CPUs that support "load IA32_EFER", always switch EFER
2307 * atomically, since it's faster than switching it manually.
2308 */
2309 if (cpu_has_load_ia32_efer ||
2310 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002311 if (!(guest_efer & EFER_LMA))
2312 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002313 if (guest_efer != host_efer)
2314 add_atomic_switch_msr(vmx, MSR_EFER,
2315 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002316 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002317 } else {
2318 guest_efer &= ~ignore_bits;
2319 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002320
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002321 vmx->guest_msrs[efer_offset].data = guest_efer;
2322 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2323
2324 return true;
2325 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002326}
2327
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002328#ifdef CONFIG_X86_32
2329/*
2330 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2331 * VMCS rather than the segment table. KVM uses this helper to figure
2332 * out the current bases to poke them into the VMCS before entry.
2333 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002334static unsigned long segment_base(u16 selector)
2335{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002336 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002337 unsigned long v;
2338
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002339 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002340 return 0;
2341
Thomas Garnier45fc8752017-03-14 10:05:08 -07002342 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002343
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002344 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002345 u16 ldt_selector = kvm_read_ldt();
2346
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002347 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002348 return 0;
2349
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002350 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002351 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002352 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002353 return v;
2354}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002355#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002356
Avi Kivity04d2cc72007-09-10 18:10:54 +03002357static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002358{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002359 struct vcpu_vmx *vmx = to_vmx(vcpu);
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002360#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002361 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002362#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03002363 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002364
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002365 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002366 return;
2367
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002368 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002369 /*
2370 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2371 * allow segment selectors with cpl > 0 or ti == 1.
2372 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002373 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002374 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002375
2376#ifdef CONFIG_X86_64
2377 save_fsgs_for_kvm();
2378 vmx->host_state.fs_sel = current->thread.fsindex;
2379 vmx->host_state.gs_sel = current->thread.gsindex;
2380#else
Avi Kivity9581d442010-10-19 16:46:55 +02002381 savesegment(fs, vmx->host_state.fs_sel);
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002382 savesegment(gs, vmx->host_state.gs_sel);
2383#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002384 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002385 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002386 vmx->host_state.fs_reload_needed = 0;
2387 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002388 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002389 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002390 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002391 if (!(vmx->host_state.gs_sel & 7))
2392 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002393 else {
2394 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002395 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002396 }
2397
2398#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002399 savesegment(ds, vmx->host_state.ds_sel);
2400 savesegment(es, vmx->host_state.es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002401
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002402 vmcs_writel(HOST_FS_BASE, current->thread.fsbase);
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002403 vmcs_writel(HOST_GS_BASE, cpu_kernelmode_gs_base(cpu));
Avi Kivity707c0872007-05-02 17:33:43 +03002404
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002405 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Avi Kivityc8770e72010-11-11 12:37:26 +02002406 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002407 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002408#else
2409 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2410 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
2411#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002412 if (boot_cpu_has(X86_FEATURE_MPX))
2413 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002414 for (i = 0; i < vmx->save_nmsrs; ++i)
2415 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002416 vmx->guest_msrs[i].data,
2417 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002418}
2419
Avi Kivitya9b21b62008-06-24 11:48:49 +03002420static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002421{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002422 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002423 return;
2424
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002425 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002426 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002427#ifdef CONFIG_X86_64
2428 if (is_long_mode(&vmx->vcpu))
2429 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2430#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002431 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002432 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002433#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002434 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002435#else
2436 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002437#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002438 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002439 if (vmx->host_state.fs_reload_needed)
2440 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002441#ifdef CONFIG_X86_64
2442 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2443 loadsegment(ds, vmx->host_state.ds_sel);
2444 loadsegment(es, vmx->host_state.es_sel);
2445 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002446#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002447 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002448#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002449 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002450#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002451 if (vmx->host_state.msr_host_bndcfgs)
2452 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Thomas Garnier45fc8752017-03-14 10:05:08 -07002453 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002454}
2455
Avi Kivitya9b21b62008-06-24 11:48:49 +03002456static void vmx_load_host_state(struct vcpu_vmx *vmx)
2457{
2458 preempt_disable();
2459 __vmx_load_host_state(vmx);
2460 preempt_enable();
2461}
2462
Feng Wu28b835d2015-09-18 22:29:54 +08002463static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2464{
2465 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2466 struct pi_desc old, new;
2467 unsigned int dest;
2468
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002469 /*
2470 * In case of hot-plug or hot-unplug, we may have to undo
2471 * vmx_vcpu_pi_put even if there is no assigned device. And we
2472 * always keep PI.NDST up to date for simplicity: it makes the
2473 * code easier, and CPU migration is not a fast path.
2474 */
2475 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002476 return;
2477
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002478 /*
2479 * First handle the simple case where no cmpxchg is necessary; just
2480 * allow posting non-urgent interrupts.
2481 *
2482 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2483 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2484 * expects the VCPU to be on the blocked_vcpu_list that matches
2485 * PI.NDST.
2486 */
2487 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2488 vcpu->cpu == cpu) {
2489 pi_clear_sn(pi_desc);
2490 return;
2491 }
2492
2493 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002494 do {
2495 old.control = new.control = pi_desc->control;
2496
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002497 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002498
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002499 if (x2apic_enabled())
2500 new.ndst = dest;
2501 else
2502 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002503
Feng Wu28b835d2015-09-18 22:29:54 +08002504 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02002505 } while (cmpxchg64(&pi_desc->control, old.control,
2506 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002507}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002508
Peter Feinerc95ba922016-08-17 09:36:47 -07002509static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2510{
2511 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2512 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2513}
2514
Avi Kivity6aa8b732006-12-10 02:21:36 -08002515/*
2516 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2517 * vcpu mutex is already taken.
2518 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002519static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002520{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002521 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002522 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002523
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002524 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002525 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002526 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002527 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002528
2529 /*
2530 * Read loaded_vmcs->cpu should be before fetching
2531 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2532 * See the comments in __loaded_vmcs_clear().
2533 */
2534 smp_rmb();
2535
Nadav Har'Eld462b812011-05-24 15:26:10 +03002536 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2537 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002538 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002539 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002540 }
2541
2542 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2543 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2544 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01002545 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002546 }
2547
2548 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002549 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002550 unsigned long sysenter_esp;
2551
2552 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002553
Avi Kivity6aa8b732006-12-10 02:21:36 -08002554 /*
2555 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002556 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002557 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002558 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01002559 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002560 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002561
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002562 /*
2563 * VM exits change the host TR limit to 0x67 after a VM
2564 * exit. This is okay, since 0x67 covers everything except
2565 * the IO bitmap and have have code to handle the IO bitmap
2566 * being lost after a VM exit.
2567 */
2568 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2569
Avi Kivity6aa8b732006-12-10 02:21:36 -08002570 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2571 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002572
Nadav Har'Eld462b812011-05-24 15:26:10 +03002573 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002574 }
Feng Wu28b835d2015-09-18 22:29:54 +08002575
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002576 /* Setup TSC multiplier */
2577 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002578 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2579 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002580
Feng Wu28b835d2015-09-18 22:29:54 +08002581 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002582 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08002583 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08002584}
2585
2586static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2587{
2588 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2589
2590 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002591 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2592 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002593 return;
2594
2595 /* Set SN when the vCPU is preempted */
2596 if (vcpu->preempted)
2597 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002598}
2599
2600static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2601{
Feng Wu28b835d2015-09-18 22:29:54 +08002602 vmx_vcpu_pi_put(vcpu);
2603
Avi Kivitya9b21b62008-06-24 11:48:49 +03002604 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002605}
2606
Wanpeng Lif244dee2017-07-20 01:11:54 -07002607static bool emulation_required(struct kvm_vcpu *vcpu)
2608{
2609 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2610}
2611
Avi Kivityedcafe32009-12-30 18:07:40 +02002612static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2613
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002614/*
2615 * Return the cr0 value that a nested guest would read. This is a combination
2616 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2617 * its hypervisor (cr0_read_shadow).
2618 */
2619static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2620{
2621 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2622 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2623}
2624static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2625{
2626 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2627 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2628}
2629
Avi Kivity6aa8b732006-12-10 02:21:36 -08002630static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2631{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002632 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002633
Avi Kivity6de12732011-03-07 12:51:22 +02002634 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2635 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2636 rflags = vmcs_readl(GUEST_RFLAGS);
2637 if (to_vmx(vcpu)->rmode.vm86_active) {
2638 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2639 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2640 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2641 }
2642 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002643 }
Avi Kivity6de12732011-03-07 12:51:22 +02002644 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002645}
2646
2647static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2648{
Wanpeng Lif244dee2017-07-20 01:11:54 -07002649 unsigned long old_rflags = vmx_get_rflags(vcpu);
2650
Avi Kivity6de12732011-03-07 12:51:22 +02002651 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2652 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002653 if (to_vmx(vcpu)->rmode.vm86_active) {
2654 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002655 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002656 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002657 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07002658
2659 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2660 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002661}
2662
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002663static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002664{
2665 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2666 int ret = 0;
2667
2668 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002669 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002670 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002671 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002672
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002673 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002674}
2675
2676static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2677{
2678 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2679 u32 interruptibility = interruptibility_old;
2680
2681 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2682
Jan Kiszka48005f62010-02-19 19:38:07 +01002683 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002684 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002685 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002686 interruptibility |= GUEST_INTR_STATE_STI;
2687
2688 if ((interruptibility != interruptibility_old))
2689 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2690}
2691
Avi Kivity6aa8b732006-12-10 02:21:36 -08002692static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2693{
2694 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002695
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002696 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002697 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002698 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002699
Glauber Costa2809f5d2009-05-12 16:21:05 -04002700 /* skipping an emulated instruction also counts */
2701 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002702}
2703
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002704static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
2705 unsigned long exit_qual)
2706{
2707 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2708 unsigned int nr = vcpu->arch.exception.nr;
2709 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2710
2711 if (vcpu->arch.exception.has_error_code) {
2712 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
2713 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2714 }
2715
2716 if (kvm_exception_is_soft(nr))
2717 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2718 else
2719 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2720
2721 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
2722 vmx_get_nmi_mask(vcpu))
2723 intr_info |= INTR_INFO_UNBLOCK_NMI;
2724
2725 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
2726}
2727
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002728/*
2729 * KVM wants to inject page-faults which it got to the guest. This function
2730 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002731 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002732static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002733{
2734 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002735 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002736
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002737 if (nr == PF_VECTOR) {
2738 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002739 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002740 return 1;
2741 }
2742 /*
2743 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
2744 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2745 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
2746 * can be written only when inject_pending_event runs. This should be
2747 * conditional on a new capability---if the capability is disabled,
2748 * kvm_multiple_exception would write the ancillary information to
2749 * CR2 or DR6, for backwards ABI-compatibility.
2750 */
2751 if (nested_vmx_is_page_fault_vmexit(vmcs12,
2752 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002753 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002754 return 1;
2755 }
2756 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002757 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002758 if (nr == DB_VECTOR)
2759 *exit_qual = vcpu->arch.dr6;
2760 else
2761 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002762 return 1;
2763 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002764 }
2765
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002766 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002767}
2768
Wanpeng Licaa057a2018-03-12 04:53:03 -07002769static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
2770{
2771 /*
2772 * Ensure that we clear the HLT state in the VMCS. We don't need to
2773 * explicitly skip the instruction because if the HLT state is set,
2774 * then the instruction is already executing and RIP has already been
2775 * advanced.
2776 */
2777 if (kvm_hlt_in_guest(vcpu->kvm) &&
2778 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
2779 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
2780}
2781
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002782static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02002783{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002784 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002785 unsigned nr = vcpu->arch.exception.nr;
2786 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002787 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002788 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002789
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002790 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002791 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002792 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2793 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002794
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002795 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002796 int inc_eip = 0;
2797 if (kvm_exception_is_soft(nr))
2798 inc_eip = vcpu->arch.event_exit_inst_len;
2799 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002800 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002801 return;
2802 }
2803
Sean Christophersonadd5ff72018-03-23 09:34:00 -07002804 WARN_ON_ONCE(vmx->emulation_required);
2805
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002806 if (kvm_exception_is_soft(nr)) {
2807 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2808 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002809 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2810 } else
2811 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2812
2813 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07002814
2815 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02002816}
2817
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002818static bool vmx_rdtscp_supported(void)
2819{
2820 return cpu_has_vmx_rdtscp();
2821}
2822
Mao, Junjiead756a12012-07-02 01:18:48 +00002823static bool vmx_invpcid_supported(void)
2824{
2825 return cpu_has_vmx_invpcid() && enable_ept;
2826}
2827
Avi Kivity6aa8b732006-12-10 02:21:36 -08002828/*
Eddie Donga75beee2007-05-17 18:55:15 +03002829 * Swap MSR entry in host/guest MSR entry array.
2830 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002831static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002832{
Avi Kivity26bb0982009-09-07 11:14:12 +03002833 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002834
2835 tmp = vmx->guest_msrs[to];
2836 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2837 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002838}
2839
2840/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002841 * Set up the vmcs to automatically save and restore system
2842 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2843 * mode, as fiddling with msrs is very expensive.
2844 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002845static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002846{
Avi Kivity26bb0982009-09-07 11:14:12 +03002847 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002848
Eddie Donga75beee2007-05-17 18:55:15 +03002849 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002850#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002851 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002852 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002853 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002854 move_msr_up(vmx, index, save_nmsrs++);
2855 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002856 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002857 move_msr_up(vmx, index, save_nmsrs++);
2858 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002859 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002860 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002861 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02002862 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002863 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002864 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002865 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002866 * if efer.sce is enabled.
2867 */
Brian Gerst8c065852010-07-17 09:03:26 -04002868 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002869 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002870 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002871 }
Eddie Donga75beee2007-05-17 18:55:15 +03002872#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002873 index = __find_msr_index(vmx, MSR_EFER);
2874 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002875 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002876
Avi Kivity26bb0982009-09-07 11:14:12 +03002877 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002878
Yang Zhang8d146952013-01-25 10:18:50 +08002879 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002880 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002881}
2882
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02002883static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002884{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02002885 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002886
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02002887 if (is_guest_mode(vcpu) &&
2888 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
2889 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
2890
2891 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002892}
2893
2894/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002895 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002896 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002897static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002898{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002899 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002900 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002901 * We're here if L1 chose not to trap WRMSR to TSC. According
2902 * to the spec, this should set L1's TSC; The offset that L1
2903 * set for L2 remains unchanged, and still needs to be added
2904 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002905 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002906 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002907 /* recalculate vmcs02.TSC_OFFSET: */
2908 vmcs12 = get_vmcs12(vcpu);
2909 vmcs_write64(TSC_OFFSET, offset +
2910 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2911 vmcs12->tsc_offset : 0));
2912 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002913 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2914 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002915 vmcs_write64(TSC_OFFSET, offset);
2916 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002917}
2918
Nadav Har'El801d3422011-05-25 23:02:23 +03002919/*
2920 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2921 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2922 * all guests if the "nested" module option is off, and can also be disabled
2923 * for a single guest by disabling its VMX cpuid bit.
2924 */
2925static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2926{
Radim Krčmářd6321d42017-08-05 00:12:49 +02002927 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03002928}
2929
Avi Kivity6aa8b732006-12-10 02:21:36 -08002930/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002931 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2932 * returned for the various VMX controls MSRs when nested VMX is enabled.
2933 * The same values should also be used to verify that vmcs12 control fields are
2934 * valid during nested entry from L1 to L2.
2935 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2936 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2937 * bit in the high half is on if the corresponding bit in the control field
2938 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002939 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002940static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002941{
Paolo Bonzini13893092018-02-26 13:40:09 +01002942 if (!nested) {
2943 memset(msrs, 0, sizeof(*msrs));
2944 return;
2945 }
2946
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002947 /*
2948 * Note that as a general rule, the high half of the MSRs (bits in
2949 * the control fields which may be 1) should be initialized by the
2950 * intersection of the underlying hardware's MSR (i.e., features which
2951 * can be supported) and the list of features we want to expose -
2952 * because they are known to be properly supported in our code.
2953 * Also, usually, the low half of the MSRs (bits which must be 1) can
2954 * be set to 0, meaning that L1 may turn off any of these bits. The
2955 * reason is that if one of these bits is necessary, it will appear
2956 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2957 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02002958 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002959 * These rules have exceptions below.
2960 */
2961
2962 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002963 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002964 msrs->pinbased_ctls_low,
2965 msrs->pinbased_ctls_high);
2966 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002967 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002968 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002969 PIN_BASED_EXT_INTR_MASK |
2970 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01002971 PIN_BASED_VIRTUAL_NMIS |
2972 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002973 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002974 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002975 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002976
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002977 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002978 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002979 msrs->exit_ctls_low,
2980 msrs->exit_ctls_high);
2981 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08002982 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04002983
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002984 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002985#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002986 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002987#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01002988 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002989 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002990 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01002991 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04002992 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2993
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002994 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002995 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002996
Jan Kiszka2996fca2014-06-16 13:59:43 +02002997 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002998 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02002999
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003000 /* entry controls */
3001 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003002 msrs->entry_ctls_low,
3003 msrs->entry_ctls_high);
3004 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003005 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003006 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003007#ifdef CONFIG_X86_64
3008 VM_ENTRY_IA32E_MODE |
3009#endif
3010 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003011 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003012 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003013 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003014 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02003015
Jan Kiszka2996fca2014-06-16 13:59:43 +02003016 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003017 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003018
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003019 /* cpu-based controls */
3020 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003021 msrs->procbased_ctls_low,
3022 msrs->procbased_ctls_high);
3023 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003024 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003025 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003026 CPU_BASED_VIRTUAL_INTR_PENDING |
3027 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003028 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3029 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3030 CPU_BASED_CR3_STORE_EXITING |
3031#ifdef CONFIG_X86_64
3032 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3033#endif
3034 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003035 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3036 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3037 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3038 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003039 /*
3040 * We can allow some features even when not supported by the
3041 * hardware. For example, L1 can specify an MSR bitmap - and we
3042 * can use it to avoid exits to L1 - even when L0 runs L2
3043 * without MSR bitmaps.
3044 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003045 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003046 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003047 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003048
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003049 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003050 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003051 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3052
Paolo Bonzini80154d72017-08-24 13:55:35 +02003053 /*
3054 * secondary cpu-based controls. Do not include those that
3055 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3056 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003057 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003058 msrs->secondary_ctls_low,
3059 msrs->secondary_ctls_high);
3060 msrs->secondary_ctls_low = 0;
3061 msrs->secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01003062 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02003063 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003064 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003065 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003066 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003067 SECONDARY_EXEC_WBINVD_EXITING;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003068
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003069 if (enable_ept) {
3070 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003071 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003072 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003073 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003074 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003075 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003076 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003077 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003078 msrs->ept_caps &= vmx_capability.ept;
3079 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003080 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3081 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003082 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003083 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003084 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003085 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003086 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003087 }
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003088
Bandan Das27c42a12017-08-03 15:54:42 -04003089 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003090 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003091 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003092 /*
3093 * Advertise EPTP switching unconditionally
3094 * since we emulate it
3095 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003096 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003097 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003098 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003099 }
3100
Paolo Bonzinief697a72016-03-18 16:58:38 +01003101 /*
3102 * Old versions of KVM use the single-context version without
3103 * checking for support, so declare that it is supported even
3104 * though it is treated as global context. The alternative is
3105 * not failing the single-context invvpid, and it is worse.
3106 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003107 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003108 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003109 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003110 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003111 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003112 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003113
Radim Krčmář0790ec12015-03-17 14:02:32 +01003114 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003115 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003116 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3117
Jan Kiszkac18911a2013-03-13 16:06:41 +01003118 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003119 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003120 msrs->misc_low,
3121 msrs->misc_high);
3122 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3123 msrs->misc_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003124 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003125 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003126 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003127
3128 /*
3129 * This MSR reports some information about VMX support. We
3130 * should return information about the VMX we emulate for the
3131 * guest, and the VMCS structure we give it - not about the
3132 * VMX support of the underlying hardware.
3133 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003134 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003135 VMCS12_REVISION |
3136 VMX_BASIC_TRUE_CTLS |
3137 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3138 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3139
3140 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003141 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003142
3143 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003144 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003145 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3146 * We picked the standard core2 setting.
3147 */
3148#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3149#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003150 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3151 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003152
3153 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003154 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3155 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003156
3157 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003158 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003159}
3160
David Matlack38991522016-11-29 18:14:08 -08003161/*
3162 * if fixed0[i] == 1: val[i] must be 1
3163 * if fixed1[i] == 0: val[i] must be 0
3164 */
3165static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3166{
3167 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003168}
3169
3170static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3171{
David Matlack38991522016-11-29 18:14:08 -08003172 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003173}
3174
3175static inline u64 vmx_control_msr(u32 low, u32 high)
3176{
3177 return low | ((u64)high << 32);
3178}
3179
David Matlack62cc6b9d2016-11-29 18:14:07 -08003180static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3181{
3182 superset &= mask;
3183 subset &= mask;
3184
3185 return (superset | subset) == superset;
3186}
3187
3188static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3189{
3190 const u64 feature_and_reserved =
3191 /* feature (except bit 48; see below) */
3192 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3193 /* reserved */
3194 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003195 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003196
3197 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3198 return -EINVAL;
3199
3200 /*
3201 * KVM does not emulate a version of VMX that constrains physical
3202 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3203 */
3204 if (data & BIT_ULL(48))
3205 return -EINVAL;
3206
3207 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3208 vmx_basic_vmcs_revision_id(data))
3209 return -EINVAL;
3210
3211 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3212 return -EINVAL;
3213
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003214 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003215 return 0;
3216}
3217
3218static int
3219vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3220{
3221 u64 supported;
3222 u32 *lowp, *highp;
3223
3224 switch (msr_index) {
3225 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003226 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3227 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003228 break;
3229 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003230 lowp = &vmx->nested.msrs.procbased_ctls_low;
3231 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003232 break;
3233 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003234 lowp = &vmx->nested.msrs.exit_ctls_low;
3235 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003236 break;
3237 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003238 lowp = &vmx->nested.msrs.entry_ctls_low;
3239 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003240 break;
3241 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003242 lowp = &vmx->nested.msrs.secondary_ctls_low;
3243 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003244 break;
3245 default:
3246 BUG();
3247 }
3248
3249 supported = vmx_control_msr(*lowp, *highp);
3250
3251 /* Check must-be-1 bits are still 1. */
3252 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3253 return -EINVAL;
3254
3255 /* Check must-be-0 bits are still 0. */
3256 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3257 return -EINVAL;
3258
3259 *lowp = data;
3260 *highp = data >> 32;
3261 return 0;
3262}
3263
3264static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3265{
3266 const u64 feature_and_reserved_bits =
3267 /* feature */
3268 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3269 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3270 /* reserved */
3271 GENMASK_ULL(13, 9) | BIT_ULL(31);
3272 u64 vmx_misc;
3273
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003274 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3275 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003276
3277 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3278 return -EINVAL;
3279
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003280 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003281 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3282 vmx_misc_preemption_timer_rate(data) !=
3283 vmx_misc_preemption_timer_rate(vmx_misc))
3284 return -EINVAL;
3285
3286 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3287 return -EINVAL;
3288
3289 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3290 return -EINVAL;
3291
3292 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3293 return -EINVAL;
3294
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003295 vmx->nested.msrs.misc_low = data;
3296 vmx->nested.msrs.misc_high = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003297 return 0;
3298}
3299
3300static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3301{
3302 u64 vmx_ept_vpid_cap;
3303
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003304 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3305 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003306
3307 /* Every bit is either reserved or a feature bit. */
3308 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3309 return -EINVAL;
3310
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003311 vmx->nested.msrs.ept_caps = data;
3312 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003313 return 0;
3314}
3315
3316static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3317{
3318 u64 *msr;
3319
3320 switch (msr_index) {
3321 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003322 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003323 break;
3324 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003325 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003326 break;
3327 default:
3328 BUG();
3329 }
3330
3331 /*
3332 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3333 * must be 1 in the restored value.
3334 */
3335 if (!is_bitwise_subset(data, *msr, -1ULL))
3336 return -EINVAL;
3337
3338 *msr = data;
3339 return 0;
3340}
3341
3342/*
3343 * Called when userspace is restoring VMX MSRs.
3344 *
3345 * Returns 0 on success, non-0 otherwise.
3346 */
3347static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3348{
3349 struct vcpu_vmx *vmx = to_vmx(vcpu);
3350
3351 switch (msr_index) {
3352 case MSR_IA32_VMX_BASIC:
3353 return vmx_restore_vmx_basic(vmx, data);
3354 case MSR_IA32_VMX_PINBASED_CTLS:
3355 case MSR_IA32_VMX_PROCBASED_CTLS:
3356 case MSR_IA32_VMX_EXIT_CTLS:
3357 case MSR_IA32_VMX_ENTRY_CTLS:
3358 /*
3359 * The "non-true" VMX capability MSRs are generated from the
3360 * "true" MSRs, so we do not support restoring them directly.
3361 *
3362 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3363 * should restore the "true" MSRs with the must-be-1 bits
3364 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3365 * DEFAULT SETTINGS".
3366 */
3367 return -EINVAL;
3368 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3369 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3370 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3371 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3372 case MSR_IA32_VMX_PROCBASED_CTLS2:
3373 return vmx_restore_control_msr(vmx, msr_index, data);
3374 case MSR_IA32_VMX_MISC:
3375 return vmx_restore_vmx_misc(vmx, data);
3376 case MSR_IA32_VMX_CR0_FIXED0:
3377 case MSR_IA32_VMX_CR4_FIXED0:
3378 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3379 case MSR_IA32_VMX_CR0_FIXED1:
3380 case MSR_IA32_VMX_CR4_FIXED1:
3381 /*
3382 * These MSRs are generated based on the vCPU's CPUID, so we
3383 * do not support restoring them directly.
3384 */
3385 return -EINVAL;
3386 case MSR_IA32_VMX_EPT_VPID_CAP:
3387 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3388 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003389 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003390 return 0;
3391 default:
3392 /*
3393 * The rest of the VMX capability MSRs do not support restore.
3394 */
3395 return -EINVAL;
3396 }
3397}
3398
Jan Kiszkacae50132014-01-04 18:47:22 +01003399/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003400static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003401{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003402 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003403 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003404 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003405 break;
3406 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3407 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003408 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003409 msrs->pinbased_ctls_low,
3410 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003411 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3412 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003413 break;
3414 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3415 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003416 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003417 msrs->procbased_ctls_low,
3418 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003419 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3420 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003421 break;
3422 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3423 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003424 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003425 msrs->exit_ctls_low,
3426 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003427 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3428 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003429 break;
3430 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3431 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003432 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003433 msrs->entry_ctls_low,
3434 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003435 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3436 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003437 break;
3438 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003439 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003440 msrs->misc_low,
3441 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003442 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003443 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003444 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003445 break;
3446 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003447 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003448 break;
3449 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003450 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003451 break;
3452 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003453 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003454 break;
3455 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003456 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003457 break;
3458 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003459 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003460 msrs->secondary_ctls_low,
3461 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003462 break;
3463 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003464 *pdata = msrs->ept_caps |
3465 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003466 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003467 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003468 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04003469 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003470 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003471 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003472 }
3473
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003474 return 0;
3475}
3476
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003477static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3478 uint64_t val)
3479{
3480 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3481
3482 return !(val & ~valid_bits);
3483}
3484
Tom Lendacky801e4592018-02-21 13:39:51 -06003485static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3486{
Paolo Bonzini13893092018-02-26 13:40:09 +01003487 switch (msr->index) {
3488 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3489 if (!nested)
3490 return 1;
3491 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
3492 default:
3493 return 1;
3494 }
3495
3496 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06003497}
3498
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003499/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003500 * Reads an msr value (of 'msr_index') into 'pdata'.
3501 * Returns 0 on success, non-0 otherwise.
3502 * Assumes vcpu_load() was already called.
3503 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003504static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003505{
Borislav Petkova6cb0992017-12-20 12:50:28 +01003506 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003507 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003508
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003509 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003510#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003511 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003512 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003513 break;
3514 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003515 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003516 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003517 case MSR_KERNEL_GS_BASE:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003518 vmx_load_host_state(vmx);
3519 msr_info->data = vmx->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003520 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003521#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003522 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003523 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003524 case MSR_IA32_SPEC_CTRL:
3525 if (!msr_info->host_initiated &&
Borislav Petkove7c587d2018-05-02 18:15:14 +02003526 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003527 return 1;
3528
3529 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3530 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003531 case MSR_IA32_ARCH_CAPABILITIES:
3532 if (!msr_info->host_initiated &&
3533 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3534 return 1;
3535 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3536 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003537 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003538 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003539 break;
3540 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003541 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003542 break;
3543 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003544 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003545 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003546 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003547 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003548 (!msr_info->host_initiated &&
3549 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003550 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003551 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003552 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003553 case MSR_IA32_MCG_EXT_CTL:
3554 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01003555 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08003556 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003557 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003558 msr_info->data = vcpu->arch.mcg_ext_ctl;
3559 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003560 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003561 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003562 break;
3563 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3564 if (!nested_vmx_allowed(vcpu))
3565 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003566 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
3567 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003568 case MSR_IA32_XSS:
3569 if (!vmx_xsaves_supported())
3570 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003571 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003572 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003573 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003574 if (!msr_info->host_initiated &&
3575 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003576 return 1;
3577 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003578 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003579 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003580 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003581 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003582 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003583 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003584 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003585 }
3586
Avi Kivity6aa8b732006-12-10 02:21:36 -08003587 return 0;
3588}
3589
Jan Kiszkacae50132014-01-04 18:47:22 +01003590static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3591
Avi Kivity6aa8b732006-12-10 02:21:36 -08003592/*
3593 * Writes msr value into into the appropriate "register".
3594 * Returns 0 on success, non-0 otherwise.
3595 * Assumes vcpu_load() was already called.
3596 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003597static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003598{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003599 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003600 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003601 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003602 u32 msr_index = msr_info->index;
3603 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003604
Avi Kivity6aa8b732006-12-10 02:21:36 -08003605 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003606 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003607 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003608 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003609#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003610 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003611 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003612 vmcs_writel(GUEST_FS_BASE, data);
3613 break;
3614 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003615 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003616 vmcs_writel(GUEST_GS_BASE, data);
3617 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003618 case MSR_KERNEL_GS_BASE:
3619 vmx_load_host_state(vmx);
3620 vmx->msr_guest_kernel_gs_base = data;
3621 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003622#endif
3623 case MSR_IA32_SYSENTER_CS:
3624 vmcs_write32(GUEST_SYSENTER_CS, data);
3625 break;
3626 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003627 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003628 break;
3629 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003630 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003631 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003632 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003633 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003634 (!msr_info->host_initiated &&
3635 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003636 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08003637 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07003638 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003639 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003640 vmcs_write64(GUEST_BNDCFGS, data);
3641 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003642 case MSR_IA32_SPEC_CTRL:
3643 if (!msr_info->host_initiated &&
Borislav Petkove7c587d2018-05-02 18:15:14 +02003644 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003645 return 1;
3646
3647 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02003648 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003649 return 1;
3650
3651 vmx->spec_ctrl = data;
3652
3653 if (!data)
3654 break;
3655
3656 /*
3657 * For non-nested:
3658 * When it's written (to non-zero) for the first time, pass
3659 * it through.
3660 *
3661 * For nested:
3662 * The handling of the MSR bitmap for L2 guests is done in
3663 * nested_vmx_merge_msr_bitmap. We should not touch the
3664 * vmcs02.msr_bitmap here since it gets completely overwritten
3665 * in the merging. We update the vmcs01 here for L1 as well
3666 * since it will end up touching the MSR anyway now.
3667 */
3668 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3669 MSR_IA32_SPEC_CTRL,
3670 MSR_TYPE_RW);
3671 break;
Ashok Raj15d45072018-02-01 22:59:43 +01003672 case MSR_IA32_PRED_CMD:
3673 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01003674 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3675 return 1;
3676
3677 if (data & ~PRED_CMD_IBPB)
3678 return 1;
3679
3680 if (!data)
3681 break;
3682
3683 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3684
3685 /*
3686 * For non-nested:
3687 * When it's written (to non-zero) for the first time, pass
3688 * it through.
3689 *
3690 * For nested:
3691 * The handling of the MSR bitmap for L2 guests is done in
3692 * nested_vmx_merge_msr_bitmap. We should not touch the
3693 * vmcs02.msr_bitmap here since it gets completely overwritten
3694 * in the merging.
3695 */
3696 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3697 MSR_TYPE_W);
3698 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003699 case MSR_IA32_ARCH_CAPABILITIES:
3700 if (!msr_info->host_initiated)
3701 return 1;
3702 vmx->arch_capabilities = data;
3703 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003704 case MSR_IA32_CR_PAT:
3705 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003706 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3707 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003708 vmcs_write64(GUEST_IA32_PAT, data);
3709 vcpu->arch.pat = data;
3710 break;
3711 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003712 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003713 break;
Will Auldba904632012-11-29 12:42:50 -08003714 case MSR_IA32_TSC_ADJUST:
3715 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003716 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003717 case MSR_IA32_MCG_EXT_CTL:
3718 if ((!msr_info->host_initiated &&
3719 !(to_vmx(vcpu)->msr_ia32_feature_control &
3720 FEATURE_CONTROL_LMCE)) ||
3721 (data & ~MCG_EXT_CTL_LMCE_EN))
3722 return 1;
3723 vcpu->arch.mcg_ext_ctl = data;
3724 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003725 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003726 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003727 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003728 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3729 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003730 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003731 if (msr_info->host_initiated && data == 0)
3732 vmx_leave_nested(vcpu);
3733 break;
3734 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003735 if (!msr_info->host_initiated)
3736 return 1; /* they are read-only */
3737 if (!nested_vmx_allowed(vcpu))
3738 return 1;
3739 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08003740 case MSR_IA32_XSS:
3741 if (!vmx_xsaves_supported())
3742 return 1;
3743 /*
3744 * The only supported bit as of Skylake is bit 8, but
3745 * it is not supported on KVM.
3746 */
3747 if (data != 0)
3748 return 1;
3749 vcpu->arch.ia32_xss = data;
3750 if (vcpu->arch.ia32_xss != host_xss)
3751 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
3752 vcpu->arch.ia32_xss, host_xss);
3753 else
3754 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3755 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003756 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003757 if (!msr_info->host_initiated &&
3758 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003759 return 1;
3760 /* Check reserved bit, higher 32 bits should be zero */
3761 if ((data >> 32) != 0)
3762 return 1;
3763 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003764 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10003765 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003766 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07003767 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003768 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003769 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3770 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003771 ret = kvm_set_shared_msr(msr->index, msr->data,
3772 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03003773 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003774 if (ret)
3775 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003776 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08003777 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003778 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003779 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003780 }
3781
Eddie Dong2cc51562007-05-21 07:28:09 +03003782 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003783}
3784
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003785static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003786{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003787 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3788 switch (reg) {
3789 case VCPU_REGS_RSP:
3790 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3791 break;
3792 case VCPU_REGS_RIP:
3793 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3794 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003795 case VCPU_EXREG_PDPTR:
3796 if (enable_ept)
3797 ept_save_pdptrs(vcpu);
3798 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003799 default:
3800 break;
3801 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003802}
3803
Avi Kivity6aa8b732006-12-10 02:21:36 -08003804static __init int cpu_has_kvm_support(void)
3805{
Eduardo Habkost6210e372008-11-17 19:03:16 -02003806 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003807}
3808
3809static __init int vmx_disabled_by_bios(void)
3810{
3811 u64 msr;
3812
3813 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04003814 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08003815 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04003816 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3817 && tboot_enabled())
3818 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08003819 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04003820 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08003821 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08003822 && !tboot_enabled()) {
3823 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08003824 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04003825 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08003826 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08003827 /* launched w/o TXT and VMX disabled */
3828 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3829 && !tboot_enabled())
3830 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04003831 }
3832
3833 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003834}
3835
Dongxiao Xu7725b892010-05-11 18:29:38 +08003836static void kvm_cpu_vmxon(u64 addr)
3837{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003838 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003839 intel_pt_handle_vmx(1);
3840
Dongxiao Xu7725b892010-05-11 18:29:38 +08003841 asm volatile (ASM_VMX_VMXON_RAX
3842 : : "a"(&addr), "m"(addr)
3843 : "memory", "cc");
3844}
3845
Radim Krčmář13a34e02014-08-28 15:13:03 +02003846static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003847{
3848 int cpu = raw_smp_processor_id();
3849 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04003850 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003851
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003852 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02003853 return -EBUSY;
3854
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01003855 /*
3856 * This can happen if we hot-added a CPU but failed to allocate
3857 * VP assist page for it.
3858 */
3859 if (static_branch_unlikely(&enable_evmcs) &&
3860 !hv_get_vp_assist_page(cpu))
3861 return -EFAULT;
3862
Nadav Har'Eld462b812011-05-24 15:26:10 +03003863 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08003864 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3865 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003866
3867 /*
3868 * Now we can enable the vmclear operation in kdump
3869 * since the loaded_vmcss_on_cpu list on this cpu
3870 * has been initialized.
3871 *
3872 * Though the cpu is not in VMX operation now, there
3873 * is no problem to enable the vmclear operation
3874 * for the loaded_vmcss_on_cpu list is empty!
3875 */
3876 crash_enable_local_vmclear(cpu);
3877
Avi Kivity6aa8b732006-12-10 02:21:36 -08003878 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04003879
3880 test_bits = FEATURE_CONTROL_LOCKED;
3881 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3882 if (tboot_enabled())
3883 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3884
3885 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003886 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04003887 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3888 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003889 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02003890 if (enable_ept)
3891 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02003892
3893 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003894}
3895
Nadav Har'Eld462b812011-05-24 15:26:10 +03003896static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03003897{
3898 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03003899 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03003900
Nadav Har'Eld462b812011-05-24 15:26:10 +03003901 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3902 loaded_vmcss_on_cpu_link)
3903 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03003904}
3905
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003906
3907/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3908 * tricks.
3909 */
3910static void kvm_cpu_vmxoff(void)
3911{
3912 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003913
3914 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003915 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003916}
3917
Radim Krčmář13a34e02014-08-28 15:13:03 +02003918static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003919{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003920 vmclear_local_loaded_vmcss();
3921 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003922}
3923
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003924static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04003925 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003926{
3927 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003928 u32 ctl = ctl_min | ctl_opt;
3929
3930 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3931
3932 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3933 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3934
3935 /* Ensure minimum (required) set of control bits are supported. */
3936 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003937 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003938
3939 *result = ctl;
3940 return 0;
3941}
3942
Avi Kivity110312c2010-12-21 12:54:20 +02003943static __init bool allow_1_setting(u32 msr, u32 ctl)
3944{
3945 u32 vmx_msr_low, vmx_msr_high;
3946
3947 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3948 return vmx_msr_high & ctl;
3949}
3950
Yang, Sheng002c7f72007-07-31 14:23:01 +03003951static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003952{
3953 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08003954 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003955 u32 _pin_based_exec_control = 0;
3956 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003957 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003958 u32 _vmexit_control = 0;
3959 u32 _vmentry_control = 0;
3960
Paolo Bonzini13893092018-02-26 13:40:09 +01003961 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05303962 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003963#ifdef CONFIG_X86_64
3964 CPU_BASED_CR8_LOAD_EXITING |
3965 CPU_BASED_CR8_STORE_EXITING |
3966#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08003967 CPU_BASED_CR3_LOAD_EXITING |
3968 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e22017-12-12 16:44:21 +08003969 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003970 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03003971 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003972 CPU_BASED_MWAIT_EXITING |
3973 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02003974 CPU_BASED_INVLPG_EXITING |
3975 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06003976
Sheng Yangf78e0e22007-10-29 09:40:42 +08003977 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08003978 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08003979 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003980 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3981 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003982 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003983#ifdef CONFIG_X86_64
3984 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3985 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3986 ~CPU_BASED_CR8_STORE_EXITING;
3987#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08003988 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08003989 min2 = 0;
3990 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08003991 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08003992 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08003993 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003994 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003995 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003996 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02003997 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00003998 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08003999 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004000 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004001 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004002 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004003 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004004 SECONDARY_EXEC_RDSEED_EXITING |
4005 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004006 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004007 SECONDARY_EXEC_TSC_SCALING |
4008 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08004009 if (adjust_vmx_controls(min2, opt2,
4010 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004011 &_cpu_based_2nd_exec_control) < 0)
4012 return -EIO;
4013 }
4014#ifndef CONFIG_X86_64
4015 if (!(_cpu_based_2nd_exec_control &
4016 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4017 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4018#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004019
4020 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4021 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004022 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004023 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4024 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004025
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004026 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4027 &vmx_capability.ept, &vmx_capability.vpid);
4028
Sheng Yangd56f5462008-04-25 10:13:16 +08004029 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004030 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4031 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004032 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4033 CPU_BASED_CR3_STORE_EXITING |
4034 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004035 } else if (vmx_capability.ept) {
4036 vmx_capability.ept = 0;
4037 pr_warn_once("EPT CAP should not exist if not support "
4038 "1-setting enable EPT VM-execution control\n");
4039 }
4040 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4041 vmx_capability.vpid) {
4042 vmx_capability.vpid = 0;
4043 pr_warn_once("VPID CAP should not exist if not support "
4044 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004045 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004046
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004047 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004048#ifdef CONFIG_X86_64
4049 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4050#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004051 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004052 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004053 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4054 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004055 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004056
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004057 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4058 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4059 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004060 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4061 &_pin_based_exec_control) < 0)
4062 return -EIO;
4063
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004064 if (cpu_has_broken_vmx_preemption_timer())
4065 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004066 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004067 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004068 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4069
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004070 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004071 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004072 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4073 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004074 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004075
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004076 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004077
4078 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4079 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004080 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004081
4082#ifdef CONFIG_X86_64
4083 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4084 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004085 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004086#endif
4087
4088 /* Require Write-Back (WB) memory type for VMCS accesses. */
4089 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004090 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004091
Yang, Sheng002c7f72007-07-31 14:23:01 +03004092 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004093 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004094 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004095
4096 /* KVM supports Enlightened VMCS v1 only */
4097 if (static_branch_unlikely(&enable_evmcs))
4098 vmcs_conf->revision_id = KVM_EVMCS_VERSION;
4099 else
4100 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004101
Yang, Sheng002c7f72007-07-31 14:23:01 +03004102 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4103 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004104 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004105 vmcs_conf->vmexit_ctrl = _vmexit_control;
4106 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004107
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004108 if (static_branch_unlikely(&enable_evmcs))
4109 evmcs_sanitize_exec_ctrls(vmcs_conf);
4110
Avi Kivity110312c2010-12-21 12:54:20 +02004111 cpu_has_load_ia32_efer =
4112 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4113 VM_ENTRY_LOAD_IA32_EFER)
4114 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4115 VM_EXIT_LOAD_IA32_EFER);
4116
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004117 cpu_has_load_perf_global_ctrl =
4118 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4119 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4120 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4121 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4122
4123 /*
4124 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004125 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004126 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4127 *
4128 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4129 *
4130 * AAK155 (model 26)
4131 * AAP115 (model 30)
4132 * AAT100 (model 37)
4133 * BC86,AAY89,BD102 (model 44)
4134 * BA97 (model 46)
4135 *
4136 */
4137 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4138 switch (boot_cpu_data.x86_model) {
4139 case 26:
4140 case 30:
4141 case 37:
4142 case 44:
4143 case 46:
4144 cpu_has_load_perf_global_ctrl = false;
4145 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4146 "does not work properly. Using workaround\n");
4147 break;
4148 default:
4149 break;
4150 }
4151 }
4152
Borislav Petkov782511b2016-04-04 22:25:03 +02004153 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004154 rdmsrl(MSR_IA32_XSS, host_xss);
4155
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004156 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004157}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004158
4159static struct vmcs *alloc_vmcs_cpu(int cpu)
4160{
4161 int node = cpu_to_node(cpu);
4162 struct page *pages;
4163 struct vmcs *vmcs;
4164
Vlastimil Babka96db8002015-09-08 15:03:50 -07004165 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004166 if (!pages)
4167 return NULL;
4168 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004169 memset(vmcs, 0, vmcs_config.size);
4170 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004171 return vmcs;
4172}
4173
Avi Kivity6aa8b732006-12-10 02:21:36 -08004174static void free_vmcs(struct vmcs *vmcs)
4175{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004176 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004177}
4178
Nadav Har'Eld462b812011-05-24 15:26:10 +03004179/*
4180 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4181 */
4182static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4183{
4184 if (!loaded_vmcs->vmcs)
4185 return;
4186 loaded_vmcs_clear(loaded_vmcs);
4187 free_vmcs(loaded_vmcs->vmcs);
4188 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004189 if (loaded_vmcs->msr_bitmap)
4190 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004191 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004192}
4193
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004194static struct vmcs *alloc_vmcs(void)
4195{
4196 return alloc_vmcs_cpu(raw_smp_processor_id());
4197}
4198
4199static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4200{
4201 loaded_vmcs->vmcs = alloc_vmcs();
4202 if (!loaded_vmcs->vmcs)
4203 return -ENOMEM;
4204
4205 loaded_vmcs->shadow_vmcs = NULL;
4206 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004207
4208 if (cpu_has_vmx_msr_bitmap()) {
4209 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4210 if (!loaded_vmcs->msr_bitmap)
4211 goto out_vmcs;
4212 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
4213 }
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004214 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004215
4216out_vmcs:
4217 free_loaded_vmcs(loaded_vmcs);
4218 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004219}
4220
Sam Ravnborg39959582007-06-01 00:47:13 -07004221static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004222{
4223 int cpu;
4224
Zachary Amsden3230bb42009-09-29 11:38:37 -10004225 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004226 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004227 per_cpu(vmxarea, cpu) = NULL;
4228 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004229}
4230
Jim Mattsond37f4262017-12-22 12:12:16 -08004231enum vmcs_field_width {
4232 VMCS_FIELD_WIDTH_U16 = 0,
4233 VMCS_FIELD_WIDTH_U64 = 1,
4234 VMCS_FIELD_WIDTH_U32 = 2,
4235 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004236};
4237
Jim Mattsond37f4262017-12-22 12:12:16 -08004238static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004239{
4240 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004241 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004242 return (field >> 13) & 0x3 ;
4243}
4244
4245static inline int vmcs_field_readonly(unsigned long field)
4246{
4247 return (((field >> 10) & 0x3) == 1);
4248}
4249
Bandan Dasfe2b2012014-04-21 15:20:14 -04004250static void init_vmcs_shadow_fields(void)
4251{
4252 int i, j;
4253
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004254 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4255 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004256 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004257 (i + 1 == max_shadow_read_only_fields ||
4258 shadow_read_only_fields[i + 1] != field + 1))
4259 pr_err("Missing field from shadow_read_only_field %x\n",
4260 field + 1);
4261
4262 clear_bit(field, vmx_vmread_bitmap);
4263#ifdef CONFIG_X86_64
4264 if (field & 1)
4265 continue;
4266#endif
4267 if (j < i)
4268 shadow_read_only_fields[j] = field;
4269 j++;
4270 }
4271 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004272
4273 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004274 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004275 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004276 (i + 1 == max_shadow_read_write_fields ||
4277 shadow_read_write_fields[i + 1] != field + 1))
4278 pr_err("Missing field from shadow_read_write_field %x\n",
4279 field + 1);
4280
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004281 /*
4282 * PML and the preemption timer can be emulated, but the
4283 * processor cannot vmwrite to fields that don't exist
4284 * on bare metal.
4285 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004286 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004287 case GUEST_PML_INDEX:
4288 if (!cpu_has_vmx_pml())
4289 continue;
4290 break;
4291 case VMX_PREEMPTION_TIMER_VALUE:
4292 if (!cpu_has_vmx_preemption_timer())
4293 continue;
4294 break;
4295 case GUEST_INTR_STATUS:
4296 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004297 continue;
4298 break;
4299 default:
4300 break;
4301 }
4302
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004303 clear_bit(field, vmx_vmwrite_bitmap);
4304 clear_bit(field, vmx_vmread_bitmap);
4305#ifdef CONFIG_X86_64
4306 if (field & 1)
4307 continue;
4308#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004309 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004310 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004311 j++;
4312 }
4313 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004314}
4315
Avi Kivity6aa8b732006-12-10 02:21:36 -08004316static __init int alloc_kvm_area(void)
4317{
4318 int cpu;
4319
Zachary Amsden3230bb42009-09-29 11:38:37 -10004320 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004321 struct vmcs *vmcs;
4322
4323 vmcs = alloc_vmcs_cpu(cpu);
4324 if (!vmcs) {
4325 free_kvm_area();
4326 return -ENOMEM;
4327 }
4328
4329 per_cpu(vmxarea, cpu) = vmcs;
4330 }
4331 return 0;
4332}
4333
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004334static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004335 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004336{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004337 if (!emulate_invalid_guest_state) {
4338 /*
4339 * CS and SS RPL should be equal during guest entry according
4340 * to VMX spec, but in reality it is not always so. Since vcpu
4341 * is in the middle of the transition from real mode to
4342 * protected mode it is safe to assume that RPL 0 is a good
4343 * default value.
4344 */
4345 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004346 save->selector &= ~SEGMENT_RPL_MASK;
4347 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004348 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004349 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004350 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004351}
4352
4353static void enter_pmode(struct kvm_vcpu *vcpu)
4354{
4355 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004356 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004357
Gleb Natapovd99e4152012-12-20 16:57:45 +02004358 /*
4359 * Update real mode segment cache. It may be not up-to-date if sement
4360 * register was written while vcpu was in a guest mode.
4361 */
4362 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4363 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4364 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4365 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4366 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4367 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4368
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004369 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004370
Avi Kivity2fb92db2011-04-27 19:42:18 +03004371 vmx_segment_cache_clear(vmx);
4372
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004373 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004374
4375 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004376 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4377 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004378 vmcs_writel(GUEST_RFLAGS, flags);
4379
Rusty Russell66aee912007-07-17 23:34:16 +10004380 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4381 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004382
4383 update_exception_bitmap(vcpu);
4384
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004385 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4386 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4387 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4388 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4389 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4390 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004391}
4392
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004393static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004394{
Mathias Krause772e0312012-08-30 01:30:19 +02004395 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004396 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004397
Gleb Natapovd99e4152012-12-20 16:57:45 +02004398 var.dpl = 0x3;
4399 if (seg == VCPU_SREG_CS)
4400 var.type = 0x3;
4401
4402 if (!emulate_invalid_guest_state) {
4403 var.selector = var.base >> 4;
4404 var.base = var.base & 0xffff0;
4405 var.limit = 0xffff;
4406 var.g = 0;
4407 var.db = 0;
4408 var.present = 1;
4409 var.s = 1;
4410 var.l = 0;
4411 var.unusable = 0;
4412 var.type = 0x3;
4413 var.avl = 0;
4414 if (save->base & 0xf)
4415 printk_once(KERN_WARNING "kvm: segment base is not "
4416 "paragraph aligned when entering "
4417 "protected mode (seg=%d)", seg);
4418 }
4419
4420 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004421 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004422 vmcs_write32(sf->limit, var.limit);
4423 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004424}
4425
4426static void enter_rmode(struct kvm_vcpu *vcpu)
4427{
4428 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004429 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004430 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004431
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004432 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4433 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4434 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4435 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4436 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004437 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4438 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004439
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004440 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004441
Gleb Natapov776e58e2011-03-13 12:34:27 +02004442 /*
4443 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004444 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004445 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004446 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004447 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4448 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004449
Avi Kivity2fb92db2011-04-27 19:42:18 +03004450 vmx_segment_cache_clear(vmx);
4451
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004452 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004453 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004454 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4455
4456 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004457 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004458
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004459 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004460
4461 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004462 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004463 update_exception_bitmap(vcpu);
4464
Gleb Natapovd99e4152012-12-20 16:57:45 +02004465 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4466 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4467 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4468 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4469 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4470 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004471
Eddie Dong8668a3c2007-10-10 14:26:45 +08004472 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004473}
4474
Amit Shah401d10d2009-02-20 22:53:37 +05304475static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4476{
4477 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004478 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4479
4480 if (!msr)
4481 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304482
Avi Kivity44ea2b12009-09-06 15:55:37 +03004483 /*
4484 * Force kernel_gs_base reloading before EFER changes, as control
4485 * of this msr depends on is_long_mode().
4486 */
4487 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004488 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304489 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004490 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304491 msr->data = efer;
4492 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004493 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304494
4495 msr->data = efer & ~EFER_LME;
4496 }
4497 setup_msrs(vmx);
4498}
4499
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004500#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004501
4502static void enter_lmode(struct kvm_vcpu *vcpu)
4503{
4504 u32 guest_tr_ar;
4505
Avi Kivity2fb92db2011-04-27 19:42:18 +03004506 vmx_segment_cache_clear(to_vmx(vcpu));
4507
Avi Kivity6aa8b732006-12-10 02:21:36 -08004508 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004509 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004510 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4511 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004512 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004513 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4514 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004515 }
Avi Kivityda38f432010-07-06 11:30:49 +03004516 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004517}
4518
4519static void exit_lmode(struct kvm_vcpu *vcpu)
4520{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004521 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004522 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004523}
4524
4525#endif
4526
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004527static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4528 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004529{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004530 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004531 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4532 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004533 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004534 } else {
4535 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004536 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004537}
4538
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004539static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004540{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004541 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004542}
4543
Avi Kivitye8467fd2009-12-29 18:43:06 +02004544static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4545{
4546 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4547
4548 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4549 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4550}
4551
Avi Kivityaff48ba2010-12-05 18:56:11 +02004552static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4553{
Sean Christophersonb4d18512018-03-05 12:04:40 -08004554 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02004555 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4556 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4557}
4558
Anthony Liguori25c4c272007-04-27 09:29:21 +03004559static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004560{
Avi Kivityfc78f512009-12-07 12:16:48 +02004561 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4562
4563 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4564 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004565}
4566
Sheng Yang14394422008-04-28 12:24:45 +08004567static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4568{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004569 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4570
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004571 if (!test_bit(VCPU_EXREG_PDPTR,
4572 (unsigned long *)&vcpu->arch.regs_dirty))
4573 return;
4574
Sheng Yang14394422008-04-28 12:24:45 +08004575 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004576 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4577 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4578 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4579 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004580 }
4581}
4582
Avi Kivity8f5d5492009-05-31 18:41:29 +03004583static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4584{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004585 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4586
Avi Kivity8f5d5492009-05-31 18:41:29 +03004587 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004588 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4589 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4590 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4591 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004592 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004593
4594 __set_bit(VCPU_EXREG_PDPTR,
4595 (unsigned long *)&vcpu->arch.regs_avail);
4596 __set_bit(VCPU_EXREG_PDPTR,
4597 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004598}
4599
David Matlack38991522016-11-29 18:14:08 -08004600static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4601{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004602 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4603 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004604 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4605
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004606 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08004607 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4608 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4609 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4610
4611 return fixed_bits_valid(val, fixed0, fixed1);
4612}
4613
4614static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4615{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004616 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4617 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004618
4619 return fixed_bits_valid(val, fixed0, fixed1);
4620}
4621
4622static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4623{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004624 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
4625 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004626
4627 return fixed_bits_valid(val, fixed0, fixed1);
4628}
4629
4630/* No difference in the restrictions on guest and host CR4 in VMX operation. */
4631#define nested_guest_cr4_valid nested_cr4_valid
4632#define nested_host_cr4_valid nested_cr4_valid
4633
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004634static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004635
4636static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4637 unsigned long cr0,
4638 struct kvm_vcpu *vcpu)
4639{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004640 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4641 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004642 if (!(cr0 & X86_CR0_PG)) {
4643 /* From paging/starting to nonpaging */
4644 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004645 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004646 (CPU_BASED_CR3_LOAD_EXITING |
4647 CPU_BASED_CR3_STORE_EXITING));
4648 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004649 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004650 } else if (!is_paging(vcpu)) {
4651 /* From nonpaging to paging */
4652 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004653 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004654 ~(CPU_BASED_CR3_LOAD_EXITING |
4655 CPU_BASED_CR3_STORE_EXITING));
4656 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004657 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004658 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004659
4660 if (!(cr0 & X86_CR0_WP))
4661 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004662}
4663
Avi Kivity6aa8b732006-12-10 02:21:36 -08004664static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4665{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004666 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004667 unsigned long hw_cr0;
4668
Gleb Natapov50378782013-02-04 16:00:28 +02004669 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004670 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004671 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004672 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004673 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004674
Gleb Natapov218e7632013-01-21 15:36:45 +02004675 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4676 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004677
Gleb Natapov218e7632013-01-21 15:36:45 +02004678 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4679 enter_rmode(vcpu);
4680 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004681
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004682#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004683 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10004684 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004685 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10004686 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004687 exit_lmode(vcpu);
4688 }
4689#endif
4690
Sean Christophersonb4d18512018-03-05 12:04:40 -08004691 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08004692 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4693
Avi Kivity6aa8b732006-12-10 02:21:36 -08004694 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004695 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004696 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004697
4698 /* depends on vcpu->arch.cr0 to be set to a new value */
4699 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004700}
4701
Yu Zhang855feb62017-08-24 20:27:55 +08004702static int get_ept_level(struct kvm_vcpu *vcpu)
4703{
4704 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
4705 return 5;
4706 return 4;
4707}
4708
Peter Feiner995f00a2017-06-30 17:26:32 -07004709static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08004710{
Yu Zhang855feb62017-08-24 20:27:55 +08004711 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08004712
Yu Zhang855feb62017-08-24 20:27:55 +08004713 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08004714
Peter Feiner995f00a2017-06-30 17:26:32 -07004715 if (enable_ept_ad_bits &&
4716 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02004717 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08004718 eptp |= (root_hpa & PAGE_MASK);
4719
4720 return eptp;
4721}
4722
Avi Kivity6aa8b732006-12-10 02:21:36 -08004723static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4724{
Sheng Yang14394422008-04-28 12:24:45 +08004725 unsigned long guest_cr3;
4726 u64 eptp;
4727
4728 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02004729 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07004730 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08004731 vmcs_write64(EPT_POINTER, eptp);
Sean Christophersone90008d2018-03-05 12:04:37 -08004732 if (enable_unrestricted_guest || is_paging(vcpu) ||
4733 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02004734 guest_cr3 = kvm_read_cr3(vcpu);
4735 else
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004736 guest_cr3 = to_kvm_vmx(vcpu->kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02004737 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004738 }
4739
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004740 vmx_flush_tlb(vcpu, true);
Sheng Yang14394422008-04-28 12:24:45 +08004741 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004742}
4743
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004744static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004745{
Ben Serebrin085e68e2015-04-16 11:58:05 -07004746 /*
4747 * Pass through host's Machine Check Enable value to hw_cr4, which
4748 * is in force while we are in guest mode. Do not let guests control
4749 * this bit, even if host CR4.MCE == 0.
4750 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08004751 unsigned long hw_cr4;
4752
4753 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
4754 if (enable_unrestricted_guest)
4755 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
4756 else if (to_vmx(vcpu)->rmode.vm86_active)
4757 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
4758 else
4759 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004760
Paolo Bonzini0367f202016-07-12 10:44:55 +02004761 if ((cr4 & X86_CR4_UMIP) && !boot_cpu_has(X86_FEATURE_UMIP)) {
4762 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
4763 SECONDARY_EXEC_DESC);
4764 hw_cr4 &= ~X86_CR4_UMIP;
Radim Krčmář99158242018-01-31 18:12:50 +01004765 } else if (!is_guest_mode(vcpu) ||
4766 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
Paolo Bonzini0367f202016-07-12 10:44:55 +02004767 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
4768 SECONDARY_EXEC_DESC);
4769
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004770 if (cr4 & X86_CR4_VMXE) {
4771 /*
4772 * To use VMXON (and later other VMX instructions), a guest
4773 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4774 * So basically the check on whether to allow nested VMX
4775 * is here.
4776 */
4777 if (!nested_vmx_allowed(vcpu))
4778 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004779 }
David Matlack38991522016-11-29 18:14:08 -08004780
4781 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004782 return 1;
4783
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004784 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08004785
Sean Christopherson5dc1f042018-03-05 12:04:39 -08004786 if (!enable_unrestricted_guest) {
4787 if (enable_ept) {
4788 if (!is_paging(vcpu)) {
4789 hw_cr4 &= ~X86_CR4_PAE;
4790 hw_cr4 |= X86_CR4_PSE;
4791 } else if (!(cr4 & X86_CR4_PAE)) {
4792 hw_cr4 &= ~X86_CR4_PAE;
4793 }
4794 }
4795
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004796 /*
Huaitong Handdba2622016-03-22 16:51:15 +08004797 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4798 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4799 * to be manually disabled when guest switches to non-paging
4800 * mode.
4801 *
4802 * If !enable_unrestricted_guest, the CPU is always running
4803 * with CR0.PG=1 and CR4 needs to be modified.
4804 * If enable_unrestricted_guest, the CPU automatically
4805 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004806 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08004807 if (!is_paging(vcpu))
4808 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
4809 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004810
Sheng Yang14394422008-04-28 12:24:45 +08004811 vmcs_writel(CR4_READ_SHADOW, cr4);
4812 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004813 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004814}
4815
Avi Kivity6aa8b732006-12-10 02:21:36 -08004816static void vmx_get_segment(struct kvm_vcpu *vcpu,
4817 struct kvm_segment *var, int seg)
4818{
Avi Kivitya9179492011-01-03 14:28:52 +02004819 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004820 u32 ar;
4821
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004822 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004823 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02004824 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03004825 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004826 return;
Avi Kivity1390a282012-08-21 17:07:08 +03004827 var->base = vmx_read_guest_seg_base(vmx, seg);
4828 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4829 return;
Avi Kivitya9179492011-01-03 14:28:52 +02004830 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004831 var->base = vmx_read_guest_seg_base(vmx, seg);
4832 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4833 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4834 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03004835 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004836 var->type = ar & 15;
4837 var->s = (ar >> 4) & 1;
4838 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03004839 /*
4840 * Some userspaces do not preserve unusable property. Since usable
4841 * segment has to be present according to VMX spec we can use present
4842 * property to amend userspace bug by making unusable segment always
4843 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4844 * segment as unusable.
4845 */
4846 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004847 var->avl = (ar >> 12) & 1;
4848 var->l = (ar >> 13) & 1;
4849 var->db = (ar >> 14) & 1;
4850 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004851}
4852
Avi Kivitya9179492011-01-03 14:28:52 +02004853static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4854{
Avi Kivitya9179492011-01-03 14:28:52 +02004855 struct kvm_segment s;
4856
4857 if (to_vmx(vcpu)->rmode.vm86_active) {
4858 vmx_get_segment(vcpu, &s, seg);
4859 return s.base;
4860 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004861 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02004862}
4863
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004864static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02004865{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004866 struct vcpu_vmx *vmx = to_vmx(vcpu);
4867
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004868 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02004869 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004870 else {
4871 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004872 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02004873 }
Avi Kivity69c73022011-03-07 15:26:44 +02004874}
4875
Avi Kivity653e3102007-05-07 10:55:37 +03004876static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004877{
Avi Kivity6aa8b732006-12-10 02:21:36 -08004878 u32 ar;
4879
Avi Kivityf0495f92012-06-07 17:06:10 +03004880 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004881 ar = 1 << 16;
4882 else {
4883 ar = var->type & 15;
4884 ar |= (var->s & 1) << 4;
4885 ar |= (var->dpl & 3) << 5;
4886 ar |= (var->present & 1) << 7;
4887 ar |= (var->avl & 1) << 12;
4888 ar |= (var->l & 1) << 13;
4889 ar |= (var->db & 1) << 14;
4890 ar |= (var->g & 1) << 15;
4891 }
Avi Kivity653e3102007-05-07 10:55:37 +03004892
4893 return ar;
4894}
4895
4896static void vmx_set_segment(struct kvm_vcpu *vcpu,
4897 struct kvm_segment *var, int seg)
4898{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004899 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02004900 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03004901
Avi Kivity2fb92db2011-04-27 19:42:18 +03004902 vmx_segment_cache_clear(vmx);
4903
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004904 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4905 vmx->rmode.segs[seg] = *var;
4906 if (seg == VCPU_SREG_TR)
4907 vmcs_write16(sf->selector, var->selector);
4908 else if (var->s)
4909 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004910 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03004911 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004912
Avi Kivity653e3102007-05-07 10:55:37 +03004913 vmcs_writel(sf->base, var->base);
4914 vmcs_write32(sf->limit, var->limit);
4915 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004916
4917 /*
4918 * Fix the "Accessed" bit in AR field of segment registers for older
4919 * qemu binaries.
4920 * IA32 arch specifies that at the time of processor reset the
4921 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08004922 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004923 * state vmexit when "unrestricted guest" mode is turned on.
4924 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4925 * tree. Newer qemu binaries with that qemu fix would not need this
4926 * kvm hack.
4927 */
4928 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02004929 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004930
Gleb Natapovf924d662012-12-12 19:10:55 +02004931 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02004932
4933out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01004934 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004935}
4936
Avi Kivity6aa8b732006-12-10 02:21:36 -08004937static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4938{
Avi Kivity2fb92db2011-04-27 19:42:18 +03004939 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004940
4941 *db = (ar >> 14) & 1;
4942 *l = (ar >> 13) & 1;
4943}
4944
Gleb Natapov89a27f42010-02-16 10:51:48 +02004945static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004946{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004947 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4948 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004949}
4950
Gleb Natapov89a27f42010-02-16 10:51:48 +02004951static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004952{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004953 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4954 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004955}
4956
Gleb Natapov89a27f42010-02-16 10:51:48 +02004957static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004958{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004959 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4960 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004961}
4962
Gleb Natapov89a27f42010-02-16 10:51:48 +02004963static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004964{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004965 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4966 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004967}
4968
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004969static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4970{
4971 struct kvm_segment var;
4972 u32 ar;
4973
4974 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02004975 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02004976 if (seg == VCPU_SREG_CS)
4977 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004978 ar = vmx_segment_access_rights(&var);
4979
4980 if (var.base != (var.selector << 4))
4981 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02004982 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004983 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02004984 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004985 return false;
4986
4987 return true;
4988}
4989
4990static bool code_segment_valid(struct kvm_vcpu *vcpu)
4991{
4992 struct kvm_segment cs;
4993 unsigned int cs_rpl;
4994
4995 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004996 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004997
Avi Kivity1872a3f2009-01-04 23:26:52 +02004998 if (cs.unusable)
4999 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005000 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005001 return false;
5002 if (!cs.s)
5003 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005004 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005005 if (cs.dpl > cs_rpl)
5006 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005007 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005008 if (cs.dpl != cs_rpl)
5009 return false;
5010 }
5011 if (!cs.present)
5012 return false;
5013
5014 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5015 return true;
5016}
5017
5018static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5019{
5020 struct kvm_segment ss;
5021 unsigned int ss_rpl;
5022
5023 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005024 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005025
Avi Kivity1872a3f2009-01-04 23:26:52 +02005026 if (ss.unusable)
5027 return true;
5028 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005029 return false;
5030 if (!ss.s)
5031 return false;
5032 if (ss.dpl != ss_rpl) /* DPL != RPL */
5033 return false;
5034 if (!ss.present)
5035 return false;
5036
5037 return true;
5038}
5039
5040static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5041{
5042 struct kvm_segment var;
5043 unsigned int rpl;
5044
5045 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005046 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005047
Avi Kivity1872a3f2009-01-04 23:26:52 +02005048 if (var.unusable)
5049 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005050 if (!var.s)
5051 return false;
5052 if (!var.present)
5053 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005054 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005055 if (var.dpl < rpl) /* DPL < RPL */
5056 return false;
5057 }
5058
5059 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5060 * rights flags
5061 */
5062 return true;
5063}
5064
5065static bool tr_valid(struct kvm_vcpu *vcpu)
5066{
5067 struct kvm_segment tr;
5068
5069 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5070
Avi Kivity1872a3f2009-01-04 23:26:52 +02005071 if (tr.unusable)
5072 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005073 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005074 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005075 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005076 return false;
5077 if (!tr.present)
5078 return false;
5079
5080 return true;
5081}
5082
5083static bool ldtr_valid(struct kvm_vcpu *vcpu)
5084{
5085 struct kvm_segment ldtr;
5086
5087 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5088
Avi Kivity1872a3f2009-01-04 23:26:52 +02005089 if (ldtr.unusable)
5090 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005091 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005092 return false;
5093 if (ldtr.type != 2)
5094 return false;
5095 if (!ldtr.present)
5096 return false;
5097
5098 return true;
5099}
5100
5101static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5102{
5103 struct kvm_segment cs, ss;
5104
5105 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5106 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5107
Nadav Amitb32a9912015-03-29 16:33:04 +03005108 return ((cs.selector & SEGMENT_RPL_MASK) ==
5109 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005110}
5111
5112/*
5113 * Check if guest state is valid. Returns true if valid, false if
5114 * not.
5115 * We assume that registers are always usable
5116 */
5117static bool guest_state_valid(struct kvm_vcpu *vcpu)
5118{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005119 if (enable_unrestricted_guest)
5120 return true;
5121
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005122 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005123 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005124 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5125 return false;
5126 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5127 return false;
5128 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5129 return false;
5130 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5131 return false;
5132 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5133 return false;
5134 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5135 return false;
5136 } else {
5137 /* protected mode guest state checks */
5138 if (!cs_ss_rpl_check(vcpu))
5139 return false;
5140 if (!code_segment_valid(vcpu))
5141 return false;
5142 if (!stack_segment_valid(vcpu))
5143 return false;
5144 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5145 return false;
5146 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5147 return false;
5148 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5149 return false;
5150 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5151 return false;
5152 if (!tr_valid(vcpu))
5153 return false;
5154 if (!ldtr_valid(vcpu))
5155 return false;
5156 }
5157 /* TODO:
5158 * - Add checks on RIP
5159 * - Add checks on RFLAGS
5160 */
5161
5162 return true;
5163}
5164
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005165static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5166{
5167 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5168}
5169
Mike Dayd77c26f2007-10-08 09:02:08 -04005170static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005171{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005172 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005173 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005174 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005175
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005176 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005177 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005178 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5179 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005180 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005181 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005182 r = kvm_write_guest_page(kvm, fn++, &data,
5183 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005184 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005185 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005186 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5187 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005188 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005189 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5190 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005191 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005192 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005193 r = kvm_write_guest_page(kvm, fn, &data,
5194 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5195 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005196out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005197 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005198 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005199}
5200
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005201static int init_rmode_identity_map(struct kvm *kvm)
5202{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005203 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005204 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005205 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005206 u32 tmp;
5207
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005208 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005209 mutex_lock(&kvm->slots_lock);
5210
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005211 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005212 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005213
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005214 if (!kvm_vmx->ept_identity_map_addr)
5215 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5216 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005217
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005218 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005219 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005220 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005221 goto out2;
5222
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005223 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005224 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5225 if (r < 0)
5226 goto out;
5227 /* Set up identity-mapping pagetable for EPT in real mode */
5228 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5229 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5230 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5231 r = kvm_write_guest_page(kvm, identity_map_pfn,
5232 &tmp, i * sizeof(tmp), sizeof(tmp));
5233 if (r < 0)
5234 goto out;
5235 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005236 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005237
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005238out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005239 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005240
5241out2:
5242 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005243 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005244}
5245
Avi Kivity6aa8b732006-12-10 02:21:36 -08005246static void seg_setup(int seg)
5247{
Mathias Krause772e0312012-08-30 01:30:19 +02005248 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005249 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005250
5251 vmcs_write16(sf->selector, 0);
5252 vmcs_writel(sf->base, 0);
5253 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005254 ar = 0x93;
5255 if (seg == VCPU_SREG_CS)
5256 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005257
5258 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005259}
5260
Sheng Yangf78e0e22007-10-29 09:40:42 +08005261static int alloc_apic_access_page(struct kvm *kvm)
5262{
Xiao Guangrong44841412012-09-07 14:14:20 +08005263 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005264 int r = 0;
5265
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005266 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005267 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005268 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005269 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5270 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005271 if (r)
5272 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005273
Tang Chen73a6d942014-09-11 13:38:00 +08005274 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005275 if (is_error_page(page)) {
5276 r = -EFAULT;
5277 goto out;
5278 }
5279
Tang Chenc24ae0d2014-09-24 15:57:58 +08005280 /*
5281 * Do not pin the page in memory, so that memory hot-unplug
5282 * is able to migrate it.
5283 */
5284 put_page(page);
5285 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005286out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005287 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005288 return r;
5289}
5290
Wanpeng Li991e7a02015-09-16 17:30:05 +08005291static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005292{
5293 int vpid;
5294
Avi Kivity919818a2009-03-23 18:01:29 +02005295 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005296 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005297 spin_lock(&vmx_vpid_lock);
5298 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005299 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005300 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005301 else
5302 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005303 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005304 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005305}
5306
Wanpeng Li991e7a02015-09-16 17:30:05 +08005307static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005308{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005309 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005310 return;
5311 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005312 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005313 spin_unlock(&vmx_vpid_lock);
5314}
5315
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005316static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5317 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005318{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005319 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005320
5321 if (!cpu_has_vmx_msr_bitmap())
5322 return;
5323
5324 /*
5325 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5326 * have the write-low and read-high bitmap offsets the wrong way round.
5327 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5328 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005329 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005330 if (type & MSR_TYPE_R)
5331 /* read-low */
5332 __clear_bit(msr, msr_bitmap + 0x000 / f);
5333
5334 if (type & MSR_TYPE_W)
5335 /* write-low */
5336 __clear_bit(msr, msr_bitmap + 0x800 / f);
5337
Sheng Yang25c5f222008-03-28 13:18:56 +08005338 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5339 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005340 if (type & MSR_TYPE_R)
5341 /* read-high */
5342 __clear_bit(msr, msr_bitmap + 0x400 / f);
5343
5344 if (type & MSR_TYPE_W)
5345 /* write-high */
5346 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5347
5348 }
5349}
5350
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005351static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5352 u32 msr, int type)
5353{
5354 int f = sizeof(unsigned long);
5355
5356 if (!cpu_has_vmx_msr_bitmap())
5357 return;
5358
5359 /*
5360 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5361 * have the write-low and read-high bitmap offsets the wrong way round.
5362 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5363 */
5364 if (msr <= 0x1fff) {
5365 if (type & MSR_TYPE_R)
5366 /* read-low */
5367 __set_bit(msr, msr_bitmap + 0x000 / f);
5368
5369 if (type & MSR_TYPE_W)
5370 /* write-low */
5371 __set_bit(msr, msr_bitmap + 0x800 / f);
5372
5373 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5374 msr &= 0x1fff;
5375 if (type & MSR_TYPE_R)
5376 /* read-high */
5377 __set_bit(msr, msr_bitmap + 0x400 / f);
5378
5379 if (type & MSR_TYPE_W)
5380 /* write-high */
5381 __set_bit(msr, msr_bitmap + 0xc00 / f);
5382
5383 }
5384}
5385
5386static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5387 u32 msr, int type, bool value)
5388{
5389 if (value)
5390 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5391 else
5392 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5393}
5394
Wincy Vanf2b93282015-02-03 23:56:03 +08005395/*
5396 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5397 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5398 */
5399static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5400 unsigned long *msr_bitmap_nested,
5401 u32 msr, int type)
5402{
5403 int f = sizeof(unsigned long);
5404
Wincy Vanf2b93282015-02-03 23:56:03 +08005405 /*
5406 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5407 * have the write-low and read-high bitmap offsets the wrong way round.
5408 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5409 */
5410 if (msr <= 0x1fff) {
5411 if (type & MSR_TYPE_R &&
5412 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5413 /* read-low */
5414 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5415
5416 if (type & MSR_TYPE_W &&
5417 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5418 /* write-low */
5419 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5420
5421 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5422 msr &= 0x1fff;
5423 if (type & MSR_TYPE_R &&
5424 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5425 /* read-high */
5426 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5427
5428 if (type & MSR_TYPE_W &&
5429 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5430 /* write-high */
5431 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5432
5433 }
5434}
5435
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005436static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02005437{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005438 u8 mode = 0;
5439
5440 if (cpu_has_secondary_exec_ctrls() &&
5441 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5442 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5443 mode |= MSR_BITMAP_MODE_X2APIC;
5444 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5445 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5446 }
5447
5448 if (is_long_mode(vcpu))
5449 mode |= MSR_BITMAP_MODE_LM;
5450
5451 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005452}
5453
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005454#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5455
5456static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5457 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005458{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005459 int msr;
5460
5461 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5462 unsigned word = msr / BITS_PER_LONG;
5463 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5464 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005465 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005466
5467 if (mode & MSR_BITMAP_MODE_X2APIC) {
5468 /*
5469 * TPR reads and writes can be virtualized even if virtual interrupt
5470 * delivery is not in use.
5471 */
5472 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5473 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5474 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5475 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5476 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5477 }
5478 }
5479}
5480
5481static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5482{
5483 struct vcpu_vmx *vmx = to_vmx(vcpu);
5484 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5485 u8 mode = vmx_msr_bitmap_mode(vcpu);
5486 u8 changed = mode ^ vmx->msr_bitmap_mode;
5487
5488 if (!changed)
5489 return;
5490
5491 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5492 !(mode & MSR_BITMAP_MODE_LM));
5493
5494 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5495 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5496
5497 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005498}
5499
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005500static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005501{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005502 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005503}
5504
David Matlackc9f04402017-08-01 14:00:40 -07005505static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5506{
5507 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5508 gfn_t gfn;
5509
5510 /*
5511 * Don't need to mark the APIC access page dirty; it is never
5512 * written to by the CPU during APIC virtualization.
5513 */
5514
5515 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5516 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5517 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5518 }
5519
5520 if (nested_cpu_has_posted_intr(vmcs12)) {
5521 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5522 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5523 }
5524}
5525
5526
David Hildenbrand6342c502017-01-25 11:58:58 +01005527static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005528{
5529 struct vcpu_vmx *vmx = to_vmx(vcpu);
5530 int max_irr;
5531 void *vapic_page;
5532 u16 status;
5533
David Matlackc9f04402017-08-01 14:00:40 -07005534 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5535 return;
Wincy Van705699a2015-02-03 23:58:17 +08005536
David Matlackc9f04402017-08-01 14:00:40 -07005537 vmx->nested.pi_pending = false;
5538 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5539 return;
Wincy Van705699a2015-02-03 23:58:17 +08005540
David Matlackc9f04402017-08-01 14:00:40 -07005541 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5542 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005543 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02005544 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5545 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08005546 kunmap(vmx->nested.virtual_apic_page);
5547
5548 status = vmcs_read16(GUEST_INTR_STATUS);
5549 if ((u8)max_irr > ((u8)status & 0xff)) {
5550 status &= ~0xff;
5551 status |= (u8)max_irr;
5552 vmcs_write16(GUEST_INTR_STATUS, status);
5553 }
5554 }
David Matlackc9f04402017-08-01 14:00:40 -07005555
5556 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005557}
5558
Wincy Van06a55242017-04-28 13:13:59 +08005559static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5560 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005561{
5562#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005563 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5564
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005565 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005566 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005567 * The vector of interrupt to be delivered to vcpu had
5568 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005569 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005570 * Following cases will be reached in this block, and
5571 * we always send a notification event in all cases as
5572 * explained below.
5573 *
5574 * Case 1: vcpu keeps in non-root mode. Sending a
5575 * notification event posts the interrupt to vcpu.
5576 *
5577 * Case 2: vcpu exits to root mode and is still
5578 * runnable. PIR will be synced to vIRR before the
5579 * next vcpu entry. Sending a notification event in
5580 * this case has no effect, as vcpu is not in root
5581 * mode.
5582 *
5583 * Case 3: vcpu exits to root mode and is blocked.
5584 * vcpu_block() has already synced PIR to vIRR and
5585 * never blocks vcpu if vIRR is not cleared. Therefore,
5586 * a blocked vcpu here does not wait for any requested
5587 * interrupts in PIR, and sending a notification event
5588 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005589 */
Feng Wu28b835d2015-09-18 22:29:54 +08005590
Wincy Van06a55242017-04-28 13:13:59 +08005591 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005592 return true;
5593 }
5594#endif
5595 return false;
5596}
5597
Wincy Van705699a2015-02-03 23:58:17 +08005598static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5599 int vector)
5600{
5601 struct vcpu_vmx *vmx = to_vmx(vcpu);
5602
5603 if (is_guest_mode(vcpu) &&
5604 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005605 /*
5606 * If a posted intr is not recognized by hardware,
5607 * we will accomplish it in the next vmentry.
5608 */
5609 vmx->nested.pi_pending = true;
5610 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02005611 /* the PIR and ON have been set by L1. */
5612 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
5613 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005614 return 0;
5615 }
5616 return -1;
5617}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005618/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005619 * Send interrupt to vcpu via posted interrupt way.
5620 * 1. If target vcpu is running(non-root mode), send posted interrupt
5621 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5622 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5623 * interrupt from PIR in next vmentry.
5624 */
5625static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5626{
5627 struct vcpu_vmx *vmx = to_vmx(vcpu);
5628 int r;
5629
Wincy Van705699a2015-02-03 23:58:17 +08005630 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5631 if (!r)
5632 return;
5633
Yang Zhanga20ed542013-04-11 19:25:15 +08005634 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5635 return;
5636
Paolo Bonzinib95234c2016-12-19 13:57:33 +01005637 /* If a previous notification has sent the IPI, nothing to do. */
5638 if (pi_test_and_set_on(&vmx->pi_desc))
5639 return;
5640
Wincy Van06a55242017-04-28 13:13:59 +08005641 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08005642 kvm_vcpu_kick(vcpu);
5643}
5644
Avi Kivity6aa8b732006-12-10 02:21:36 -08005645/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005646 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5647 * will not change in the lifetime of the guest.
5648 * Note that host-state that does change is set elsewhere. E.g., host-state
5649 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5650 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005651static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005652{
5653 u32 low32, high32;
5654 unsigned long tmpl;
5655 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005656 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005657
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07005658 cr0 = read_cr0();
5659 WARN_ON(cr0 & X86_CR0_TS);
5660 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005661
5662 /*
5663 * Save the most likely value for this task's CR3 in the VMCS.
5664 * We can't use __get_current_cr3_fast() because we're not atomic.
5665 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07005666 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005667 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02005668 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005669
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005670 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005671 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005672 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02005673 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005674
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005675 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005676#ifdef CONFIG_X86_64
5677 /*
5678 * Load null selectors, so we can avoid reloading them in
5679 * __vmx_load_host_state(), in case userspace uses the null selectors
5680 * too (the expected case).
5681 */
5682 vmcs_write16(HOST_DS_SELECTOR, 0);
5683 vmcs_write16(HOST_ES_SELECTOR, 0);
5684#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005685 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5686 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005687#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005688 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5689 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5690
Juergen Gross87930012017-09-04 12:25:27 +02005691 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005692 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005693 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005694
Avi Kivity83287ea422012-09-16 15:10:57 +03005695 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005696
5697 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5698 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5699 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5700 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5701
5702 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5703 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5704 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5705 }
5706}
5707
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005708static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5709{
5710 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5711 if (enable_ept)
5712 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005713 if (is_guest_mode(&vmx->vcpu))
5714 vmx->vcpu.arch.cr4_guest_owned_bits &=
5715 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005716 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5717}
5718
Yang Zhang01e439b2013-04-11 19:25:12 +08005719static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5720{
5721 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5722
Andrey Smetanind62caab2015-11-10 15:36:33 +03005723 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005724 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005725
5726 if (!enable_vnmi)
5727 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
5728
Yunhong Jiang64672c92016-06-13 14:19:59 -07005729 /* Enable the preemption timer dynamically */
5730 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08005731 return pin_based_exec_ctrl;
5732}
5733
Andrey Smetanind62caab2015-11-10 15:36:33 +03005734static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5735{
5736 struct vcpu_vmx *vmx = to_vmx(vcpu);
5737
5738 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03005739 if (cpu_has_secondary_exec_ctrls()) {
5740 if (kvm_vcpu_apicv_active(vcpu))
5741 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5742 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5743 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5744 else
5745 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5746 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5747 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5748 }
5749
5750 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005751 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03005752}
5753
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005754static u32 vmx_exec_control(struct vcpu_vmx *vmx)
5755{
5756 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01005757
5758 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
5759 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5760
Paolo Bonzini35754c92015-07-29 12:05:37 +02005761 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005762 exec_control &= ~CPU_BASED_TPR_SHADOW;
5763#ifdef CONFIG_X86_64
5764 exec_control |= CPU_BASED_CR8_STORE_EXITING |
5765 CPU_BASED_CR8_LOAD_EXITING;
5766#endif
5767 }
5768 if (!enable_ept)
5769 exec_control |= CPU_BASED_CR3_STORE_EXITING |
5770 CPU_BASED_CR3_LOAD_EXITING |
5771 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07005772 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
5773 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
5774 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07005775 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
5776 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005777 return exec_control;
5778}
5779
Jim Mattson45ec3682017-08-23 16:32:04 -07005780static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005781{
Jim Mattson45ec3682017-08-23 16:32:04 -07005782 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02005783 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005784}
5785
Jim Mattson75f4fc82017-08-23 16:32:03 -07005786static bool vmx_rdseed_supported(void)
5787{
5788 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02005789 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005790}
5791
Paolo Bonzini80154d72017-08-24 13:55:35 +02005792static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005793{
Paolo Bonzini80154d72017-08-24 13:55:35 +02005794 struct kvm_vcpu *vcpu = &vmx->vcpu;
5795
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005796 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02005797
Paolo Bonzini80154d72017-08-24 13:55:35 +02005798 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005799 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5800 if (vmx->vpid == 0)
5801 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
5802 if (!enable_ept) {
5803 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
5804 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00005805 /* Enable INVPCID for non-ept guests may cause performance regression. */
5806 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005807 }
5808 if (!enable_unrestricted_guest)
5809 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07005810 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005811 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02005812 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08005813 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
5814 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08005815 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02005816
5817 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
5818 * in vmx_set_cr4. */
5819 exec_control &= ~SECONDARY_EXEC_DESC;
5820
Abel Gordonabc4fc52013-04-18 14:35:25 +03005821 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
5822 (handle_vmptrld).
5823 We can NOT enable shadow_vmcs here because we don't have yet
5824 a current VMCS12
5825 */
5826 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08005827
5828 if (!enable_pml)
5829 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08005830
Paolo Bonzini3db13482017-08-24 14:48:03 +02005831 if (vmx_xsaves_supported()) {
5832 /* Exposing XSAVES only when XSAVE is exposed */
5833 bool xsaves_enabled =
5834 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
5835 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
5836
5837 if (!xsaves_enabled)
5838 exec_control &= ~SECONDARY_EXEC_XSAVES;
5839
5840 if (nested) {
5841 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005842 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02005843 SECONDARY_EXEC_XSAVES;
5844 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005845 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02005846 ~SECONDARY_EXEC_XSAVES;
5847 }
5848 }
5849
Paolo Bonzini80154d72017-08-24 13:55:35 +02005850 if (vmx_rdtscp_supported()) {
5851 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
5852 if (!rdtscp_enabled)
5853 exec_control &= ~SECONDARY_EXEC_RDTSCP;
5854
5855 if (nested) {
5856 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005857 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02005858 SECONDARY_EXEC_RDTSCP;
5859 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005860 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02005861 ~SECONDARY_EXEC_RDTSCP;
5862 }
5863 }
5864
5865 if (vmx_invpcid_supported()) {
5866 /* Exposing INVPCID only when PCID is exposed */
5867 bool invpcid_enabled =
5868 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
5869 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
5870
5871 if (!invpcid_enabled) {
5872 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
5873 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
5874 }
5875
5876 if (nested) {
5877 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005878 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02005879 SECONDARY_EXEC_ENABLE_INVPCID;
5880 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005881 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02005882 ~SECONDARY_EXEC_ENABLE_INVPCID;
5883 }
5884 }
5885
Jim Mattson45ec3682017-08-23 16:32:04 -07005886 if (vmx_rdrand_supported()) {
5887 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
5888 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02005889 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005890
5891 if (nested) {
5892 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005893 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005894 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005895 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005896 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005897 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005898 }
5899 }
5900
Jim Mattson75f4fc82017-08-23 16:32:03 -07005901 if (vmx_rdseed_supported()) {
5902 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
5903 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02005904 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005905
5906 if (nested) {
5907 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005908 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005909 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005910 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005911 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005912 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005913 }
5914 }
5915
Paolo Bonzini80154d72017-08-24 13:55:35 +02005916 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005917}
5918
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005919static void ept_set_mmio_spte_mask(void)
5920{
5921 /*
5922 * EPT Misconfigurations can be generated if the value of bits 2:0
5923 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005924 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07005925 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
5926 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005927}
5928
Wanpeng Lif53cd632014-12-02 19:14:58 +08005929#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005930/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005931 * Sets up the vmcs for emulated real mode.
5932 */
David Hildenbrand12d79912017-08-24 20:51:26 +02005933static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005934{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005935#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005936 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005937#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08005938 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005939
Abel Gordon4607c2d2013-04-18 14:35:55 +03005940 if (enable_shadow_vmcs) {
5941 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5942 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
5943 }
Sheng Yang25c5f222008-03-28 13:18:56 +08005944 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005945 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08005946
Avi Kivity6aa8b732006-12-10 02:21:36 -08005947 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5948
Avi Kivity6aa8b732006-12-10 02:21:36 -08005949 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08005950 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07005951 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005952
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005953 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005954
Dan Williamsdfa169b2016-06-02 11:17:24 -07005955 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02005956 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005957 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02005958 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07005959 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08005960
Andrey Smetanind62caab2015-11-10 15:36:33 +03005961 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08005962 vmcs_write64(EOI_EXIT_BITMAP0, 0);
5963 vmcs_write64(EOI_EXIT_BITMAP1, 0);
5964 vmcs_write64(EOI_EXIT_BITMAP2, 0);
5965 vmcs_write64(EOI_EXIT_BITMAP3, 0);
5966
5967 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08005968
Li RongQing0bcf2612015-12-03 13:29:34 +08005969 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08005970 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08005971 }
5972
Wanpeng Lib31c1142018-03-12 04:53:04 -07005973 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005974 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02005975 vmx->ple_window = ple_window;
5976 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005977 }
5978
Xiao Guangrongc3707952011-07-12 03:28:04 +08005979 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5980 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005981 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5982
Avi Kivity9581d442010-10-19 16:46:55 +02005983 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
5984 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005985 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005986#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005987 rdmsrl(MSR_FS_BASE, a);
5988 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
5989 rdmsrl(MSR_GS_BASE, a);
5990 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
5991#else
5992 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
5993 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
5994#endif
5995
Bandan Das2a499e42017-08-03 15:54:41 -04005996 if (cpu_has_vmx_vmfunc())
5997 vmcs_write64(VM_FUNCTION_CONTROL, 0);
5998
Eddie Dong2cc51562007-05-21 07:28:09 +03005999 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6000 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006001 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03006002 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006003 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006004
Radim Krčmář74545702015-04-27 15:11:25 +02006005 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6006 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006007
Paolo Bonzini03916db2014-07-24 14:21:57 +02006008 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006009 u32 index = vmx_msr_index[i];
6010 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006011 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006012
6013 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6014 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006015 if (wrmsr_safe(index, data_low, data_high) < 0)
6016 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006017 vmx->guest_msrs[j].index = i;
6018 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006019 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006020 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006021 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006022
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01006023 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
6024 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02006025
6026 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006027
6028 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006029 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006030
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006031 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6032 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6033
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006034 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006035
Wanpeng Lif53cd632014-12-02 19:14:58 +08006036 if (vmx_xsaves_supported())
6037 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6038
Peter Feiner4e595162016-07-07 14:49:58 -07006039 if (enable_pml) {
6040 ASSERT(vmx->pml_pg);
6041 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6042 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6043 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006044}
6045
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006046static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006047{
6048 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006049 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006050 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006051
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006052 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006053 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006054
Wanpeng Li518e7b92018-02-28 14:03:31 +08006055 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006056 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006057 kvm_set_cr8(vcpu, 0);
6058
6059 if (!init_event) {
6060 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6061 MSR_IA32_APICBASE_ENABLE;
6062 if (kvm_vcpu_is_reset_bsp(vcpu))
6063 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6064 apic_base_msr.host_initiated = true;
6065 kvm_set_apic_base(vcpu, &apic_base_msr);
6066 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006067
Avi Kivity2fb92db2011-04-27 19:42:18 +03006068 vmx_segment_cache_clear(vmx);
6069
Avi Kivity5706be02008-08-20 15:07:31 +03006070 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006071 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006072 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006073
6074 seg_setup(VCPU_SREG_DS);
6075 seg_setup(VCPU_SREG_ES);
6076 seg_setup(VCPU_SREG_FS);
6077 seg_setup(VCPU_SREG_GS);
6078 seg_setup(VCPU_SREG_SS);
6079
6080 vmcs_write16(GUEST_TR_SELECTOR, 0);
6081 vmcs_writel(GUEST_TR_BASE, 0);
6082 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6083 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6084
6085 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6086 vmcs_writel(GUEST_LDTR_BASE, 0);
6087 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6088 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6089
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006090 if (!init_event) {
6091 vmcs_write32(GUEST_SYSENTER_CS, 0);
6092 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6093 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6094 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6095 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006096
Wanpeng Lic37c2872017-11-20 14:52:21 -08006097 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006098 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006099
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006100 vmcs_writel(GUEST_GDTR_BASE, 0);
6101 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6102
6103 vmcs_writel(GUEST_IDTR_BASE, 0);
6104 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6105
Anthony Liguori443381a2010-12-06 10:53:38 -06006106 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006107 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006108 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006109 if (kvm_mpx_supported())
6110 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006111
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006112 setup_msrs(vmx);
6113
Avi Kivity6aa8b732006-12-10 02:21:36 -08006114 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6115
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006116 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006117 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006118 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006119 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006120 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006121 vmcs_write32(TPR_THRESHOLD, 0);
6122 }
6123
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006124 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006125
Sheng Yang2384d2b2008-01-17 15:14:33 +08006126 if (vmx->vpid != 0)
6127 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6128
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006129 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006130 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006131 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006132 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006133 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006134
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006135 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006136
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006137 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006138 if (init_event)
6139 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006140}
6141
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006142/*
6143 * In nested virtualization, check if L1 asked to exit on external interrupts.
6144 * For most existing hypervisors, this will always return true.
6145 */
6146static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6147{
6148 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6149 PIN_BASED_EXT_INTR_MASK;
6150}
6151
Bandan Das77b0f5d2014-04-19 18:17:45 -04006152/*
6153 * In nested virtualization, check if L1 has set
6154 * VM_EXIT_ACK_INTR_ON_EXIT
6155 */
6156static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6157{
6158 return get_vmcs12(vcpu)->vm_exit_controls &
6159 VM_EXIT_ACK_INTR_ON_EXIT;
6160}
6161
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006162static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6163{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006164 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006165}
6166
Jan Kiszkac9a79532014-03-07 20:03:15 +01006167static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006168{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006169 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6170 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006171}
6172
Jan Kiszkac9a79532014-03-07 20:03:15 +01006173static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006174{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006175 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006176 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006177 enable_irq_window(vcpu);
6178 return;
6179 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006180
Paolo Bonzini47c01522016-12-19 11:44:07 +01006181 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6182 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006183}
6184
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006185static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006186{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006187 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006188 uint32_t intr;
6189 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006190
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006191 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006192
Avi Kivityfa89a812008-09-01 15:57:51 +03006193 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006194 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006195 int inc_eip = 0;
6196 if (vcpu->arch.interrupt.soft)
6197 inc_eip = vcpu->arch.event_exit_inst_len;
6198 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006199 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006200 return;
6201 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006202 intr = irq | INTR_INFO_VALID_MASK;
6203 if (vcpu->arch.interrupt.soft) {
6204 intr |= INTR_TYPE_SOFT_INTR;
6205 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6206 vmx->vcpu.arch.event_exit_inst_len);
6207 } else
6208 intr |= INTR_TYPE_EXT_INTR;
6209 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006210
6211 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006212}
6213
Sheng Yangf08864b2008-05-15 18:23:25 +08006214static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6215{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006216 struct vcpu_vmx *vmx = to_vmx(vcpu);
6217
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006218 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006219 /*
6220 * Tracking the NMI-blocked state in software is built upon
6221 * finding the next open IRQ window. This, in turn, depends on
6222 * well-behaving guests: They have to keep IRQs disabled at
6223 * least as long as the NMI handler runs. Otherwise we may
6224 * cause NMI nesting, maybe breaking the guest. But as this is
6225 * highly unlikely, we can live with the residual risk.
6226 */
6227 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6228 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6229 }
6230
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006231 ++vcpu->stat.nmi_injections;
6232 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006233
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006234 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006235 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006236 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006237 return;
6238 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006239
Sheng Yangf08864b2008-05-15 18:23:25 +08006240 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6241 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006242
6243 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006244}
6245
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006246static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6247{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006248 struct vcpu_vmx *vmx = to_vmx(vcpu);
6249 bool masked;
6250
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006251 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006252 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006253 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006254 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006255 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6256 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6257 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006258}
6259
6260static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6261{
6262 struct vcpu_vmx *vmx = to_vmx(vcpu);
6263
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006264 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006265 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6266 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6267 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6268 }
6269 } else {
6270 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6271 if (masked)
6272 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6273 GUEST_INTR_STATE_NMI);
6274 else
6275 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6276 GUEST_INTR_STATE_NMI);
6277 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006278}
6279
Jan Kiszka2505dc92013-04-14 12:12:47 +02006280static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6281{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006282 if (to_vmx(vcpu)->nested.nested_run_pending)
6283 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006284
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006285 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006286 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6287 return 0;
6288
Jan Kiszka2505dc92013-04-14 12:12:47 +02006289 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6290 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6291 | GUEST_INTR_STATE_NMI));
6292}
6293
Gleb Natapov78646122009-03-23 12:12:11 +02006294static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6295{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006296 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6297 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006298 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6299 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006300}
6301
Izik Eiduscbc94022007-10-25 00:29:55 +02006302static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6303{
6304 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006305
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006306 if (enable_unrestricted_guest)
6307 return 0;
6308
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006309 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6310 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006311 if (ret)
6312 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006313 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006314 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006315}
6316
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006317static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6318{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006319 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006320 return 0;
6321}
6322
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006323static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006324{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006325 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006326 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006327 /*
6328 * Update instruction length as we may reinject the exception
6329 * from user space while in guest debugging mode.
6330 */
6331 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6332 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006333 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006334 return false;
6335 /* fall through */
6336 case DB_VECTOR:
6337 if (vcpu->guest_debug &
6338 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6339 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006340 /* fall through */
6341 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006342 case OF_VECTOR:
6343 case BR_VECTOR:
6344 case UD_VECTOR:
6345 case DF_VECTOR:
6346 case SS_VECTOR:
6347 case GP_VECTOR:
6348 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006349 return true;
6350 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006351 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006352 return false;
6353}
6354
6355static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6356 int vec, u32 err_code)
6357{
6358 /*
6359 * Instruction with address size override prefix opcode 0x67
6360 * Cause the #SS fault with 0 error code in VM86 mode.
6361 */
6362 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6363 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6364 if (vcpu->arch.halt_request) {
6365 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006366 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006367 }
6368 return 1;
6369 }
6370 return 0;
6371 }
6372
6373 /*
6374 * Forward all other exceptions that are valid in real mode.
6375 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6376 * the required debugging infrastructure rework.
6377 */
6378 kvm_queue_exception(vcpu, vec);
6379 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006380}
6381
Andi Kleena0861c02009-06-08 17:37:09 +08006382/*
6383 * Trigger machine check on the host. We assume all the MSRs are already set up
6384 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6385 * We pass a fake environment to the machine check handler because we want
6386 * the guest to be always treated like user space, no matter what context
6387 * it used internally.
6388 */
6389static void kvm_machine_check(void)
6390{
6391#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6392 struct pt_regs regs = {
6393 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6394 .flags = X86_EFLAGS_IF,
6395 };
6396
6397 do_machine_check(&regs, 0);
6398#endif
6399}
6400
Avi Kivity851ba692009-08-24 11:10:17 +03006401static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08006402{
6403 /* already handled by vcpu_run */
6404 return 1;
6405}
6406
Avi Kivity851ba692009-08-24 11:10:17 +03006407static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006408{
Avi Kivity1155f762007-11-22 11:30:47 +02006409 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006410 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006411 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006412 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006413 u32 vect_info;
6414 enum emulation_result er;
6415
Avi Kivity1155f762007-11-22 11:30:47 +02006416 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02006417 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006418
Andi Kleena0861c02009-06-08 17:37:09 +08006419 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03006420 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006421
Jim Mattsonef85b672016-12-12 11:01:37 -08006422 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02006423 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03006424
Wanpeng Li082d06e2018-04-03 16:28:48 -07006425 if (is_invalid_opcode(intr_info))
6426 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006427
Avi Kivity6aa8b732006-12-10 02:21:36 -08006428 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06006429 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006430 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006431
Liran Alon9e869482018-03-12 13:12:51 +02006432 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
6433 WARN_ON_ONCE(!enable_vmware_backdoor);
6434 er = emulate_instruction(vcpu,
6435 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
6436 if (er == EMULATE_USER_EXIT)
6437 return 0;
6438 else if (er != EMULATE_DONE)
6439 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
6440 return 1;
6441 }
6442
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006443 /*
6444 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6445 * MMIO, it is better to report an internal error.
6446 * See the comments in vmx_handle_exit.
6447 */
6448 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6449 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6450 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6451 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006452 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006453 vcpu->run->internal.data[0] = vect_info;
6454 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006455 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006456 return 0;
6457 }
6458
Avi Kivity6aa8b732006-12-10 02:21:36 -08006459 if (is_page_fault(intr_info)) {
6460 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006461 /* EPT won't cause page fault directly */
6462 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02006463 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006464 }
6465
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006466 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006467
6468 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6469 return handle_rmode_exception(vcpu, ex_no, error_code);
6470
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006471 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01006472 case AC_VECTOR:
6473 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6474 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006475 case DB_VECTOR:
6476 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6477 if (!(vcpu->guest_debug &
6478 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01006479 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006480 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07006481 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01006482 skip_emulated_instruction(vcpu);
6483
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006484 kvm_queue_exception(vcpu, DB_VECTOR);
6485 return 1;
6486 }
6487 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6488 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6489 /* fall through */
6490 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006491 /*
6492 * Update instruction length as we may reinject #BP from
6493 * user space while in guest debugging mode. Reading it for
6494 * #DB as well causes no harm, it is not used in that case.
6495 */
6496 vmx->vcpu.arch.event_exit_inst_len =
6497 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006498 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006499 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006500 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6501 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006502 break;
6503 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006504 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6505 kvm_run->ex.exception = ex_no;
6506 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006507 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006508 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006509 return 0;
6510}
6511
Avi Kivity851ba692009-08-24 11:10:17 +03006512static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006513{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006514 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006515 return 1;
6516}
6517
Avi Kivity851ba692009-08-24 11:10:17 +03006518static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006519{
Avi Kivity851ba692009-08-24 11:10:17 +03006520 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006521 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006522 return 0;
6523}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006524
Avi Kivity851ba692009-08-24 11:10:17 +03006525static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006526{
He, Qingbfdaab02007-09-12 14:18:28 +08006527 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08006528 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02006529 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006530
He, Qingbfdaab02007-09-12 14:18:28 +08006531 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006532 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006533
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006534 ++vcpu->stat.io_exits;
6535
Sean Christopherson432baf62018-03-08 08:57:26 -08006536 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01006537 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006538
6539 port = exit_qualification >> 16;
6540 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08006541 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006542
Sean Christophersondca7f122018-03-08 08:57:27 -08006543 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006544}
6545
Ingo Molnar102d8322007-02-19 14:37:47 +02006546static void
6547vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6548{
6549 /*
6550 * Patch in the VMCALL instruction:
6551 */
6552 hypercall[0] = 0x0f;
6553 hypercall[1] = 0x01;
6554 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006555}
6556
Guo Chao0fa06072012-06-28 15:16:19 +08006557/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006558static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6559{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006560 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006561 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6562 unsigned long orig_val = val;
6563
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006564 /*
6565 * We get here when L2 changed cr0 in a way that did not change
6566 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006567 * but did change L0 shadowed bits. So we first calculate the
6568 * effective cr0 value that L1 would like to write into the
6569 * hardware. It consists of the L2-owned bits from the new
6570 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006571 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006572 val = (val & ~vmcs12->cr0_guest_host_mask) |
6573 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6574
David Matlack38991522016-11-29 18:14:08 -08006575 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006576 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006577
6578 if (kvm_set_cr0(vcpu, val))
6579 return 1;
6580 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006581 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006582 } else {
6583 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08006584 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006585 return 1;
David Matlack38991522016-11-29 18:14:08 -08006586
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006587 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006588 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006589}
6590
6591static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6592{
6593 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006594 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6595 unsigned long orig_val = val;
6596
6597 /* analogously to handle_set_cr0 */
6598 val = (val & ~vmcs12->cr4_guest_host_mask) |
6599 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6600 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006601 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006602 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006603 return 0;
6604 } else
6605 return kvm_set_cr4(vcpu, val);
6606}
6607
Paolo Bonzini0367f202016-07-12 10:44:55 +02006608static int handle_desc(struct kvm_vcpu *vcpu)
6609{
6610 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
6611 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6612}
6613
Avi Kivity851ba692009-08-24 11:10:17 +03006614static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006615{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006616 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006617 int cr;
6618 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03006619 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006620 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006621
He, Qingbfdaab02007-09-12 14:18:28 +08006622 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006623 cr = exit_qualification & 15;
6624 reg = (exit_qualification >> 8) & 15;
6625 switch ((exit_qualification >> 4) & 3) {
6626 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03006627 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006628 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006629 switch (cr) {
6630 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006631 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006632 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006633 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006634 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03006635 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006636 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006637 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006638 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006639 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006640 case 8: {
6641 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03006642 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006643 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006644 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006645 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08006646 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006647 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006648 return ret;
6649 /*
6650 * TODO: we might be squashing a
6651 * KVM_GUESTDBG_SINGLESTEP-triggered
6652 * KVM_EXIT_DEBUG here.
6653 */
Avi Kivity851ba692009-08-24 11:10:17 +03006654 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006655 return 0;
6656 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02006657 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006658 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03006659 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006660 WARN_ONCE(1, "Guest should always own CR0.TS");
6661 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02006662 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08006663 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006664 case 1: /*mov from cr*/
6665 switch (cr) {
6666 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006667 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02006668 val = kvm_read_cr3(vcpu);
6669 kvm_register_write(vcpu, reg, val);
6670 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006671 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006672 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006673 val = kvm_get_cr8(vcpu);
6674 kvm_register_write(vcpu, reg, val);
6675 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006676 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006677 }
6678 break;
6679 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02006680 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02006681 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02006682 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006683
Kyle Huey6affcbe2016-11-29 12:40:40 -08006684 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006685 default:
6686 break;
6687 }
Avi Kivity851ba692009-08-24 11:10:17 +03006688 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03006689 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08006690 (int)(exit_qualification >> 4) & 3, cr);
6691 return 0;
6692}
6693
Avi Kivity851ba692009-08-24 11:10:17 +03006694static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006695{
He, Qingbfdaab02007-09-12 14:18:28 +08006696 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006697 int dr, dr7, reg;
6698
6699 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6700 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6701
6702 /* First, if DR does not exist, trigger UD */
6703 if (!kvm_require_dr(vcpu, dr))
6704 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006705
Jan Kiszkaf2483412010-01-20 18:20:20 +01006706 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03006707 if (!kvm_require_cpl(vcpu, 0))
6708 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006709 dr7 = vmcs_readl(GUEST_DR7);
6710 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006711 /*
6712 * As the vm-exit takes precedence over the debug trap, we
6713 * need to emulate the latter, either for the host or the
6714 * guest debugging itself.
6715 */
6716 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03006717 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006718 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02006719 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006720 vcpu->run->debug.arch.exception = DB_VECTOR;
6721 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006722 return 0;
6723 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02006724 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006725 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006726 kvm_queue_exception(vcpu, DB_VECTOR);
6727 return 1;
6728 }
6729 }
6730
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006731 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01006732 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6733 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006734
6735 /*
6736 * No more DR vmexits; force a reload of the debug registers
6737 * and reenter on this instruction. The next vmexit will
6738 * retrieve the full state of the debug registers.
6739 */
6740 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
6741 return 1;
6742 }
6743
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006744 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
6745 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03006746 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006747
6748 if (kvm_get_dr(vcpu, dr, &val))
6749 return 1;
6750 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03006751 } else
Nadav Amit57773922014-06-18 17:19:23 +03006752 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006753 return 1;
6754
Kyle Huey6affcbe2016-11-29 12:40:40 -08006755 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006756}
6757
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01006758static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
6759{
6760 return vcpu->arch.dr6;
6761}
6762
6763static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
6764{
6765}
6766
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006767static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
6768{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006769 get_debugreg(vcpu->arch.db[0], 0);
6770 get_debugreg(vcpu->arch.db[1], 1);
6771 get_debugreg(vcpu->arch.db[2], 2);
6772 get_debugreg(vcpu->arch.db[3], 3);
6773 get_debugreg(vcpu->arch.dr6, 6);
6774 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
6775
6776 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01006777 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006778}
6779
Gleb Natapov020df072010-04-13 10:05:23 +03006780static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
6781{
6782 vmcs_writel(GUEST_DR7, val);
6783}
6784
Avi Kivity851ba692009-08-24 11:10:17 +03006785static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006786{
Kyle Huey6a908b62016-11-29 12:40:37 -08006787 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006788}
6789
Avi Kivity851ba692009-08-24 11:10:17 +03006790static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006791{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006792 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006793 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006794
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006795 msr_info.index = ecx;
6796 msr_info.host_initiated = false;
6797 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02006798 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006799 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006800 return 1;
6801 }
6802
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006803 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006804
Avi Kivity6aa8b732006-12-10 02:21:36 -08006805 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006806 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
6807 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006808 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006809}
6810
Avi Kivity851ba692009-08-24 11:10:17 +03006811static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006812{
Will Auld8fe8ab42012-11-29 12:42:12 -08006813 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006814 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6815 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
6816 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006817
Will Auld8fe8ab42012-11-29 12:42:12 -08006818 msr.data = data;
6819 msr.index = ecx;
6820 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03006821 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02006822 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006823 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006824 return 1;
6825 }
6826
Avi Kivity59200272010-01-25 19:47:02 +02006827 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006828 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006829}
6830
Avi Kivity851ba692009-08-24 11:10:17 +03006831static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006832{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01006833 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006834 return 1;
6835}
6836
Avi Kivity851ba692009-08-24 11:10:17 +03006837static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006838{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006839 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6840 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006841
Avi Kivity3842d132010-07-27 12:30:24 +03006842 kvm_make_request(KVM_REQ_EVENT, vcpu);
6843
Jan Kiszkaa26bf122008-09-26 09:30:45 +02006844 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006845 return 1;
6846}
6847
Avi Kivity851ba692009-08-24 11:10:17 +03006848static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006849{
Avi Kivityd3bef152007-06-05 15:53:05 +03006850 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006851}
6852
Avi Kivity851ba692009-08-24 11:10:17 +03006853static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02006854{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03006855 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02006856}
6857
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006858static int handle_invd(struct kvm_vcpu *vcpu)
6859{
Andre Przywara51d8b662010-12-21 11:12:02 +01006860 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006861}
6862
Avi Kivity851ba692009-08-24 11:10:17 +03006863static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03006864{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006865 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006866
6867 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006868 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006869}
6870
Avi Kivityfee84b02011-11-10 14:57:25 +02006871static int handle_rdpmc(struct kvm_vcpu *vcpu)
6872{
6873 int err;
6874
6875 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006876 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02006877}
6878
Avi Kivity851ba692009-08-24 11:10:17 +03006879static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02006880{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006881 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02006882}
6883
Dexuan Cui2acf9232010-06-10 11:27:12 +08006884static int handle_xsetbv(struct kvm_vcpu *vcpu)
6885{
6886 u64 new_bv = kvm_read_edx_eax(vcpu);
6887 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
6888
6889 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006890 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08006891 return 1;
6892}
6893
Wanpeng Lif53cd632014-12-02 19:14:58 +08006894static int handle_xsaves(struct kvm_vcpu *vcpu)
6895{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006896 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08006897 WARN(1, "this should never happen\n");
6898 return 1;
6899}
6900
6901static int handle_xrstors(struct kvm_vcpu *vcpu)
6902{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006903 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08006904 WARN(1, "this should never happen\n");
6905 return 1;
6906}
6907
Avi Kivity851ba692009-08-24 11:10:17 +03006908static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08006909{
Kevin Tian58fbbf22011-08-30 13:56:17 +03006910 if (likely(fasteoi)) {
6911 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6912 int access_type, offset;
6913
6914 access_type = exit_qualification & APIC_ACCESS_TYPE;
6915 offset = exit_qualification & APIC_ACCESS_OFFSET;
6916 /*
6917 * Sane guest uses MOV to write EOI, with written value
6918 * not cared. So make a short-circuit here by avoiding
6919 * heavy instruction emulation.
6920 */
6921 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6922 (offset == APIC_EOI)) {
6923 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006924 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03006925 }
6926 }
Andre Przywara51d8b662010-12-21 11:12:02 +01006927 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08006928}
6929
Yang Zhangc7c9c562013-01-25 10:18:51 +08006930static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6931{
6932 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6933 int vector = exit_qualification & 0xff;
6934
6935 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6936 kvm_apic_set_eoi_accelerated(vcpu, vector);
6937 return 1;
6938}
6939
Yang Zhang83d4c282013-01-25 10:18:49 +08006940static int handle_apic_write(struct kvm_vcpu *vcpu)
6941{
6942 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6943 u32 offset = exit_qualification & 0xfff;
6944
6945 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6946 kvm_apic_write_nodecode(vcpu, offset);
6947 return 1;
6948}
6949
Avi Kivity851ba692009-08-24 11:10:17 +03006950static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02006951{
Jan Kiszka60637aa2008-09-26 09:30:47 +02006952 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02006953 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02006954 bool has_error_code = false;
6955 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02006956 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006957 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006958
6959 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006960 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006961 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02006962
6963 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6964
6965 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006966 if (reason == TASK_SWITCH_GATE && idt_v) {
6967 switch (type) {
6968 case INTR_TYPE_NMI_INTR:
6969 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02006970 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006971 break;
6972 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006973 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006974 kvm_clear_interrupt_queue(vcpu);
6975 break;
6976 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02006977 if (vmx->idt_vectoring_info &
6978 VECTORING_INFO_DELIVER_CODE_MASK) {
6979 has_error_code = true;
6980 error_code =
6981 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6982 }
6983 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006984 case INTR_TYPE_SOFT_EXCEPTION:
6985 kvm_clear_exception_queue(vcpu);
6986 break;
6987 default:
6988 break;
6989 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02006990 }
Izik Eidus37817f22008-03-24 23:14:53 +02006991 tss_selector = exit_qualification;
6992
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006993 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
6994 type != INTR_TYPE_EXT_INTR &&
6995 type != INTR_TYPE_NMI_INTR))
6996 skip_emulated_instruction(vcpu);
6997
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006998 if (kvm_task_switch(vcpu, tss_selector,
6999 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7000 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007001 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7002 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7003 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007004 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007005 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007006
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007007 /*
7008 * TODO: What about debug traps on tss switch?
7009 * Are we supposed to inject them and update dr6?
7010 */
7011
7012 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007013}
7014
Avi Kivity851ba692009-08-24 11:10:17 +03007015static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007016{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007017 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007018 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007019 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007020
Sheng Yangf9c617f2009-03-25 10:08:52 +08007021 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007022
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007023 /*
7024 * EPT violation happened while executing iret from NMI,
7025 * "blocked by NMI" bit has to be set before next VM entry.
7026 * There are errata that may cause this bit to not be set:
7027 * AAK134, BY25.
7028 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007029 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007030 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007031 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007032 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7033
Sheng Yang14394422008-04-28 12:24:45 +08007034 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007035 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007036
Junaid Shahid27959a42016-12-06 16:46:10 -08007037 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007038 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007039 ? PFERR_USER_MASK : 0;
7040 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007041 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007042 ? PFERR_WRITE_MASK : 0;
7043 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007044 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007045 ? PFERR_FETCH_MASK : 0;
7046 /* ept page table entry is present? */
7047 error_code |= (exit_qualification &
7048 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7049 EPT_VIOLATION_EXECUTABLE))
7050 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007051
Paolo Bonzinieebed242016-11-28 14:39:58 +01007052 error_code |= (exit_qualification & 0x100) != 0 ?
7053 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007054
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007055 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007056 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007057}
7058
Avi Kivity851ba692009-08-24 11:10:17 +03007059static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007060{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007061 gpa_t gpa;
7062
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007063 /*
7064 * A nested guest cannot optimize MMIO vmexits, because we have an
7065 * nGPA here instead of the required GPA.
7066 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007067 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007068 if (!is_guest_mode(vcpu) &&
7069 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007070 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007071 /*
7072 * Doing kvm_skip_emulated_instruction() depends on undefined
7073 * behavior: Intel's manual doesn't mandate
7074 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7075 * occurs and while on real hardware it was observed to be set,
7076 * other hypervisors (namely Hyper-V) don't set it, we end up
7077 * advancing IP with some random value. Disable fast mmio when
7078 * running nested and keep it for real hardware in hope that
7079 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7080 */
7081 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7082 return kvm_skip_emulated_instruction(vcpu);
7083 else
7084 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
7085 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007086 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007087
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007088 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007089}
7090
Avi Kivity851ba692009-08-24 11:10:17 +03007091static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007092{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007093 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007094 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7095 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007096 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007097 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007098
7099 return 1;
7100}
7101
Mohammed Gamal80ced182009-09-01 12:48:18 +02007102static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007103{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007104 struct vcpu_vmx *vmx = to_vmx(vcpu);
7105 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007106 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007107 u32 cpu_exec_ctrl;
7108 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007109 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007110
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007111 /*
7112 * We should never reach the point where we are emulating L2
7113 * due to invalid guest state as that means we incorrectly
7114 * allowed a nested VMEntry with an invalid vmcs12.
7115 */
7116 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7117
Avi Kivity49e9d552010-09-19 14:34:08 +02007118 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7119 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007120
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007121 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007122 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007123 return handle_interrupt_window(&vmx->vcpu);
7124
Radim Krčmář72875d82017-04-26 22:32:19 +02007125 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007126 return 1;
7127
Liran Alon9b8ae632017-11-05 16:56:34 +02007128 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007129
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007130 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007131 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007132 ret = 0;
7133 goto out;
7134 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007135
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007136 if (err != EMULATE_DONE)
7137 goto emulation_error;
7138
7139 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7140 vcpu->arch.exception.pending)
7141 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007142
Gleb Natapov8d76c492013-05-08 18:38:44 +03007143 if (vcpu->arch.halt_request) {
7144 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007145 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007146 goto out;
7147 }
7148
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007149 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007150 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007151 if (need_resched())
7152 schedule();
7153 }
7154
Mohammed Gamal80ced182009-09-01 12:48:18 +02007155out:
7156 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007157
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007158emulation_error:
7159 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7160 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7161 vcpu->run->internal.ndata = 0;
7162 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007163}
7164
7165static void grow_ple_window(struct kvm_vcpu *vcpu)
7166{
7167 struct vcpu_vmx *vmx = to_vmx(vcpu);
7168 int old = vmx->ple_window;
7169
Babu Mogerc8e88712018-03-16 16:37:24 -04007170 vmx->ple_window = __grow_ple_window(old, ple_window,
7171 ple_window_grow,
7172 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007173
7174 if (vmx->ple_window != old)
7175 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007176
7177 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007178}
7179
7180static void shrink_ple_window(struct kvm_vcpu *vcpu)
7181{
7182 struct vcpu_vmx *vmx = to_vmx(vcpu);
7183 int old = vmx->ple_window;
7184
Babu Mogerc8e88712018-03-16 16:37:24 -04007185 vmx->ple_window = __shrink_ple_window(old, ple_window,
7186 ple_window_shrink,
7187 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007188
7189 if (vmx->ple_window != old)
7190 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007191
7192 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007193}
7194
7195/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007196 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7197 */
7198static void wakeup_handler(void)
7199{
7200 struct kvm_vcpu *vcpu;
7201 int cpu = smp_processor_id();
7202
7203 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7204 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7205 blocked_vcpu_list) {
7206 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7207
7208 if (pi_test_on(pi_desc) == 1)
7209 kvm_vcpu_kick(vcpu);
7210 }
7211 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7212}
7213
Peng Haoe01bca22018-04-07 05:47:32 +08007214static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007215{
7216 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7217 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7218 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7219 0ull, VMX_EPT_EXECUTABLE_MASK,
7220 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007221 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007222
7223 ept_set_mmio_spte_mask();
7224 kvm_enable_tdp();
7225}
7226
Tiejun Chenf2c76482014-10-28 10:14:47 +08007227static __init int hardware_setup(void)
7228{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007229 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007230
7231 rdmsrl_safe(MSR_EFER, &host_efer);
7232
7233 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7234 kvm_define_shared_msr(i, vmx_msr_index[i]);
7235
Radim Krčmář23611332016-09-29 22:41:33 +02007236 for (i = 0; i < VMX_BITMAP_NR; i++) {
7237 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7238 if (!vmx_bitmap[i])
7239 goto out;
7240 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007241
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007242 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7243 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7244
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007245 if (setup_vmcs_config(&vmcs_config) < 0) {
7246 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007247 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007248 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007249
7250 if (boot_cpu_has(X86_FEATURE_NX))
7251 kvm_enable_efer_bits(EFER_NX);
7252
Wanpeng Li08d839c2017-03-23 05:30:08 -07007253 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7254 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007255 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007256
Tiejun Chenf2c76482014-10-28 10:14:47 +08007257 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007258 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007259 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007260 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007261 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007262
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007263 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007264 enable_ept_ad_bits = 0;
7265
Wanpeng Li8ad81822017-10-09 15:51:53 -07007266 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007267 enable_unrestricted_guest = 0;
7268
Paolo Bonziniad15a292015-01-30 16:18:49 +01007269 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007270 flexpriority_enabled = 0;
7271
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007272 if (!cpu_has_virtual_nmis())
7273 enable_vnmi = 0;
7274
Paolo Bonziniad15a292015-01-30 16:18:49 +01007275 /*
7276 * set_apic_access_page_addr() is used to reload apic access
7277 * page upon invalidation. No need to do anything if not
7278 * using the APIC_ACCESS_ADDR VMCS field.
7279 */
7280 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007281 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007282
7283 if (!cpu_has_vmx_tpr_shadow())
7284 kvm_x86_ops->update_cr8_intercept = NULL;
7285
7286 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7287 kvm_disable_largepages();
7288
Wanpeng Li0f107682017-09-28 18:06:24 -07007289 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007290 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007291 ple_window = 0;
7292 ple_window_grow = 0;
7293 ple_window_max = 0;
7294 ple_window_shrink = 0;
7295 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007296
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007297 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007298 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007299 kvm_x86_ops->sync_pir_to_irr = NULL;
7300 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007301
Haozhong Zhang64903d62015-10-20 15:39:09 +08007302 if (cpu_has_vmx_tsc_scaling()) {
7303 kvm_has_tsc_control = true;
7304 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7305 kvm_tsc_scaling_ratio_frac_bits = 48;
7306 }
7307
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007308 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7309
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007310 if (enable_ept)
7311 vmx_enable_tdp();
7312 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007313 kvm_disable_tdp();
7314
Kai Huang843e4332015-01-28 10:54:28 +08007315 /*
7316 * Only enable PML when hardware supports PML feature, and both EPT
7317 * and EPT A/D bit features are enabled -- PML depends on them to work.
7318 */
7319 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7320 enable_pml = 0;
7321
7322 if (!enable_pml) {
7323 kvm_x86_ops->slot_enable_log_dirty = NULL;
7324 kvm_x86_ops->slot_disable_log_dirty = NULL;
7325 kvm_x86_ops->flush_log_dirty = NULL;
7326 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7327 }
7328
Yunhong Jiang64672c92016-06-13 14:19:59 -07007329 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7330 u64 vmx_msr;
7331
7332 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7333 cpu_preemption_timer_multi =
7334 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7335 } else {
7336 kvm_x86_ops->set_hv_timer = NULL;
7337 kvm_x86_ops->cancel_hv_timer = NULL;
7338 }
7339
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007340 if (!cpu_has_vmx_shadow_vmcs())
7341 enable_shadow_vmcs = 0;
7342 if (enable_shadow_vmcs)
7343 init_vmcs_shadow_fields();
7344
Feng Wubf9f6ac2015-09-18 22:29:55 +08007345 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007346 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007347
Ashok Rajc45dcc72016-06-22 14:59:56 +08007348 kvm_mce_cap_supported |= MCG_LMCE_P;
7349
Tiejun Chenf2c76482014-10-28 10:14:47 +08007350 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007351
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007352out:
Radim Krčmář23611332016-09-29 22:41:33 +02007353 for (i = 0; i < VMX_BITMAP_NR; i++)
7354 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007355
7356 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007357}
7358
7359static __exit void hardware_unsetup(void)
7360{
Radim Krčmář23611332016-09-29 22:41:33 +02007361 int i;
7362
7363 for (i = 0; i < VMX_BITMAP_NR; i++)
7364 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007365
Tiejun Chenf2c76482014-10-28 10:14:47 +08007366 free_kvm_area();
7367}
7368
Avi Kivity6aa8b732006-12-10 02:21:36 -08007369/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007370 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7371 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7372 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03007373static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007374{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007375 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007376 grow_ple_window(vcpu);
7377
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08007378 /*
7379 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7380 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7381 * never set PAUSE_EXITING and just set PLE if supported,
7382 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7383 */
7384 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007385 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007386}
7387
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007388static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08007389{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007390 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08007391}
7392
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007393static int handle_mwait(struct kvm_vcpu *vcpu)
7394{
7395 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7396 return handle_nop(vcpu);
7397}
7398
Jim Mattson45ec3682017-08-23 16:32:04 -07007399static int handle_invalid_op(struct kvm_vcpu *vcpu)
7400{
7401 kvm_queue_exception(vcpu, UD_VECTOR);
7402 return 1;
7403}
7404
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007405static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7406{
7407 return 1;
7408}
7409
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007410static int handle_monitor(struct kvm_vcpu *vcpu)
7411{
7412 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7413 return handle_nop(vcpu);
7414}
7415
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007416/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007417 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7418 * set the success or error code of an emulated VMX instruction, as specified
7419 * by Vol 2B, VMX Instruction Reference, "Conventions".
7420 */
7421static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7422{
7423 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7424 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7425 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7426}
7427
7428static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7429{
7430 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7431 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7432 X86_EFLAGS_SF | X86_EFLAGS_OF))
7433 | X86_EFLAGS_CF);
7434}
7435
Abel Gordon145c28d2013-04-18 14:36:55 +03007436static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007437 u32 vm_instruction_error)
7438{
7439 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7440 /*
7441 * failValid writes the error number to the current VMCS, which
7442 * can't be done there isn't a current VMCS.
7443 */
7444 nested_vmx_failInvalid(vcpu);
7445 return;
7446 }
7447 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7448 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7449 X86_EFLAGS_SF | X86_EFLAGS_OF))
7450 | X86_EFLAGS_ZF);
7451 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7452 /*
7453 * We don't need to force a shadow sync because
7454 * VM_INSTRUCTION_ERROR is not shadowed
7455 */
7456}
Abel Gordon145c28d2013-04-18 14:36:55 +03007457
Wincy Vanff651cb2014-12-11 08:52:58 +03007458static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7459{
7460 /* TODO: not to reset guest simply here. */
7461 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007462 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007463}
7464
Jan Kiszkaf4124502014-03-07 20:03:13 +01007465static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7466{
7467 struct vcpu_vmx *vmx =
7468 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7469
7470 vmx->nested.preemption_timer_expired = true;
7471 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7472 kvm_vcpu_kick(&vmx->vcpu);
7473
7474 return HRTIMER_NORESTART;
7475}
7476
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007477/*
Bandan Das19677e32014-05-06 02:19:15 -04007478 * Decode the memory-address operand of a vmx instruction, as recorded on an
7479 * exit caused by such an instruction (run by a guest hypervisor).
7480 * On success, returns 0. When the operand is invalid, returns 1 and throws
7481 * #UD or #GP.
7482 */
7483static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7484 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007485 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007486{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007487 gva_t off;
7488 bool exn;
7489 struct kvm_segment s;
7490
Bandan Das19677e32014-05-06 02:19:15 -04007491 /*
7492 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7493 * Execution", on an exit, vmx_instruction_info holds most of the
7494 * addressing components of the operand. Only the displacement part
7495 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7496 * For how an actual address is calculated from all these components,
7497 * refer to Vol. 1, "Operand Addressing".
7498 */
7499 int scaling = vmx_instruction_info & 3;
7500 int addr_size = (vmx_instruction_info >> 7) & 7;
7501 bool is_reg = vmx_instruction_info & (1u << 10);
7502 int seg_reg = (vmx_instruction_info >> 15) & 7;
7503 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7504 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7505 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7506 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7507
7508 if (is_reg) {
7509 kvm_queue_exception(vcpu, UD_VECTOR);
7510 return 1;
7511 }
7512
7513 /* Addr = segment_base + offset */
7514 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007515 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007516 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007517 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007518 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007519 off += kvm_register_read(vcpu, index_reg)<<scaling;
7520 vmx_get_segment(vcpu, &s, seg_reg);
7521 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007522
7523 if (addr_size == 1) /* 32 bit */
7524 *ret &= 0xffffffff;
7525
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007526 /* Checks for #GP/#SS exceptions. */
7527 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007528 if (is_long_mode(vcpu)) {
7529 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7530 * non-canonical form. This is the only check on the memory
7531 * destination for long mode!
7532 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007533 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007534 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007535 /* Protected mode: apply checks for segment validity in the
7536 * following order:
7537 * - segment type check (#GP(0) may be thrown)
7538 * - usability check (#GP(0)/#SS(0))
7539 * - limit check (#GP(0)/#SS(0))
7540 */
7541 if (wr)
7542 /* #GP(0) if the destination operand is located in a
7543 * read-only data segment or any code segment.
7544 */
7545 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7546 else
7547 /* #GP(0) if the source operand is located in an
7548 * execute-only code segment
7549 */
7550 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007551 if (exn) {
7552 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7553 return 1;
7554 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007555 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7556 */
7557 exn = (s.unusable != 0);
7558 /* Protected mode: #GP(0)/#SS(0) if the memory
7559 * operand is outside the segment limit.
7560 */
7561 exn = exn || (off + sizeof(u64) > s.limit);
7562 }
7563 if (exn) {
7564 kvm_queue_exception_e(vcpu,
7565 seg_reg == VCPU_SREG_SS ?
7566 SS_VECTOR : GP_VECTOR,
7567 0);
7568 return 1;
7569 }
7570
Bandan Das19677e32014-05-06 02:19:15 -04007571 return 0;
7572}
7573
Radim Krčmářcbf71272017-05-19 15:48:51 +02007574static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007575{
7576 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04007577 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04007578
7579 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007580 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007581 return 1;
7582
Radim Krčmářcbf71272017-05-19 15:48:51 +02007583 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, vmpointer,
7584 sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007585 kvm_inject_page_fault(vcpu, &e);
7586 return 1;
7587 }
7588
Bandan Das3573e222014-05-06 02:19:16 -04007589 return 0;
7590}
7591
Jim Mattsone29acc52016-11-30 12:03:43 -08007592static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7593{
7594 struct vcpu_vmx *vmx = to_vmx(vcpu);
7595 struct vmcs *shadow_vmcs;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007596 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08007597
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007598 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7599 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06007600 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08007601
7602 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7603 if (!vmx->nested.cached_vmcs12)
7604 goto out_cached_vmcs12;
7605
7606 if (enable_shadow_vmcs) {
7607 shadow_vmcs = alloc_vmcs();
7608 if (!shadow_vmcs)
7609 goto out_shadow_vmcs;
7610 /* mark vmcs as shadow */
7611 shadow_vmcs->revision_id |= (1u << 31);
7612 /* init shadow vmcs */
7613 vmcs_clear(shadow_vmcs);
7614 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7615 }
7616
Jim Mattsone29acc52016-11-30 12:03:43 -08007617 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7618 HRTIMER_MODE_REL_PINNED);
7619 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7620
7621 vmx->nested.vmxon = true;
7622 return 0;
7623
7624out_shadow_vmcs:
7625 kfree(vmx->nested.cached_vmcs12);
7626
7627out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06007628 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08007629
Jim Mattsonde3a0022017-11-27 17:22:25 -06007630out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08007631 return -ENOMEM;
7632}
7633
Bandan Das3573e222014-05-06 02:19:16 -04007634/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007635 * Emulate the VMXON instruction.
7636 * Currently, we just remember that VMX is active, and do not save or even
7637 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7638 * do not currently need to store anything in that guest-allocated memory
7639 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7640 * argument is different from the VMXON pointer (which the spec says they do).
7641 */
7642static int handle_vmon(struct kvm_vcpu *vcpu)
7643{
Jim Mattsone29acc52016-11-30 12:03:43 -08007644 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007645 gpa_t vmptr;
7646 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007647 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007648 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7649 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007650
Jim Mattson70f3aac2017-04-26 08:53:46 -07007651 /*
7652 * The Intel VMX Instruction Reference lists a bunch of bits that are
7653 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
7654 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
7655 * Otherwise, we should fail with #UD. But most faulting conditions
7656 * have already been checked by hardware, prior to the VM-exit for
7657 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
7658 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007659 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07007660 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007661 kvm_queue_exception(vcpu, UD_VECTOR);
7662 return 1;
7663 }
7664
Abel Gordon145c28d2013-04-18 14:36:55 +03007665 if (vmx->nested.vmxon) {
7666 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007667 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03007668 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007669
Haozhong Zhang3b840802016-06-22 14:59:54 +08007670 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007671 != VMXON_NEEDED_FEATURES) {
7672 kvm_inject_gp(vcpu, 0);
7673 return 1;
7674 }
7675
Radim Krčmářcbf71272017-05-19 15:48:51 +02007676 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08007677 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007678
7679 /*
7680 * SDM 3: 24.11.5
7681 * The first 4 bytes of VMXON region contain the supported
7682 * VMCS revision identifier
7683 *
7684 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
7685 * which replaces physical address width with 32
7686 */
7687 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7688 nested_vmx_failInvalid(vcpu);
7689 return kvm_skip_emulated_instruction(vcpu);
7690 }
7691
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02007692 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7693 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02007694 nested_vmx_failInvalid(vcpu);
7695 return kvm_skip_emulated_instruction(vcpu);
7696 }
7697 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
7698 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007699 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007700 nested_vmx_failInvalid(vcpu);
7701 return kvm_skip_emulated_instruction(vcpu);
7702 }
7703 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007704 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007705
7706 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08007707 ret = enter_vmx_operation(vcpu);
7708 if (ret)
7709 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007710
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007711 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007712 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007713}
7714
7715/*
7716 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7717 * for running VMX instructions (except VMXON, whose prerequisites are
7718 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07007719 * Note that many of these exceptions have priority over VM exits, so they
7720 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007721 */
7722static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7723{
Jim Mattson70f3aac2017-04-26 08:53:46 -07007724 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007725 kvm_queue_exception(vcpu, UD_VECTOR);
7726 return 0;
7727 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007728 return 1;
7729}
7730
David Matlack8ca44e82017-08-01 14:00:39 -07007731static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
7732{
7733 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
7734 vmcs_write64(VMCS_LINK_POINTER, -1ull);
7735}
7736
Abel Gordone7953d72013-04-18 14:37:55 +03007737static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7738{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007739 if (vmx->nested.current_vmptr == -1ull)
7740 return;
7741
Abel Gordon012f83c2013-04-18 14:39:25 +03007742 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007743 /* copy to memory all shadowed fields in case
7744 they were modified */
7745 copy_shadow_to_vmcs12(vmx);
7746 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07007747 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03007748 }
Wincy Van705699a2015-02-03 23:58:17 +08007749 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07007750
7751 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02007752 kvm_vcpu_write_guest_page(&vmx->vcpu,
7753 vmx->nested.current_vmptr >> PAGE_SHIFT,
7754 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07007755
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007756 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03007757}
7758
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007759/*
7760 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7761 * just stops using VMX.
7762 */
7763static void free_nested(struct vcpu_vmx *vmx)
7764{
Wanpeng Lib7455822017-11-22 14:04:00 -08007765 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007766 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007767
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007768 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08007769 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07007770 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07007771 vmx->nested.posted_intr_nv = -1;
7772 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007773 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07007774 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007775 vmcs_clear(vmx->vmcs01.shadow_vmcs);
7776 free_vmcs(vmx->vmcs01.shadow_vmcs);
7777 vmx->vmcs01.shadow_vmcs = NULL;
7778 }
David Matlack4f2777b2016-07-13 17:16:37 -07007779 kfree(vmx->nested.cached_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06007780 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007781 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02007782 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007783 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007784 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007785 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02007786 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007787 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007788 }
Wincy Van705699a2015-02-03 23:58:17 +08007789 if (vmx->nested.pi_desc_page) {
7790 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007791 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08007792 vmx->nested.pi_desc_page = NULL;
7793 vmx->nested.pi_desc = NULL;
7794 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007795
Jim Mattsonde3a0022017-11-27 17:22:25 -06007796 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007797}
7798
7799/* Emulate the VMXOFF instruction */
7800static int handle_vmoff(struct kvm_vcpu *vcpu)
7801{
7802 if (!nested_vmx_check_permission(vcpu))
7803 return 1;
7804 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007805 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007806 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007807}
7808
Nadav Har'El27d6c862011-05-25 23:06:59 +03007809/* Emulate the VMCLEAR instruction */
7810static int handle_vmclear(struct kvm_vcpu *vcpu)
7811{
7812 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08007813 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007814 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007815
7816 if (!nested_vmx_check_permission(vcpu))
7817 return 1;
7818
Radim Krčmářcbf71272017-05-19 15:48:51 +02007819 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03007820 return 1;
7821
Radim Krčmářcbf71272017-05-19 15:48:51 +02007822 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7823 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
7824 return kvm_skip_emulated_instruction(vcpu);
7825 }
7826
7827 if (vmptr == vmx->nested.vmxon_ptr) {
7828 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
7829 return kvm_skip_emulated_instruction(vcpu);
7830 }
7831
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007832 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03007833 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007834
Jim Mattson587d7e722017-03-02 12:41:48 -08007835 kvm_vcpu_write_guest(vcpu,
7836 vmptr + offsetof(struct vmcs12, launch_state),
7837 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03007838
Nadav Har'El27d6c862011-05-25 23:06:59 +03007839 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007840 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007841}
7842
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007843static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7844
7845/* Emulate the VMLAUNCH instruction */
7846static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7847{
7848 return nested_vmx_run(vcpu, true);
7849}
7850
7851/* Emulate the VMRESUME instruction */
7852static int handle_vmresume(struct kvm_vcpu *vcpu)
7853{
7854
7855 return nested_vmx_run(vcpu, false);
7856}
7857
Nadav Har'El49f705c2011-05-25 23:08:30 +03007858/*
7859 * Read a vmcs12 field. Since these can have varying lengths and we return
7860 * one type, we chose the biggest type (u64) and zero-extend the return value
7861 * to that size. Note that the caller, handle_vmread, might need to use only
7862 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7863 * 64-bit fields are to be returned).
7864 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007865static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7866 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03007867{
7868 short offset = vmcs_field_to_offset(field);
7869 char *p;
7870
7871 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007872 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007873
7874 p = ((char *)(get_vmcs12(vcpu))) + offset;
7875
Jim Mattsond37f4262017-12-22 12:12:16 -08007876 switch (vmcs_field_width(field)) {
7877 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007878 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007879 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007880 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007881 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007882 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007883 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007884 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007885 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007886 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007887 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007888 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007889 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007890 WARN_ON(1);
7891 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007892 }
7893}
7894
Abel Gordon20b97fe2013-04-18 14:36:25 +03007895
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007896static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7897 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03007898 short offset = vmcs_field_to_offset(field);
7899 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7900 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007901 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007902
Jim Mattsond37f4262017-12-22 12:12:16 -08007903 switch (vmcs_field_width(field)) {
7904 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007905 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007906 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007907 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007908 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007909 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007910 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007911 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007912 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007913 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007914 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007915 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007916 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007917 WARN_ON(1);
7918 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007919 }
7920
7921}
7922
Abel Gordon16f5b902013-04-18 14:38:25 +03007923static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7924{
7925 int i;
7926 unsigned long field;
7927 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007928 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007929 const u16 *fields = shadow_read_write_fields;
Mathias Krausec2bae892013-06-26 20:36:21 +02007930 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03007931
Jan Kiszka282da872014-10-08 18:05:39 +02007932 preempt_disable();
7933
Abel Gordon16f5b902013-04-18 14:38:25 +03007934 vmcs_load(shadow_vmcs);
7935
7936 for (i = 0; i < num_fields; i++) {
7937 field = fields[i];
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007938 field_value = __vmcs_readl(field);
Abel Gordon16f5b902013-04-18 14:38:25 +03007939 vmcs12_write_any(&vmx->vcpu, field, field_value);
7940 }
7941
7942 vmcs_clear(shadow_vmcs);
7943 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02007944
7945 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03007946}
7947
Abel Gordonc3114422013-04-18 14:38:55 +03007948static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7949{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007950 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02007951 shadow_read_write_fields,
7952 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03007953 };
Mathias Krausec2bae892013-06-26 20:36:21 +02007954 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03007955 max_shadow_read_write_fields,
7956 max_shadow_read_only_fields
7957 };
7958 int i, q;
7959 unsigned long field;
7960 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007961 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03007962
7963 vmcs_load(shadow_vmcs);
7964
Mathias Krausec2bae892013-06-26 20:36:21 +02007965 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03007966 for (i = 0; i < max_fields[q]; i++) {
7967 field = fields[q][i];
7968 vmcs12_read_any(&vmx->vcpu, field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007969 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03007970 }
7971 }
7972
7973 vmcs_clear(shadow_vmcs);
7974 vmcs_load(vmx->loaded_vmcs->vmcs);
7975}
7976
Nadav Har'El49f705c2011-05-25 23:08:30 +03007977/*
7978 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7979 * used before) all generate the same failure when it is missing.
7980 */
7981static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
7982{
7983 struct vcpu_vmx *vmx = to_vmx(vcpu);
7984 if (vmx->nested.current_vmptr == -1ull) {
7985 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007986 return 0;
7987 }
7988 return 1;
7989}
7990
7991static int handle_vmread(struct kvm_vcpu *vcpu)
7992{
7993 unsigned long field;
7994 u64 field_value;
7995 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7996 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7997 gva_t gva = 0;
7998
Kyle Hueyeb277562016-11-29 12:40:39 -08007999 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008000 return 1;
8001
Kyle Huey6affcbe2016-11-29 12:40:40 -08008002 if (!nested_vmx_check_vmcs12(vcpu))
8003 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008004
Nadav Har'El49f705c2011-05-25 23:08:30 +03008005 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008006 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008007 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008008 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008009 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008010 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008011 }
8012 /*
8013 * Now copy part of this value to register or memory, as requested.
8014 * Note that the number of bits actually copied is 32 or 64 depending
8015 * on the guest's mode (32 or 64 bit), not on the given field's length.
8016 */
8017 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008018 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008019 field_value);
8020 } else {
8021 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008022 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008023 return 1;
Jim Mattson70f3aac2017-04-26 08:53:46 -07008024 /* _system ok, as hardware has verified cpl=0 */
Nadav Har'El49f705c2011-05-25 23:08:30 +03008025 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
8026 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
8027 }
8028
8029 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008030 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008031}
8032
8033
8034static int handle_vmwrite(struct kvm_vcpu *vcpu)
8035{
8036 unsigned long field;
8037 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008038 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008039 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8040 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008041
Nadav Har'El49f705c2011-05-25 23:08:30 +03008042 /* The value to write might be 32 or 64 bits, depending on L1's long
8043 * mode, and eventually we need to write that into a field of several
8044 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008045 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008046 * bits into the vmcs12 field.
8047 */
8048 u64 field_value = 0;
8049 struct x86_exception e;
8050
Kyle Hueyeb277562016-11-29 12:40:39 -08008051 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008052 return 1;
8053
Kyle Huey6affcbe2016-11-29 12:40:40 -08008054 if (!nested_vmx_check_vmcs12(vcpu))
8055 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008056
Nadav Har'El49f705c2011-05-25 23:08:30 +03008057 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008058 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008059 (((vmx_instruction_info) >> 3) & 0xf));
8060 else {
8061 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008062 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008063 return 1;
8064 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
Nadav Amit27e6fb52014-06-18 17:19:26 +03008065 &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008066 kvm_inject_page_fault(vcpu, &e);
8067 return 1;
8068 }
8069 }
8070
8071
Nadav Amit27e6fb52014-06-18 17:19:26 +03008072 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008073 if (vmcs_field_readonly(field)) {
8074 nested_vmx_failValid(vcpu,
8075 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008076 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008077 }
8078
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008079 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008080 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008081 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008082 }
8083
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008084 switch (field) {
8085#define SHADOW_FIELD_RW(x) case x:
8086#include "vmx_shadow_fields.h"
8087 /*
8088 * The fields that can be updated by L1 without a vmexit are
8089 * always updated in the vmcs02, the others go down the slow
8090 * path of prepare_vmcs02.
8091 */
8092 break;
8093 default:
8094 vmx->nested.dirty_vmcs12 = true;
8095 break;
8096 }
8097
Nadav Har'El49f705c2011-05-25 23:08:30 +03008098 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008099 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008100}
8101
Jim Mattsona8bc2842016-11-30 12:03:44 -08008102static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8103{
8104 vmx->nested.current_vmptr = vmptr;
8105 if (enable_shadow_vmcs) {
8106 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8107 SECONDARY_EXEC_SHADOW_VMCS);
8108 vmcs_write64(VMCS_LINK_POINTER,
8109 __pa(vmx->vmcs01.shadow_vmcs));
8110 vmx->nested.sync_shadow_vmcs = true;
8111 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008112 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008113}
8114
Nadav Har'El63846662011-05-25 23:07:29 +03008115/* Emulate the VMPTRLD instruction */
8116static int handle_vmptrld(struct kvm_vcpu *vcpu)
8117{
8118 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008119 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008120
8121 if (!nested_vmx_check_permission(vcpu))
8122 return 1;
8123
Radim Krčmářcbf71272017-05-19 15:48:51 +02008124 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008125 return 1;
8126
Radim Krčmářcbf71272017-05-19 15:48:51 +02008127 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8128 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8129 return kvm_skip_emulated_instruction(vcpu);
8130 }
8131
8132 if (vmptr == vmx->nested.vmxon_ptr) {
8133 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8134 return kvm_skip_emulated_instruction(vcpu);
8135 }
8136
Nadav Har'El63846662011-05-25 23:07:29 +03008137 if (vmx->nested.current_vmptr != vmptr) {
8138 struct vmcs12 *new_vmcs12;
8139 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008140 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8141 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008142 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008143 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008144 }
8145 new_vmcs12 = kmap(page);
8146 if (new_vmcs12->revision_id != VMCS12_REVISION) {
8147 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008148 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008149 nested_vmx_failValid(vcpu,
8150 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008151 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008152 }
Nadav Har'El63846662011-05-25 23:07:29 +03008153
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008154 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008155 /*
8156 * Load VMCS12 from guest memory since it is not already
8157 * cached.
8158 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008159 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8160 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008161 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008162
Jim Mattsona8bc2842016-11-30 12:03:44 -08008163 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008164 }
8165
8166 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008167 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008168}
8169
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008170/* Emulate the VMPTRST instruction */
8171static int handle_vmptrst(struct kvm_vcpu *vcpu)
8172{
8173 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8174 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8175 gva_t vmcs_gva;
8176 struct x86_exception e;
8177
8178 if (!nested_vmx_check_permission(vcpu))
8179 return 1;
8180
8181 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008182 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008183 return 1;
Jim Mattson70f3aac2017-04-26 08:53:46 -07008184 /* ok to use *_system, as hardware has verified cpl=0 */
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008185 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
8186 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8187 sizeof(u64), &e)) {
8188 kvm_inject_page_fault(vcpu, &e);
8189 return 1;
8190 }
8191 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008192 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008193}
8194
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008195/* Emulate the INVEPT instruction */
8196static int handle_invept(struct kvm_vcpu *vcpu)
8197{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008198 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008199 u32 vmx_instruction_info, types;
8200 unsigned long type;
8201 gva_t gva;
8202 struct x86_exception e;
8203 struct {
8204 u64 eptp, gpa;
8205 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008206
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008207 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008208 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008209 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008210 kvm_queue_exception(vcpu, UD_VECTOR);
8211 return 1;
8212 }
8213
8214 if (!nested_vmx_check_permission(vcpu))
8215 return 1;
8216
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008217 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008218 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008219
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008220 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008221
Jim Mattson85c856b2016-10-26 08:38:38 -07008222 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008223 nested_vmx_failValid(vcpu,
8224 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008225 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008226 }
8227
8228 /* According to the Intel VMX instruction reference, the memory
8229 * operand is read even if it isn't needed (e.g., for type==global)
8230 */
8231 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008232 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008233 return 1;
8234 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
8235 sizeof(operand), &e)) {
8236 kvm_inject_page_fault(vcpu, &e);
8237 return 1;
8238 }
8239
8240 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008241 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008242 /*
8243 * TODO: track mappings and invalidate
8244 * single context requests appropriately
8245 */
8246 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008247 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008248 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008249 nested_vmx_succeed(vcpu);
8250 break;
8251 default:
8252 BUG_ON(1);
8253 break;
8254 }
8255
Kyle Huey6affcbe2016-11-29 12:40:40 -08008256 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008257}
8258
Petr Matouseka642fc32014-09-23 20:22:30 +02008259static int handle_invvpid(struct kvm_vcpu *vcpu)
8260{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008261 struct vcpu_vmx *vmx = to_vmx(vcpu);
8262 u32 vmx_instruction_info;
8263 unsigned long type, types;
8264 gva_t gva;
8265 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008266 struct {
8267 u64 vpid;
8268 u64 gla;
8269 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008270
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008271 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008272 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008273 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008274 kvm_queue_exception(vcpu, UD_VECTOR);
8275 return 1;
8276 }
8277
8278 if (!nested_vmx_check_permission(vcpu))
8279 return 1;
8280
8281 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8282 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8283
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008284 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008285 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008286
Jim Mattson85c856b2016-10-26 08:38:38 -07008287 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008288 nested_vmx_failValid(vcpu,
8289 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008290 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008291 }
8292
8293 /* according to the intel vmx instruction reference, the memory
8294 * operand is read even if it isn't needed (e.g., for type==global)
8295 */
8296 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8297 vmx_instruction_info, false, &gva))
8298 return 1;
Jim Mattson40352602017-06-28 09:37:37 -07008299 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
8300 sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008301 kvm_inject_page_fault(vcpu, &e);
8302 return 1;
8303 }
Jim Mattson40352602017-06-28 09:37:37 -07008304 if (operand.vpid >> 16) {
8305 nested_vmx_failValid(vcpu,
8306 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8307 return kvm_skip_emulated_instruction(vcpu);
8308 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008309
8310 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008311 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Yu Zhangfd8cb432017-08-24 20:27:56 +08008312 if (is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07008313 nested_vmx_failValid(vcpu,
8314 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8315 return kvm_skip_emulated_instruction(vcpu);
8316 }
8317 /* fall through */
Paolo Bonzinief697a72016-03-18 16:58:38 +01008318 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008319 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07008320 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008321 nested_vmx_failValid(vcpu,
8322 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008323 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008324 }
8325 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008326 case VMX_VPID_EXTENT_ALL_CONTEXT:
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008327 break;
8328 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008329 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008330 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008331 }
8332
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08008333 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008334 nested_vmx_succeed(vcpu);
8335
Kyle Huey6affcbe2016-11-29 12:40:40 -08008336 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02008337}
8338
Kai Huang843e4332015-01-28 10:54:28 +08008339static int handle_pml_full(struct kvm_vcpu *vcpu)
8340{
8341 unsigned long exit_qualification;
8342
8343 trace_kvm_pml_full(vcpu->vcpu_id);
8344
8345 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8346
8347 /*
8348 * PML buffer FULL happened while executing iret from NMI,
8349 * "blocked by NMI" bit has to be set before next VM entry.
8350 */
8351 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008352 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08008353 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8354 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8355 GUEST_INTR_STATE_NMI);
8356
8357 /*
8358 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8359 * here.., and there's no userspace involvement needed for PML.
8360 */
8361 return 1;
8362}
8363
Yunhong Jiang64672c92016-06-13 14:19:59 -07008364static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8365{
8366 kvm_lapic_expired_hv_timer(vcpu);
8367 return 1;
8368}
8369
Bandan Das41ab9372017-08-03 15:54:43 -04008370static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8371{
8372 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04008373 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8374
8375 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008376 switch (address & VMX_EPTP_MT_MASK) {
8377 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008378 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008379 return false;
8380 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02008381 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008382 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008383 return false;
8384 break;
8385 default:
8386 return false;
8387 }
8388
David Hildenbrandbb97a012017-08-10 23:15:28 +02008389 /* only 4 levels page-walk length are valid */
8390 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04008391 return false;
8392
8393 /* Reserved bits should not be set */
8394 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8395 return false;
8396
8397 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008398 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008399 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008400 return false;
8401 }
8402
8403 return true;
8404}
8405
8406static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8407 struct vmcs12 *vmcs12)
8408{
8409 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8410 u64 address;
8411 bool accessed_dirty;
8412 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8413
8414 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8415 !nested_cpu_has_ept(vmcs12))
8416 return 1;
8417
8418 if (index >= VMFUNC_EPTP_ENTRIES)
8419 return 1;
8420
8421
8422 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8423 &address, index * 8, 8))
8424 return 1;
8425
David Hildenbrandbb97a012017-08-10 23:15:28 +02008426 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04008427
8428 /*
8429 * If the (L2) guest does a vmfunc to the currently
8430 * active ept pointer, we don't have to do anything else
8431 */
8432 if (vmcs12->ept_pointer != address) {
8433 if (!valid_ept_address(vcpu, address))
8434 return 1;
8435
8436 kvm_mmu_unload(vcpu);
8437 mmu->ept_ad = accessed_dirty;
8438 mmu->base_role.ad_disabled = !accessed_dirty;
8439 vmcs12->ept_pointer = address;
8440 /*
8441 * TODO: Check what's the correct approach in case
8442 * mmu reload fails. Currently, we just let the next
8443 * reload potentially fail
8444 */
8445 kvm_mmu_reload(vcpu);
8446 }
8447
8448 return 0;
8449}
8450
Bandan Das2a499e42017-08-03 15:54:41 -04008451static int handle_vmfunc(struct kvm_vcpu *vcpu)
8452{
Bandan Das27c42a12017-08-03 15:54:42 -04008453 struct vcpu_vmx *vmx = to_vmx(vcpu);
8454 struct vmcs12 *vmcs12;
8455 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8456
8457 /*
8458 * VMFUNC is only supported for nested guests, but we always enable the
8459 * secondary control for simplicity; for non-nested mode, fake that we
8460 * didn't by injecting #UD.
8461 */
8462 if (!is_guest_mode(vcpu)) {
8463 kvm_queue_exception(vcpu, UD_VECTOR);
8464 return 1;
8465 }
8466
8467 vmcs12 = get_vmcs12(vcpu);
8468 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8469 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04008470
8471 switch (function) {
8472 case 0:
8473 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8474 goto fail;
8475 break;
8476 default:
8477 goto fail;
8478 }
8479 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04008480
8481fail:
8482 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8483 vmcs_read32(VM_EXIT_INTR_INFO),
8484 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04008485 return 1;
8486}
8487
Nadav Har'El0140cae2011-05-25 23:06:28 +03008488/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08008489 * The exit handlers return 1 if the exit was handled fully and guest execution
8490 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8491 * to be done to userspace and return 0.
8492 */
Mathias Krause772e0312012-08-30 01:30:19 +02008493static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08008494 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8495 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08008496 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08008497 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008498 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008499 [EXIT_REASON_CR_ACCESS] = handle_cr,
8500 [EXIT_REASON_DR_ACCESS] = handle_dr,
8501 [EXIT_REASON_CPUID] = handle_cpuid,
8502 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8503 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8504 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8505 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02008506 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03008507 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02008508 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02008509 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03008510 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008511 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03008512 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008513 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008514 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008515 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008516 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008517 [EXIT_REASON_VMOFF] = handle_vmoff,
8518 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08008519 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8520 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08008521 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008522 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02008523 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08008524 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02008525 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08008526 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02008527 [EXIT_REASON_GDTR_IDTR] = handle_desc,
8528 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03008529 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8530 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008531 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008532 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008533 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008534 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008535 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02008536 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07008537 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07008538 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08008539 [EXIT_REASON_XSAVES] = handle_xsaves,
8540 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08008541 [EXIT_REASON_PML_FULL] = handle_pml_full,
Bandan Das2a499e42017-08-03 15:54:41 -04008542 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07008543 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008544};
8545
8546static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04008547 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008548
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008549static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8550 struct vmcs12 *vmcs12)
8551{
8552 unsigned long exit_qualification;
8553 gpa_t bitmap, last_bitmap;
8554 unsigned int port;
8555 int size;
8556 u8 b;
8557
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008558 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05008559 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008560
8561 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8562
8563 port = exit_qualification >> 16;
8564 size = (exit_qualification & 7) + 1;
8565
8566 last_bitmap = (gpa_t)-1;
8567 b = -1;
8568
8569 while (size > 0) {
8570 if (port < 0x8000)
8571 bitmap = vmcs12->io_bitmap_a;
8572 else if (port < 0x10000)
8573 bitmap = vmcs12->io_bitmap_b;
8574 else
Joe Perches1d804d02015-03-30 16:46:09 -07008575 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008576 bitmap += (port & 0x7fff) / 8;
8577
8578 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008579 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008580 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008581 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07008582 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008583
8584 port++;
8585 size--;
8586 last_bitmap = bitmap;
8587 }
8588
Joe Perches1d804d02015-03-30 16:46:09 -07008589 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008590}
8591
Nadav Har'El644d7112011-05-25 23:12:35 +03008592/*
8593 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8594 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8595 * disinterest in the current event (read or write a specific MSR) by using an
8596 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8597 */
8598static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8599 struct vmcs12 *vmcs12, u32 exit_reason)
8600{
8601 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8602 gpa_t bitmap;
8603
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01008604 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07008605 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008606
8607 /*
8608 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8609 * for the four combinations of read/write and low/high MSR numbers.
8610 * First we need to figure out which of the four to use:
8611 */
8612 bitmap = vmcs12->msr_bitmap;
8613 if (exit_reason == EXIT_REASON_MSR_WRITE)
8614 bitmap += 2048;
8615 if (msr_index >= 0xc0000000) {
8616 msr_index -= 0xc0000000;
8617 bitmap += 1024;
8618 }
8619
8620 /* Then read the msr_index'th bit from this bitmap: */
8621 if (msr_index < 1024*8) {
8622 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008623 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008624 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008625 return 1 & (b >> (msr_index & 7));
8626 } else
Joe Perches1d804d02015-03-30 16:46:09 -07008627 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03008628}
8629
8630/*
8631 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8632 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8633 * intercept (via guest_host_mask etc.) the current event.
8634 */
8635static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8636 struct vmcs12 *vmcs12)
8637{
8638 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8639 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008640 int reg;
8641 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03008642
8643 switch ((exit_qualification >> 4) & 3) {
8644 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008645 reg = (exit_qualification >> 8) & 15;
8646 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03008647 switch (cr) {
8648 case 0:
8649 if (vmcs12->cr0_guest_host_mask &
8650 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008651 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008652 break;
8653 case 3:
8654 if ((vmcs12->cr3_target_count >= 1 &&
8655 vmcs12->cr3_target_value0 == val) ||
8656 (vmcs12->cr3_target_count >= 2 &&
8657 vmcs12->cr3_target_value1 == val) ||
8658 (vmcs12->cr3_target_count >= 3 &&
8659 vmcs12->cr3_target_value2 == val) ||
8660 (vmcs12->cr3_target_count >= 4 &&
8661 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07008662 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008663 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008664 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008665 break;
8666 case 4:
8667 if (vmcs12->cr4_guest_host_mask &
8668 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07008669 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008670 break;
8671 case 8:
8672 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008673 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008674 break;
8675 }
8676 break;
8677 case 2: /* clts */
8678 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
8679 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008680 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008681 break;
8682 case 1: /* mov from cr */
8683 switch (cr) {
8684 case 3:
8685 if (vmcs12->cpu_based_vm_exec_control &
8686 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008687 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008688 break;
8689 case 8:
8690 if (vmcs12->cpu_based_vm_exec_control &
8691 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008692 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008693 break;
8694 }
8695 break;
8696 case 3: /* lmsw */
8697 /*
8698 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
8699 * cr0. Other attempted changes are ignored, with no exit.
8700 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008701 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03008702 if (vmcs12->cr0_guest_host_mask & 0xe &
8703 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008704 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008705 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
8706 !(vmcs12->cr0_read_shadow & 0x1) &&
8707 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07008708 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008709 break;
8710 }
Joe Perches1d804d02015-03-30 16:46:09 -07008711 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008712}
8713
8714/*
8715 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8716 * should handle it ourselves in L0 (and then continue L2). Only call this
8717 * when in is_guest_mode (L2).
8718 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02008719static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03008720{
Nadav Har'El644d7112011-05-25 23:12:35 +03008721 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8722 struct vcpu_vmx *vmx = to_vmx(vcpu);
8723 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8724
Jim Mattson4f350c62017-09-14 16:31:44 -07008725 if (vmx->nested.nested_run_pending)
8726 return false;
8727
8728 if (unlikely(vmx->fail)) {
8729 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8730 vmcs_read32(VM_INSTRUCTION_ERROR));
8731 return true;
8732 }
Jan Kiszka542060e2014-01-04 18:47:21 +01008733
David Matlackc9f04402017-08-01 14:00:40 -07008734 /*
8735 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06008736 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
8737 * Page). The CPU may write to these pages via their host
8738 * physical address while L2 is running, bypassing any
8739 * address-translation-based dirty tracking (e.g. EPT write
8740 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07008741 *
8742 * Mark them dirty on every exit from L2 to prevent them from
8743 * getting out of sync with dirty tracking.
8744 */
8745 nested_mark_vmcs12_pages_dirty(vcpu);
8746
Jim Mattson4f350c62017-09-14 16:31:44 -07008747 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
8748 vmcs_readl(EXIT_QUALIFICATION),
8749 vmx->idt_vectoring_info,
8750 intr_info,
8751 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8752 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03008753
8754 switch (exit_reason) {
8755 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08008756 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07008757 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008758 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07008759 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01008760 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01008761 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008762 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01008763 else if (is_debug(intr_info) &&
8764 vcpu->guest_debug &
8765 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8766 return false;
8767 else if (is_breakpoint(intr_info) &&
8768 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8769 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008770 return vmcs12->exception_bitmap &
8771 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8772 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07008773 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008774 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07008775 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008776 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008777 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008778 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008779 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008780 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07008781 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008782 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07008783 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008784 case EXIT_REASON_HLT:
8785 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8786 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07008787 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008788 case EXIT_REASON_INVLPG:
8789 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8790 case EXIT_REASON_RDPMC:
8791 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02008792 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02008793 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02008794 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02008795 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01008796 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03008797 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8798 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8799 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8800 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8801 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8802 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02008803 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03008804 /*
8805 * VMX instructions trap unconditionally. This allows L1 to
8806 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8807 */
Joe Perches1d804d02015-03-30 16:46:09 -07008808 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008809 case EXIT_REASON_CR_ACCESS:
8810 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8811 case EXIT_REASON_DR_ACCESS:
8812 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8813 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008814 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02008815 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
8816 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03008817 case EXIT_REASON_MSR_READ:
8818 case EXIT_REASON_MSR_WRITE:
8819 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8820 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07008821 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008822 case EXIT_REASON_MWAIT_INSTRUCTION:
8823 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008824 case EXIT_REASON_MONITOR_TRAP_FLAG:
8825 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03008826 case EXIT_REASON_MONITOR_INSTRUCTION:
8827 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8828 case EXIT_REASON_PAUSE_INSTRUCTION:
8829 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8830 nested_cpu_has2(vmcs12,
8831 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8832 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07008833 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008834 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008835 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03008836 case EXIT_REASON_APIC_ACCESS:
8837 return nested_cpu_has2(vmcs12,
8838 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Wincy Van82f0dd42015-02-03 23:57:18 +08008839 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08008840 case EXIT_REASON_EOI_INDUCED:
8841 /* apic_write and eoi_induced should exit unconditionally. */
Joe Perches1d804d02015-03-30 16:46:09 -07008842 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008843 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008844 /*
8845 * L0 always deals with the EPT violation. If nested EPT is
8846 * used, and the nested mmu code discovers that the address is
8847 * missing in the guest EPT table (EPT12), the EPT violation
8848 * will be injected with nested_ept_inject_page_fault()
8849 */
Joe Perches1d804d02015-03-30 16:46:09 -07008850 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008851 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008852 /*
8853 * L2 never uses directly L1's EPT, but rather L0's own EPT
8854 * table (shadow on EPT) or a merged EPT table that L0 built
8855 * (EPT on EPT). So any problems with the structure of the
8856 * table is L0's fault.
8857 */
Joe Perches1d804d02015-03-30 16:46:09 -07008858 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02008859 case EXIT_REASON_INVPCID:
8860 return
8861 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
8862 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008863 case EXIT_REASON_WBINVD:
8864 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8865 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07008866 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08008867 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8868 /*
8869 * This should never happen, since it is not possible to
8870 * set XSS to a non-zero value---neither in L1 nor in L2.
8871 * If if it were, XSS would have to be checked against
8872 * the XSS exit bitmap in vmcs12.
8873 */
8874 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08008875 case EXIT_REASON_PREEMPTION_TIMER:
8876 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02008877 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04008878 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02008879 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04008880 case EXIT_REASON_VMFUNC:
8881 /* VM functions are emulated through L2->L0 vmexits. */
8882 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008883 default:
Joe Perches1d804d02015-03-30 16:46:09 -07008884 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008885 }
8886}
8887
Paolo Bonzini7313c692017-07-27 10:31:25 +02008888static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
8889{
8890 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8891
8892 /*
8893 * At this point, the exit interruption info in exit_intr_info
8894 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
8895 * we need to query the in-kernel LAPIC.
8896 */
8897 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
8898 if ((exit_intr_info &
8899 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
8900 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
8901 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8902 vmcs12->vm_exit_intr_error_code =
8903 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
8904 }
8905
8906 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
8907 vmcs_readl(EXIT_QUALIFICATION));
8908 return 1;
8909}
8910
Avi Kivity586f9602010-11-18 13:09:54 +02008911static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8912{
8913 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8914 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8915}
8916
Kai Huanga3eaa862015-11-04 13:46:05 +08008917static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08008918{
Kai Huanga3eaa862015-11-04 13:46:05 +08008919 if (vmx->pml_pg) {
8920 __free_page(vmx->pml_pg);
8921 vmx->pml_pg = NULL;
8922 }
Kai Huang843e4332015-01-28 10:54:28 +08008923}
8924
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008925static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08008926{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008927 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008928 u64 *pml_buf;
8929 u16 pml_idx;
8930
8931 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8932
8933 /* Do nothing if PML buffer is empty */
8934 if (pml_idx == (PML_ENTITY_NUM - 1))
8935 return;
8936
8937 /* PML index always points to next available PML buffer entity */
8938 if (pml_idx >= PML_ENTITY_NUM)
8939 pml_idx = 0;
8940 else
8941 pml_idx++;
8942
8943 pml_buf = page_address(vmx->pml_pg);
8944 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8945 u64 gpa;
8946
8947 gpa = pml_buf[pml_idx];
8948 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008949 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08008950 }
8951
8952 /* reset PML index */
8953 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8954}
8955
8956/*
8957 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8958 * Called before reporting dirty_bitmap to userspace.
8959 */
8960static void kvm_flush_pml_buffers(struct kvm *kvm)
8961{
8962 int i;
8963 struct kvm_vcpu *vcpu;
8964 /*
8965 * We only need to kick vcpu out of guest mode here, as PML buffer
8966 * is flushed at beginning of all VMEXITs, and it's obvious that only
8967 * vcpus running in guest are possible to have unflushed GPAs in PML
8968 * buffer.
8969 */
8970 kvm_for_each_vcpu(i, vcpu, kvm)
8971 kvm_vcpu_kick(vcpu);
8972}
8973
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008974static void vmx_dump_sel(char *name, uint32_t sel)
8975{
8976 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05008977 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008978 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8979 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
8980 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
8981}
8982
8983static void vmx_dump_dtsel(char *name, uint32_t limit)
8984{
8985 pr_err("%s limit=0x%08x, base=0x%016lx\n",
8986 name, vmcs_read32(limit),
8987 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
8988}
8989
8990static void dump_vmcs(void)
8991{
8992 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
8993 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
8994 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
8995 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
8996 u32 secondary_exec_control = 0;
8997 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01008998 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008999 int i, n;
9000
9001 if (cpu_has_secondary_exec_ctrls())
9002 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9003
9004 pr_err("*** Guest State ***\n");
9005 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9006 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9007 vmcs_readl(CR0_GUEST_HOST_MASK));
9008 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9009 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9010 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9011 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9012 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9013 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009014 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9015 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9016 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9017 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009018 }
9019 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9020 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9021 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9022 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9023 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9024 vmcs_readl(GUEST_SYSENTER_ESP),
9025 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9026 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9027 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9028 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9029 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9030 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9031 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9032 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9033 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9034 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9035 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9036 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9037 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009038 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9039 efer, vmcs_read64(GUEST_IA32_PAT));
9040 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9041 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009042 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009043 if (cpu_has_load_perf_global_ctrl &&
9044 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009045 pr_err("PerfGlobCtl = 0x%016llx\n",
9046 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009047 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009048 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009049 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9050 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9051 vmcs_read32(GUEST_ACTIVITY_STATE));
9052 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9053 pr_err("InterruptStatus = %04x\n",
9054 vmcs_read16(GUEST_INTR_STATUS));
9055
9056 pr_err("*** Host State ***\n");
9057 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9058 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9059 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9060 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9061 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9062 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9063 vmcs_read16(HOST_TR_SELECTOR));
9064 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9065 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9066 vmcs_readl(HOST_TR_BASE));
9067 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9068 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9069 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9070 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9071 vmcs_readl(HOST_CR4));
9072 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9073 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9074 vmcs_read32(HOST_IA32_SYSENTER_CS),
9075 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9076 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009077 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9078 vmcs_read64(HOST_IA32_EFER),
9079 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009080 if (cpu_has_load_perf_global_ctrl &&
9081 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009082 pr_err("PerfGlobCtl = 0x%016llx\n",
9083 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009084
9085 pr_err("*** Control State ***\n");
9086 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9087 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9088 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9089 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9090 vmcs_read32(EXCEPTION_BITMAP),
9091 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9092 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9093 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9094 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9095 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9096 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9097 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9098 vmcs_read32(VM_EXIT_INTR_INFO),
9099 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9100 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9101 pr_err(" reason=%08x qualification=%016lx\n",
9102 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9103 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9104 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9105 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009106 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009107 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009108 pr_err("TSC Multiplier = 0x%016llx\n",
9109 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009110 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9111 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9112 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9113 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9114 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009115 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009116 n = vmcs_read32(CR3_TARGET_COUNT);
9117 for (i = 0; i + 1 < n; i += 4)
9118 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9119 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9120 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9121 if (i < n)
9122 pr_err("CR3 target%u=%016lx\n",
9123 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9124 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9125 pr_err("PLE Gap=%08x Window=%08x\n",
9126 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9127 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9128 pr_err("Virtual processor ID = 0x%04x\n",
9129 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9130}
9131
Avi Kivity6aa8b732006-12-10 02:21:36 -08009132/*
9133 * The guest has exited. See if we can fix it or if we need userspace
9134 * assistance.
9135 */
Avi Kivity851ba692009-08-24 11:10:17 +03009136static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009137{
Avi Kivity29bd8a72007-09-10 17:27:03 +03009138 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08009139 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02009140 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03009141
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01009142 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
9143
Kai Huang843e4332015-01-28 10:54:28 +08009144 /*
9145 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
9146 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
9147 * querying dirty_bitmap, we only need to kick all vcpus out of guest
9148 * mode as if vcpus is in root mode, the PML buffer must has been
9149 * flushed already.
9150 */
9151 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009152 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009153
Mohammed Gamal80ced182009-09-01 12:48:18 +02009154 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02009155 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02009156 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01009157
Paolo Bonzini7313c692017-07-27 10:31:25 +02009158 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
9159 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03009160
Mohammed Gamal51207022010-05-31 22:40:54 +03009161 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009162 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03009163 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9164 vcpu->run->fail_entry.hardware_entry_failure_reason
9165 = exit_reason;
9166 return 0;
9167 }
9168
Avi Kivity29bd8a72007-09-10 17:27:03 +03009169 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009170 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9171 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03009172 = vmcs_read32(VM_INSTRUCTION_ERROR);
9173 return 0;
9174 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009175
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009176 /*
9177 * Note:
9178 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9179 * delivery event since it indicates guest is accessing MMIO.
9180 * The vm-exit can be triggered again after return to guest that
9181 * will cause infinite loop.
9182 */
Mike Dayd77c26f2007-10-08 09:02:08 -04009183 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08009184 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02009185 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00009186 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009187 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9188 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9189 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009190 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009191 vcpu->run->internal.data[0] = vectoring_info;
9192 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009193 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9194 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9195 vcpu->run->internal.ndata++;
9196 vcpu->run->internal.data[3] =
9197 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9198 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009199 return 0;
9200 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009201
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009202 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009203 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9204 if (vmx_interrupt_allowed(vcpu)) {
9205 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9206 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9207 vcpu->arch.nmi_pending) {
9208 /*
9209 * This CPU don't support us in finding the end of an
9210 * NMI-blocked window if the guest runs with IRQs
9211 * disabled. So we pull the trigger after 1 s of
9212 * futile waiting, but inform the user about this.
9213 */
9214 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9215 "state on VCPU %d after 1 s timeout\n",
9216 __func__, vcpu->vcpu_id);
9217 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9218 }
9219 }
9220
Avi Kivity6aa8b732006-12-10 02:21:36 -08009221 if (exit_reason < kvm_vmx_max_exit_handlers
9222 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03009223 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009224 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01009225 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9226 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03009227 kvm_queue_exception(vcpu, UD_VECTOR);
9228 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009229 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009230}
9231
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009232static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009233{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009234 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9235
9236 if (is_guest_mode(vcpu) &&
9237 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9238 return;
9239
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009240 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009241 vmcs_write32(TPR_THRESHOLD, 0);
9242 return;
9243 }
9244
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009245 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009246}
9247
Yang Zhang8d146952013-01-25 10:18:50 +08009248static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
9249{
9250 u32 sec_exec_control;
9251
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009252 /* Postpone execution until vmcs01 is the current VMCS. */
9253 if (is_guest_mode(vcpu)) {
9254 to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
9255 return;
9256 }
9257
Wanpeng Lif6e90f92016-09-22 07:43:25 +08009258 if (!cpu_has_vmx_virtualize_x2apic_mode())
Yang Zhang8d146952013-01-25 10:18:50 +08009259 return;
9260
Paolo Bonzini35754c92015-07-29 12:05:37 +02009261 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08009262 return;
9263
9264 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9265
9266 if (set) {
9267 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9268 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9269 } else {
9270 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9271 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Junaid Shahida468f2d2018-04-26 13:09:50 -07009272 vmx_flush_tlb(vcpu, true);
Yang Zhang8d146952013-01-25 10:18:50 +08009273 }
9274 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9275
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009276 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009277}
9278
Tang Chen38b99172014-09-24 15:57:54 +08009279static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9280{
9281 struct vcpu_vmx *vmx = to_vmx(vcpu);
9282
9283 /*
9284 * Currently we do not handle the nested case where L2 has an
9285 * APIC access page of its own; that page is still pinned.
9286 * Hence, we skip the case where the VCPU is in guest mode _and_
9287 * L1 prepared an APIC access page for L2.
9288 *
9289 * For the case where L1 and L2 share the same APIC access page
9290 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
9291 * in the vmcs12), this function will only update either the vmcs01
9292 * or the vmcs02. If the former, the vmcs02 will be updated by
9293 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
9294 * the next L2->L1 exit.
9295 */
9296 if (!is_guest_mode(vcpu) ||
David Matlack4f2777b2016-07-13 17:16:37 -07009297 !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009298 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Tang Chen38b99172014-09-24 15:57:54 +08009299 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -07009300 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009301 }
Tang Chen38b99172014-09-24 15:57:54 +08009302}
9303
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009304static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009305{
9306 u16 status;
9307 u8 old;
9308
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009309 if (max_isr == -1)
9310 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009311
9312 status = vmcs_read16(GUEST_INTR_STATUS);
9313 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009314 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08009315 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009316 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009317 vmcs_write16(GUEST_INTR_STATUS, status);
9318 }
9319}
9320
9321static void vmx_set_rvi(int vector)
9322{
9323 u16 status;
9324 u8 old;
9325
Wei Wang4114c272014-11-05 10:53:43 +08009326 if (vector == -1)
9327 vector = 0;
9328
Yang Zhangc7c9c562013-01-25 10:18:51 +08009329 status = vmcs_read16(GUEST_INTR_STATUS);
9330 old = (u8)status & 0xff;
9331 if ((u8)vector != old) {
9332 status &= ~0xff;
9333 status |= (u8)vector;
9334 vmcs_write16(GUEST_INTR_STATUS, status);
9335 }
9336}
9337
9338static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9339{
Liran Alon851c1a182017-12-24 18:12:56 +02009340 /*
9341 * When running L2, updating RVI is only relevant when
9342 * vmcs12 virtual-interrupt-delivery enabled.
9343 * However, it can be enabled only when L1 also
9344 * intercepts external-interrupts and in that case
9345 * we should not update vmcs02 RVI but instead intercept
9346 * interrupt. Therefore, do nothing when running L2.
9347 */
9348 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08009349 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009350}
9351
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009352static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009353{
9354 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009355 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02009356 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009357
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009358 WARN_ON(!vcpu->arch.apicv_active);
9359 if (pi_test_on(&vmx->pi_desc)) {
9360 pi_clear_on(&vmx->pi_desc);
9361 /*
9362 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9363 * But on x86 this is just a compiler barrier anyway.
9364 */
9365 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02009366 max_irr_updated =
9367 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
9368
9369 /*
9370 * If we are running L2 and L1 has a new pending interrupt
9371 * which can be injected, we should re-evaluate
9372 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02009373 * If L1 intercepts external-interrupts, we should
9374 * exit from L2 to L1. Otherwise, interrupt should be
9375 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02009376 */
Liran Alon851c1a182017-12-24 18:12:56 +02009377 if (is_guest_mode(vcpu) && max_irr_updated) {
9378 if (nested_exit_on_intr(vcpu))
9379 kvm_vcpu_exiting_guest_mode(vcpu);
9380 else
9381 kvm_make_request(KVM_REQ_EVENT, vcpu);
9382 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009383 } else {
9384 max_irr = kvm_lapic_find_highest_irr(vcpu);
9385 }
9386 vmx_hwapic_irr_update(vcpu, max_irr);
9387 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009388}
9389
Andrey Smetanin63086302015-11-10 15:36:32 +03009390static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009391{
Andrey Smetanind62caab2015-11-10 15:36:33 +03009392 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08009393 return;
9394
Yang Zhangc7c9c562013-01-25 10:18:51 +08009395 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9396 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9397 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9398 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9399}
9400
Paolo Bonzini967235d2016-12-19 14:03:45 +01009401static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9402{
9403 struct vcpu_vmx *vmx = to_vmx(vcpu);
9404
9405 pi_clear_on(&vmx->pi_desc);
9406 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9407}
9408
Avi Kivity51aa01d2010-07-20 14:31:20 +03009409static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03009410{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009411 u32 exit_intr_info = 0;
9412 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02009413
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009414 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9415 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02009416 return;
9417
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009418 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9419 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9420 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08009421
Wanpeng Li1261bfa2017-07-13 18:30:40 -07009422 /* if exit due to PF check for async PF */
9423 if (is_page_fault(exit_intr_info))
9424 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9425
Andi Kleena0861c02009-06-08 17:37:09 +08009426 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009427 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9428 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08009429 kvm_machine_check();
9430
Gleb Natapov20f65982009-05-11 13:35:55 +03009431 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08009432 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -07009433 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03009434 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -07009435 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009436 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03009437}
Gleb Natapov20f65982009-05-11 13:35:55 +03009438
Yang Zhanga547c6d2013-04-11 19:25:10 +08009439static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9440{
9441 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9442
Yang Zhanga547c6d2013-04-11 19:25:10 +08009443 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9444 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9445 unsigned int vector;
9446 unsigned long entry;
9447 gate_desc *desc;
9448 struct vcpu_vmx *vmx = to_vmx(vcpu);
9449#ifdef CONFIG_X86_64
9450 unsigned long tmp;
9451#endif
9452
9453 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9454 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02009455 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009456 asm volatile(
9457#ifdef CONFIG_X86_64
9458 "mov %%" _ASM_SP ", %[sp]\n\t"
9459 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
9460 "push $%c[ss]\n\t"
9461 "push %[sp]\n\t"
9462#endif
9463 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08009464 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009465 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08009466 :
9467#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06009468 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009469#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -05009470 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +08009471 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009472 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009473 [ss]"i"(__KERNEL_DS),
9474 [cs]"i"(__KERNEL_CS)
9475 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02009476 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08009477}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009478STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009479
Tom Lendackybc226f02018-05-10 22:06:39 +02009480static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009481{
Tom Lendackybc226f02018-05-10 22:06:39 +02009482 switch (index) {
9483 case MSR_IA32_SMBASE:
9484 /*
9485 * We cannot do SMM unless we can run the guest in big
9486 * real mode.
9487 */
9488 return enable_unrestricted_guest || emulate_invalid_guest_state;
9489 case MSR_AMD64_VIRT_SPEC_CTRL:
9490 /* This is AMD only. */
9491 return false;
9492 default:
9493 return true;
9494 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009495}
9496
Liu, Jinsongda8999d2014-02-24 10:55:46 +00009497static bool vmx_mpx_supported(void)
9498{
9499 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
9500 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
9501}
9502
Wanpeng Li55412b22014-12-02 19:21:30 +08009503static bool vmx_xsaves_supported(void)
9504{
9505 return vmcs_config.cpu_based_2nd_exec_ctrl &
9506 SECONDARY_EXEC_XSAVES;
9507}
9508
Paolo Bonzini66336ca2016-07-12 10:36:41 +02009509static bool vmx_umip_emulated(void)
9510{
Paolo Bonzini0367f202016-07-12 10:44:55 +02009511 return vmcs_config.cpu_based_2nd_exec_ctrl &
9512 SECONDARY_EXEC_DESC;
Paolo Bonzini66336ca2016-07-12 10:36:41 +02009513}
9514
Avi Kivity51aa01d2010-07-20 14:31:20 +03009515static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9516{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02009517 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03009518 bool unblock_nmi;
9519 u8 vector;
9520 bool idtv_info_valid;
9521
9522 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03009523
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009524 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009525 if (vmx->loaded_vmcs->nmi_known_unmasked)
9526 return;
9527 /*
9528 * Can't use vmx->exit_intr_info since we're not sure what
9529 * the exit reason is.
9530 */
9531 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9532 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9533 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9534 /*
9535 * SDM 3: 27.7.1.2 (September 2008)
9536 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9537 * a guest IRET fault.
9538 * SDM 3: 23.2.2 (September 2008)
9539 * Bit 12 is undefined in any of the following cases:
9540 * If the VM exit sets the valid bit in the IDT-vectoring
9541 * information field.
9542 * If the VM exit is due to a double fault.
9543 */
9544 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9545 vector != DF_VECTOR && !idtv_info_valid)
9546 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9547 GUEST_INTR_STATE_NMI);
9548 else
9549 vmx->loaded_vmcs->nmi_known_unmasked =
9550 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9551 & GUEST_INTR_STATE_NMI);
9552 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
9553 vmx->loaded_vmcs->vnmi_blocked_time +=
9554 ktime_to_ns(ktime_sub(ktime_get(),
9555 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03009556}
9557
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009558static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03009559 u32 idt_vectoring_info,
9560 int instr_len_field,
9561 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03009562{
Avi Kivity51aa01d2010-07-20 14:31:20 +03009563 u8 vector;
9564 int type;
9565 bool idtv_info_valid;
9566
9567 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03009568
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009569 vcpu->arch.nmi_injected = false;
9570 kvm_clear_exception_queue(vcpu);
9571 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009572
9573 if (!idtv_info_valid)
9574 return;
9575
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009576 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03009577
Avi Kivity668f6122008-07-02 09:28:55 +03009578 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
9579 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009580
Gleb Natapov64a7ec02009-03-30 16:03:29 +03009581 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03009582 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009583 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03009584 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009585 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03009586 * Clear bit "block by NMI" before VM entry if a NMI
9587 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03009588 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009589 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009590 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009591 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009592 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009593 /* fall through */
9594 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03009595 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03009596 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03009597 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03009598 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03009599 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009600 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009601 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009602 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009603 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03009604 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009605 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009606 break;
9607 default:
9608 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03009609 }
Avi Kivitycf393f72008-07-01 16:20:21 +03009610}
9611
Avi Kivity83422e12010-07-20 14:43:23 +03009612static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
9613{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009614 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03009615 VM_EXIT_INSTRUCTION_LEN,
9616 IDT_VECTORING_ERROR_CODE);
9617}
9618
Avi Kivityb463a6f2010-07-20 15:06:17 +03009619static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
9620{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009621 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03009622 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9623 VM_ENTRY_INSTRUCTION_LEN,
9624 VM_ENTRY_EXCEPTION_ERROR_CODE);
9625
9626 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
9627}
9628
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009629static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
9630{
9631 int i, nr_msrs;
9632 struct perf_guest_switch_msr *msrs;
9633
9634 msrs = perf_guest_get_msrs(&nr_msrs);
9635
9636 if (!msrs)
9637 return;
9638
9639 for (i = 0; i < nr_msrs; i++)
9640 if (msrs[i].host == msrs[i].guest)
9641 clear_atomic_switch_msr(vmx, msrs[i].msr);
9642 else
9643 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
9644 msrs[i].host);
9645}
9646
Jiang Biao33365e72016-11-03 15:03:37 +08009647static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07009648{
9649 struct vcpu_vmx *vmx = to_vmx(vcpu);
9650 u64 tscl;
9651 u32 delta_tsc;
9652
9653 if (vmx->hv_deadline_tsc == -1)
9654 return;
9655
9656 tscl = rdtsc();
9657 if (vmx->hv_deadline_tsc > tscl)
9658 /* sure to be 32 bit only because checked on set_hv_timer */
9659 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
9660 cpu_preemption_timer_multi);
9661 else
9662 delta_tsc = 0;
9663
9664 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
9665}
9666
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08009667static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009668{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009669 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009670 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +02009671
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009672 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009673 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009674 vmx->loaded_vmcs->soft_vnmi_blocked))
9675 vmx->loaded_vmcs->entry_time = ktime_get();
9676
Avi Kivity104f2262010-11-18 13:12:52 +02009677 /* Don't enter VMX if guest state is invalid, let the exit handler
9678 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02009679 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02009680 return;
9681
Radim Krčmářa7653ec2014-08-21 18:08:07 +02009682 if (vmx->ple_window_dirty) {
9683 vmx->ple_window_dirty = false;
9684 vmcs_write32(PLE_WINDOW, vmx->ple_window);
9685 }
9686
Abel Gordon012f83c2013-04-18 14:39:25 +03009687 if (vmx->nested.sync_shadow_vmcs) {
9688 copy_vmcs12_to_shadow(vmx);
9689 vmx->nested.sync_shadow_vmcs = false;
9690 }
9691
Avi Kivity104f2262010-11-18 13:12:52 +02009692 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
9693 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
9694 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
9695 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
9696
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009697 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +02009698 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009699 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +02009700 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009701 }
9702
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07009703 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +02009704 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009705 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +02009706 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009707 }
9708
Avi Kivity104f2262010-11-18 13:12:52 +02009709 /* When single-stepping over STI and MOV SS, we must clear the
9710 * corresponding interruptibility bits in the guest state. Otherwise
9711 * vmentry fails as it then expects bit 14 (BS) in pending debug
9712 * exceptions being set, but that's not correct for the guest debugging
9713 * case. */
9714 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9715 vmx_set_interrupt_shadow(vcpu, 0);
9716
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02009717 if (static_cpu_has(X86_FEATURE_PKU) &&
9718 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
9719 vcpu->arch.pkru != vmx->host_pkru)
9720 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009721
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009722 atomic_switch_perf_msrs(vmx);
9723
Yunhong Jiang64672c92016-06-13 14:19:59 -07009724 vmx_arm_hv_timer(vcpu);
9725
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009726 /*
9727 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
9728 * it's non-zero. Since vmentry is serialising on affected CPUs, there
9729 * is no need to worry about the conditional branch over the wrmsr
9730 * being speculatively taken.
9731 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +02009732 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009733
Nadav Har'Eld462b812011-05-24 15:26:10 +03009734 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009735
9736 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
9737 (unsigned long)&current_evmcs->host_rsp : 0;
9738
Avi Kivity104f2262010-11-18 13:12:52 +02009739 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08009740 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009741 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
9742 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
9743 "push %%" _ASM_CX " \n\t"
9744 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03009745 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009746 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009747 /* Avoid VMWRITE when Enlightened VMCS is in use */
9748 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
9749 "jz 2f \n\t"
9750 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
9751 "jmp 1f \n\t"
9752 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009753 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03009754 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009755 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009756 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
9757 "mov %%cr2, %%" _ASM_DX " \n\t"
9758 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009759 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009760 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009761 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009762 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02009763 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009764 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009765 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
9766 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
9767 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
9768 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
9769 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
9770 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009771#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009772 "mov %c[r8](%0), %%r8 \n\t"
9773 "mov %c[r9](%0), %%r9 \n\t"
9774 "mov %c[r10](%0), %%r10 \n\t"
9775 "mov %c[r11](%0), %%r11 \n\t"
9776 "mov %c[r12](%0), %%r12 \n\t"
9777 "mov %c[r13](%0), %%r13 \n\t"
9778 "mov %c[r14](%0), %%r14 \n\t"
9779 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009780#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009781 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03009782
Avi Kivity6aa8b732006-12-10 02:21:36 -08009783 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03009784 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009785 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009786 "jmp 2f \n\t"
9787 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
9788 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08009789 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009790 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02009791 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -08009792 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009793 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
9794 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
9795 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
9796 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
9797 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
9798 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
9799 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009800#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009801 "mov %%r8, %c[r8](%0) \n\t"
9802 "mov %%r9, %c[r9](%0) \n\t"
9803 "mov %%r10, %c[r10](%0) \n\t"
9804 "mov %%r11, %c[r11](%0) \n\t"
9805 "mov %%r12, %c[r12](%0) \n\t"
9806 "mov %%r13, %c[r13](%0) \n\t"
9807 "mov %%r14, %c[r14](%0) \n\t"
9808 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -08009809 "xor %%r8d, %%r8d \n\t"
9810 "xor %%r9d, %%r9d \n\t"
9811 "xor %%r10d, %%r10d \n\t"
9812 "xor %%r11d, %%r11d \n\t"
9813 "xor %%r12d, %%r12d \n\t"
9814 "xor %%r13d, %%r13d \n\t"
9815 "xor %%r14d, %%r14d \n\t"
9816 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009817#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009818 "mov %%cr2, %%" _ASM_AX " \n\t"
9819 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03009820
Jim Mattson0cb5b302018-01-03 14:31:38 -08009821 "xor %%eax, %%eax \n\t"
9822 "xor %%ebx, %%ebx \n\t"
9823 "xor %%esi, %%esi \n\t"
9824 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009825 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009826 ".pushsection .rodata \n\t"
9827 ".global vmx_return \n\t"
9828 "vmx_return: " _ASM_PTR " 2b \n\t"
9829 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009830 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +03009831 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02009832 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +03009833 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009834 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
9835 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
9836 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
9837 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
9838 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
9839 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
9840 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009841#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009842 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
9843 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
9844 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
9845 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
9846 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
9847 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
9848 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
9849 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08009850#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02009851 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
9852 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02009853 : "cc", "memory"
9854#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009855 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009856 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009857#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009858 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009859#endif
9860 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08009861
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009862 /*
9863 * We do not use IBRS in the kernel. If this vCPU has used the
9864 * SPEC_CTRL MSR it may have left it on; save the value and
9865 * turn it off. This is much more efficient than blindly adding
9866 * it to the atomic save/restore list. Especially as the former
9867 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
9868 *
9869 * For non-nested case:
9870 * If the L01 MSR bitmap does not intercept the MSR, then we need to
9871 * save it.
9872 *
9873 * For nested case:
9874 * If the L02 MSR bitmap does not intercept the MSR, then we need to
9875 * save it.
9876 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +01009877 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +01009878 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009879
Thomas Gleixnerccbcd262018-05-09 23:01:01 +02009880 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009881
David Woodhouse117cc7a2018-01-12 11:11:27 +00009882 /* Eliminate branch target predictions from guest mode */
9883 vmexit_fill_RSB();
9884
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009885 /* All fields are clean at this point */
9886 if (static_branch_unlikely(&enable_evmcs))
9887 current_evmcs->hv_clean_fields |=
9888 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
9889
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009890 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -08009891 if (vmx->host_debugctlmsr)
9892 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009893
Avi Kivityaa67f602012-08-01 16:48:03 +03009894#ifndef CONFIG_X86_64
9895 /*
9896 * The sysexit path does not restore ds/es, so we must set them to
9897 * a reasonable value ourselves.
9898 *
9899 * We can't defer this to vmx_load_host_state() since that function
9900 * may be executed in interrupt context, which saves and restore segments
9901 * around it, nullifying its effect.
9902 */
9903 loadsegment(ds, __USER_DS);
9904 loadsegment(es, __USER_DS);
9905#endif
9906
Avi Kivity6de4f3a2009-05-31 22:58:47 +03009907 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02009908 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009909 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03009910 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009911 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009912 vcpu->arch.regs_dirty = 0;
9913
Gleb Natapove0b890d2013-09-25 12:51:33 +03009914 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009915 * eager fpu is enabled if PKEY is supported and CR4 is switched
9916 * back on host, so it is safe to read guest PKRU from current
9917 * XSAVE.
9918 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02009919 if (static_cpu_has(X86_FEATURE_PKU) &&
9920 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
9921 vcpu->arch.pkru = __read_pkru();
9922 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009923 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009924 }
9925
Gleb Natapove0b890d2013-09-25 12:51:33 +03009926 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -07009927 vmx->idt_vectoring_info = 0;
9928
9929 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
9930 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9931 return;
9932
9933 vmx->loaded_vmcs->launched = 1;
9934 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +03009935
Avi Kivity51aa01d2010-07-20 14:31:20 +03009936 vmx_complete_atomic_exit(vmx);
9937 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03009938 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009939}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009940STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009941
Sean Christopherson434a1e92018-03-20 12:17:18 -07009942static struct kvm *vmx_vm_alloc(void)
9943{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07009944 struct kvm_vmx *kvm_vmx = kzalloc(sizeof(struct kvm_vmx), GFP_KERNEL);
9945 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -07009946}
9947
9948static void vmx_vm_free(struct kvm *kvm)
9949{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07009950 kfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -07009951}
9952
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009953static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009954{
9955 struct vcpu_vmx *vmx = to_vmx(vcpu);
9956 int cpu;
9957
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009958 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009959 return;
9960
9961 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009962 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009963 vmx_vcpu_put(vcpu);
9964 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009965 put_cpu();
9966}
9967
Jim Mattson2f1fe812016-07-08 15:36:06 -07009968/*
9969 * Ensure that the current vmcs of the logical processor is the
9970 * vmcs01 of the vcpu before calling free_nested().
9971 */
9972static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9973{
9974 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009975
Christoffer Dallec7660c2017-12-04 21:35:23 +01009976 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009977 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009978 free_nested(vmx);
9979 vcpu_put(vcpu);
9980}
9981
Avi Kivity6aa8b732006-12-10 02:21:36 -08009982static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9983{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009984 struct vcpu_vmx *vmx = to_vmx(vcpu);
9985
Kai Huang843e4332015-01-28 10:54:28 +08009986 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08009987 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08009988 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009989 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009990 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009991 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009992 kfree(vmx->guest_msrs);
9993 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10009994 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009995}
9996
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009997static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009998{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009999 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010000 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010001 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010002 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010003
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010004 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010005 return ERR_PTR(-ENOMEM);
10006
Wanpeng Li991e7a02015-09-16 17:30:05 +080010007 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010008
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010009 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10010 if (err)
10011 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010012
Peter Feiner4e595162016-07-07 14:49:58 -070010013 err = -ENOMEM;
10014
10015 /*
10016 * If PML is turned on, failure on enabling PML just results in failure
10017 * of creating the vcpu, therefore we can simplify PML logic (by
10018 * avoiding dealing with cases, such as enabling PML partially on vcpus
10019 * for the guest, etc.
10020 */
10021 if (enable_pml) {
10022 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10023 if (!vmx->pml_pg)
10024 goto uninit_vcpu;
10025 }
10026
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010027 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020010028 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10029 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030010030
Peter Feiner4e595162016-07-07 14:49:58 -070010031 if (!vmx->guest_msrs)
10032 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010033
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010034 err = alloc_loaded_vmcs(&vmx->vmcs01);
10035 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010036 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010037
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010038 msr_bitmap = vmx->vmcs01.msr_bitmap;
10039 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10040 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10041 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10042 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10043 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10044 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10045 vmx->msr_bitmap_mode = 0;
10046
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010047 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030010048 cpu = get_cpu();
10049 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100010050 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020010051 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010052 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030010053 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020010054 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020010055 err = alloc_apic_access_page(kvm);
10056 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020010057 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020010058 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080010059
Sean Christophersone90008d2018-03-05 12:04:37 -080010060 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080010061 err = init_rmode_identity_map(kvm);
10062 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020010063 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080010064 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080010065
Wanpeng Li5c614b32015-10-13 09:18:36 -070010066 if (nested) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010067 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
10068 kvm_vcpu_apicv_active(&vmx->vcpu));
Wanpeng Li5c614b32015-10-13 09:18:36 -070010069 vmx->nested.vpid02 = allocate_vpid();
10070 }
Wincy Vanb9c237b2015-02-03 23:56:30 +080010071
Wincy Van705699a2015-02-03 23:58:17 +080010072 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010073 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010074
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010075 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
10076
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020010077 /*
10078 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
10079 * or POSTED_INTR_WAKEUP_VECTOR.
10080 */
10081 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
10082 vmx->pi_desc.sn = 1;
10083
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010084 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010085
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010086free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -070010087 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080010088 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010089free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010090 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070010091free_pml:
10092 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010093uninit_vcpu:
10094 kvm_vcpu_uninit(&vmx->vcpu);
10095free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080010096 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100010097 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010098 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010099}
10100
Wanpeng Lib31c1142018-03-12 04:53:04 -070010101static int vmx_vm_init(struct kvm *kvm)
10102{
10103 if (!ple_gap)
10104 kvm->arch.pause_in_guest = true;
10105 return 0;
10106}
10107
Yang, Sheng002c7f72007-07-31 14:23:01 +030010108static void __init vmx_check_processor_compat(void *rtn)
10109{
10110 struct vmcs_config vmcs_conf;
10111
10112 *(int *)rtn = 0;
10113 if (setup_vmcs_config(&vmcs_conf) < 0)
10114 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010010115 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030010116 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
10117 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
10118 smp_processor_id());
10119 *(int *)rtn = -EIO;
10120 }
10121}
10122
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010123static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080010124{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010125 u8 cache;
10126 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010127
Sheng Yang522c68c2009-04-27 20:35:43 +080010128 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020010129 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080010130 * 2. EPT with VT-d:
10131 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020010132 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080010133 * b. VT-d with snooping control feature: snooping control feature of
10134 * VT-d engine can guarantee the cache correctness. Just set it
10135 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080010136 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080010137 * consistent with host MTRR
10138 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020010139 if (is_mmio) {
10140 cache = MTRR_TYPE_UNCACHABLE;
10141 goto exit;
10142 }
10143
10144 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010145 ipat = VMX_EPT_IPAT_BIT;
10146 cache = MTRR_TYPE_WRBACK;
10147 goto exit;
10148 }
10149
10150 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
10151 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020010152 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080010153 cache = MTRR_TYPE_WRBACK;
10154 else
10155 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010156 goto exit;
10157 }
10158
Xiao Guangrongff536042015-06-15 16:55:22 +080010159 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010160
10161exit:
10162 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080010163}
10164
Sheng Yang17cc3932010-01-05 19:02:27 +080010165static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020010166{
Sheng Yang878403b2010-01-05 19:02:29 +080010167 if (enable_ept && !cpu_has_vmx_ept_1g_page())
10168 return PT_DIRECTORY_LEVEL;
10169 else
10170 /* For shadow and EPT supported 1GB page */
10171 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020010172}
10173
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010174static void vmcs_set_secondary_exec_control(u32 new_ctl)
10175{
10176 /*
10177 * These bits in the secondary execution controls field
10178 * are dynamic, the others are mostly based on the hypervisor
10179 * architecture and the guest's CPUID. Do not touch the
10180 * dynamic bits.
10181 */
10182 u32 mask =
10183 SECONDARY_EXEC_SHADOW_VMCS |
10184 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020010185 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10186 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010187
10188 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10189
10190 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10191 (new_ctl & ~mask) | (cur_ctl & mask));
10192}
10193
David Matlack8322ebb2016-11-29 18:14:09 -080010194/*
10195 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10196 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10197 */
10198static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10199{
10200 struct vcpu_vmx *vmx = to_vmx(vcpu);
10201 struct kvm_cpuid_entry2 *entry;
10202
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010203 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
10204 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080010205
10206#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10207 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010208 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080010209} while (0)
10210
10211 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10212 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10213 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10214 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10215 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10216 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10217 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10218 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10219 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10220 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10221 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10222 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10223 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10224 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10225 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10226
10227 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10228 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10229 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10230 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10231 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010010232 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080010233
10234#undef cr4_fixed1_update
10235}
10236
Sheng Yang0e851882009-12-18 16:48:46 +080010237static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10238{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010239 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010240
Paolo Bonzini80154d72017-08-24 13:55:35 +020010241 if (cpu_has_secondary_exec_ctrls()) {
10242 vmx_compute_secondary_exec_control(vmx);
10243 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010244 }
Mao, Junjiead756a12012-07-02 01:18:48 +000010245
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010246 if (nested_vmx_allowed(vcpu))
10247 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10248 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10249 else
10250 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10251 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080010252
10253 if (nested_vmx_allowed(vcpu))
10254 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +080010255}
10256
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010257static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10258{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030010259 if (func == 1 && nested)
10260 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010261}
10262
Yang Zhang25d92082013-08-06 12:00:32 +030010263static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10264 struct x86_exception *fault)
10265{
Jan Kiszka533558b2014-01-04 18:47:20 +010010266 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040010267 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010268 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010269 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030010270
Bandan Dasc5f983f2017-05-05 15:25:14 -040010271 if (vmx->nested.pml_full) {
10272 exit_reason = EXIT_REASON_PML_FULL;
10273 vmx->nested.pml_full = false;
10274 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10275 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010010276 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030010277 else
Jan Kiszka533558b2014-01-04 18:47:20 +010010278 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010279
10280 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030010281 vmcs12->guest_physical_address = fault->address;
10282}
10283
Peter Feiner995f00a2017-06-30 17:26:32 -070010284static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10285{
David Hildenbrandbb97a012017-08-10 23:15:28 +020010286 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070010287}
10288
Nadav Har'El155a97a2013-08-05 11:07:16 +030010289/* Callbacks for nested_ept_init_mmu_context: */
10290
10291static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10292{
10293 /* return the page table to be shadowed - in our case, EPT12 */
10294 return get_vmcs12(vcpu)->ept_pointer;
10295}
10296
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010297static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030010298{
Paolo Bonziniad896af2013-10-02 16:56:14 +020010299 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020010300 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010301 return 1;
10302
10303 kvm_mmu_unload(vcpu);
Paolo Bonziniad896af2013-10-02 16:56:14 +020010304 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010305 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010306 VMX_EPT_EXECUTE_ONLY_BIT,
David Hildenbranda057e0e2017-08-10 23:36:54 +020010307 nested_ept_ad_enabled(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030010308 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10309 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10310 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10311
10312 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010313 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030010314}
10315
10316static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10317{
10318 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10319}
10320
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030010321static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10322 u16 error_code)
10323{
10324 bool inequality, bit;
10325
10326 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10327 inequality =
10328 (error_code & vmcs12->page_fault_error_code_mask) !=
10329 vmcs12->page_fault_error_code_match;
10330 return inequality ^ bit;
10331}
10332
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010333static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10334 struct x86_exception *fault)
10335{
10336 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10337
10338 WARN_ON(!is_guest_mode(vcpu));
10339
Wanpeng Li305d0ab2017-09-28 18:16:44 -070010340 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10341 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020010342 vmcs12->vm_exit_intr_error_code = fault->error_code;
10343 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10344 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10345 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10346 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010347 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010348 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010349 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010350}
10351
Paolo Bonzinic9923842017-12-13 14:16:30 +010010352static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10353 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010354
10355static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010356 struct vmcs12 *vmcs12)
10357{
10358 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010359 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010360 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010361
10362 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010363 /*
10364 * Translate L1 physical address to host physical
10365 * address for vmcs02. Keep the page pinned, so this
10366 * physical address remains valid. We keep a reference
10367 * to it so we can release it later.
10368 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010369 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010370 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010371 vmx->nested.apic_access_page = NULL;
10372 }
10373 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010374 /*
10375 * If translation failed, no matter: This feature asks
10376 * to exit when accessing the given address, and if it
10377 * can never be accessed, this feature won't do
10378 * anything anyway.
10379 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010380 if (!is_error_page(page)) {
10381 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010382 hpa = page_to_phys(vmx->nested.apic_access_page);
10383 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10384 } else {
10385 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
10386 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10387 }
10388 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
10389 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
10390 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
10391 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10392 kvm_vcpu_reload_apic_access_page(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010393 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010394
10395 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010396 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010397 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010398 vmx->nested.virtual_apic_page = NULL;
10399 }
10400 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010401
10402 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010403 * If translation failed, VM entry will fail because
10404 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10405 * Failing the vm entry is _not_ what the processor
10406 * does but it's basically the only possibility we
10407 * have. We could still enter the guest if CR8 load
10408 * exits are enabled, CR8 store exits are enabled, and
10409 * virtualize APIC access is disabled; in this case
10410 * the processor would never use the TPR shadow and we
10411 * could simply clear the bit from the execution
10412 * control. But such a configuration is useless, so
10413 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010414 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010415 if (!is_error_page(page)) {
10416 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010417 hpa = page_to_phys(vmx->nested.virtual_apic_page);
10418 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
10419 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010420 }
10421
Wincy Van705699a2015-02-03 23:58:17 +080010422 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010423 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
10424 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010425 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010426 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080010427 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010428 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
10429 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010430 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010431 vmx->nested.pi_desc_page = page;
10432 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080010433 vmx->nested.pi_desc =
10434 (struct pi_desc *)((void *)vmx->nested.pi_desc +
10435 (unsigned long)(vmcs12->posted_intr_desc_addr &
10436 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010437 vmcs_write64(POSTED_INTR_DESC_ADDR,
10438 page_to_phys(vmx->nested.pi_desc_page) +
10439 (unsigned long)(vmcs12->posted_intr_desc_addr &
10440 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080010441 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080010442 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000010443 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
10444 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010445 else
10446 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
10447 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010448}
10449
Jan Kiszkaf4124502014-03-07 20:03:13 +010010450static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
10451{
10452 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
10453 struct vcpu_vmx *vmx = to_vmx(vcpu);
10454
10455 if (vcpu->arch.virtual_tsc_khz == 0)
10456 return;
10457
10458 /* Make sure short timeouts reliably trigger an immediate vmexit.
10459 * hrtimer_start does not guarantee this. */
10460 if (preemption_timeout <= 1) {
10461 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
10462 return;
10463 }
10464
10465 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10466 preemption_timeout *= 1000000;
10467 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
10468 hrtimer_start(&vmx->nested.preemption_timer,
10469 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
10470}
10471
Jim Mattson56a20512017-07-06 16:33:06 -070010472static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
10473 struct vmcs12 *vmcs12)
10474{
10475 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
10476 return 0;
10477
10478 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
10479 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
10480 return -EINVAL;
10481
10482 return 0;
10483}
10484
Wincy Van3af18d92015-02-03 23:49:31 +080010485static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
10486 struct vmcs12 *vmcs12)
10487{
Wincy Van3af18d92015-02-03 23:49:31 +080010488 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10489 return 0;
10490
Jim Mattson5fa99cb2017-07-06 16:33:07 -070010491 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080010492 return -EINVAL;
10493
10494 return 0;
10495}
10496
Jim Mattson712b12d2017-08-24 13:24:47 -070010497static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
10498 struct vmcs12 *vmcs12)
10499{
10500 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10501 return 0;
10502
10503 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
10504 return -EINVAL;
10505
10506 return 0;
10507}
10508
Wincy Van3af18d92015-02-03 23:49:31 +080010509/*
10510 * Merge L0's and L1's MSR bitmap, return false to indicate that
10511 * we do not use the hardware.
10512 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010010513static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10514 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080010515{
Wincy Van82f0dd42015-02-03 23:57:18 +080010516 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080010517 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020010518 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010519 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010010520 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010521 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010010522 *
10523 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
10524 * ensures that we do not accidentally generate an L02 MSR bitmap
10525 * from the L12 MSR bitmap that is too permissive.
10526 * 2. That L1 or L2s have actually used the MSR. This avoids
10527 * unnecessarily merging of the bitmap if the MSR is unused. This
10528 * works properly because we only update the L01 MSR bitmap lazily.
10529 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
10530 * updated to reflect this when L1 (or its L2s) actually write to
10531 * the MSR.
10532 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000010533 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
10534 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080010535
Paolo Bonzinic9923842017-12-13 14:16:30 +010010536 /* Nothing to do if the MSR bitmap is not in use. */
10537 if (!cpu_has_vmx_msr_bitmap() ||
10538 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10539 return false;
10540
Ashok Raj15d45072018-02-01 22:59:43 +010010541 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010542 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080010543 return false;
10544
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010545 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
10546 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080010547 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010010548
Radim Krčmářd048c092016-08-08 20:16:22 +020010549 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010010550 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
10551 /*
10552 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
10553 * just lets the processor take the value from the virtual-APIC page;
10554 * take those 256 bits directly from the L1 bitmap.
10555 */
10556 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10557 unsigned word = msr / BITS_PER_LONG;
10558 msr_bitmap_l0[word] = msr_bitmap_l1[word];
10559 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080010560 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010010561 } else {
10562 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10563 unsigned word = msr / BITS_PER_LONG;
10564 msr_bitmap_l0[word] = ~0;
10565 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
10566 }
10567 }
10568
10569 nested_vmx_disable_intercept_for_msr(
10570 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010571 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010572 MSR_TYPE_W);
10573
10574 if (nested_cpu_has_vid(vmcs12)) {
10575 nested_vmx_disable_intercept_for_msr(
10576 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010577 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010578 MSR_TYPE_W);
10579 nested_vmx_disable_intercept_for_msr(
10580 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010581 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010582 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080010583 }
Ashok Raj15d45072018-02-01 22:59:43 +010010584
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010585 if (spec_ctrl)
10586 nested_vmx_disable_intercept_for_msr(
10587 msr_bitmap_l1, msr_bitmap_l0,
10588 MSR_IA32_SPEC_CTRL,
10589 MSR_TYPE_R | MSR_TYPE_W);
10590
Ashok Raj15d45072018-02-01 22:59:43 +010010591 if (pred_cmd)
10592 nested_vmx_disable_intercept_for_msr(
10593 msr_bitmap_l1, msr_bitmap_l0,
10594 MSR_IA32_PRED_CMD,
10595 MSR_TYPE_W);
10596
Wincy Vanf2b93282015-02-03 23:56:03 +080010597 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010598 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080010599
10600 return true;
10601}
10602
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040010603static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
10604 struct vmcs12 *vmcs12)
10605{
10606 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
10607 !page_address_valid(vcpu, vmcs12->apic_access_addr))
10608 return -EINVAL;
10609 else
10610 return 0;
10611}
10612
Wincy Vanf2b93282015-02-03 23:56:03 +080010613static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
10614 struct vmcs12 *vmcs12)
10615{
Wincy Van82f0dd42015-02-03 23:57:18 +080010616 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080010617 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080010618 !nested_cpu_has_vid(vmcs12) &&
10619 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080010620 return 0;
10621
10622 /*
10623 * If virtualize x2apic mode is enabled,
10624 * virtualize apic access must be disabled.
10625 */
Wincy Van82f0dd42015-02-03 23:57:18 +080010626 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10627 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080010628 return -EINVAL;
10629
Wincy Van608406e2015-02-03 23:57:51 +080010630 /*
10631 * If virtual interrupt delivery is enabled,
10632 * we must exit on external interrupts.
10633 */
10634 if (nested_cpu_has_vid(vmcs12) &&
10635 !nested_exit_on_intr(vcpu))
10636 return -EINVAL;
10637
Wincy Van705699a2015-02-03 23:58:17 +080010638 /*
10639 * bits 15:8 should be zero in posted_intr_nv,
10640 * the descriptor address has been already checked
10641 * in nested_get_vmcs12_pages.
10642 */
10643 if (nested_cpu_has_posted_intr(vmcs12) &&
10644 (!nested_cpu_has_vid(vmcs12) ||
10645 !nested_exit_intr_ack_set(vcpu) ||
10646 vmcs12->posted_intr_nv & 0xff00))
10647 return -EINVAL;
10648
Wincy Vanf2b93282015-02-03 23:56:03 +080010649 /* tpr shadow is needed by all apicv features. */
10650 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10651 return -EINVAL;
10652
10653 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080010654}
10655
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010656static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
10657 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010658 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030010659{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010660 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010661 u64 count, addr;
10662
10663 if (vmcs12_read_any(vcpu, count_field, &count) ||
10664 vmcs12_read_any(vcpu, addr_field, &addr)) {
10665 WARN_ON(1);
10666 return -EINVAL;
10667 }
10668 if (count == 0)
10669 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010670 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010671 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
10672 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010673 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010674 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
10675 addr_field, maxphyaddr, count, addr);
10676 return -EINVAL;
10677 }
10678 return 0;
10679}
10680
10681static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
10682 struct vmcs12 *vmcs12)
10683{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010684 if (vmcs12->vm_exit_msr_load_count == 0 &&
10685 vmcs12->vm_exit_msr_store_count == 0 &&
10686 vmcs12->vm_entry_msr_load_count == 0)
10687 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010688 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010689 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010690 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010691 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010692 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010693 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030010694 return -EINVAL;
10695 return 0;
10696}
10697
Bandan Dasc5f983f2017-05-05 15:25:14 -040010698static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
10699 struct vmcs12 *vmcs12)
10700{
10701 u64 address = vmcs12->pml_address;
10702 int maxphyaddr = cpuid_maxphyaddr(vcpu);
10703
10704 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
10705 if (!nested_cpu_has_ept(vmcs12) ||
10706 !IS_ALIGNED(address, 4096) ||
10707 address >> maxphyaddr)
10708 return -EINVAL;
10709 }
10710
10711 return 0;
10712}
10713
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010714static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
10715 struct vmx_msr_entry *e)
10716{
10717 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020010718 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010719 return -EINVAL;
10720 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
10721 e->index == MSR_IA32_UCODE_REV)
10722 return -EINVAL;
10723 if (e->reserved != 0)
10724 return -EINVAL;
10725 return 0;
10726}
10727
10728static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
10729 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030010730{
10731 if (e->index == MSR_FS_BASE ||
10732 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010733 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
10734 nested_vmx_msr_check_common(vcpu, e))
10735 return -EINVAL;
10736 return 0;
10737}
10738
10739static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
10740 struct vmx_msr_entry *e)
10741{
10742 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
10743 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030010744 return -EINVAL;
10745 return 0;
10746}
10747
10748/*
10749 * Load guest's/host's msr at nested entry/exit.
10750 * return 0 for success, entry index for failure.
10751 */
10752static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10753{
10754 u32 i;
10755 struct vmx_msr_entry e;
10756 struct msr_data msr;
10757
10758 msr.host_initiated = false;
10759 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010760 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
10761 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010762 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010763 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10764 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010765 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010766 }
10767 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010768 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010769 "%s check failed (%u, 0x%x, 0x%x)\n",
10770 __func__, i, e.index, e.reserved);
10771 goto fail;
10772 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010773 msr.index = e.index;
10774 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010775 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010776 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010777 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10778 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030010779 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010780 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010781 }
10782 return 0;
10783fail:
10784 return i + 1;
10785}
10786
10787static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10788{
10789 u32 i;
10790 struct vmx_msr_entry e;
10791
10792 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010793 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010794 if (kvm_vcpu_read_guest(vcpu,
10795 gpa + i * sizeof(e),
10796 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010797 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010798 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10799 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010800 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010801 }
10802 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010803 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010804 "%s check failed (%u, 0x%x, 0x%x)\n",
10805 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030010806 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010807 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010808 msr_info.host_initiated = false;
10809 msr_info.index = e.index;
10810 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010811 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010812 "%s cannot read MSR (%u, 0x%x)\n",
10813 __func__, i, e.index);
10814 return -EINVAL;
10815 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010816 if (kvm_vcpu_write_guest(vcpu,
10817 gpa + i * sizeof(e) +
10818 offsetof(struct vmx_msr_entry, value),
10819 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010820 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010821 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010822 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010823 return -EINVAL;
10824 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010825 }
10826 return 0;
10827}
10828
Ladi Prosek1dc35da2016-11-30 16:03:11 +010010829static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
10830{
10831 unsigned long invalid_mask;
10832
10833 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
10834 return (val & invalid_mask) == 0;
10835}
10836
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010837/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010838 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
10839 * emulating VM entry into a guest with EPT enabled.
10840 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10841 * is assigned to entry_failure_code on failure.
10842 */
10843static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080010844 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010845{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010846 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010010847 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010848 *entry_failure_code = ENTRY_FAIL_DEFAULT;
10849 return 1;
10850 }
10851
10852 /*
10853 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
10854 * must not be dereferenced.
10855 */
10856 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
10857 !nested_ept) {
10858 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
10859 *entry_failure_code = ENTRY_FAIL_PDPTE;
10860 return 1;
10861 }
10862 }
10863
10864 vcpu->arch.cr3 = cr3;
10865 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
10866 }
10867
10868 kvm_mmu_reset_context(vcpu);
10869 return 0;
10870}
10871
Paolo Bonzini74a497f2017-12-20 13:55:39 +010010872static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10873 bool from_vmentry)
10874{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010010875 struct vcpu_vmx *vmx = to_vmx(vcpu);
10876
10877 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
10878 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
10879 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
10880 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
10881 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
10882 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
10883 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
10884 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
10885 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
10886 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
10887 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
10888 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
10889 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
10890 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
10891 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
10892 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
10893 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
10894 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
10895 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
10896 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
10897 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
10898 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
10899 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
10900 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
10901 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
10902 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
10903 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
10904 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
10905 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
10906 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
10907 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010010908
10909 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
10910 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
10911 vmcs12->guest_pending_dbg_exceptions);
10912 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
10913 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
10914
10915 if (nested_cpu_has_xsaves(vmcs12))
10916 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
10917 vmcs_write64(VMCS_LINK_POINTER, -1ull);
10918
10919 if (cpu_has_vmx_posted_intr())
10920 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
10921
10922 /*
10923 * Whether page-faults are trapped is determined by a combination of
10924 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
10925 * If enable_ept, L0 doesn't care about page faults and we should
10926 * set all of these to L1's desires. However, if !enable_ept, L0 does
10927 * care about (at least some) page faults, and because it is not easy
10928 * (if at all possible?) to merge L0 and L1's desires, we simply ask
10929 * to exit on each and every L2 page fault. This is done by setting
10930 * MASK=MATCH=0 and (see below) EB.PF=1.
10931 * Note that below we don't need special code to set EB.PF beyond the
10932 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
10933 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
10934 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
10935 */
10936 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
10937 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
10938 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
10939 enable_ept ? vmcs12->page_fault_error_code_match : 0);
10940
10941 /* All VMFUNCs are currently emulated through L0 vmexits. */
10942 if (cpu_has_vmx_vmfunc())
10943 vmcs_write64(VM_FUNCTION_CONTROL, 0);
10944
10945 if (cpu_has_vmx_apicv()) {
10946 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
10947 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
10948 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
10949 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
10950 }
10951
10952 /*
10953 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
10954 * Some constant fields are set here by vmx_set_constant_host_state().
10955 * Other fields are different per CPU, and will be set later when
10956 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
10957 */
10958 vmx_set_constant_host_state(vmx);
10959
10960 /*
10961 * Set the MSR load/store lists to match L0's settings.
10962 */
10963 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
10964 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10965 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
10966 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10967 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
10968
10969 set_cr4_guest_host_mask(vmx);
10970
10971 if (vmx_mpx_supported())
10972 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
10973
10974 if (enable_vpid) {
10975 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
10976 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
10977 else
10978 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
10979 }
10980
10981 /*
10982 * L1 may access the L2's PDPTR, so save them to construct vmcs12
10983 */
10984 if (enable_ept) {
10985 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
10986 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
10987 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
10988 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
10989 }
Radim Krčmář80132f42018-02-02 18:26:58 +010010990
10991 if (cpu_has_vmx_msr_bitmap())
10992 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010010993}
10994
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010995/*
10996 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
10997 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080010998 * 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 +030010999 * guest in a way that will both be appropriate to L1's requests, and our
11000 * needs. In addition to modifying the active vmcs (which is vmcs02), this
11001 * function also has additional necessary side-effects, like setting various
11002 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010011003 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11004 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011005 */
Ladi Prosekee146c12016-11-30 16:03:09 +010011006static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011007 bool from_vmentry, u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011008{
11009 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040011010 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011011
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011012 if (vmx->nested.dirty_vmcs12) {
11013 prepare_vmcs02_full(vcpu, vmcs12, from_vmentry);
11014 vmx->nested.dirty_vmcs12 = false;
11015 }
11016
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011017 /*
11018 * First, the fields that are shadowed. This must be kept in sync
11019 * with vmx_shadow_fields.h.
11020 */
11021
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011022 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011023 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011024 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011025 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
11026 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011027
11028 /*
11029 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
11030 * HOST_FS_BASE, HOST_GS_BASE.
11031 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011032
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011033 if (from_vmentry &&
11034 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020011035 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
11036 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
11037 } else {
11038 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
11039 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
11040 }
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011041 if (from_vmentry) {
11042 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
11043 vmcs12->vm_entry_intr_info_field);
11044 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
11045 vmcs12->vm_entry_exception_error_code);
11046 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
11047 vmcs12->vm_entry_instruction_len);
11048 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
11049 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070011050 vmx->loaded_vmcs->nmi_known_unmasked =
11051 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011052 } else {
11053 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
11054 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030011055 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011056
Jan Kiszkaf4124502014-03-07 20:03:13 +010011057 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080011058
Paolo Bonzini93140062016-07-06 13:23:51 +020011059 /* Preemption timer setting is only taken from vmcs01. */
11060 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11061 exec_control |= vmcs_config.pin_based_exec_ctrl;
11062 if (vmx->hv_deadline_tsc == -1)
11063 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11064
11065 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080011066 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011067 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
11068 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011069 } else {
Wincy Van705699a2015-02-03 23:58:17 +080011070 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011071 }
Wincy Van705699a2015-02-03 23:58:17 +080011072
Jan Kiszkaf4124502014-03-07 20:03:13 +010011073 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011074
Jan Kiszkaf4124502014-03-07 20:03:13 +010011075 vmx->nested.preemption_timer_expired = false;
11076 if (nested_cpu_has_preemption_timer(vmcs12))
11077 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010011078
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011079 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020011080 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080011081
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011082 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011083 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020011084 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010011085 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020011086 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011087 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040011088 SECONDARY_EXEC_APIC_REGISTER_VIRT |
11089 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011090 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040011091 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
11092 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
11093 ~SECONDARY_EXEC_ENABLE_PML;
11094 exec_control |= vmcs12_exec_ctrl;
11095 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011096
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011097 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080011098 vmcs_write16(GUEST_INTR_STATUS,
11099 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080011100
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011101 /*
11102 * Write an illegal value to APIC_ACCESS_ADDR. Later,
11103 * nested_get_vmcs12_pages will either fix it up or
11104 * remove the VM execution control.
11105 */
11106 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
11107 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
11108
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011109 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
11110 }
11111
Jim Mattson83bafef2016-10-04 10:48:38 -070011112 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011113 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
11114 * entry, but only if the current (host) sp changed from the value
11115 * we wrote last (vmx->host_rsp). This cache is no longer relevant
11116 * if we switch vmcs, and rather than hold a separate cache per vmcs,
11117 * here we just force the write to happen on entry.
11118 */
11119 vmx->host_rsp = 0;
11120
11121 exec_control = vmx_exec_control(vmx); /* L0's desires */
11122 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
11123 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
11124 exec_control &= ~CPU_BASED_TPR_SHADOW;
11125 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011126
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011127 /*
11128 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
11129 * nested_get_vmcs12_pages can't fix it up, the illegal value
11130 * will result in a VM entry failure.
11131 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011132 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011133 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011134 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070011135 } else {
11136#ifdef CONFIG_X86_64
11137 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
11138 CPU_BASED_CR8_STORE_EXITING;
11139#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011140 }
11141
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011142 /*
Quan Xu8eb73e22017-12-12 16:44:21 +080011143 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
11144 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011145 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011146 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
11147 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
11148
11149 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
11150
11151 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
11152 * bitwise-or of what L1 wants to trap for L2, and what we want to
11153 * trap. Note that CR0.TS also needs updating - we do this later.
11154 */
11155 update_exception_bitmap(vcpu);
11156 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
11157 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11158
Nadav Har'El8049d652013-08-05 11:07:06 +030011159 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
11160 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
11161 * bits are further modified by vmx_set_efer() below.
11162 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010011163 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030011164
11165 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
11166 * emulated by vmx_set_efer(), below.
11167 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020011168 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030011169 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
11170 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011171 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
11172
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011173 if (from_vmentry &&
11174 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011175 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011176 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011177 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011178 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011179 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011180
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011181 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11182
Peter Feinerc95ba922016-08-17 09:36:47 -070011183 if (kvm_has_tsc_control)
11184 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011185
11186 if (enable_vpid) {
11187 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070011188 * There is no direct mapping between vpid02 and vpid12, the
11189 * vpid02 is per-vCPU for L0 and reused while the value of
11190 * vpid12 is changed w/ one invvpid during nested vmentry.
11191 * The vpid12 is allocated by L1 for L2, so it will not
11192 * influence global bitmap(for vpid01 and vpid02 allocation)
11193 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011194 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070011195 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070011196 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
11197 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011198 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011199 }
11200 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011201 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011202 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011203 }
11204
Ladi Prosek1fb883b2017-04-04 14:18:53 +020011205 if (enable_pml) {
11206 /*
11207 * Conceptually we want to copy the PML address and index from
11208 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11209 * since we always flush the log on each vmexit, this happens
11210 * to be equivalent to simply resetting the fields in vmcs02.
11211 */
11212 ASSERT(vmx->pml_pg);
11213 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11214 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11215 }
11216
Nadav Har'El155a97a2013-08-05 11:07:16 +030011217 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011218 if (nested_ept_init_mmu_context(vcpu)) {
11219 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11220 return 1;
11221 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011222 } else if (nested_cpu_has2(vmcs12,
11223 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070011224 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011225 }
11226
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011227 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011228 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11229 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011230 * The CR0_READ_SHADOW is what L2 should have expected to read given
11231 * the specifications by L1; It's not enough to take
11232 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11233 * have more bits than L1 expected.
11234 */
11235 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11236 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11237
11238 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11239 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11240
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011241 if (from_vmentry &&
11242 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080011243 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11244 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11245 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11246 else
11247 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11248 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11249 vmx_set_efer(vcpu, vcpu->arch.efer);
11250
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011251 /*
11252 * Guest state is invalid and unrestricted guest is disabled,
11253 * which means L1 attempted VMEntry to L2 with invalid state.
11254 * Fail the VMEntry.
11255 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010011256 if (vmx->emulation_required) {
11257 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011258 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010011259 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011260
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011261 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010011262 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011263 entry_failure_code))
11264 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010011265
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011266 if (!enable_ept)
11267 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11268
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011269 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11270 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010011271 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011272}
11273
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011274static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11275{
11276 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11277 nested_cpu_has_virtual_nmis(vmcs12))
11278 return -EINVAL;
11279
11280 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11281 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
11282 return -EINVAL;
11283
11284 return 0;
11285}
11286
Jim Mattsonca0bde22016-11-30 12:03:46 -080011287static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11288{
11289 struct vcpu_vmx *vmx = to_vmx(vcpu);
11290
11291 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
11292 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11293 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11294
Jim Mattson56a20512017-07-06 16:33:06 -070011295 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11296 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11297
Jim Mattsonca0bde22016-11-30 12:03:46 -080011298 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11299 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11300
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011301 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
11302 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11303
Jim Mattson712b12d2017-08-24 13:24:47 -070011304 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11305 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11306
Jim Mattsonca0bde22016-11-30 12:03:46 -080011307 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
11308 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11309
11310 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11311 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11312
Bandan Dasc5f983f2017-05-05 15:25:14 -040011313 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
11314 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11315
Jim Mattsonca0bde22016-11-30 12:03:46 -080011316 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011317 vmx->nested.msrs.procbased_ctls_low,
11318 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070011319 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
11320 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011321 vmx->nested.msrs.secondary_ctls_low,
11322 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011323 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011324 vmx->nested.msrs.pinbased_ctls_low,
11325 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011326 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011327 vmx->nested.msrs.exit_ctls_low,
11328 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011329 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011330 vmx->nested.msrs.entry_ctls_low,
11331 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011332 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11333
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011334 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011335 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11336
Bandan Das41ab9372017-08-03 15:54:43 -040011337 if (nested_cpu_has_vmfunc(vmcs12)) {
11338 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011339 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040011340 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11341
11342 if (nested_cpu_has_eptp_switching(vmcs12)) {
11343 if (!nested_cpu_has_ept(vmcs12) ||
11344 !page_address_valid(vcpu, vmcs12->eptp_list_address))
11345 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11346 }
11347 }
Bandan Das27c42a12017-08-03 15:54:42 -040011348
Jim Mattsonc7c2c702017-05-05 11:28:09 -070011349 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
11350 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11351
Jim Mattsonca0bde22016-11-30 12:03:46 -080011352 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
11353 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
11354 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
11355 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11356
11357 return 0;
11358}
11359
11360static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11361 u32 *exit_qual)
11362{
11363 bool ia32e;
11364
11365 *exit_qual = ENTRY_FAIL_DEFAULT;
11366
11367 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
11368 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
11369 return 1;
11370
11371 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
11372 vmcs12->vmcs_link_pointer != -1ull) {
11373 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
11374 return 1;
11375 }
11376
11377 /*
11378 * If the load IA32_EFER VM-entry control is 1, the following checks
11379 * are performed on the field for the IA32_EFER MSR:
11380 * - Bits reserved in the IA32_EFER MSR must be 0.
11381 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
11382 * the IA-32e mode guest VM-exit control. It must also be identical
11383 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
11384 * CR0.PG) is 1.
11385 */
11386 if (to_vmx(vcpu)->nested.nested_run_pending &&
11387 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
11388 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
11389 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
11390 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
11391 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
11392 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
11393 return 1;
11394 }
11395
11396 /*
11397 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
11398 * IA32_EFER MSR must be 0 in the field for that register. In addition,
11399 * the values of the LMA and LME bits in the field must each be that of
11400 * the host address-space size VM-exit control.
11401 */
11402 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
11403 ia32e = (vmcs12->vm_exit_controls &
11404 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
11405 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
11406 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
11407 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
11408 return 1;
11409 }
11410
Wanpeng Lif1b026a2017-11-05 16:54:48 -080011411 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
11412 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
11413 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
11414 return 1;
11415
Jim Mattsonca0bde22016-11-30 12:03:46 -080011416 return 0;
11417}
11418
Jim Mattson858e25c2016-11-30 12:03:47 -080011419static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry)
11420{
11421 struct vcpu_vmx *vmx = to_vmx(vcpu);
11422 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080011423 u32 msr_entry_idx;
11424 u32 exit_qual;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011425 int r;
Jim Mattson858e25c2016-11-30 12:03:47 -080011426
Jim Mattson858e25c2016-11-30 12:03:47 -080011427 enter_guest_mode(vcpu);
11428
11429 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
11430 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11431
Jim Mattsonde3a0022017-11-27 17:22:25 -060011432 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080011433 vmx_segment_cache_clear(vmx);
11434
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011435 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11436 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
11437
11438 r = EXIT_REASON_INVALID_STATE;
11439 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &exit_qual))
11440 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080011441
11442 nested_get_vmcs12_pages(vcpu, vmcs12);
11443
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011444 r = EXIT_REASON_MSR_LOAD_FAIL;
Jim Mattson858e25c2016-11-30 12:03:47 -080011445 msr_entry_idx = nested_vmx_load_msr(vcpu,
11446 vmcs12->vm_entry_msr_load_addr,
11447 vmcs12->vm_entry_msr_load_count);
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011448 if (msr_entry_idx)
11449 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080011450
Jim Mattson858e25c2016-11-30 12:03:47 -080011451 /*
11452 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
11453 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
11454 * returned as far as L1 is concerned. It will only return (and set
11455 * the success flag) when L2 exits (see nested_vmx_vmexit()).
11456 */
11457 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011458
11459fail:
11460 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11461 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
11462 leave_guest_mode(vcpu);
11463 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
11464 nested_vmx_entry_failure(vcpu, vmcs12, r, exit_qual);
11465 return 1;
Jim Mattson858e25c2016-11-30 12:03:47 -080011466}
11467
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011468/*
11469 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
11470 * for running an L2 nested guest.
11471 */
11472static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
11473{
11474 struct vmcs12 *vmcs12;
11475 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011476 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080011477 u32 exit_qual;
11478 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011479
Kyle Hueyeb277562016-11-29 12:40:39 -080011480 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011481 return 1;
11482
Kyle Hueyeb277562016-11-29 12:40:39 -080011483 if (!nested_vmx_check_vmcs12(vcpu))
11484 goto out;
11485
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011486 vmcs12 = get_vmcs12(vcpu);
11487
Abel Gordon012f83c2013-04-18 14:39:25 +030011488 if (enable_shadow_vmcs)
11489 copy_shadow_to_vmcs12(vmx);
11490
Nadav Har'El7c177932011-05-25 23:12:04 +030011491 /*
11492 * The nested entry process starts with enforcing various prerequisites
11493 * on vmcs12 as required by the Intel SDM, and act appropriately when
11494 * they fail: As the SDM explains, some conditions should cause the
11495 * instruction to fail, while others will cause the instruction to seem
11496 * to succeed, but return an EXIT_REASON_INVALID_STATE.
11497 * To speed up the normal (success) code path, we should avoid checking
11498 * for misconfigurations which will anyway be caught by the processor
11499 * when using the merged vmcs02.
11500 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011501 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
11502 nested_vmx_failValid(vcpu,
11503 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
11504 goto out;
11505 }
11506
Nadav Har'El7c177932011-05-25 23:12:04 +030011507 if (vmcs12->launch_state == launch) {
11508 nested_vmx_failValid(vcpu,
11509 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
11510 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080011511 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030011512 }
11513
Jim Mattsonca0bde22016-11-30 12:03:46 -080011514 ret = check_vmentry_prereqs(vcpu, vmcs12);
11515 if (ret) {
11516 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080011517 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020011518 }
11519
Nadav Har'El7c177932011-05-25 23:12:04 +030011520 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080011521 * After this point, the trap flag no longer triggers a singlestep trap
11522 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
11523 * This is not 100% correct; for performance reasons, we delegate most
11524 * of the checks on host state to the processor. If those fail,
11525 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020011526 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080011527 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020011528
Jim Mattsonca0bde22016-11-30 12:03:46 -080011529 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
11530 if (ret) {
11531 nested_vmx_entry_failure(vcpu, vmcs12,
11532 EXIT_REASON_INVALID_STATE, exit_qual);
11533 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020011534 }
11535
11536 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030011537 * We're finally done with prerequisite checking, and can start with
11538 * the nested entry.
11539 */
11540
Jim Mattson858e25c2016-11-30 12:03:47 -080011541 ret = enter_vmx_non_root_mode(vcpu, true);
11542 if (ret)
11543 return ret;
Wincy Vanff651cb2014-12-11 08:52:58 +030011544
Chao Gao135a06c2018-02-11 10:06:30 +080011545 /*
11546 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
11547 * by event injection, halt vcpu.
11548 */
11549 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
11550 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK))
Joel Schopp5cb56052015-03-02 13:43:31 -060011551 return kvm_vcpu_halt(vcpu);
Jan Kiszka6dfacad2013-12-04 08:58:54 +010011552
Jan Kiszka7af40ad32014-01-04 18:47:23 +010011553 vmx->nested.nested_run_pending = 1;
11554
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011555 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080011556
11557out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080011558 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011559}
11560
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011561/*
11562 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
11563 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
11564 * This function returns the new value we should put in vmcs12.guest_cr0.
11565 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
11566 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
11567 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
11568 * didn't trap the bit, because if L1 did, so would L0).
11569 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
11570 * been modified by L2, and L1 knows it. So just leave the old value of
11571 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
11572 * isn't relevant, because if L0 traps this bit it can set it to anything.
11573 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
11574 * changed these bits, and therefore they need to be updated, but L0
11575 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
11576 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
11577 */
11578static inline unsigned long
11579vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11580{
11581 return
11582 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
11583 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
11584 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
11585 vcpu->arch.cr0_guest_owned_bits));
11586}
11587
11588static inline unsigned long
11589vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11590{
11591 return
11592 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
11593 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
11594 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
11595 vcpu->arch.cr4_guest_owned_bits));
11596}
11597
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011598static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
11599 struct vmcs12 *vmcs12)
11600{
11601 u32 idt_vectoring;
11602 unsigned int nr;
11603
Wanpeng Li664f8e22017-08-24 03:35:09 -070011604 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011605 nr = vcpu->arch.exception.nr;
11606 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11607
11608 if (kvm_exception_is_soft(nr)) {
11609 vmcs12->vm_exit_instruction_len =
11610 vcpu->arch.event_exit_inst_len;
11611 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
11612 } else
11613 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
11614
11615 if (vcpu->arch.exception.has_error_code) {
11616 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
11617 vmcs12->idt_vectoring_error_code =
11618 vcpu->arch.exception.error_code;
11619 }
11620
11621 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010011622 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011623 vmcs12->idt_vectoring_info_field =
11624 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030011625 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011626 nr = vcpu->arch.interrupt.nr;
11627 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11628
11629 if (vcpu->arch.interrupt.soft) {
11630 idt_vectoring |= INTR_TYPE_SOFT_INTR;
11631 vmcs12->vm_entry_instruction_len =
11632 vcpu->arch.event_exit_inst_len;
11633 } else
11634 idt_vectoring |= INTR_TYPE_EXT_INTR;
11635
11636 vmcs12->idt_vectoring_info_field = idt_vectoring;
11637 }
11638}
11639
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011640static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
11641{
11642 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011643 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020011644 bool block_nested_events =
11645 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080011646
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011647 if (vcpu->arch.exception.pending &&
11648 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020011649 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011650 return -EBUSY;
11651 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011652 return 0;
11653 }
11654
Jan Kiszkaf4124502014-03-07 20:03:13 +010011655 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
11656 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020011657 if (block_nested_events)
Jan Kiszkaf4124502014-03-07 20:03:13 +010011658 return -EBUSY;
11659 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
11660 return 0;
11661 }
11662
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011663 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020011664 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011665 return -EBUSY;
11666 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11667 NMI_VECTOR | INTR_TYPE_NMI_INTR |
11668 INTR_INFO_VALID_MASK, 0);
11669 /*
11670 * The NMI-triggered VM exit counts as injection:
11671 * clear this one and block further NMIs.
11672 */
11673 vcpu->arch.nmi_pending = 0;
11674 vmx_set_nmi_mask(vcpu, true);
11675 return 0;
11676 }
11677
11678 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
11679 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020011680 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011681 return -EBUSY;
11682 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080011683 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011684 }
11685
David Hildenbrand6342c502017-01-25 11:58:58 +010011686 vmx_complete_nested_posted_interrupt(vcpu);
11687 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011688}
11689
Jan Kiszkaf4124502014-03-07 20:03:13 +010011690static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
11691{
11692 ktime_t remaining =
11693 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
11694 u64 value;
11695
11696 if (ktime_to_ns(remaining) <= 0)
11697 return 0;
11698
11699 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
11700 do_div(value, 1000000);
11701 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11702}
11703
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011704/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011705 * Update the guest state fields of vmcs12 to reflect changes that
11706 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
11707 * VM-entry controls is also updated, since this is really a guest
11708 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011709 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011710static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011711{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011712 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
11713 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
11714
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011715 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
11716 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
11717 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
11718
11719 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
11720 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
11721 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
11722 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
11723 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
11724 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
11725 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
11726 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
11727 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
11728 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
11729 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
11730 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
11731 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
11732 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
11733 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
11734 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
11735 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
11736 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
11737 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
11738 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
11739 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
11740 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
11741 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
11742 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
11743 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
11744 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
11745 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
11746 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
11747 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
11748 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
11749 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
11750 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
11751 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
11752 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
11753 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
11754 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
11755
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011756 vmcs12->guest_interruptibility_info =
11757 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
11758 vmcs12->guest_pending_dbg_exceptions =
11759 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010011760 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
11761 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
11762 else
11763 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011764
Jan Kiszkaf4124502014-03-07 20:03:13 +010011765 if (nested_cpu_has_preemption_timer(vmcs12)) {
11766 if (vmcs12->vm_exit_controls &
11767 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
11768 vmcs12->vmx_preemption_timer_value =
11769 vmx_get_preemption_timer_value(vcpu);
11770 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
11771 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080011772
Nadav Har'El3633cfc2013-08-05 11:07:07 +030011773 /*
11774 * In some cases (usually, nested EPT), L2 is allowed to change its
11775 * own CR3 without exiting. If it has changed it, we must keep it.
11776 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
11777 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
11778 *
11779 * Additionally, restore L2's PDPTR to vmcs12.
11780 */
11781 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010011782 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030011783 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
11784 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
11785 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
11786 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
11787 }
11788
Jim Mattsond281e132017-06-01 12:44:46 -070011789 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030011790
Wincy Van608406e2015-02-03 23:57:51 +080011791 if (nested_cpu_has_vid(vmcs12))
11792 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
11793
Jan Kiszkac18911a2013-03-13 16:06:41 +010011794 vmcs12->vm_entry_controls =
11795 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020011796 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010011797
Jan Kiszka2996fca2014-06-16 13:59:43 +020011798 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
11799 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
11800 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11801 }
11802
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011803 /* TODO: These cannot have changed unless we have MSR bitmaps and
11804 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020011805 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011806 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020011807 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
11808 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011809 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
11810 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
11811 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010011812 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011813 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011814}
11815
11816/*
11817 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
11818 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
11819 * and this function updates it to reflect the changes to the guest state while
11820 * L2 was running (and perhaps made some exits which were handled directly by L0
11821 * without going back to L1), and to reflect the exit reason.
11822 * Note that we do not have to copy here all VMCS fields, just those that
11823 * could have changed by the L2 guest or the exit - i.e., the guest-state and
11824 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
11825 * which already writes to vmcs12 directly.
11826 */
11827static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11828 u32 exit_reason, u32 exit_intr_info,
11829 unsigned long exit_qualification)
11830{
11831 /* update guest state fields: */
11832 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011833
11834 /* update exit information fields: */
11835
Jan Kiszka533558b2014-01-04 18:47:20 +010011836 vmcs12->vm_exit_reason = exit_reason;
11837 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010011838 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020011839
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011840 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011841 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
11842 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
11843
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011844 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070011845 vmcs12->launch_state = 1;
11846
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011847 /* vm_entry_intr_info_field is cleared on exit. Emulate this
11848 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011849 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011850
11851 /*
11852 * Transfer the event that L0 or L1 may wanted to inject into
11853 * L2 to IDT_VECTORING_INFO_FIELD.
11854 */
11855 vmcs12_save_pending_event(vcpu, vmcs12);
11856 }
11857
11858 /*
11859 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
11860 * preserved above and would only end up incorrectly in L1.
11861 */
11862 vcpu->arch.nmi_injected = false;
11863 kvm_clear_exception_queue(vcpu);
11864 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011865}
11866
Wanpeng Li5af41572017-11-05 16:54:49 -080011867static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
11868 struct vmcs12 *vmcs12)
11869{
11870 u32 entry_failure_code;
11871
11872 nested_ept_uninit_mmu_context(vcpu);
11873
11874 /*
11875 * Only PDPTE load can fail as the value of cr3 was checked on entry and
11876 * couldn't have changed.
11877 */
11878 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
11879 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
11880
11881 if (!enable_ept)
11882 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
11883}
11884
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011885/*
11886 * A part of what we need to when the nested L2 guest exits and we want to
11887 * run its L1 parent, is to reset L1's guest state to the host state specified
11888 * in vmcs12.
11889 * This function is to be called not only on normal nested exit, but also on
11890 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
11891 * Failures During or After Loading Guest State").
11892 * This function should be called when the active VMCS is L1's (vmcs01).
11893 */
Jan Kiszka733568f2013-02-23 15:07:47 +010011894static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
11895 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011896{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011897 struct kvm_segment seg;
11898
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011899 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
11900 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020011901 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011902 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11903 else
11904 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11905 vmx_set_efer(vcpu, vcpu->arch.efer);
11906
11907 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
11908 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070011909 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011910 /*
11911 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011912 * actually changed, because vmx_set_cr0 refers to efer set above.
11913 *
11914 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
11915 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011916 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011917 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020011918 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011919
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011920 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011921 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080011922 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011923
Wanpeng Li5af41572017-11-05 16:54:49 -080011924 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011925
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011926 if (enable_vpid) {
11927 /*
11928 * Trivially support vpid by letting L2s share their parent
11929 * L1's vpid. TODO: move to a more elaborate solution, giving
11930 * each L2 its own vpid and exposing the vpid feature to L1.
11931 */
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011932 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011933 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011934
11935 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
11936 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
11937 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
11938 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
11939 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020011940 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
11941 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011942
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011943 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
11944 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
11945 vmcs_write64(GUEST_BNDCFGS, 0);
11946
Jan Kiszka44811c02013-08-04 17:17:27 +020011947 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011948 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011949 vcpu->arch.pat = vmcs12->host_ia32_pat;
11950 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011951 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
11952 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
11953 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011954
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011955 /* Set L1 segment info according to Intel SDM
11956 27.5.2 Loading Host Segment and Descriptor-Table Registers */
11957 seg = (struct kvm_segment) {
11958 .base = 0,
11959 .limit = 0xFFFFFFFF,
11960 .selector = vmcs12->host_cs_selector,
11961 .type = 11,
11962 .present = 1,
11963 .s = 1,
11964 .g = 1
11965 };
11966 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
11967 seg.l = 1;
11968 else
11969 seg.db = 1;
11970 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
11971 seg = (struct kvm_segment) {
11972 .base = 0,
11973 .limit = 0xFFFFFFFF,
11974 .type = 3,
11975 .present = 1,
11976 .s = 1,
11977 .db = 1,
11978 .g = 1
11979 };
11980 seg.selector = vmcs12->host_ds_selector;
11981 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
11982 seg.selector = vmcs12->host_es_selector;
11983 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
11984 seg.selector = vmcs12->host_ss_selector;
11985 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
11986 seg.selector = vmcs12->host_fs_selector;
11987 seg.base = vmcs12->host_fs_base;
11988 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
11989 seg.selector = vmcs12->host_gs_selector;
11990 seg.base = vmcs12->host_gs_base;
11991 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
11992 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030011993 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011994 .limit = 0x67,
11995 .selector = vmcs12->host_tr_selector,
11996 .type = 11,
11997 .present = 1
11998 };
11999 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
12000
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012001 kvm_set_dr(vcpu, 7, 0x400);
12002 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030012003
Wincy Van3af18d92015-02-03 23:49:31 +080012004 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010012005 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080012006
Wincy Vanff651cb2014-12-11 08:52:58 +030012007 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
12008 vmcs12->vm_exit_msr_load_count))
12009 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012010}
12011
12012/*
12013 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
12014 * and modify vmcs12 to make it see what it would expect to see there if
12015 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
12016 */
Jan Kiszka533558b2014-01-04 18:47:20 +010012017static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
12018 u32 exit_intr_info,
12019 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012020{
12021 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012022 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12023
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012024 /* trying to cancel vmlaunch/vmresume is a bug */
12025 WARN_ON_ONCE(vmx->nested.nested_run_pending);
12026
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012027 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070012028 * The only expected VM-instruction error is "VM entry with
12029 * invalid control field(s)." Anything else indicates a
12030 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012031 */
Jim Mattson4f350c62017-09-14 16:31:44 -070012032 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
12033 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
12034
12035 leave_guest_mode(vcpu);
12036
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012037 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12038 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12039
Jim Mattson4f350c62017-09-14 16:31:44 -070012040 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012041 if (exit_reason == -1)
12042 sync_vmcs12(vcpu, vmcs12);
12043 else
12044 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
12045 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070012046
12047 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
12048 vmcs12->vm_exit_msr_store_count))
12049 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040012050 }
12051
Jim Mattson4f350c62017-09-14 16:31:44 -070012052 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020012053 vm_entry_controls_reset_shadow(vmx);
12054 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010012055 vmx_segment_cache_clear(vmx);
12056
Paolo Bonzini93140062016-07-06 13:23:51 +020012057 /* Update any VMCS fields that might have changed while L2 ran */
Jim Mattson83bafef2016-10-04 10:48:38 -070012058 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
12059 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010012060 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini93140062016-07-06 13:23:51 +020012061 if (vmx->hv_deadline_tsc == -1)
12062 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12063 PIN_BASED_VMX_PREEMPTION_TIMER);
12064 else
12065 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12066 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070012067 if (kvm_has_tsc_control)
12068 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012069
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020012070 if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
12071 vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
12072 vmx_set_virtual_x2apic_mode(vcpu,
12073 vcpu->arch.apic_base & X2APIC_ENABLE);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012074 } else if (!nested_cpu_has_ept(vmcs12) &&
12075 nested_cpu_has2(vmcs12,
12076 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070012077 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020012078 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012079
12080 /* This is needed for same reason as it was needed in prepare_vmcs02 */
12081 vmx->host_rsp = 0;
12082
12083 /* Unpin physical memory we referred to in vmcs02 */
12084 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012085 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012086 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012087 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012088 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012089 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012090 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012091 }
Wincy Van705699a2015-02-03 23:58:17 +080012092 if (vmx->nested.pi_desc_page) {
12093 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012094 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080012095 vmx->nested.pi_desc_page = NULL;
12096 vmx->nested.pi_desc = NULL;
12097 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012098
12099 /*
Tang Chen38b99172014-09-24 15:57:54 +080012100 * We are now running in L2, mmu_notifier will force to reload the
12101 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
12102 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080012103 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080012104
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012105 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030012106 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012107
12108 /* in case we halted in L2 */
12109 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070012110
12111 if (likely(!vmx->fail)) {
12112 /*
12113 * TODO: SDM says that with acknowledge interrupt on
12114 * exit, bit 31 of the VM-exit interrupt information
12115 * (valid interrupt) is always set to 1 on
12116 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
12117 * need kvm_cpu_has_interrupt(). See the commit
12118 * message for details.
12119 */
12120 if (nested_exit_intr_ack_set(vcpu) &&
12121 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
12122 kvm_cpu_has_interrupt(vcpu)) {
12123 int irq = kvm_cpu_get_interrupt(vcpu);
12124 WARN_ON(irq < 0);
12125 vmcs12->vm_exit_intr_info = irq |
12126 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
12127 }
12128
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012129 if (exit_reason != -1)
12130 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
12131 vmcs12->exit_qualification,
12132 vmcs12->idt_vectoring_info_field,
12133 vmcs12->vm_exit_intr_info,
12134 vmcs12->vm_exit_intr_error_code,
12135 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070012136
12137 load_vmcs12_host_state(vcpu, vmcs12);
12138
12139 return;
12140 }
12141
12142 /*
12143 * After an early L2 VM-entry failure, we're now back
12144 * in L1 which thinks it just finished a VMLAUNCH or
12145 * VMRESUME instruction, so we need to set the failure
12146 * flag and the VM-instruction error field of the VMCS
12147 * accordingly.
12148 */
12149 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080012150
12151 load_vmcs12_mmu_host_state(vcpu, vmcs12);
12152
Jim Mattson4f350c62017-09-14 16:31:44 -070012153 /*
12154 * The emulated instruction was already skipped in
12155 * nested_vmx_run, but the updated RIP was never
12156 * written back to the vmcs01.
12157 */
12158 skip_emulated_instruction(vcpu);
12159 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012160}
12161
Nadav Har'El7c177932011-05-25 23:12:04 +030012162/*
Jan Kiszka42124922014-01-04 18:47:19 +010012163 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
12164 */
12165static void vmx_leave_nested(struct kvm_vcpu *vcpu)
12166{
Wanpeng Li2f707d92017-03-06 04:03:28 -080012167 if (is_guest_mode(vcpu)) {
12168 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010012169 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080012170 }
Jan Kiszka42124922014-01-04 18:47:19 +010012171 free_nested(to_vmx(vcpu));
12172}
12173
12174/*
Nadav Har'El7c177932011-05-25 23:12:04 +030012175 * L1's failure to enter L2 is a subset of a normal exit, as explained in
12176 * 23.7 "VM-entry failures during or after loading guest state" (this also
12177 * lists the acceptable exit-reason and exit-qualification parameters).
12178 * It should only be called before L2 actually succeeded to run, and when
12179 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
12180 */
12181static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
12182 struct vmcs12 *vmcs12,
12183 u32 reason, unsigned long qualification)
12184{
12185 load_vmcs12_host_state(vcpu, vmcs12);
12186 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
12187 vmcs12->exit_qualification = qualification;
12188 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030012189 if (enable_shadow_vmcs)
12190 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030012191}
12192
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012193static int vmx_check_intercept(struct kvm_vcpu *vcpu,
12194 struct x86_instruction_info *info,
12195 enum x86_intercept_stage stage)
12196{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020012197 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12198 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
12199
12200 /*
12201 * RDPID causes #UD if disabled through secondary execution controls.
12202 * Because it is marked as EmulateOnUD, we need to intercept it here.
12203 */
12204 if (info->intercept == x86_intercept_rdtscp &&
12205 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
12206 ctxt->exception.vector = UD_VECTOR;
12207 ctxt->exception.error_code_valid = false;
12208 return X86EMUL_PROPAGATE_FAULT;
12209 }
12210
12211 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012212 return X86EMUL_CONTINUE;
12213}
12214
Yunhong Jiang64672c92016-06-13 14:19:59 -070012215#ifdef CONFIG_X86_64
12216/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
12217static inline int u64_shl_div_u64(u64 a, unsigned int shift,
12218 u64 divisor, u64 *result)
12219{
12220 u64 low = a << shift, high = a >> (64 - shift);
12221
12222 /* To avoid the overflow on divq */
12223 if (high >= divisor)
12224 return 1;
12225
12226 /* Low hold the result, high hold rem which is discarded */
12227 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
12228 "rm" (divisor), "0" (low), "1" (high));
12229 *result = low;
12230
12231 return 0;
12232}
12233
12234static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
12235{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012236 struct vcpu_vmx *vmx;
12237 u64 tscl, guest_tscl, delta_tsc;
12238
12239 if (kvm_mwait_in_guest(vcpu->kvm))
12240 return -EOPNOTSUPP;
12241
12242 vmx = to_vmx(vcpu);
12243 tscl = rdtsc();
12244 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
12245 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012246
12247 /* Convert to host delta tsc if tsc scaling is enabled */
12248 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
12249 u64_shl_div_u64(delta_tsc,
12250 kvm_tsc_scaling_ratio_frac_bits,
12251 vcpu->arch.tsc_scaling_ratio,
12252 &delta_tsc))
12253 return -ERANGE;
12254
12255 /*
12256 * If the delta tsc can't fit in the 32 bit after the multi shift,
12257 * we can't use the preemption timer.
12258 * It's possible that it fits on later vmentries, but checking
12259 * on every vmentry is costly so we just use an hrtimer.
12260 */
12261 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
12262 return -ERANGE;
12263
12264 vmx->hv_deadline_tsc = tscl + delta_tsc;
12265 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12266 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070012267
12268 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012269}
12270
12271static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
12272{
12273 struct vcpu_vmx *vmx = to_vmx(vcpu);
12274 vmx->hv_deadline_tsc = -1;
12275 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12276 PIN_BASED_VMX_PREEMPTION_TIMER);
12277}
12278#endif
12279
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012280static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012281{
Wanpeng Lib31c1142018-03-12 04:53:04 -070012282 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020012283 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012284}
12285
Kai Huang843e4332015-01-28 10:54:28 +080012286static void vmx_slot_enable_log_dirty(struct kvm *kvm,
12287 struct kvm_memory_slot *slot)
12288{
12289 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
12290 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
12291}
12292
12293static void vmx_slot_disable_log_dirty(struct kvm *kvm,
12294 struct kvm_memory_slot *slot)
12295{
12296 kvm_mmu_slot_set_dirty(kvm, slot);
12297}
12298
12299static void vmx_flush_log_dirty(struct kvm *kvm)
12300{
12301 kvm_flush_pml_buffers(kvm);
12302}
12303
Bandan Dasc5f983f2017-05-05 15:25:14 -040012304static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
12305{
12306 struct vmcs12 *vmcs12;
12307 struct vcpu_vmx *vmx = to_vmx(vcpu);
12308 gpa_t gpa;
12309 struct page *page = NULL;
12310 u64 *pml_address;
12311
12312 if (is_guest_mode(vcpu)) {
12313 WARN_ON_ONCE(vmx->nested.pml_full);
12314
12315 /*
12316 * Check if PML is enabled for the nested guest.
12317 * Whether eptp bit 6 is set is already checked
12318 * as part of A/D emulation.
12319 */
12320 vmcs12 = get_vmcs12(vcpu);
12321 if (!nested_cpu_has_pml(vmcs12))
12322 return 0;
12323
Dan Carpenter47698862017-05-10 22:43:17 +030012324 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040012325 vmx->nested.pml_full = true;
12326 return 1;
12327 }
12328
12329 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
12330
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020012331 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
12332 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040012333 return 0;
12334
12335 pml_address = kmap(page);
12336 pml_address[vmcs12->guest_pml_index--] = gpa;
12337 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012338 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040012339 }
12340
12341 return 0;
12342}
12343
Kai Huang843e4332015-01-28 10:54:28 +080012344static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
12345 struct kvm_memory_slot *memslot,
12346 gfn_t offset, unsigned long mask)
12347{
12348 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
12349}
12350
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012351static void __pi_post_block(struct kvm_vcpu *vcpu)
12352{
12353 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12354 struct pi_desc old, new;
12355 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012356
12357 do {
12358 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012359 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
12360 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012361
12362 dest = cpu_physical_id(vcpu->cpu);
12363
12364 if (x2apic_enabled())
12365 new.ndst = dest;
12366 else
12367 new.ndst = (dest << 8) & 0xFF00;
12368
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012369 /* set 'NV' to 'notification vector' */
12370 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012371 } while (cmpxchg64(&pi_desc->control, old.control,
12372 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012373
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012374 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
12375 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012376 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012377 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012378 vcpu->pre_pcpu = -1;
12379 }
12380}
12381
Feng Wuefc64402015-09-18 22:29:51 +080012382/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080012383 * This routine does the following things for vCPU which is going
12384 * to be blocked if VT-d PI is enabled.
12385 * - Store the vCPU to the wakeup list, so when interrupts happen
12386 * we can find the right vCPU to wake up.
12387 * - Change the Posted-interrupt descriptor as below:
12388 * 'NDST' <-- vcpu->pre_pcpu
12389 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
12390 * - If 'ON' is set during this process, which means at least one
12391 * interrupt is posted for this vCPU, we cannot block it, in
12392 * this case, return 1, otherwise, return 0.
12393 *
12394 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070012395static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012396{
Feng Wubf9f6ac2015-09-18 22:29:55 +080012397 unsigned int dest;
12398 struct pi_desc old, new;
12399 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12400
12401 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012402 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12403 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080012404 return 0;
12405
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012406 WARN_ON(irqs_disabled());
12407 local_irq_disable();
12408 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
12409 vcpu->pre_pcpu = vcpu->cpu;
12410 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12411 list_add_tail(&vcpu->blocked_vcpu_list,
12412 &per_cpu(blocked_vcpu_on_cpu,
12413 vcpu->pre_pcpu));
12414 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12415 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080012416
12417 do {
12418 old.control = new.control = pi_desc->control;
12419
Feng Wubf9f6ac2015-09-18 22:29:55 +080012420 WARN((pi_desc->sn == 1),
12421 "Warning: SN field of posted-interrupts "
12422 "is set before blocking\n");
12423
12424 /*
12425 * Since vCPU can be preempted during this process,
12426 * vcpu->cpu could be different with pre_pcpu, we
12427 * need to set pre_pcpu as the destination of wakeup
12428 * notification event, then we can find the right vCPU
12429 * to wakeup in wakeup handler if interrupts happen
12430 * when the vCPU is in blocked state.
12431 */
12432 dest = cpu_physical_id(vcpu->pre_pcpu);
12433
12434 if (x2apic_enabled())
12435 new.ndst = dest;
12436 else
12437 new.ndst = (dest << 8) & 0xFF00;
12438
12439 /* set 'NV' to 'wakeup vector' */
12440 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012441 } while (cmpxchg64(&pi_desc->control, old.control,
12442 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012443
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012444 /* We should not block the vCPU if an interrupt is posted for it. */
12445 if (pi_test_on(pi_desc) == 1)
12446 __pi_post_block(vcpu);
12447
12448 local_irq_enable();
12449 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012450}
12451
Yunhong Jiangbc225122016-06-13 14:19:58 -070012452static int vmx_pre_block(struct kvm_vcpu *vcpu)
12453{
12454 if (pi_pre_block(vcpu))
12455 return 1;
12456
Yunhong Jiang64672c92016-06-13 14:19:59 -070012457 if (kvm_lapic_hv_timer_in_use(vcpu))
12458 kvm_lapic_switch_to_sw_timer(vcpu);
12459
Yunhong Jiangbc225122016-06-13 14:19:58 -070012460 return 0;
12461}
12462
12463static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012464{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012465 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012466 return;
12467
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012468 WARN_ON(irqs_disabled());
12469 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012470 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012471 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080012472}
12473
Yunhong Jiangbc225122016-06-13 14:19:58 -070012474static void vmx_post_block(struct kvm_vcpu *vcpu)
12475{
Yunhong Jiang64672c92016-06-13 14:19:59 -070012476 if (kvm_x86_ops->set_hv_timer)
12477 kvm_lapic_switch_to_hv_timer(vcpu);
12478
Yunhong Jiangbc225122016-06-13 14:19:58 -070012479 pi_post_block(vcpu);
12480}
12481
Feng Wubf9f6ac2015-09-18 22:29:55 +080012482/*
Feng Wuefc64402015-09-18 22:29:51 +080012483 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
12484 *
12485 * @kvm: kvm
12486 * @host_irq: host irq of the interrupt
12487 * @guest_irq: gsi of the interrupt
12488 * @set: set or unset PI
12489 * returns 0 on success, < 0 on failure
12490 */
12491static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
12492 uint32_t guest_irq, bool set)
12493{
12494 struct kvm_kernel_irq_routing_entry *e;
12495 struct kvm_irq_routing_table *irq_rt;
12496 struct kvm_lapic_irq irq;
12497 struct kvm_vcpu *vcpu;
12498 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012499 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080012500
12501 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012502 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12503 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080012504 return 0;
12505
12506 idx = srcu_read_lock(&kvm->irq_srcu);
12507 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012508 if (guest_irq >= irq_rt->nr_rt_entries ||
12509 hlist_empty(&irq_rt->map[guest_irq])) {
12510 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
12511 guest_irq, irq_rt->nr_rt_entries);
12512 goto out;
12513 }
Feng Wuefc64402015-09-18 22:29:51 +080012514
12515 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
12516 if (e->type != KVM_IRQ_ROUTING_MSI)
12517 continue;
12518 /*
12519 * VT-d PI cannot support posting multicast/broadcast
12520 * interrupts to a vCPU, we still use interrupt remapping
12521 * for these kind of interrupts.
12522 *
12523 * For lowest-priority interrupts, we only support
12524 * those with single CPU as the destination, e.g. user
12525 * configures the interrupts via /proc/irq or uses
12526 * irqbalance to make the interrupts single-CPU.
12527 *
12528 * We will support full lowest-priority interrupt later.
12529 */
12530
Radim Krčmář371313132016-07-12 22:09:27 +020012531 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080012532 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
12533 /*
12534 * Make sure the IRTE is in remapped mode if
12535 * we don't handle it in posted mode.
12536 */
12537 ret = irq_set_vcpu_affinity(host_irq, NULL);
12538 if (ret < 0) {
12539 printk(KERN_INFO
12540 "failed to back to remapped mode, irq: %u\n",
12541 host_irq);
12542 goto out;
12543 }
12544
Feng Wuefc64402015-09-18 22:29:51 +080012545 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080012546 }
Feng Wuefc64402015-09-18 22:29:51 +080012547
12548 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
12549 vcpu_info.vector = irq.vector;
12550
hu huajun2698d822018-04-11 15:16:40 +080012551 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080012552 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
12553
12554 if (set)
12555 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080012556 else
Feng Wuefc64402015-09-18 22:29:51 +080012557 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080012558
12559 if (ret < 0) {
12560 printk(KERN_INFO "%s: failed to update PI IRTE\n",
12561 __func__);
12562 goto out;
12563 }
12564 }
12565
12566 ret = 0;
12567out:
12568 srcu_read_unlock(&kvm->irq_srcu, idx);
12569 return ret;
12570}
12571
Ashok Rajc45dcc72016-06-22 14:59:56 +080012572static void vmx_setup_mce(struct kvm_vcpu *vcpu)
12573{
12574 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
12575 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
12576 FEATURE_CONTROL_LMCE;
12577 else
12578 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
12579 ~FEATURE_CONTROL_LMCE;
12580}
12581
Ladi Prosek72d7b372017-10-11 16:54:41 +020012582static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
12583{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012584 /* we need a nested vmexit to enter SMM, postpone if run is pending */
12585 if (to_vmx(vcpu)->nested.nested_run_pending)
12586 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020012587 return 1;
12588}
12589
Ladi Prosek0234bf82017-10-11 16:54:40 +020012590static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
12591{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012592 struct vcpu_vmx *vmx = to_vmx(vcpu);
12593
12594 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
12595 if (vmx->nested.smm.guest_mode)
12596 nested_vmx_vmexit(vcpu, -1, 0, 0);
12597
12598 vmx->nested.smm.vmxon = vmx->nested.vmxon;
12599 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070012600 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020012601 return 0;
12602}
12603
12604static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
12605{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012606 struct vcpu_vmx *vmx = to_vmx(vcpu);
12607 int ret;
12608
12609 if (vmx->nested.smm.vmxon) {
12610 vmx->nested.vmxon = true;
12611 vmx->nested.smm.vmxon = false;
12612 }
12613
12614 if (vmx->nested.smm.guest_mode) {
12615 vcpu->arch.hflags &= ~HF_SMM_MASK;
12616 ret = enter_vmx_non_root_mode(vcpu, false);
12617 vcpu->arch.hflags |= HF_SMM_MASK;
12618 if (ret)
12619 return ret;
12620
12621 vmx->nested.smm.guest_mode = false;
12622 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020012623 return 0;
12624}
12625
Ladi Prosekcc3d9672017-10-17 16:02:39 +020012626static int enable_smi_window(struct kvm_vcpu *vcpu)
12627{
12628 return 0;
12629}
12630
Kees Cook404f6aa2016-08-08 16:29:06 -070012631static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080012632 .cpu_has_kvm_support = cpu_has_kvm_support,
12633 .disabled_by_bios = vmx_disabled_by_bios,
12634 .hardware_setup = hardware_setup,
12635 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030012636 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012637 .hardware_enable = hardware_enable,
12638 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080012639 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020012640 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012641
Wanpeng Lib31c1142018-03-12 04:53:04 -070012642 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070012643 .vm_alloc = vmx_vm_alloc,
12644 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070012645
Avi Kivity6aa8b732006-12-10 02:21:36 -080012646 .vcpu_create = vmx_create_vcpu,
12647 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030012648 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012649
Avi Kivity04d2cc72007-09-10 18:10:54 +030012650 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012651 .vcpu_load = vmx_vcpu_load,
12652 .vcpu_put = vmx_vcpu_put,
12653
Paolo Bonzinia96036b2015-11-10 11:55:36 +010012654 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060012655 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012656 .get_msr = vmx_get_msr,
12657 .set_msr = vmx_set_msr,
12658 .get_segment_base = vmx_get_segment_base,
12659 .get_segment = vmx_get_segment,
12660 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020012661 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012662 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020012663 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020012664 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030012665 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012666 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012667 .set_cr3 = vmx_set_cr3,
12668 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012669 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012670 .get_idt = vmx_get_idt,
12671 .set_idt = vmx_set_idt,
12672 .get_gdt = vmx_get_gdt,
12673 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010012674 .get_dr6 = vmx_get_dr6,
12675 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030012676 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010012677 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030012678 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012679 .get_rflags = vmx_get_rflags,
12680 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080012681
Avi Kivity6aa8b732006-12-10 02:21:36 -080012682 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012683
Avi Kivity6aa8b732006-12-10 02:21:36 -080012684 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020012685 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012686 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040012687 .set_interrupt_shadow = vmx_set_interrupt_shadow,
12688 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020012689 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030012690 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012691 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020012692 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030012693 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020012694 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012695 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010012696 .get_nmi_mask = vmx_get_nmi_mask,
12697 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012698 .enable_nmi_window = enable_nmi_window,
12699 .enable_irq_window = enable_irq_window,
12700 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +080012701 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080012702 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030012703 .get_enable_apicv = vmx_get_enable_apicv,
12704 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080012705 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010012706 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080012707 .hwapic_irr_update = vmx_hwapic_irr_update,
12708 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080012709 .sync_pir_to_irr = vmx_sync_pir_to_irr,
12710 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012711
Izik Eiduscbc94022007-10-25 00:29:55 +020012712 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070012713 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080012714 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080012715 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030012716
Avi Kivity586f9602010-11-18 13:09:54 +020012717 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020012718
Sheng Yang17cc3932010-01-05 19:02:27 +080012719 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080012720
12721 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080012722
12723 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000012724 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020012725
12726 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080012727
12728 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100012729
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012730 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100012731 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020012732
12733 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012734
12735 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080012736 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000012737 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080012738 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020012739 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012740
12741 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012742
12743 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080012744
12745 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
12746 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
12747 .flush_log_dirty = vmx_flush_log_dirty,
12748 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040012749 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020012750
Feng Wubf9f6ac2015-09-18 22:29:55 +080012751 .pre_block = vmx_pre_block,
12752 .post_block = vmx_post_block,
12753
Wei Huang25462f72015-06-19 15:45:05 +020012754 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080012755
12756 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070012757
12758#ifdef CONFIG_X86_64
12759 .set_hv_timer = vmx_set_hv_timer,
12760 .cancel_hv_timer = vmx_cancel_hv_timer,
12761#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080012762
12763 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020012764
Ladi Prosek72d7b372017-10-11 16:54:41 +020012765 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020012766 .pre_enter_smm = vmx_pre_enter_smm,
12767 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020012768 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012769};
12770
12771static int __init vmx_init(void)
12772{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010012773 int r;
12774
12775#if IS_ENABLED(CONFIG_HYPERV)
12776 /*
12777 * Enlightened VMCS usage should be recommended and the host needs
12778 * to support eVMCS v1 or above. We can also disable eVMCS support
12779 * with module parameter.
12780 */
12781 if (enlightened_vmcs &&
12782 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
12783 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
12784 KVM_EVMCS_VERSION) {
12785 int cpu;
12786
12787 /* Check that we have assist pages on all online CPUs */
12788 for_each_online_cpu(cpu) {
12789 if (!hv_get_vp_assist_page(cpu)) {
12790 enlightened_vmcs = false;
12791 break;
12792 }
12793 }
12794
12795 if (enlightened_vmcs) {
12796 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
12797 static_branch_enable(&enable_evmcs);
12798 }
12799 } else {
12800 enlightened_vmcs = false;
12801 }
12802#endif
12803
12804 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Tiejun Chen34a1cd62014-10-28 10:14:48 +080012805 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030012806 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080012807 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080012808
Dave Young2965faa2015-09-09 15:38:55 -070012809#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080012810 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
12811 crash_vmclear_local_loaded_vmcss);
12812#endif
12813
He, Qingfdef3ad2007-04-30 09:45:24 +030012814 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080012815}
12816
12817static void __exit vmx_exit(void)
12818{
Dave Young2965faa2015-09-09 15:38:55 -070012819#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053012820 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080012821 synchronize_rcu();
12822#endif
12823
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080012824 kvm_exit();
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010012825
12826#if IS_ENABLED(CONFIG_HYPERV)
12827 if (static_branch_unlikely(&enable_evmcs)) {
12828 int cpu;
12829 struct hv_vp_assist_page *vp_ap;
12830 /*
12831 * Reset everything to support using non-enlightened VMCS
12832 * access later (e.g. when we reload the module with
12833 * enlightened_vmcs=0)
12834 */
12835 for_each_online_cpu(cpu) {
12836 vp_ap = hv_get_vp_assist_page(cpu);
12837
12838 if (!vp_ap)
12839 continue;
12840
12841 vp_ap->current_nested_vmcs = 0;
12842 vp_ap->enlighten_vmentry = 0;
12843 }
12844
12845 static_branch_disable(&enable_evmcs);
12846 }
12847#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080012848}
12849
12850module_init(vmx_init)
12851module_exit(vmx_exit)