blob: cde40c52d2c620c3eaba55ec0d1061f87dc83855 [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
Sean Christopherson199b1182018-12-03 13:52:53 -080019#include <linux/frame.h>
20#include <linux/highmem.h>
21#include <linux/hrtimer.h>
22#include <linux/kernel.h>
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080024#include <linux/module.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020025#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070026#include <linux/mod_devicetable.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080027#include <linux/mm.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080028#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040030#include <linux/tboot.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080031#include <linux/trace_events.h>
Avi Kivitye4956062007-06-28 14:15:57 -040032
Sean Christopherson199b1182018-12-03 13:52:53 -080033#include <asm/apic.h>
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +020034#include <asm/asm.h>
Feng Wu28b835d2015-09-18 22:29:54 +080035#include <asm/cpu.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010036#include <asm/debugreg.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080037#include <asm/desc.h>
38#include <asm/fpu/internal.h>
39#include <asm/io.h>
Feng Wuefc64402015-09-18 22:29:51 +080040#include <asm/irq_remapping.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080041#include <asm/kexec.h>
42#include <asm/perf_event.h>
43#include <asm/mce.h>
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070044#include <asm/mmu_context.h>
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010045#include <asm/mshyperv.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080046#include <asm/spec-ctrl.h>
47#include <asm/virtext.h>
48#include <asm/vmx.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080049
Sean Christopherson3077c192018-12-03 13:53:02 -080050#include "capabilities.h"
Sean Christopherson199b1182018-12-03 13:52:53 -080051#include "cpuid.h"
Sean Christopherson4cebd742018-12-03 13:52:58 -080052#include "evmcs.h"
Sean Christopherson199b1182018-12-03 13:52:53 -080053#include "hyperv.h"
54#include "irq.h"
55#include "kvm_cache_regs.h"
56#include "lapic.h"
57#include "mmu.h"
Sean Christopherson89b0c9f2018-12-03 13:53:07 -080058#include "ops.h"
Wei Huang25462f72015-06-19 15:45:05 +020059#include "pmu.h"
Sean Christopherson199b1182018-12-03 13:52:53 -080060#include "trace.h"
Sean Christophersoncb1d4742018-12-03 13:53:04 -080061#include "vmcs.h"
Sean Christopherson609363c2018-12-03 13:53:05 -080062#include "vmcs12.h"
Sean Christopherson89b0c9f2018-12-03 13:53:07 -080063#include "vmx.h"
Sean Christopherson199b1182018-12-03 13:52:53 -080064#include "x86.h"
Sean Christopherson8373d252018-12-03 13:53:08 -080065#include "vmx.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030066
Avi Kivity6aa8b732006-12-10 02:21:36 -080067MODULE_AUTHOR("Qumranet");
68MODULE_LICENSE("GPL");
69
Josh Triplette9bda3b2012-03-20 23:33:51 -070070static const struct x86_cpu_id vmx_cpu_id[] = {
71 X86_FEATURE_MATCH(X86_FEATURE_VMX),
72 {}
73};
74MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
75
Sean Christopherson2c4fd912018-12-03 13:53:03 -080076bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020077module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080078
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010079static bool __read_mostly enable_vnmi = 1;
80module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
81
Sean Christopherson2c4fd912018-12-03 13:53:03 -080082bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020083module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020084
Sean Christopherson2c4fd912018-12-03 13:53:03 -080085bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020086module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080087
Sean Christopherson2c4fd912018-12-03 13:53:03 -080088bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070089module_param_named(unrestricted_guest,
90 enable_unrestricted_guest, bool, S_IRUGO);
91
Sean Christopherson2c4fd912018-12-03 13:53:03 -080092bool __read_mostly enable_ept_ad_bits = 1;
Xudong Hao83c3a332012-05-28 19:33:35 +080093module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
94
Avi Kivitya27685c2012-06-12 20:30:18 +030095static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020096module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030097
Rusty Russell476bc002012-01-13 09:32:18 +103098static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030099module_param(fasteoi, bool, S_IRUGO);
100
Yang Zhang5a717852013-04-11 19:25:16 +0800101static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +0800102module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800103
Abel Gordonabc4fc52013-04-18 14:35:25 +0300104static bool __read_mostly enable_shadow_vmcs = 1;
105module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300106/*
107 * If nested=1, nested virtualization is supported, i.e., guests may use
108 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
109 * use VMX instructions.
110 */
Paolo Bonzini1e58e5e2018-10-17 00:55:22 +0200111static bool __read_mostly nested = 1;
Nadav Har'El801d3422011-05-25 23:02:23 +0300112module_param(nested, bool, S_IRUGO);
113
Sean Christopherson52017602018-09-26 09:23:57 -0700114static bool __read_mostly nested_early_check = 0;
115module_param(nested_early_check, bool, S_IRUGO);
116
Wanpeng Li20300092014-12-02 19:14:59 +0800117static u64 __read_mostly host_xss;
118
Sean Christopherson2c4fd912018-12-03 13:53:03 -0800119bool __read_mostly enable_pml = 1;
Kai Huang843e4332015-01-28 10:54:28 +0800120module_param_named(pml, enable_pml, bool, S_IRUGO);
121
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100122#define MSR_BITMAP_MODE_X2APIC 1
123#define MSR_BITMAP_MODE_X2APIC_APICV 2
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100124
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
Sean Christopherson3de63472018-07-13 08:42:30 -0700134#define KVM_VM_CR0_ALWAYS_OFF (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 Kiszkaf41245002014-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;
Luiz Capitulinoa87c99e2018-11-23 12:02:14 -0500173module_param(ple_gap, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200174
Babu Moger7fbc85a2018-03-16 16:37:22 -0400175static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
176module_param(ple_window, uint, 0444);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800177
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200178/* Default doubles per-vcpu window every exit. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400179static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400180module_param(ple_window_grow, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200181
182/* Default resets per-vcpu window every exit to ple_window. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400183static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400184module_param(ple_window_shrink, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200185
186/* Default is to compute the maximum so we can never overflow. */
Babu Moger7fbc85a2018-03-16 16:37:22 -0400187static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
188module_param(ple_window_max, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200189
Avi Kivity83287ea422012-09-16 15:10:57 +0300190extern const ulong vmx_return;
Sean Christopherson52017602018-09-26 09:23:57 -0700191extern const ulong vmx_early_consistency_check_return;
Avi Kivity83287ea422012-09-16 15:10:57 +0300192
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200193static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
Nicolai Stange427362a2018-07-21 22:25:00 +0200194static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200195static DEFINE_MUTEX(vmx_l1d_flush_mutex);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200196
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200197/* Storage for pre module init parameter parsing */
198static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200199
200static const struct {
201 const char *option;
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200202 bool for_parse;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200203} vmentry_l1d_param[] = {
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200204 [VMENTER_L1D_FLUSH_AUTO] = {"auto", true},
205 [VMENTER_L1D_FLUSH_NEVER] = {"never", true},
206 [VMENTER_L1D_FLUSH_COND] = {"cond", true},
207 [VMENTER_L1D_FLUSH_ALWAYS] = {"always", true},
208 [VMENTER_L1D_FLUSH_EPT_DISABLED] = {"EPT disabled", false},
209 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = {"not required", false},
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200210};
211
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200212#define L1D_CACHE_ORDER 4
213static void *vmx_l1d_flush_pages;
214
215static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
216{
217 struct page *page;
Nicolai Stange288d1522018-07-18 19:07:38 +0200218 unsigned int i;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200219
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200220 if (!enable_ept) {
221 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
222 return 0;
223 }
224
Yi Wangd806afa2018-08-16 13:42:39 +0800225 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) {
226 u64 msr;
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200227
Yi Wangd806afa2018-08-16 13:42:39 +0800228 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr);
229 if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) {
230 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
231 return 0;
232 }
233 }
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200234
Jiri Kosinad90a7a02018-07-13 16:23:25 +0200235 /* If set to auto use the default l1tf mitigation method */
236 if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
237 switch (l1tf_mitigation) {
238 case L1TF_MITIGATION_OFF:
239 l1tf = VMENTER_L1D_FLUSH_NEVER;
240 break;
241 case L1TF_MITIGATION_FLUSH_NOWARN:
242 case L1TF_MITIGATION_FLUSH:
243 case L1TF_MITIGATION_FLUSH_NOSMT:
244 l1tf = VMENTER_L1D_FLUSH_COND;
245 break;
246 case L1TF_MITIGATION_FULL:
247 case L1TF_MITIGATION_FULL_FORCE:
248 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
249 break;
250 }
251 } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) {
252 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
253 }
254
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200255 if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
256 !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
257 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
258 if (!page)
259 return -ENOMEM;
260 vmx_l1d_flush_pages = page_address(page);
Nicolai Stange288d1522018-07-18 19:07:38 +0200261
262 /*
263 * Initialize each page with a different pattern in
264 * order to protect against KSM in the nested
265 * virtualization case.
266 */
267 for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) {
268 memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1,
269 PAGE_SIZE);
270 }
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200271 }
272
273 l1tf_vmx_mitigation = l1tf;
274
Thomas Gleixner895ae472018-07-13 16:23:22 +0200275 if (l1tf != VMENTER_L1D_FLUSH_NEVER)
276 static_branch_enable(&vmx_l1d_should_flush);
277 else
278 static_branch_disable(&vmx_l1d_should_flush);
Thomas Gleixner4c6523e2018-07-13 16:23:20 +0200279
Nicolai Stange427362a2018-07-21 22:25:00 +0200280 if (l1tf == VMENTER_L1D_FLUSH_COND)
281 static_branch_enable(&vmx_l1d_flush_cond);
Thomas Gleixner895ae472018-07-13 16:23:22 +0200282 else
Nicolai Stange427362a2018-07-21 22:25:00 +0200283 static_branch_disable(&vmx_l1d_flush_cond);
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200284 return 0;
285}
286
287static int vmentry_l1d_flush_parse(const char *s)
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200288{
289 unsigned int i;
290
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200291 if (s) {
292 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200293 if (vmentry_l1d_param[i].for_parse &&
294 sysfs_streq(s, vmentry_l1d_param[i].option))
295 return i;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200296 }
297 }
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200298 return -EINVAL;
299}
300
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200301static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
302{
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200303 int l1tf, ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200304
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200305 l1tf = vmentry_l1d_flush_parse(s);
306 if (l1tf < 0)
307 return l1tf;
308
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200309 if (!boot_cpu_has(X86_BUG_L1TF))
310 return 0;
311
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200312 /*
313 * Has vmx_init() run already? If not then this is the pre init
314 * parameter parsing. In that case just store the value and let
315 * vmx_init() do the proper setup after enable_ept has been
316 * established.
317 */
318 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
319 vmentry_l1d_flush_param = l1tf;
320 return 0;
321 }
322
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200323 mutex_lock(&vmx_l1d_flush_mutex);
324 ret = vmx_setup_l1d_flush(l1tf);
325 mutex_unlock(&vmx_l1d_flush_mutex);
326 return ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200327}
328
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200329static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
330{
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200331 if (WARN_ON_ONCE(l1tf_vmx_mitigation >= ARRAY_SIZE(vmentry_l1d_param)))
332 return sprintf(s, "???\n");
333
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200334 return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200335}
336
337static const struct kernel_param_ops vmentry_l1d_flush_ops = {
338 .set = vmentry_l1d_flush_set,
339 .get = vmentry_l1d_flush_get,
340};
Thomas Gleixner895ae472018-07-13 16:23:22 +0200341module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200342
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100343static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100344#define SHADOW_FIELD_RO(x) x,
Sean Christophersone0123112018-12-03 13:52:57 -0800345#include "vmcs_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300346};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400347static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300348 ARRAY_SIZE(shadow_read_only_fields);
349
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100350static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100351#define SHADOW_FIELD_RW(x) x,
Sean Christophersone0123112018-12-03 13:52:57 -0800352#include "vmcs_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300353};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400354static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300355 ARRAY_SIZE(shadow_read_write_fields);
356
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300357static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
358{
David Matlack4f2777b2016-07-13 17:16:37 -0700359 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300360}
361
Liran Alon61ada742018-06-23 02:35:08 +0300362static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu)
363{
364 return to_vmx(vcpu)->nested.cached_shadow_vmcs12;
365}
366
Peter Feiner995f00a2017-06-30 17:26:32 -0700367static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +0300368static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Orit Wassermanb246dd52012-05-31 14:49:22 +0300369static void vmx_set_segment(struct kvm_vcpu *vcpu,
370 struct kvm_segment *var, int seg);
371static void vmx_get_segment(struct kvm_vcpu *vcpu,
372 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +0200373static bool guest_state_valid(struct kvm_vcpu *vcpu);
374static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +0300375static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +0200376static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
377static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
378static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
379 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100380static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Yi Wang1e4329ee2018-11-08 11:22:21 +0800381static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
Ashok Raj15d45072018-02-01 22:59:43 +0100382 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +0300383
Avi Kivity6aa8b732006-12-10 02:21:36 -0800384static DEFINE_PER_CPU(struct vmcs *, vmxarea);
Sean Christopherson75edce82018-12-03 13:53:06 -0800385DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300386/*
387 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
388 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
389 */
390static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800391
Feng Wubf9f6ac2015-09-18 22:29:55 +0800392/*
393 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
394 * can find which vCPU should be waken up.
395 */
396static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
397static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
398
Radim Krčmář23611332016-09-29 22:41:33 +0200399enum {
Radim Krčmář23611332016-09-29 22:41:33 +0200400 VMX_VMREAD_BITMAP,
401 VMX_VMWRITE_BITMAP,
402 VMX_BITMAP_NR
403};
404
405static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
406
Radim Krčmář23611332016-09-29 22:41:33 +0200407#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
408#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +0300409
Sheng Yang2384d2b2008-01-17 15:14:33 +0800410static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
411static DEFINE_SPINLOCK(vmx_vpid_lock);
412
Sean Christopherson3077c192018-12-03 13:53:02 -0800413struct vmcs_config vmcs_config;
414struct vmx_capability vmx_capability;
Sheng Yangd56f5462008-04-25 10:13:16 +0800415
Avi Kivity6aa8b732006-12-10 02:21:36 -0800416#define VMX_SEGMENT_FIELD(seg) \
417 [VCPU_SREG_##seg] = { \
418 .selector = GUEST_##seg##_SELECTOR, \
419 .base = GUEST_##seg##_BASE, \
420 .limit = GUEST_##seg##_LIMIT, \
421 .ar_bytes = GUEST_##seg##_AR_BYTES, \
422 }
423
Mathias Krause772e0312012-08-30 01:30:19 +0200424static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800425 unsigned selector;
426 unsigned base;
427 unsigned limit;
428 unsigned ar_bytes;
429} kvm_vmx_segment_fields[] = {
430 VMX_SEGMENT_FIELD(CS),
431 VMX_SEGMENT_FIELD(DS),
432 VMX_SEGMENT_FIELD(ES),
433 VMX_SEGMENT_FIELD(FS),
434 VMX_SEGMENT_FIELD(GS),
435 VMX_SEGMENT_FIELD(SS),
436 VMX_SEGMENT_FIELD(TR),
437 VMX_SEGMENT_FIELD(LDTR),
438};
439
Avi Kivity26bb0982009-09-07 11:14:12 +0300440static u64 host_efer;
441
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300442static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
443
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300444/*
Brian Gerst8c065852010-07-17 09:03:26 -0400445 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300446 * away by decrementing the array size.
447 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800448static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800449#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300450 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800451#endif
Brian Gerst8c065852010-07-17 09:03:26 -0400452 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800453};
Avi Kivity6aa8b732006-12-10 02:21:36 -0800454
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +0100455#if IS_ENABLED(CONFIG_HYPERV)
456static bool __read_mostly enlightened_vmcs = true;
457module_param(enlightened_vmcs, bool, 0444);
458
Tianyu Lan877ad952018-07-19 08:40:23 +0000459/* check_ept_pointer() should be under protection of ept_pointer_lock. */
460static void check_ept_pointer_match(struct kvm *kvm)
461{
462 struct kvm_vcpu *vcpu;
463 u64 tmp_eptp = INVALID_PAGE;
464 int i;
465
466 kvm_for_each_vcpu(i, vcpu, kvm) {
467 if (!VALID_PAGE(tmp_eptp)) {
468 tmp_eptp = to_vmx(vcpu)->ept_pointer;
469 } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
470 to_kvm_vmx(kvm)->ept_pointers_match
471 = EPT_POINTERS_MISMATCH;
472 return;
473 }
474 }
475
476 to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
477}
478
479static int vmx_hv_remote_flush_tlb(struct kvm *kvm)
480{
Lan Tianyua5c214d2018-10-13 22:54:05 +0800481 struct kvm_vcpu *vcpu;
482 int ret = -ENOTSUPP, i;
Tianyu Lan877ad952018-07-19 08:40:23 +0000483
484 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
485
486 if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
487 check_ept_pointer_match(kvm);
488
Vitaly Kuznetsov5f8bb002018-10-11 12:03:12 +0200489 /*
490 * FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE hypercall needs the address of the
491 * base of EPT PML4 table, strip off EPT configuration information.
492 */
Tianyu Lan877ad952018-07-19 08:40:23 +0000493 if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
Lan Tianyua5c214d2018-10-13 22:54:05 +0800494 kvm_for_each_vcpu(i, vcpu, kvm)
495 ret |= hyperv_flush_guest_mapping(
Linus Torvalds0d1e8b82018-10-25 17:57:35 -0700496 to_vmx(kvm_get_vcpu(kvm, i))->ept_pointer & PAGE_MASK);
Lan Tianyua5c214d2018-10-13 22:54:05 +0800497 } else {
498 ret = hyperv_flush_guest_mapping(
Linus Torvalds0d1e8b82018-10-25 17:57:35 -0700499 to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer & PAGE_MASK);
Tianyu Lan877ad952018-07-19 08:40:23 +0000500 }
Tianyu Lan877ad952018-07-19 08:40:23 +0000501
Tianyu Lan877ad952018-07-19 08:40:23 +0000502 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
503 return ret;
504}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +0100505#endif /* IS_ENABLED(CONFIG_HYPERV) */
506
Yunhong Jiang64672c92016-06-13 14:19:59 -0700507/*
508 * Comment's format: document - errata name - stepping - processor name.
509 * Refer from
510 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
511 */
512static u32 vmx_preemption_cpu_tfms[] = {
513/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
5140x000206E6,
515/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
516/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
517/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
5180x00020652,
519/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
5200x00020655,
521/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
522/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
523/*
524 * 320767.pdf - AAP86 - B1 -
525 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
526 */
5270x000106E5,
528/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
5290x000106A0,
530/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
5310x000106A1,
532/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
5330x000106A4,
534 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
535 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
536 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
5370x000106A5,
538};
539
540static inline bool cpu_has_broken_vmx_preemption_timer(void)
541{
542 u32 eax = cpuid_eax(0x00000001), i;
543
544 /* Clear the reserved bits */
545 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +0000546 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -0700547 if (eax == vmx_preemption_cpu_tfms[i])
548 return true;
549
550 return false;
551}
552
Paolo Bonzini35754c92015-07-29 12:05:37 +0200553static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800554{
Paolo Bonzini35754c92015-07-29 12:05:37 +0200555 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800556}
557
Sheng Yang04547152009-04-01 15:52:31 +0800558static inline bool report_flexpriority(void)
559{
560 return flexpriority_enabled;
561}
562
Jim Mattsonc7c2c7092017-05-05 11:28:09 -0700563static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
564{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100565 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c7092017-05-05 11:28:09 -0700566}
567
Jim Mattsonf4160e42018-05-29 09:11:33 -0700568/*
569 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
570 * to modify any valid field of the VMCS, or are the VM-exit
571 * information fields read-only?
572 */
573static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
574{
575 return to_vmx(vcpu)->nested.msrs.misc_low &
576 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
577}
578
Marc Orr04473782018-06-20 17:21:29 -0700579static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
580{
581 return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
582}
583
584static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
585{
586 return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
587 CPU_BASED_MONITOR_TRAP_FLAG;
588}
589
Liran Alonfa97d7d2018-07-18 14:07:59 +0200590static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu)
591{
592 return to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
593 SECONDARY_EXEC_SHADOW_VMCS;
594}
595
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300596static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
597{
598 return vmcs12->cpu_based_vm_exec_control & bit;
599}
600
601static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
602{
603 return (vmcs12->cpu_based_vm_exec_control &
604 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
605 (vmcs12->secondary_vm_exec_control & bit);
606}
607
Jan Kiszkaf41245002014-03-07 20:03:13 +0100608static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
609{
610 return vmcs12->pin_based_vm_exec_control &
611 PIN_BASED_VMX_PREEMPTION_TIMER;
612}
613
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -0500614static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
615{
616 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
617}
618
619static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
620{
621 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
622}
623
Nadav Har'El155a97a2013-08-05 11:07:16 +0300624static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
625{
626 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
627}
628
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800629static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
630{
Paolo Bonzini3db13482017-08-24 14:48:03 +0200631 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800632}
633
Bandan Dasc5f983f2017-05-05 15:25:14 -0400634static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
635{
636 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
637}
638
Wincy Vanf2b93282015-02-03 23:56:03 +0800639static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
640{
641 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
642}
643
Wanpeng Li5c614b32015-10-13 09:18:36 -0700644static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
645{
646 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
647}
648
Wincy Van82f0dd42015-02-03 23:57:18 +0800649static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
650{
651 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
652}
653
Wincy Van608406e2015-02-03 23:57:51 +0800654static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
655{
656 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
657}
658
Wincy Van705699a2015-02-03 23:58:17 +0800659static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
660{
661 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
662}
663
Bandan Das27c42a12017-08-03 15:54:42 -0400664static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
665{
666 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
667}
668
Bandan Das41ab9372017-08-03 15:54:43 -0400669static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
670{
671 return nested_cpu_has_vmfunc(vmcs12) &&
672 (vmcs12->vm_function_control &
673 VMX_VMFUNC_EPTP_SWITCHING);
674}
675
Liran Alonf792d272018-06-23 02:35:05 +0300676static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12)
677{
678 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS);
679}
680
Krish Sadhukhan14aa61d2018-11-01 01:21:58 -0400681static inline bool nested_cpu_has_save_preemption_timer(struct vmcs12 *vmcs12)
682{
683 return vmcs12->vm_exit_controls &
684 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER;
685}
686
Jan Kiszka533558b2014-01-04 18:47:20 +0100687static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
688 u32 exit_intr_info,
689 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +0300690
Rusty Russell8b9cf982007-07-30 16:31:43 +1000691static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800692{
693 int i;
694
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400695 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +0300696 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300697 return i;
698 return -1;
699}
700
Avi Kivity26bb0982009-09-07 11:14:12 +0300701static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300702{
703 int i;
704
Rusty Russell8b9cf982007-07-30 16:31:43 +1000705 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300706 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400707 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000708 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800709}
710
Sean Christopherson89b0c9f2018-12-03 13:53:07 -0800711void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
Nadav Har'Eld462b812011-05-24 15:26:10 +0300712{
713 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -0700714 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
715 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300716 loaded_vmcs->cpu = -1;
717 loaded_vmcs->launched = 0;
718}
719
Dave Young2965faa2015-09-09 15:38:55 -0700720#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +0800721/*
722 * This bitmap is used to indicate whether the vmclear
723 * operation is enabled on all cpus. All disabled by
724 * default.
725 */
726static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
727
728static inline void crash_enable_local_vmclear(int cpu)
729{
730 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
731}
732
733static inline void crash_disable_local_vmclear(int cpu)
734{
735 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
736}
737
738static inline int crash_local_vmclear_enabled(int cpu)
739{
740 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
741}
742
743static void crash_vmclear_local_loaded_vmcss(void)
744{
745 int cpu = raw_smp_processor_id();
746 struct loaded_vmcs *v;
747
748 if (!crash_local_vmclear_enabled(cpu))
749 return;
750
751 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
752 loaded_vmcss_on_cpu_link)
753 vmcs_clear(v->vmcs);
754}
755#else
756static inline void crash_enable_local_vmclear(int cpu) { }
757static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -0700758#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +0800759
Nadav Har'Eld462b812011-05-24 15:26:10 +0300760static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800761{
Nadav Har'Eld462b812011-05-24 15:26:10 +0300762 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800763 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800764
Nadav Har'Eld462b812011-05-24 15:26:10 +0300765 if (loaded_vmcs->cpu != cpu)
766 return; /* vcpu migration can race with cpu offline */
767 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800768 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +0800769 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300770 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +0800771
772 /*
773 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
774 * is before setting loaded_vmcs->vcpu to -1 which is done in
775 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
776 * then adds the vmcs into percpu list before it is deleted.
777 */
778 smp_wmb();
779
Nadav Har'Eld462b812011-05-24 15:26:10 +0300780 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +0800781 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800782}
783
Sean Christopherson89b0c9f2018-12-03 13:53:07 -0800784void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800785{
Xiao Guangronge6c7d322012-11-28 20:53:15 +0800786 int cpu = loaded_vmcs->cpu;
787
788 if (cpu != -1)
789 smp_call_function_single(cpu,
790 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800791}
792
Avi Kivity2fb92db2011-04-27 19:42:18 +0300793static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
794 unsigned field)
795{
796 bool ret;
797 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
798
799 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
800 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
801 vmx->segment_cache.bitmask = 0;
802 }
803 ret = vmx->segment_cache.bitmask & mask;
804 vmx->segment_cache.bitmask |= mask;
805 return ret;
806}
807
808static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
809{
810 u16 *p = &vmx->segment_cache.seg[seg].selector;
811
812 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
813 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
814 return *p;
815}
816
817static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
818{
819 ulong *p = &vmx->segment_cache.seg[seg].base;
820
821 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
822 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
823 return *p;
824}
825
826static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
827{
828 u32 *p = &vmx->segment_cache.seg[seg].limit;
829
830 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
831 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
832 return *p;
833}
834
835static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
836{
837 u32 *p = &vmx->segment_cache.seg[seg].ar;
838
839 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
840 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
841 return *p;
842}
843
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300844static void update_exception_bitmap(struct kvm_vcpu *vcpu)
845{
846 u32 eb;
847
Jan Kiszkafd7373c2010-01-20 18:20:20 +0100848 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -0800849 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +0200850 /*
851 * Guest access to VMware backdoor ports could legitimately
852 * trigger #GP because of TSS I/O permission bitmap.
853 * We intercept those #GP and allow access to them anyway
854 * as VMware does.
855 */
856 if (enable_vmware_backdoor)
857 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +0100858 if ((vcpu->guest_debug &
859 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
860 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
861 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300862 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300863 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +0200864 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +0800865 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +0300866
867 /* When we are running a nested L2 guest and L1 specified for it a
868 * certain exception bitmap, we must trap the same exceptions and pass
869 * them to L1. When running L2, we will only handle the exceptions
870 * specified above if L1 did not want them.
871 */
872 if (is_guest_mode(vcpu))
873 eb |= get_vmcs12(vcpu)->exception_bitmap;
874
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300875 vmcs_write32(EXCEPTION_BITMAP, eb);
876}
877
Ashok Raj15d45072018-02-01 22:59:43 +0100878/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100879 * Check if MSR is intercepted for currently loaded MSR bitmap.
880 */
881static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
882{
883 unsigned long *msr_bitmap;
884 int f = sizeof(unsigned long);
885
886 if (!cpu_has_vmx_msr_bitmap())
887 return true;
888
889 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
890
891 if (msr <= 0x1fff) {
892 return !!test_bit(msr, msr_bitmap + 0x800 / f);
893 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
894 msr &= 0x1fff;
895 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
896 }
897
898 return true;
899}
900
901/*
Ashok Raj15d45072018-02-01 22:59:43 +0100902 * Check if MSR is intercepted for L01 MSR bitmap.
903 */
904static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
905{
906 unsigned long *msr_bitmap;
907 int f = sizeof(unsigned long);
908
909 if (!cpu_has_vmx_msr_bitmap())
910 return true;
911
912 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
913
914 if (msr <= 0x1fff) {
915 return !!test_bit(msr, msr_bitmap + 0x800 / f);
916 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
917 msr &= 0x1fff;
918 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
919 }
920
921 return true;
922}
923
Gleb Natapov2961e8762013-11-25 15:37:13 +0200924static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
925 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200926{
Gleb Natapov2961e8762013-11-25 15:37:13 +0200927 vm_entry_controls_clearbit(vmx, entry);
928 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200929}
930
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400931static int find_msr(struct vmx_msrs *m, unsigned int msr)
932{
933 unsigned int i;
934
935 for (i = 0; i < m->nr; ++i) {
936 if (m->val[i].index == msr)
937 return i;
938 }
939 return -ENOENT;
940}
941
Avi Kivity61d2ef22010-04-28 16:40:38 +0300942static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
943{
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400944 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300945 struct msr_autoload *m = &vmx->msr_autoload;
946
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200947 switch (msr) {
948 case MSR_EFER:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800949 if (cpu_has_load_ia32_efer()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200950 clear_atomic_switch_msr_special(vmx,
951 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200952 VM_EXIT_LOAD_IA32_EFER);
953 return;
954 }
955 break;
956 case MSR_CORE_PERF_GLOBAL_CTRL:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800957 if (cpu_has_load_perf_global_ctrl()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200958 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200959 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
960 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
961 return;
962 }
963 break;
Avi Kivity110312c2010-12-21 12:54:20 +0200964 }
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400965 i = find_msr(&m->guest, msr);
966 if (i < 0)
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400967 goto skip_guest;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400968 --m->guest.nr;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400969 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400970 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Avi Kivity110312c2010-12-21 12:54:20 +0200971
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400972skip_guest:
973 i = find_msr(&m->host, msr);
974 if (i < 0)
Avi Kivity61d2ef22010-04-28 16:40:38 +0300975 return;
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400976
977 --m->host.nr;
978 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400979 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +0300980}
981
Gleb Natapov2961e8762013-11-25 15:37:13 +0200982static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
983 unsigned long entry, unsigned long exit,
984 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
985 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200986{
987 vmcs_write64(guest_val_vmcs, guest_val);
Sean Christopherson5a5e8a12018-09-26 09:23:56 -0700988 if (host_val_vmcs != HOST_IA32_EFER)
989 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +0200990 vm_entry_controls_setbit(vmx, entry);
991 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200992}
993
Avi Kivity61d2ef22010-04-28 16:40:38 +0300994static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400995 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +0300996{
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400997 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300998 struct msr_autoload *m = &vmx->msr_autoload;
999
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001000 switch (msr) {
1001 case MSR_EFER:
Sean Christophersonc73da3f2018-12-03 13:53:00 -08001002 if (cpu_has_load_ia32_efer()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001003 add_atomic_switch_msr_special(vmx,
1004 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001005 VM_EXIT_LOAD_IA32_EFER,
1006 GUEST_IA32_EFER,
1007 HOST_IA32_EFER,
1008 guest_val, host_val);
1009 return;
1010 }
1011 break;
1012 case MSR_CORE_PERF_GLOBAL_CTRL:
Sean Christophersonc73da3f2018-12-03 13:53:00 -08001013 if (cpu_has_load_perf_global_ctrl()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001014 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001015 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1016 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
1017 GUEST_IA32_PERF_GLOBAL_CTRL,
1018 HOST_IA32_PERF_GLOBAL_CTRL,
1019 guest_val, host_val);
1020 return;
1021 }
1022 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01001023 case MSR_IA32_PEBS_ENABLE:
1024 /* PEBS needs a quiescent period after being disabled (to write
1025 * a record). Disabling PEBS through VMX MSR swapping doesn't
1026 * provide that period, so a CPU could write host's record into
1027 * guest's memory.
1028 */
1029 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02001030 }
1031
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04001032 i = find_msr(&m->guest, msr);
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04001033 if (!entry_only)
1034 j = find_msr(&m->host, msr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03001035
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04001036 if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02001037 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02001038 "Can't add msr %x\n", msr);
1039 return;
Avi Kivity61d2ef22010-04-28 16:40:38 +03001040 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04001041 if (i < 0) {
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04001042 i = m->guest.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04001043 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04001044 }
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04001045 m->guest.val[i].index = msr;
1046 m->guest.val[i].value = guest_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03001047
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04001048 if (entry_only)
1049 return;
1050
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04001051 if (j < 0) {
1052 j = m->host.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04001053 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03001054 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04001055 m->host.val[j].index = msr;
1056 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03001057}
1058
Avi Kivity92c0d902009-10-29 11:00:16 +02001059static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03001060{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001061 u64 guest_efer = vmx->vcpu.arch.efer;
1062 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03001063
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001064 if (!enable_ept) {
1065 /*
1066 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
1067 * host CPUID is more efficient than testing guest CPUID
1068 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
1069 */
1070 if (boot_cpu_has(X86_FEATURE_SMEP))
1071 guest_efer |= EFER_NX;
1072 else if (!(guest_efer & EFER_NX))
1073 ignore_bits |= EFER_NX;
1074 }
Roel Kluin3a34a882009-08-04 02:08:45 -07001075
Avi Kivity51c6cf62007-08-29 03:48:05 +03001076 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001077 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03001078 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001079 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03001080#ifdef CONFIG_X86_64
1081 ignore_bits |= EFER_LMA | EFER_LME;
1082 /* SCE is meaningful only in long mode on Intel */
1083 if (guest_efer & EFER_LMA)
1084 ignore_bits &= ~(u64)EFER_SCE;
1085#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03001086
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08001087 /*
1088 * On EPT, we can't emulate NX, so we must switch EFER atomically.
1089 * On CPUs that support "load IA32_EFER", always switch EFER
1090 * atomically, since it's faster than switching it manually.
1091 */
Sean Christophersonc73da3f2018-12-03 13:53:00 -08001092 if (cpu_has_load_ia32_efer() ||
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08001093 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03001094 if (!(guest_efer & EFER_LMA))
1095 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08001096 if (guest_efer != host_efer)
1097 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04001098 guest_efer, host_efer, false);
Sean Christopherson02343cf2018-09-26 09:23:43 -07001099 else
1100 clear_atomic_switch_msr(vmx, MSR_EFER);
Avi Kivity84ad33e2010-04-28 16:42:29 +03001101 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001102 } else {
Sean Christopherson02343cf2018-09-26 09:23:43 -07001103 clear_atomic_switch_msr(vmx, MSR_EFER);
1104
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001105 guest_efer &= ~ignore_bits;
1106 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03001107
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001108 vmx->guest_msrs[efer_offset].data = guest_efer;
1109 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
1110
1111 return true;
1112 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03001113}
1114
Andy Lutomirskie28baea2017-02-20 08:56:11 -08001115#ifdef CONFIG_X86_32
1116/*
1117 * On 32-bit kernels, VM exits still load the FS and GS bases from the
1118 * VMCS rather than the segment table. KVM uses this helper to figure
1119 * out the current bases to poke them into the VMCS before entry.
1120 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001121static unsigned long segment_base(u16 selector)
1122{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001123 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001124 unsigned long v;
1125
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001126 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001127 return 0;
1128
Thomas Garnier45fc8752017-03-14 10:05:08 -07001129 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001130
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001131 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001132 u16 ldt_selector = kvm_read_ldt();
1133
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001134 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001135 return 0;
1136
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001137 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001138 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001139 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001140 return v;
1141}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08001142#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001143
Sean Christopherson6d6095b2018-07-23 12:32:44 -07001144static void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03001145{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001146 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersond7ee0392018-07-23 12:32:47 -07001147 struct vmcs_host_state *host_state;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02001148#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01001149 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02001150#endif
Sean Christophersone368b872018-07-23 12:32:41 -07001151 unsigned long fs_base, gs_base;
1152 u16 fs_sel, gs_sel;
Avi Kivity26bb0982009-09-07 11:14:12 +03001153 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001154
Sean Christophersond264ee02018-08-27 15:21:12 -07001155 vmx->req_immediate_exit = false;
1156
Liran Alonf48b4712018-11-20 18:03:25 +02001157 /*
1158 * Note that guest MSRs to be saved/restored can also be changed
1159 * when guest state is loaded. This happens when guest transitions
1160 * to/from long-mode by setting MSR_EFER.LMA.
1161 */
1162 if (!vmx->loaded_cpu_state || vmx->guest_msrs_dirty) {
1163 vmx->guest_msrs_dirty = false;
1164 for (i = 0; i < vmx->save_nmsrs; ++i)
1165 kvm_set_shared_msr(vmx->guest_msrs[i].index,
1166 vmx->guest_msrs[i].data,
1167 vmx->guest_msrs[i].mask);
1168
1169 }
1170
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001171 if (vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03001172 return;
1173
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001174 vmx->loaded_cpu_state = vmx->loaded_vmcs;
Sean Christophersond7ee0392018-07-23 12:32:47 -07001175 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001176
Avi Kivity33ed6322007-05-02 16:54:03 +03001177 /*
1178 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
1179 * allow segment selectors with cpl > 0 or ti == 1.
1180 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07001181 host_state->ldt_sel = kvm_read_ldt();
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01001182
1183#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07001184 savesegment(ds, host_state->ds_sel);
1185 savesegment(es, host_state->es_sel);
Sean Christophersone368b872018-07-23 12:32:41 -07001186
1187 gs_base = cpu_kernelmode_gs_base(cpu);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02001188 if (likely(is_64bit_mm(current->mm))) {
1189 save_fsgs_for_kvm();
Sean Christophersone368b872018-07-23 12:32:41 -07001190 fs_sel = current->thread.fsindex;
1191 gs_sel = current->thread.gsindex;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02001192 fs_base = current->thread.fsbase;
Sean Christophersone368b872018-07-23 12:32:41 -07001193 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02001194 } else {
Sean Christophersone368b872018-07-23 12:32:41 -07001195 savesegment(fs, fs_sel);
1196 savesegment(gs, gs_sel);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02001197 fs_base = read_msr(MSR_FS_BASE);
Sean Christophersone368b872018-07-23 12:32:41 -07001198 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
Avi Kivity33ed6322007-05-02 16:54:03 +03001199 }
1200
Paolo Bonzini4679b612018-09-24 17:23:01 +02001201 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03001202#else
Sean Christophersone368b872018-07-23 12:32:41 -07001203 savesegment(fs, fs_sel);
1204 savesegment(gs, gs_sel);
1205 fs_base = segment_base(fs_sel);
1206 gs_base = segment_base(gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001207#endif
Sean Christophersone368b872018-07-23 12:32:41 -07001208
Sean Christopherson8f21a0b2018-07-23 12:32:49 -07001209 if (unlikely(fs_sel != host_state->fs_sel)) {
1210 if (!(fs_sel & 7))
1211 vmcs_write16(HOST_FS_SELECTOR, fs_sel);
1212 else
1213 vmcs_write16(HOST_FS_SELECTOR, 0);
1214 host_state->fs_sel = fs_sel;
1215 }
1216 if (unlikely(gs_sel != host_state->gs_sel)) {
1217 if (!(gs_sel & 7))
1218 vmcs_write16(HOST_GS_SELECTOR, gs_sel);
1219 else
1220 vmcs_write16(HOST_GS_SELECTOR, 0);
1221 host_state->gs_sel = gs_sel;
1222 }
Sean Christopherson5e079c72018-07-23 12:32:50 -07001223 if (unlikely(fs_base != host_state->fs_base)) {
1224 vmcs_writel(HOST_FS_BASE, fs_base);
1225 host_state->fs_base = fs_base;
1226 }
1227 if (unlikely(gs_base != host_state->gs_base)) {
1228 vmcs_writel(HOST_GS_BASE, gs_base);
1229 host_state->gs_base = gs_base;
1230 }
Avi Kivity33ed6322007-05-02 16:54:03 +03001231}
1232
Sean Christopherson6d6095b2018-07-23 12:32:44 -07001233static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03001234{
Sean Christophersond7ee0392018-07-23 12:32:47 -07001235 struct vmcs_host_state *host_state;
1236
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001237 if (!vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03001238 return;
1239
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001240 WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs);
Sean Christophersond7ee0392018-07-23 12:32:47 -07001241 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001242
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001243 ++vmx->vcpu.stat.host_state_reload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001244 vmx->loaded_cpu_state = NULL;
1245
Avi Kivityc8770e72010-11-11 12:37:26 +02001246#ifdef CONFIG_X86_64
Paolo Bonzini4679b612018-09-24 17:23:01 +02001247 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivityc8770e72010-11-11 12:37:26 +02001248#endif
Sean Christophersond7ee0392018-07-23 12:32:47 -07001249 if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
1250 kvm_load_ldt(host_state->ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001251#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07001252 load_gs_index(host_state->gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02001253#else
Sean Christophersond7ee0392018-07-23 12:32:47 -07001254 loadsegment(gs, host_state->gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001255#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03001256 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07001257 if (host_state->fs_sel & 7)
1258 loadsegment(fs, host_state->fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03001259#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07001260 if (unlikely(host_state->ds_sel | host_state->es_sel)) {
1261 loadsegment(ds, host_state->ds_sel);
1262 loadsegment(es, host_state->es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03001263 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03001264#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08001265 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03001266#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02001267 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001268#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07001269 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03001270}
1271
Sean Christopherson678e3152018-07-23 12:32:43 -07001272#ifdef CONFIG_X86_64
1273static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
Avi Kivitya9b21b62008-06-24 11:48:49 +03001274{
Paolo Bonzini4679b612018-09-24 17:23:01 +02001275 preempt_disable();
1276 if (vmx->loaded_cpu_state)
1277 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
1278 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07001279 return vmx->msr_guest_kernel_gs_base;
Avi Kivitya9b21b62008-06-24 11:48:49 +03001280}
1281
Sean Christopherson678e3152018-07-23 12:32:43 -07001282static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
1283{
Paolo Bonzini4679b612018-09-24 17:23:01 +02001284 preempt_disable();
1285 if (vmx->loaded_cpu_state)
1286 wrmsrl(MSR_KERNEL_GS_BASE, data);
1287 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07001288 vmx->msr_guest_kernel_gs_base = data;
1289}
1290#endif
1291
Feng Wu28b835d2015-09-18 22:29:54 +08001292static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
1293{
1294 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
1295 struct pi_desc old, new;
1296 unsigned int dest;
1297
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02001298 /*
1299 * In case of hot-plug or hot-unplug, we may have to undo
1300 * vmx_vcpu_pi_put even if there is no assigned device. And we
1301 * always keep PI.NDST up to date for simplicity: it makes the
1302 * code easier, and CPU migration is not a fast path.
1303 */
1304 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08001305 return;
1306
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02001307 /*
1308 * First handle the simple case where no cmpxchg is necessary; just
1309 * allow posting non-urgent interrupts.
1310 *
1311 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
1312 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
1313 * expects the VCPU to be on the blocked_vcpu_list that matches
1314 * PI.NDST.
1315 */
1316 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
1317 vcpu->cpu == cpu) {
1318 pi_clear_sn(pi_desc);
1319 return;
1320 }
1321
1322 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08001323 do {
1324 old.control = new.control = pi_desc->control;
1325
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02001326 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08001327
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02001328 if (x2apic_enabled())
1329 new.ndst = dest;
1330 else
1331 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08001332
Feng Wu28b835d2015-09-18 22:29:54 +08001333 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02001334 } while (cmpxchg64(&pi_desc->control, old.control,
1335 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08001336}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08001337
Avi Kivity6aa8b732006-12-10 02:21:36 -08001338/*
1339 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
1340 * vcpu mutex is already taken.
1341 */
Avi Kivity15ad7142007-07-11 18:17:21 +03001342static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001343{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001344 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001345 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001346
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001347 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01001348 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001349 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001350 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001351
1352 /*
1353 * Read loaded_vmcs->cpu should be before fetching
1354 * loaded_vmcs->loaded_vmcss_on_cpu_link.
1355 * See the comments in __loaded_vmcs_clear().
1356 */
1357 smp_rmb();
1358
Nadav Har'Eld462b812011-05-24 15:26:10 +03001359 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
1360 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001361 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001362 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001363 }
1364
1365 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
1366 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
1367 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01001368 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001369 }
1370
1371 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07001372 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001373 unsigned long sysenter_esp;
1374
1375 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001376
Avi Kivity6aa8b732006-12-10 02:21:36 -08001377 /*
1378 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08001379 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001380 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08001381 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01001382 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07001383 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001384
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08001385 /*
1386 * VM exits change the host TR limit to 0x67 after a VM
1387 * exit. This is okay, since 0x67 covers everything except
1388 * the IO bitmap and have have code to handle the IO bitmap
1389 * being lost after a VM exit.
1390 */
1391 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
1392
Avi Kivity6aa8b732006-12-10 02:21:36 -08001393 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
1394 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08001395
Nadav Har'Eld462b812011-05-24 15:26:10 +03001396 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001397 }
Feng Wu28b835d2015-09-18 22:29:54 +08001398
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001399 /* Setup TSC multiplier */
1400 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07001401 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
1402 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001403
Feng Wu28b835d2015-09-18 22:29:54 +08001404 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08001405 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08001406 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08001407}
1408
1409static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
1410{
1411 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
1412
1413 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08001414 !irq_remapping_cap(IRQ_POSTING_CAP) ||
1415 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08001416 return;
1417
1418 /* Set SN when the vCPU is preempted */
1419 if (vcpu->preempted)
1420 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001421}
1422
1423static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
1424{
Feng Wu28b835d2015-09-18 22:29:54 +08001425 vmx_vcpu_pi_put(vcpu);
1426
Sean Christopherson6d6095b2018-07-23 12:32:44 -07001427 vmx_prepare_switch_to_host(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001428}
1429
Wanpeng Lif244dee2017-07-20 01:11:54 -07001430static bool emulation_required(struct kvm_vcpu *vcpu)
1431{
1432 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
1433}
1434
Avi Kivityedcafe32009-12-30 18:07:40 +02001435static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
1436
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001437/*
1438 * Return the cr0 value that a nested guest would read. This is a combination
1439 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
1440 * its hypervisor (cr0_read_shadow).
1441 */
1442static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
1443{
1444 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
1445 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
1446}
1447static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
1448{
1449 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
1450 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
1451}
1452
Avi Kivity6aa8b732006-12-10 02:21:36 -08001453static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
1454{
Avi Kivity78ac8b42010-04-08 18:19:35 +03001455 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03001456
Avi Kivity6de12732011-03-07 12:51:22 +02001457 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
1458 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
1459 rflags = vmcs_readl(GUEST_RFLAGS);
1460 if (to_vmx(vcpu)->rmode.vm86_active) {
1461 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
1462 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
1463 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
1464 }
1465 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001466 }
Avi Kivity6de12732011-03-07 12:51:22 +02001467 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001468}
1469
1470static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1471{
Wanpeng Lif244dee2017-07-20 01:11:54 -07001472 unsigned long old_rflags = vmx_get_rflags(vcpu);
1473
Avi Kivity6de12732011-03-07 12:51:22 +02001474 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
1475 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001476 if (to_vmx(vcpu)->rmode.vm86_active) {
1477 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001478 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001479 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001480 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07001481
1482 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
1483 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001484}
1485
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02001486static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001487{
1488 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1489 int ret = 0;
1490
1491 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01001492 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001493 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01001494 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001495
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02001496 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001497}
1498
1499static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
1500{
1501 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1502 u32 interruptibility = interruptibility_old;
1503
1504 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
1505
Jan Kiszka48005f62010-02-19 19:38:07 +01001506 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001507 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01001508 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001509 interruptibility |= GUEST_INTR_STATE_STI;
1510
1511 if ((interruptibility != interruptibility_old))
1512 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
1513}
1514
Avi Kivity6aa8b732006-12-10 02:21:36 -08001515static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
1516{
1517 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001518
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001519 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001520 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001521 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001522
Glauber Costa2809f5d2009-05-12 16:21:05 -04001523 /* skipping an emulated instruction also counts */
1524 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001525}
1526
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001527static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
1528 unsigned long exit_qual)
1529{
1530 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1531 unsigned int nr = vcpu->arch.exception.nr;
1532 u32 intr_info = nr | INTR_INFO_VALID_MASK;
1533
1534 if (vcpu->arch.exception.has_error_code) {
1535 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
1536 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
1537 }
1538
1539 if (kvm_exception_is_soft(nr))
1540 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
1541 else
1542 intr_info |= INTR_TYPE_HARD_EXCEPTION;
1543
1544 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
1545 vmx_get_nmi_mask(vcpu))
1546 intr_info |= INTR_INFO_UNBLOCK_NMI;
1547
1548 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
1549}
1550
Nadav Har'El0b6ac342011-05-25 23:13:36 +03001551/*
1552 * KVM wants to inject page-faults which it got to the guest. This function
1553 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03001554 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07001555static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03001556{
1557 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07001558 unsigned int nr = vcpu->arch.exception.nr;
Jim Mattsonda998b42018-10-16 14:29:22 -07001559 bool has_payload = vcpu->arch.exception.has_payload;
1560 unsigned long payload = vcpu->arch.exception.payload;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03001561
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001562 if (nr == PF_VECTOR) {
1563 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07001564 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001565 return 1;
1566 }
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001567 if (nested_vmx_is_page_fault_vmexit(vmcs12,
1568 vcpu->arch.exception.error_code)) {
Jim Mattsonda998b42018-10-16 14:29:22 -07001569 *exit_qual = has_payload ? payload : vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001570 return 1;
1571 }
Jim Mattsonf10c7292018-10-16 14:29:23 -07001572 } else if (vmcs12->exception_bitmap & (1u << nr)) {
1573 if (nr == DB_VECTOR) {
1574 if (!has_payload) {
1575 payload = vcpu->arch.dr6;
1576 payload &= ~(DR6_FIXED_1 | DR6_BT);
1577 payload ^= DR6_RTM;
Jim Mattsoncfb634f2018-09-21 10:36:17 -07001578 }
Jim Mattsonf10c7292018-10-16 14:29:23 -07001579 *exit_qual = payload;
1580 } else
1581 *exit_qual = 0;
1582 return 1;
Wanpeng Liadfe20f2017-07-13 18:30:41 -07001583 }
1584
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001585 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03001586}
1587
Wanpeng Licaa057a2018-03-12 04:53:03 -07001588static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
1589{
1590 /*
1591 * Ensure that we clear the HLT state in the VMCS. We don't need to
1592 * explicitly skip the instruction because if the HLT state is set,
1593 * then the instruction is already executing and RIP has already been
1594 * advanced.
1595 */
1596 if (kvm_hlt_in_guest(vcpu->kvm) &&
1597 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
1598 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
1599}
1600
Wanpeng Licfcd20e2017-07-13 18:30:39 -07001601static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02001602{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001603 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07001604 unsigned nr = vcpu->arch.exception.nr;
1605 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07001606 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001607 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001608
Jim Mattsonda998b42018-10-16 14:29:22 -07001609 kvm_deliver_exception_payload(vcpu);
1610
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001611 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001612 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001613 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
1614 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001615
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001616 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05001617 int inc_eip = 0;
1618 if (kvm_exception_is_soft(nr))
1619 inc_eip = vcpu->arch.event_exit_inst_len;
1620 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02001621 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001622 return;
1623 }
1624
Sean Christophersonadd5ff72018-03-23 09:34:00 -07001625 WARN_ON_ONCE(vmx->emulation_required);
1626
Gleb Natapov66fd3f72009-05-11 13:35:50 +03001627 if (kvm_exception_is_soft(nr)) {
1628 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
1629 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001630 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
1631 } else
1632 intr_info |= INTR_TYPE_HARD_EXCEPTION;
1633
1634 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07001635
1636 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02001637}
1638
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001639static bool vmx_rdtscp_supported(void)
1640{
1641 return cpu_has_vmx_rdtscp();
1642}
1643
Mao, Junjiead756a12012-07-02 01:18:48 +00001644static bool vmx_invpcid_supported(void)
1645{
Junaid Shahideb4b2482018-06-27 14:59:14 -07001646 return cpu_has_vmx_invpcid();
Mao, Junjiead756a12012-07-02 01:18:48 +00001647}
1648
Avi Kivity6aa8b732006-12-10 02:21:36 -08001649/*
Eddie Donga75beee2007-05-17 18:55:15 +03001650 * Swap MSR entry in host/guest MSR entry array.
1651 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001652static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03001653{
Avi Kivity26bb0982009-09-07 11:14:12 +03001654 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001655
1656 tmp = vmx->guest_msrs[to];
1657 vmx->guest_msrs[to] = vmx->guest_msrs[from];
1658 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03001659}
1660
1661/*
Avi Kivitye38aea32007-04-19 13:22:48 +03001662 * Set up the vmcs to automatically save and restore system
1663 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
1664 * mode, as fiddling with msrs is very expensive.
1665 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001666static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03001667{
Avi Kivity26bb0982009-09-07 11:14:12 +03001668 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03001669
Eddie Donga75beee2007-05-17 18:55:15 +03001670 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001671#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10001672 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10001673 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03001674 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001675 move_msr_up(vmx, index, save_nmsrs++);
1676 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03001677 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001678 move_msr_up(vmx, index, save_nmsrs++);
1679 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03001680 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001681 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001682 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02001683 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001684 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03001685 /*
Brian Gerst8c065852010-07-17 09:03:26 -04001686 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03001687 * if efer.sce is enabled.
1688 */
Brian Gerst8c065852010-07-17 09:03:26 -04001689 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02001690 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10001691 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001692 }
Eddie Donga75beee2007-05-17 18:55:15 +03001693#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02001694 index = __find_msr_index(vmx, MSR_EFER);
1695 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03001696 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001697
Avi Kivity26bb0982009-09-07 11:14:12 +03001698 vmx->save_nmsrs = save_nmsrs;
Liran Alonf48b4712018-11-20 18:03:25 +02001699 vmx->guest_msrs_dirty = true;
Avi Kivity58972972009-02-24 22:26:47 +02001700
Yang Zhang8d146952013-01-25 10:18:50 +08001701 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001702 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03001703}
1704
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001705static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001706{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001707 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001708
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001709 if (is_guest_mode(vcpu) &&
1710 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
1711 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
1712
1713 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001714}
1715
Leonid Shatz326e7422018-11-06 12:14:25 +02001716static u64 vmx_write_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001717{
Paolo Bonzini45c3af92018-11-25 18:45:35 +01001718 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1719 u64 g_tsc_offset = 0;
Leonid Shatz326e7422018-11-06 12:14:25 +02001720
Paolo Bonzini45c3af92018-11-25 18:45:35 +01001721 /*
1722 * We're here if L1 chose not to trap WRMSR to TSC. According
1723 * to the spec, this should set L1's TSC; The offset that L1
1724 * set for L2 remains unchanged, and still needs to be added
1725 * to the newly set TSC to get L2's TSC.
1726 */
1727 if (is_guest_mode(vcpu) &&
1728 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
1729 g_tsc_offset = vmcs12->tsc_offset;
1730
1731 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
1732 vcpu->arch.tsc_offset - g_tsc_offset,
1733 offset);
1734 vmcs_write64(TSC_OFFSET, offset + g_tsc_offset);
1735 return offset + g_tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001736}
1737
Nadav Har'El801d3422011-05-25 23:02:23 +03001738/*
1739 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
1740 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
1741 * all guests if the "nested" module option is off, and can also be disabled
1742 * for a single guest by disabling its VMX cpuid bit.
1743 */
1744static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
1745{
Radim Krčmářd6321d42017-08-05 00:12:49 +02001746 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03001747}
1748
Avi Kivity6aa8b732006-12-10 02:21:36 -08001749/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03001750 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
1751 * returned for the various VMX controls MSRs when nested VMX is enabled.
1752 * The same values should also be used to verify that vmcs12 control fields are
1753 * valid during nested entry from L1 to L2.
1754 * Each of these control msrs has a low and high 32-bit half: A low bit is on
1755 * if the corresponding bit in the (32-bit) control field *must* be on, and a
1756 * bit in the high half is on if the corresponding bit in the control field
1757 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03001758 */
Sean Christopherson7caaa712018-12-03 13:53:01 -08001759static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs,
1760 u32 ept_caps, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03001761{
Paolo Bonzini13893092018-02-26 13:40:09 +01001762 if (!nested) {
1763 memset(msrs, 0, sizeof(*msrs));
1764 return;
1765 }
1766
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03001767 /*
1768 * Note that as a general rule, the high half of the MSRs (bits in
1769 * the control fields which may be 1) should be initialized by the
1770 * intersection of the underlying hardware's MSR (i.e., features which
1771 * can be supported) and the list of features we want to expose -
1772 * because they are known to be properly supported in our code.
1773 * Also, usually, the low half of the MSRs (bits which must be 1) can
1774 * be set to 0, meaning that L1 may turn off any of these bits. The
1775 * reason is that if one of these bits is necessary, it will appear
1776 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
1777 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02001778 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03001779 * These rules have exceptions below.
1780 */
1781
1782 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01001783 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001784 msrs->pinbased_ctls_low,
1785 msrs->pinbased_ctls_high);
1786 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08001787 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001788 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08001789 PIN_BASED_EXT_INTR_MASK |
1790 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01001791 PIN_BASED_VIRTUAL_NMIS |
1792 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001793 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08001794 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01001795 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03001796
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02001797 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08001798 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001799 msrs->exit_ctls_low,
1800 msrs->exit_ctls_high);
1801 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08001802 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04001803
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001804 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03001805#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08001806 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03001807#endif
Jan Kiszkaf41245002014-03-07 20:03:13 +01001808 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001809 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08001810 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf41245002014-03-07 20:03:13 +01001811 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04001812 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
1813
Jan Kiszka2996fca2014-06-16 13:59:43 +02001814 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001815 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02001816
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03001817 /* entry controls */
1818 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001819 msrs->entry_ctls_low,
1820 msrs->entry_ctls_high);
1821 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08001822 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001823 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02001824#ifdef CONFIG_X86_64
1825 VM_ENTRY_IA32E_MODE |
1826#endif
1827 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001828 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08001829 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Jan Kiszka57435342013-08-06 10:39:56 +02001830
Jan Kiszka2996fca2014-06-16 13:59:43 +02001831 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001832 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02001833
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03001834 /* cpu-based controls */
1835 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001836 msrs->procbased_ctls_low,
1837 msrs->procbased_ctls_high);
1838 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08001839 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001840 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01001841 CPU_BASED_VIRTUAL_INTR_PENDING |
1842 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03001843 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
1844 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
1845 CPU_BASED_CR3_STORE_EXITING |
1846#ifdef CONFIG_X86_64
1847 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
1848#endif
1849 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03001850 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
1851 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
1852 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
1853 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03001854 /*
1855 * We can allow some features even when not supported by the
1856 * hardware. For example, L1 can specify an MSR bitmap - and we
1857 * can use it to avoid exits to L1 - even when L0 runs L2
1858 * without MSR bitmaps.
1859 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001860 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08001861 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02001862 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03001863
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02001864 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001865 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02001866 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
1867
Paolo Bonzini80154d72017-08-24 13:55:35 +02001868 /*
1869 * secondary cpu-based controls. Do not include those that
1870 * depend on CPUID bits, they are added later by vmx_cpuid_update.
1871 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03001872 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001873 msrs->secondary_ctls_low,
1874 msrs->secondary_ctls_high);
1875 msrs->secondary_ctls_low = 0;
1876 msrs->secondary_ctls_high &=
Paolo Bonzini1b073042016-10-25 16:06:30 +02001877 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08001878 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08001879 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08001880 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02001881 SECONDARY_EXEC_WBINVD_EXITING;
Paolo Bonzini2cf7ea92018-10-03 10:34:00 +02001882
Liran Alon32c7acf2018-06-23 02:35:11 +03001883 /*
1884 * We can emulate "VMCS shadowing," even if the hardware
1885 * doesn't support it.
1886 */
1887 msrs->secondary_ctls_high |=
1888 SECONDARY_EXEC_SHADOW_VMCS;
Jan Kiszkac18911a2013-03-13 16:06:41 +01001889
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02001890 if (enable_ept) {
1891 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001892 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01001893 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001894 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01001895 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04001896 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001897 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04001898 VMX_EPT_EXECUTE_ONLY_BIT;
Sean Christopherson7caaa712018-12-03 13:53:01 -08001899 msrs->ept_caps &= ept_caps;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001900 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01001901 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
1902 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04001903 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001904 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04001905 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001906 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04001907 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02001908 }
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02001909
Bandan Das27c42a12017-08-03 15:54:42 -04001910 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001911 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04001912 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04001913 /*
1914 * Advertise EPTP switching unconditionally
1915 * since we emulate it
1916 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08001917 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001918 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08001919 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04001920 }
1921
Paolo Bonzinief697a72016-03-18 16:58:38 +01001922 /*
1923 * Old versions of KVM use the single-context version without
1924 * checking for support, so declare that it is supported even
1925 * though it is treated as global context. The alternative is
1926 * not failing the single-context invvpid, and it is worse.
1927 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07001928 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001929 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07001930 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001931 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03001932 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02001933 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07001934
Radim Krčmář0790ec12015-03-17 14:02:32 +01001935 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001936 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01001937 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1938
Paolo Bonzini2cf7ea92018-10-03 10:34:00 +02001939 if (flexpriority_enabled)
1940 msrs->secondary_ctls_high |=
1941 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1942
Jan Kiszkac18911a2013-03-13 16:06:41 +01001943 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08001944 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001945 msrs->misc_low,
1946 msrs->misc_high);
1947 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
1948 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07001949 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08001950 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf41245002014-03-07 20:03:13 +01001951 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001952 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08001953
1954 /*
1955 * This MSR reports some information about VMX support. We
1956 * should return information about the VMX we emulate for the
1957 * guest, and the VMCS structure we give it - not about the
1958 * VMX support of the underlying hardware.
1959 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001960 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08001961 VMCS12_REVISION |
1962 VMX_BASIC_TRUE_CTLS |
1963 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
1964 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
1965
1966 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001967 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08001968
1969 /*
David Matlack8322ebb2016-11-29 18:14:09 -08001970 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08001971 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
1972 * We picked the standard core2 setting.
1973 */
1974#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
1975#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001976 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
1977 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08001978
1979 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001980 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
1981 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08001982
1983 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001984 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03001985}
1986
David Matlack38991522016-11-29 18:14:08 -08001987/*
1988 * if fixed0[i] == 1: val[i] must be 1
1989 * if fixed1[i] == 0: val[i] must be 0
1990 */
1991static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
1992{
1993 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03001994}
1995
1996static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
1997{
David Matlack38991522016-11-29 18:14:08 -08001998 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03001999}
2000
2001static inline u64 vmx_control_msr(u32 low, u32 high)
2002{
2003 return low | ((u64)high << 32);
2004}
2005
David Matlack62cc6b9d2016-11-29 18:14:07 -08002006static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
2007{
2008 superset &= mask;
2009 subset &= mask;
2010
2011 return (superset | subset) == superset;
2012}
2013
2014static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
2015{
2016 const u64 feature_and_reserved =
2017 /* feature (except bit 48; see below) */
2018 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
2019 /* reserved */
2020 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002021 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08002022
2023 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
2024 return -EINVAL;
2025
2026 /*
2027 * KVM does not emulate a version of VMX that constrains physical
2028 * addresses of VMX structures (e.g. VMCS) to 32-bits.
2029 */
2030 if (data & BIT_ULL(48))
2031 return -EINVAL;
2032
2033 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
2034 vmx_basic_vmcs_revision_id(data))
2035 return -EINVAL;
2036
2037 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
2038 return -EINVAL;
2039
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002040 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08002041 return 0;
2042}
2043
2044static int
2045vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
2046{
2047 u64 supported;
2048 u32 *lowp, *highp;
2049
2050 switch (msr_index) {
2051 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002052 lowp = &vmx->nested.msrs.pinbased_ctls_low;
2053 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08002054 break;
2055 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002056 lowp = &vmx->nested.msrs.procbased_ctls_low;
2057 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08002058 break;
2059 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002060 lowp = &vmx->nested.msrs.exit_ctls_low;
2061 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08002062 break;
2063 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002064 lowp = &vmx->nested.msrs.entry_ctls_low;
2065 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08002066 break;
2067 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002068 lowp = &vmx->nested.msrs.secondary_ctls_low;
2069 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08002070 break;
2071 default:
2072 BUG();
2073 }
2074
2075 supported = vmx_control_msr(*lowp, *highp);
2076
2077 /* Check must-be-1 bits are still 1. */
2078 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
2079 return -EINVAL;
2080
2081 /* Check must-be-0 bits are still 0. */
2082 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
2083 return -EINVAL;
2084
2085 *lowp = data;
2086 *highp = data >> 32;
2087 return 0;
2088}
2089
2090static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
2091{
2092 const u64 feature_and_reserved_bits =
2093 /* feature */
2094 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
2095 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
2096 /* reserved */
2097 GENMASK_ULL(13, 9) | BIT_ULL(31);
2098 u64 vmx_misc;
2099
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002100 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
2101 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08002102
2103 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
2104 return -EINVAL;
2105
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002106 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08002107 PIN_BASED_VMX_PREEMPTION_TIMER) &&
2108 vmx_misc_preemption_timer_rate(data) !=
2109 vmx_misc_preemption_timer_rate(vmx_misc))
2110 return -EINVAL;
2111
2112 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
2113 return -EINVAL;
2114
2115 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
2116 return -EINVAL;
2117
2118 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
2119 return -EINVAL;
2120
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002121 vmx->nested.msrs.misc_low = data;
2122 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07002123
2124 /*
2125 * If L1 has read-only VM-exit information fields, use the
2126 * less permissive vmx_vmwrite_bitmap to specify write
2127 * permissions for the shadow VMCS.
2128 */
2129 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
2130 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
2131
David Matlack62cc6b9d2016-11-29 18:14:07 -08002132 return 0;
2133}
2134
2135static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
2136{
2137 u64 vmx_ept_vpid_cap;
2138
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002139 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
2140 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08002141
2142 /* Every bit is either reserved or a feature bit. */
2143 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
2144 return -EINVAL;
2145
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002146 vmx->nested.msrs.ept_caps = data;
2147 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08002148 return 0;
2149}
2150
2151static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
2152{
2153 u64 *msr;
2154
2155 switch (msr_index) {
2156 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002157 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08002158 break;
2159 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002160 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08002161 break;
2162 default:
2163 BUG();
2164 }
2165
2166 /*
2167 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
2168 * must be 1 in the restored value.
2169 */
2170 if (!is_bitwise_subset(data, *msr, -1ULL))
2171 return -EINVAL;
2172
2173 *msr = data;
2174 return 0;
2175}
2176
2177/*
2178 * Called when userspace is restoring VMX MSRs.
2179 *
2180 * Returns 0 on success, non-0 otherwise.
2181 */
2182static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
2183{
2184 struct vcpu_vmx *vmx = to_vmx(vcpu);
2185
Jim Mattsona943ac52018-05-29 09:11:32 -07002186 /*
2187 * Don't allow changes to the VMX capability MSRs while the vCPU
2188 * is in VMX operation.
2189 */
2190 if (vmx->nested.vmxon)
2191 return -EBUSY;
2192
David Matlack62cc6b9d2016-11-29 18:14:07 -08002193 switch (msr_index) {
2194 case MSR_IA32_VMX_BASIC:
2195 return vmx_restore_vmx_basic(vmx, data);
2196 case MSR_IA32_VMX_PINBASED_CTLS:
2197 case MSR_IA32_VMX_PROCBASED_CTLS:
2198 case MSR_IA32_VMX_EXIT_CTLS:
2199 case MSR_IA32_VMX_ENTRY_CTLS:
2200 /*
2201 * The "non-true" VMX capability MSRs are generated from the
2202 * "true" MSRs, so we do not support restoring them directly.
2203 *
2204 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
2205 * should restore the "true" MSRs with the must-be-1 bits
2206 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
2207 * DEFAULT SETTINGS".
2208 */
2209 return -EINVAL;
2210 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
2211 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
2212 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
2213 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
2214 case MSR_IA32_VMX_PROCBASED_CTLS2:
2215 return vmx_restore_control_msr(vmx, msr_index, data);
2216 case MSR_IA32_VMX_MISC:
2217 return vmx_restore_vmx_misc(vmx, data);
2218 case MSR_IA32_VMX_CR0_FIXED0:
2219 case MSR_IA32_VMX_CR4_FIXED0:
2220 return vmx_restore_fixed0_msr(vmx, msr_index, data);
2221 case MSR_IA32_VMX_CR0_FIXED1:
2222 case MSR_IA32_VMX_CR4_FIXED1:
2223 /*
2224 * These MSRs are generated based on the vCPU's CPUID, so we
2225 * do not support restoring them directly.
2226 */
2227 return -EINVAL;
2228 case MSR_IA32_VMX_EPT_VPID_CAP:
2229 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
2230 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002231 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08002232 return 0;
2233 default:
2234 /*
2235 * The rest of the VMX capability MSRs do not support restore.
2236 */
2237 return -EINVAL;
2238 }
2239}
2240
Jan Kiszkacae50132014-01-04 18:47:22 +01002241/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002242static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002243{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002244 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002245 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002246 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002247 break;
2248 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
2249 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002250 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002251 msrs->pinbased_ctls_low,
2252 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08002253 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
2254 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002255 break;
2256 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
2257 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002258 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002259 msrs->procbased_ctls_low,
2260 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08002261 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
2262 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002263 break;
2264 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
2265 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002266 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002267 msrs->exit_ctls_low,
2268 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08002269 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
2270 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002271 break;
2272 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
2273 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002274 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002275 msrs->entry_ctls_low,
2276 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08002277 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
2278 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002279 break;
2280 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002281 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002282 msrs->misc_low,
2283 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002284 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002285 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002286 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002287 break;
2288 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002289 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002290 break;
2291 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002292 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002293 break;
2294 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002295 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002296 break;
2297 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002298 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002299 break;
2300 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002301 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002302 msrs->secondary_ctls_low,
2303 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002304 break;
2305 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002306 *pdata = msrs->ept_caps |
2307 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002308 break;
Bandan Das27c42a12017-08-03 15:54:42 -04002309 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002310 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04002311 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002312 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002313 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08002314 }
2315
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002316 return 0;
2317}
2318
Haozhong Zhang37e4c992016-06-22 14:59:55 +08002319static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
2320 uint64_t val)
2321{
2322 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
2323
2324 return !(val & ~valid_bits);
2325}
2326
Tom Lendacky801e4592018-02-21 13:39:51 -06002327static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
2328{
Paolo Bonzini13893092018-02-26 13:40:09 +01002329 switch (msr->index) {
2330 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
2331 if (!nested)
2332 return 1;
2333 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
2334 default:
2335 return 1;
2336 }
2337
2338 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06002339}
2340
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002341/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002342 * Reads an msr value (of 'msr_index') into 'pdata'.
2343 * Returns 0 on success, non-0 otherwise.
2344 * Assumes vcpu_load() was already called.
2345 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002346static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002347{
Borislav Petkova6cb0992017-12-20 12:50:28 +01002348 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002349 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002350
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002351 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002352#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002353 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002354 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002355 break;
2356 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002357 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002358 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002359 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07002360 msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002361 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03002362#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08002363 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002364 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002365 case MSR_IA32_SPEC_CTRL:
2366 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002367 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
2368 return 1;
2369
2370 msr_info->data = to_vmx(vcpu)->spec_ctrl;
2371 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01002372 case MSR_IA32_ARCH_CAPABILITIES:
2373 if (!msr_info->host_initiated &&
2374 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
2375 return 1;
2376 msr_info->data = to_vmx(vcpu)->arch_capabilities;
2377 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002378 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002379 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002380 break;
2381 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002382 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002383 break;
2384 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002385 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002386 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002387 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08002388 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02002389 (!msr_info->host_initiated &&
2390 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01002391 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002392 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002393 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08002394 case MSR_IA32_MCG_EXT_CTL:
2395 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01002396 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08002397 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01002398 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08002399 msr_info->data = vcpu->arch.mcg_ext_ctl;
2400 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01002401 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01002402 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01002403 break;
2404 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
2405 if (!nested_vmx_allowed(vcpu))
2406 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002407 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
2408 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08002409 case MSR_IA32_XSS:
2410 if (!vmx_xsaves_supported())
2411 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002412 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08002413 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002414 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002415 if (!msr_info->host_initiated &&
2416 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002417 return 1;
2418 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002419 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01002420 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08002421 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002422 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08002423 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002424 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002425 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002426 }
2427
Avi Kivity6aa8b732006-12-10 02:21:36 -08002428 return 0;
2429}
2430
Jan Kiszkacae50132014-01-04 18:47:22 +01002431static void vmx_leave_nested(struct kvm_vcpu *vcpu);
2432
Avi Kivity6aa8b732006-12-10 02:21:36 -08002433/*
2434 * Writes msr value into into the appropriate "register".
2435 * Returns 0 on success, non-0 otherwise.
2436 * Assumes vcpu_load() was already called.
2437 */
Will Auld8fe8ab42012-11-29 12:42:12 -08002438static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002439{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002440 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002441 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03002442 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08002443 u32 msr_index = msr_info->index;
2444 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03002445
Avi Kivity6aa8b732006-12-10 02:21:36 -08002446 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08002447 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08002448 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03002449 break;
Avi Kivity16175a72009-03-23 22:13:44 +02002450#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002451 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03002452 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002453 vmcs_writel(GUEST_FS_BASE, data);
2454 break;
2455 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03002456 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002457 vmcs_writel(GUEST_GS_BASE, data);
2458 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002459 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07002460 vmx_write_guest_kernel_gs_base(vmx, data);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002461 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002462#endif
2463 case MSR_IA32_SYSENTER_CS:
2464 vmcs_write32(GUEST_SYSENTER_CS, data);
2465 break;
2466 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002467 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002468 break;
2469 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002470 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002471 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002472 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08002473 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02002474 (!msr_info->host_initiated &&
2475 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01002476 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08002477 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07002478 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002479 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08002480 vmcs_write64(GUEST_BNDCFGS, data);
2481 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002482 case MSR_IA32_SPEC_CTRL:
2483 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002484 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
2485 return 1;
2486
2487 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02002488 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002489 return 1;
2490
2491 vmx->spec_ctrl = data;
2492
2493 if (!data)
2494 break;
2495
2496 /*
2497 * For non-nested:
2498 * When it's written (to non-zero) for the first time, pass
2499 * it through.
2500 *
2501 * For nested:
2502 * The handling of the MSR bitmap for L2 guests is done in
2503 * nested_vmx_merge_msr_bitmap. We should not touch the
2504 * vmcs02.msr_bitmap here since it gets completely overwritten
2505 * in the merging. We update the vmcs01 here for L1 as well
2506 * since it will end up touching the MSR anyway now.
2507 */
2508 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
2509 MSR_IA32_SPEC_CTRL,
2510 MSR_TYPE_RW);
2511 break;
Ashok Raj15d45072018-02-01 22:59:43 +01002512 case MSR_IA32_PRED_CMD:
2513 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01002514 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
2515 return 1;
2516
2517 if (data & ~PRED_CMD_IBPB)
2518 return 1;
2519
2520 if (!data)
2521 break;
2522
2523 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
2524
2525 /*
2526 * For non-nested:
2527 * When it's written (to non-zero) for the first time, pass
2528 * it through.
2529 *
2530 * For nested:
2531 * The handling of the MSR bitmap for L2 guests is done in
2532 * nested_vmx_merge_msr_bitmap. We should not touch the
2533 * vmcs02.msr_bitmap here since it gets completely overwritten
2534 * in the merging.
2535 */
2536 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
2537 MSR_TYPE_W);
2538 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01002539 case MSR_IA32_ARCH_CAPABILITIES:
2540 if (!msr_info->host_initiated)
2541 return 1;
2542 vmx->arch_capabilities = data;
2543 break;
Sheng Yang468d4722008-10-09 16:01:55 +08002544 case MSR_IA32_CR_PAT:
2545 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03002546 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
2547 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08002548 vmcs_write64(GUEST_IA32_PAT, data);
2549 vcpu->arch.pat = data;
2550 break;
2551 }
Will Auld8fe8ab42012-11-29 12:42:12 -08002552 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002553 break;
Will Auldba904632012-11-29 12:42:50 -08002554 case MSR_IA32_TSC_ADJUST:
2555 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002556 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08002557 case MSR_IA32_MCG_EXT_CTL:
2558 if ((!msr_info->host_initiated &&
2559 !(to_vmx(vcpu)->msr_ia32_feature_control &
2560 FEATURE_CONTROL_LMCE)) ||
2561 (data & ~MCG_EXT_CTL_LMCE_EN))
2562 return 1;
2563 vcpu->arch.mcg_ext_ctl = data;
2564 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01002565 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08002566 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08002567 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01002568 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
2569 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08002570 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01002571 if (msr_info->host_initiated && data == 0)
2572 vmx_leave_nested(vcpu);
2573 break;
2574 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08002575 if (!msr_info->host_initiated)
2576 return 1; /* they are read-only */
2577 if (!nested_vmx_allowed(vcpu))
2578 return 1;
2579 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08002580 case MSR_IA32_XSS:
2581 if (!vmx_xsaves_supported())
2582 return 1;
2583 /*
2584 * The only supported bit as of Skylake is bit 8, but
2585 * it is not supported on KVM.
2586 */
2587 if (data != 0)
2588 return 1;
2589 vcpu->arch.ia32_xss = data;
2590 if (vcpu->arch.ia32_xss != host_xss)
2591 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002592 vcpu->arch.ia32_xss, host_xss, false);
Wanpeng Li20300092014-12-02 19:14:59 +08002593 else
2594 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
2595 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002596 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002597 if (!msr_info->host_initiated &&
2598 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002599 return 1;
2600 /* Check reserved bit, higher 32 bits should be zero */
2601 if ((data >> 32) != 0)
2602 return 1;
2603 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002604 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10002605 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08002606 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07002607 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08002608 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03002609 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
2610 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07002611 ret = kvm_set_shared_msr(msr->index, msr->data,
2612 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03002613 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07002614 if (ret)
2615 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03002616 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08002617 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002618 }
Will Auld8fe8ab42012-11-29 12:42:12 -08002619 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002620 }
2621
Eddie Dong2cc51562007-05-21 07:28:09 +03002622 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002623}
2624
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002625static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002626{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002627 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
2628 switch (reg) {
2629 case VCPU_REGS_RSP:
2630 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
2631 break;
2632 case VCPU_REGS_RIP:
2633 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
2634 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002635 case VCPU_EXREG_PDPTR:
2636 if (enable_ept)
2637 ept_save_pdptrs(vcpu);
2638 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002639 default:
2640 break;
2641 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002642}
2643
Avi Kivity6aa8b732006-12-10 02:21:36 -08002644static __init int cpu_has_kvm_support(void)
2645{
Eduardo Habkost6210e372008-11-17 19:03:16 -02002646 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002647}
2648
2649static __init int vmx_disabled_by_bios(void)
2650{
2651 u64 msr;
2652
2653 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04002654 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08002655 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04002656 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
2657 && tboot_enabled())
2658 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08002659 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04002660 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08002661 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08002662 && !tboot_enabled()) {
2663 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08002664 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04002665 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08002666 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08002667 /* launched w/o TXT and VMX disabled */
2668 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
2669 && !tboot_enabled())
2670 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04002671 }
2672
2673 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002674}
2675
Dongxiao Xu7725b892010-05-11 18:29:38 +08002676static void kvm_cpu_vmxon(u64 addr)
2677{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002678 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03002679 intel_pt_handle_vmx(1);
2680
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002681 asm volatile ("vmxon %0" : : "m"(addr));
Dongxiao Xu7725b892010-05-11 18:29:38 +08002682}
2683
Radim Krčmář13a34e02014-08-28 15:13:03 +02002684static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002685{
2686 int cpu = raw_smp_processor_id();
2687 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04002688 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002689
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07002690 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02002691 return -EBUSY;
2692
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002693 /*
2694 * This can happen if we hot-added a CPU but failed to allocate
2695 * VP assist page for it.
2696 */
2697 if (static_branch_unlikely(&enable_evmcs) &&
2698 !hv_get_vp_assist_page(cpu))
2699 return -EFAULT;
2700
Nadav Har'Eld462b812011-05-24 15:26:10 +03002701 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08002702 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
2703 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002704
2705 /*
2706 * Now we can enable the vmclear operation in kdump
2707 * since the loaded_vmcss_on_cpu list on this cpu
2708 * has been initialized.
2709 *
2710 * Though the cpu is not in VMX operation now, there
2711 * is no problem to enable the vmclear operation
2712 * for the loaded_vmcss_on_cpu list is empty!
2713 */
2714 crash_enable_local_vmclear(cpu);
2715
Avi Kivity6aa8b732006-12-10 02:21:36 -08002716 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04002717
2718 test_bits = FEATURE_CONTROL_LOCKED;
2719 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
2720 if (tboot_enabled())
2721 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
2722
2723 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002724 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04002725 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
2726 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002727 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02002728 if (enable_ept)
2729 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02002730
2731 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002732}
2733
Nadav Har'Eld462b812011-05-24 15:26:10 +03002734static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03002735{
2736 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03002737 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03002738
Nadav Har'Eld462b812011-05-24 15:26:10 +03002739 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
2740 loaded_vmcss_on_cpu_link)
2741 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03002742}
2743
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02002744
2745/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
2746 * tricks.
2747 */
2748static void kvm_cpu_vmxoff(void)
2749{
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002750 asm volatile (__ex("vmxoff"));
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03002751
2752 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002753 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02002754}
2755
Radim Krčmář13a34e02014-08-28 15:13:03 +02002756static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002757{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002758 vmclear_local_loaded_vmcss();
2759 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002760}
2761
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002762static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04002763 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002764{
2765 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002766 u32 ctl = ctl_min | ctl_opt;
2767
2768 rdmsr(msr, vmx_msr_low, vmx_msr_high);
2769
2770 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
2771 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
2772
2773 /* Ensure minimum (required) set of control bits are supported. */
2774 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002775 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002776
2777 *result = ctl;
2778 return 0;
2779}
2780
Sean Christopherson7caaa712018-12-03 13:53:01 -08002781static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf,
2782 struct vmx_capability *vmx_cap)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002783{
2784 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08002785 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002786 u32 _pin_based_exec_control = 0;
2787 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08002788 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002789 u32 _vmexit_control = 0;
2790 u32 _vmentry_control = 0;
2791
Paolo Bonzini13893092018-02-26 13:40:09 +01002792 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05302793 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002794#ifdef CONFIG_X86_64
2795 CPU_BASED_CR8_LOAD_EXITING |
2796 CPU_BASED_CR8_STORE_EXITING |
2797#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08002798 CPU_BASED_CR3_LOAD_EXITING |
2799 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e22017-12-12 16:44:21 +08002800 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002801 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03002802 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07002803 CPU_BASED_MWAIT_EXITING |
2804 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02002805 CPU_BASED_INVLPG_EXITING |
2806 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06002807
Sheng Yangf78e0e22007-10-29 09:40:42 +08002808 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08002809 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08002810 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002811 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
2812 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002813 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002814#ifdef CONFIG_X86_64
2815 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
2816 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
2817 ~CPU_BASED_CR8_STORE_EXITING;
2818#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08002819 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08002820 min2 = 0;
2821 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08002822 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08002823 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08002824 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002825 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002826 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002827 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02002828 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00002829 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08002830 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08002831 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03002832 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08002833 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08002834 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02002835 SECONDARY_EXEC_RDSEED_EXITING |
2836 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08002837 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04002838 SECONDARY_EXEC_TSC_SCALING |
Sean Christopherson0b665d32018-08-14 09:33:34 -07002839 SECONDARY_EXEC_ENABLE_VMFUNC |
2840 SECONDARY_EXEC_ENCLS_EXITING;
Sheng Yangd56f5462008-04-25 10:13:16 +08002841 if (adjust_vmx_controls(min2, opt2,
2842 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08002843 &_cpu_based_2nd_exec_control) < 0)
2844 return -EIO;
2845 }
2846#ifndef CONFIG_X86_64
2847 if (!(_cpu_based_2nd_exec_control &
2848 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
2849 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
2850#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08002851
2852 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
2853 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08002854 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08002855 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
2856 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08002857
Wanpeng Li61f1dd92017-10-18 16:02:19 -07002858 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
Sean Christopherson7caaa712018-12-03 13:53:01 -08002859 &vmx_cap->ept, &vmx_cap->vpid);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07002860
Sheng Yangd56f5462008-04-25 10:13:16 +08002861 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03002862 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
2863 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03002864 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
2865 CPU_BASED_CR3_STORE_EXITING |
2866 CPU_BASED_INVLPG_EXITING);
Sean Christopherson7caaa712018-12-03 13:53:01 -08002867 } else if (vmx_cap->ept) {
2868 vmx_cap->ept = 0;
Wanpeng Li61f1dd92017-10-18 16:02:19 -07002869 pr_warn_once("EPT CAP should not exist if not support "
2870 "1-setting enable EPT VM-execution control\n");
2871 }
2872 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
Sean Christopherson7caaa712018-12-03 13:53:01 -08002873 vmx_cap->vpid) {
2874 vmx_cap->vpid = 0;
Wanpeng Li61f1dd92017-10-18 16:02:19 -07002875 pr_warn_once("VPID CAP should not exist if not support "
2876 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08002877 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002878
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02002879 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002880#ifdef CONFIG_X86_64
2881 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
2882#endif
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002883 opt = VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL |
2884 VM_EXIT_SAVE_IA32_PAT |
2885 VM_EXIT_LOAD_IA32_PAT |
2886 VM_EXIT_LOAD_IA32_EFER |
2887 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002888 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
2889 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002890 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002891
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01002892 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
2893 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
2894 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08002895 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
2896 &_pin_based_exec_control) < 0)
2897 return -EIO;
2898
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02002899 if (cpu_has_broken_vmx_preemption_timer())
2900 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08002901 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02002902 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08002903 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
2904
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01002905 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002906 opt = VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL |
2907 VM_ENTRY_LOAD_IA32_PAT |
2908 VM_ENTRY_LOAD_IA32_EFER |
2909 VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002910 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
2911 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002912 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002913
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002914 /*
2915 * Some cpus support VM_{ENTRY,EXIT}_IA32_PERF_GLOBAL_CTRL but they
2916 * can't be used due to an errata where VM Exit may incorrectly clear
2917 * IA32_PERF_GLOBAL_CTRL[34:32]. Workaround the errata by using the
2918 * MSR load mechanism to switch IA32_PERF_GLOBAL_CTRL.
2919 */
2920 if (boot_cpu_data.x86 == 0x6) {
2921 switch (boot_cpu_data.x86_model) {
2922 case 26: /* AAK155 */
2923 case 30: /* AAP115 */
2924 case 37: /* AAT100 */
2925 case 44: /* BC86,AAY89,BD102 */
2926 case 46: /* BA97 */
2927 _vmexit_control &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
2928 _vmexit_control &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
2929 pr_warn_once("kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
2930 "does not work properly. Using workaround\n");
2931 break;
2932 default:
2933 break;
2934 }
2935 }
2936
2937
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002938 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002939
2940 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
2941 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002942 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002943
2944#ifdef CONFIG_X86_64
2945 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
2946 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03002947 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002948#endif
2949
2950 /* Require Write-Back (WB) memory type for VMCS accesses. */
2951 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002952 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002953
Yang, Sheng002c7f72007-07-31 14:23:01 +03002954 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02002955 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03002956 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002957
Liran Alon2307af12018-06-29 22:59:04 +03002958 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002959
Yang, Sheng002c7f72007-07-31 14:23:01 +03002960 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
2961 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08002962 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002963 vmcs_conf->vmexit_ctrl = _vmexit_control;
2964 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002965
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002966 if (static_branch_unlikely(&enable_evmcs))
2967 evmcs_sanitize_exec_ctrls(vmcs_conf);
2968
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002969 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002970}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002971
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08002972struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002973{
2974 int node = cpu_to_node(cpu);
2975 struct page *pages;
2976 struct vmcs *vmcs;
2977
Vlastimil Babka96db8002015-09-08 15:03:50 -07002978 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002979 if (!pages)
2980 return NULL;
2981 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002982 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03002983
2984 /* KVM supports Enlightened VMCS v1 only */
2985 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03002986 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03002987 else
Liran Alon392b2f22018-06-23 02:35:01 +03002988 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03002989
Liran Alon491a6032018-06-23 02:35:12 +03002990 if (shadow)
2991 vmcs->hdr.shadow_vmcs = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002992 return vmcs;
2993}
2994
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08002995void free_vmcs(struct vmcs *vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002996{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002997 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002998}
2999
Nadav Har'Eld462b812011-05-24 15:26:10 +03003000/*
3001 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
3002 */
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08003003void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
Nadav Har'Eld462b812011-05-24 15:26:10 +03003004{
3005 if (!loaded_vmcs->vmcs)
3006 return;
3007 loaded_vmcs_clear(loaded_vmcs);
3008 free_vmcs(loaded_vmcs->vmcs);
3009 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003010 if (loaded_vmcs->msr_bitmap)
3011 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07003012 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03003013}
3014
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08003015int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
Paolo Bonzinif21f1652018-01-11 12:16:15 +01003016{
Liran Alon491a6032018-06-23 02:35:12 +03003017 loaded_vmcs->vmcs = alloc_vmcs(false);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01003018 if (!loaded_vmcs->vmcs)
3019 return -ENOMEM;
3020
3021 loaded_vmcs->shadow_vmcs = NULL;
3022 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003023
3024 if (cpu_has_vmx_msr_bitmap()) {
3025 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
3026 if (!loaded_vmcs->msr_bitmap)
3027 goto out_vmcs;
3028 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02003029
Arnd Bergmann1f008e12018-05-25 17:36:17 +02003030 if (IS_ENABLED(CONFIG_HYPERV) &&
3031 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02003032 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
3033 struct hv_enlightened_vmcs *evmcs =
3034 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
3035
3036 evmcs->hv_enlightenments_control.msr_bitmap = 1;
3037 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003038 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07003039
3040 memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
3041
Paolo Bonzinif21f1652018-01-11 12:16:15 +01003042 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003043
3044out_vmcs:
3045 free_loaded_vmcs(loaded_vmcs);
3046 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01003047}
3048
Sam Ravnborg39959582007-06-01 00:47:13 -07003049static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003050{
3051 int cpu;
3052
Zachary Amsden3230bb42009-09-29 11:38:37 -10003053 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003054 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10003055 per_cpu(vmxarea, cpu) = NULL;
3056 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003057}
3058
Bandan Dasfe2b2012014-04-21 15:20:14 -04003059static void init_vmcs_shadow_fields(void)
3060{
3061 int i, j;
3062
Sean Christophersondfae3c02018-12-03 13:52:52 -08003063 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
3064 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
3065
Paolo Bonzini44900ba2017-12-13 12:58:02 +01003066 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
3067 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08003068 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01003069 (i + 1 == max_shadow_read_only_fields ||
3070 shadow_read_only_fields[i + 1] != field + 1))
3071 pr_err("Missing field from shadow_read_only_field %x\n",
3072 field + 1);
3073
3074 clear_bit(field, vmx_vmread_bitmap);
3075#ifdef CONFIG_X86_64
3076 if (field & 1)
3077 continue;
3078#endif
3079 if (j < i)
3080 shadow_read_only_fields[j] = field;
3081 j++;
3082 }
3083 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04003084
3085 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01003086 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08003087 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01003088 (i + 1 == max_shadow_read_write_fields ||
3089 shadow_read_write_fields[i + 1] != field + 1))
3090 pr_err("Missing field from shadow_read_write_field %x\n",
3091 field + 1);
3092
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01003093 /*
3094 * PML and the preemption timer can be emulated, but the
3095 * processor cannot vmwrite to fields that don't exist
3096 * on bare metal.
3097 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01003098 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01003099 case GUEST_PML_INDEX:
3100 if (!cpu_has_vmx_pml())
3101 continue;
3102 break;
3103 case VMX_PREEMPTION_TIMER_VALUE:
3104 if (!cpu_has_vmx_preemption_timer())
3105 continue;
3106 break;
3107 case GUEST_INTR_STATUS:
3108 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04003109 continue;
3110 break;
3111 default:
3112 break;
3113 }
3114
Paolo Bonzini44900ba2017-12-13 12:58:02 +01003115 clear_bit(field, vmx_vmwrite_bitmap);
3116 clear_bit(field, vmx_vmread_bitmap);
3117#ifdef CONFIG_X86_64
3118 if (field & 1)
3119 continue;
3120#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04003121 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01003122 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04003123 j++;
3124 }
3125 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04003126}
3127
Avi Kivity6aa8b732006-12-10 02:21:36 -08003128static __init int alloc_kvm_area(void)
3129{
3130 int cpu;
3131
Zachary Amsden3230bb42009-09-29 11:38:37 -10003132 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003133 struct vmcs *vmcs;
3134
Liran Alon491a6032018-06-23 02:35:12 +03003135 vmcs = alloc_vmcs_cpu(false, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003136 if (!vmcs) {
3137 free_kvm_area();
3138 return -ENOMEM;
3139 }
3140
Liran Alon2307af12018-06-29 22:59:04 +03003141 /*
3142 * When eVMCS is enabled, alloc_vmcs_cpu() sets
3143 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
3144 * revision_id reported by MSR_IA32_VMX_BASIC.
3145 *
3146 * However, even though not explictly documented by
3147 * TLFS, VMXArea passed as VMXON argument should
3148 * still be marked with revision_id reported by
3149 * physical CPU.
3150 */
3151 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03003152 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03003153
Avi Kivity6aa8b732006-12-10 02:21:36 -08003154 per_cpu(vmxarea, cpu) = vmcs;
3155 }
3156 return 0;
3157}
3158
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003159static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02003160 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003161{
Gleb Natapovd99e4152012-12-20 16:57:45 +02003162 if (!emulate_invalid_guest_state) {
3163 /*
3164 * CS and SS RPL should be equal during guest entry according
3165 * to VMX spec, but in reality it is not always so. Since vcpu
3166 * is in the middle of the transition from real mode to
3167 * protected mode it is safe to assume that RPL 0 is a good
3168 * default value.
3169 */
3170 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03003171 save->selector &= ~SEGMENT_RPL_MASK;
3172 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02003173 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003174 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02003175 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003176}
3177
3178static void enter_pmode(struct kvm_vcpu *vcpu)
3179{
3180 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003181 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003182
Gleb Natapovd99e4152012-12-20 16:57:45 +02003183 /*
3184 * Update real mode segment cache. It may be not up-to-date if sement
3185 * register was written while vcpu was in a guest mode.
3186 */
3187 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3188 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3189 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3190 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
3191 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3192 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3193
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003194 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003195
Avi Kivity2fb92db2011-04-27 19:42:18 +03003196 vmx_segment_cache_clear(vmx);
3197
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003198 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003199
3200 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003201 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3202 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003203 vmcs_writel(GUEST_RFLAGS, flags);
3204
Rusty Russell66aee912007-07-17 23:34:16 +10003205 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
3206 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003207
3208 update_exception_bitmap(vcpu);
3209
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003210 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3211 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3212 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3213 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3214 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
3215 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003216}
3217
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003218static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003219{
Mathias Krause772e0312012-08-30 01:30:19 +02003220 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02003221 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003222
Gleb Natapovd99e4152012-12-20 16:57:45 +02003223 var.dpl = 0x3;
3224 if (seg == VCPU_SREG_CS)
3225 var.type = 0x3;
3226
3227 if (!emulate_invalid_guest_state) {
3228 var.selector = var.base >> 4;
3229 var.base = var.base & 0xffff0;
3230 var.limit = 0xffff;
3231 var.g = 0;
3232 var.db = 0;
3233 var.present = 1;
3234 var.s = 1;
3235 var.l = 0;
3236 var.unusable = 0;
3237 var.type = 0x3;
3238 var.avl = 0;
3239 if (save->base & 0xf)
3240 printk_once(KERN_WARNING "kvm: segment base is not "
3241 "paragraph aligned when entering "
3242 "protected mode (seg=%d)", seg);
3243 }
3244
3245 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05003246 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02003247 vmcs_write32(sf->limit, var.limit);
3248 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003249}
3250
3251static void enter_rmode(struct kvm_vcpu *vcpu)
3252{
3253 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003254 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003255 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003256
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003257 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
3258 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3259 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3260 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3261 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003262 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3263 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003264
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003265 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003266
Gleb Natapov776e58e2011-03-13 12:34:27 +02003267 /*
3268 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003269 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02003270 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003271 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02003272 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
3273 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02003274
Avi Kivity2fb92db2011-04-27 19:42:18 +03003275 vmx_segment_cache_clear(vmx);
3276
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003277 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003278 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003279 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
3280
3281 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003282 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003283
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003284 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003285
3286 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10003287 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003288 update_exception_bitmap(vcpu);
3289
Gleb Natapovd99e4152012-12-20 16:57:45 +02003290 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3291 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3292 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3293 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3294 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
3295 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003296
Eddie Dong8668a3c2007-10-10 14:26:45 +08003297 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003298}
3299
Amit Shah401d10d2009-02-20 22:53:37 +05303300static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
3301{
3302 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003303 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
3304
3305 if (!msr)
3306 return;
Amit Shah401d10d2009-02-20 22:53:37 +05303307
Avi Kivityf6801df2010-01-21 15:31:50 +02003308 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05303309 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003310 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303311 msr->data = efer;
3312 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003313 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303314
3315 msr->data = efer & ~EFER_LME;
3316 }
3317 setup_msrs(vmx);
3318}
3319
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003320#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003321
3322static void enter_lmode(struct kvm_vcpu *vcpu)
3323{
3324 u32 guest_tr_ar;
3325
Avi Kivity2fb92db2011-04-27 19:42:18 +03003326 vmx_segment_cache_clear(to_vmx(vcpu));
3327
Avi Kivity6aa8b732006-12-10 02:21:36 -08003328 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003329 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02003330 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
3331 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003332 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003333 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
3334 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003335 }
Avi Kivityda38f432010-07-06 11:30:49 +03003336 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003337}
3338
3339static void exit_lmode(struct kvm_vcpu *vcpu)
3340{
Gleb Natapov2961e8762013-11-25 15:37:13 +02003341 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03003342 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003343}
3344
3345#endif
3346
Junaid Shahidfaff8752018-06-29 13:10:05 -07003347static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
3348{
3349 int vpid = to_vmx(vcpu)->vpid;
3350
3351 if (!vpid_sync_vcpu_addr(vpid, addr))
3352 vpid_sync_context(vpid);
3353
3354 /*
3355 * If VPIDs are not supported or enabled, then the above is a no-op.
3356 * But we don't really need a TLB flush in that case anyway, because
3357 * each VM entry/exit includes an implicit flush when VPID is 0.
3358 */
3359}
3360
Avi Kivitye8467fd2009-12-29 18:43:06 +02003361static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
3362{
3363 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
3364
3365 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
3366 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
3367}
3368
Avi Kivityaff48ba2010-12-05 18:56:11 +02003369static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
3370{
Sean Christophersonb4d18512018-03-05 12:04:40 -08003371 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02003372 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
3373 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
3374}
3375
Anthony Liguori25c4c272007-04-27 09:29:21 +03003376static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08003377{
Avi Kivityfc78f512009-12-07 12:16:48 +02003378 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
3379
3380 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
3381 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08003382}
3383
Sheng Yang14394422008-04-28 12:24:45 +08003384static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
3385{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003386 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
3387
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003388 if (!test_bit(VCPU_EXREG_PDPTR,
3389 (unsigned long *)&vcpu->arch.regs_dirty))
3390 return;
3391
Sheng Yang14394422008-04-28 12:24:45 +08003392 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003393 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
3394 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
3395 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
3396 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08003397 }
3398}
3399
Avi Kivity8f5d5492009-05-31 18:41:29 +03003400static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
3401{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003402 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
3403
Avi Kivity8f5d5492009-05-31 18:41:29 +03003404 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003405 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
3406 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
3407 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
3408 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03003409 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003410
3411 __set_bit(VCPU_EXREG_PDPTR,
3412 (unsigned long *)&vcpu->arch.regs_avail);
3413 __set_bit(VCPU_EXREG_PDPTR,
3414 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03003415}
3416
David Matlack38991522016-11-29 18:14:08 -08003417static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
3418{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003419 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
3420 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08003421 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3422
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003423 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08003424 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
3425 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
3426 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
3427
3428 return fixed_bits_valid(val, fixed0, fixed1);
3429}
3430
3431static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
3432{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003433 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
3434 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08003435
3436 return fixed_bits_valid(val, fixed0, fixed1);
3437}
3438
3439static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
3440{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003441 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
3442 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08003443
3444 return fixed_bits_valid(val, fixed0, fixed1);
3445}
3446
3447/* No difference in the restrictions on guest and host CR4 in VMX operation. */
3448#define nested_guest_cr4_valid nested_cr4_valid
3449#define nested_host_cr4_valid nested_cr4_valid
3450
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003451static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08003452
3453static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
3454 unsigned long cr0,
3455 struct kvm_vcpu *vcpu)
3456{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03003457 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
3458 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003459 if (!(cr0 & X86_CR0_PG)) {
3460 /* From paging/starting to nonpaging */
3461 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08003462 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08003463 (CPU_BASED_CR3_LOAD_EXITING |
3464 CPU_BASED_CR3_STORE_EXITING));
3465 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003466 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003467 } else if (!is_paging(vcpu)) {
3468 /* From nonpaging to paging */
3469 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08003470 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08003471 ~(CPU_BASED_CR3_LOAD_EXITING |
3472 CPU_BASED_CR3_STORE_EXITING));
3473 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003474 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003475 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08003476
3477 if (!(cr0 & X86_CR0_WP))
3478 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08003479}
3480
Avi Kivity6aa8b732006-12-10 02:21:36 -08003481static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
3482{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003483 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003484 unsigned long hw_cr0;
3485
Sean Christopherson3de63472018-07-13 08:42:30 -07003486 hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003487 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02003488 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02003489 else {
Gleb Natapov50378782013-02-04 16:00:28 +02003490 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08003491
Gleb Natapov218e7632013-01-21 15:36:45 +02003492 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
3493 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003494
Gleb Natapov218e7632013-01-21 15:36:45 +02003495 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
3496 enter_rmode(vcpu);
3497 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003498
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003499#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02003500 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10003501 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003502 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10003503 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003504 exit_lmode(vcpu);
3505 }
3506#endif
3507
Sean Christophersonb4d18512018-03-05 12:04:40 -08003508 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08003509 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
3510
Avi Kivity6aa8b732006-12-10 02:21:36 -08003511 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08003512 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003513 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02003514
3515 /* depends on vcpu->arch.cr0 to be set to a new value */
3516 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003517}
3518
Yu Zhang855feb62017-08-24 20:27:55 +08003519static int get_ept_level(struct kvm_vcpu *vcpu)
3520{
3521 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
3522 return 5;
3523 return 4;
3524}
3525
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08003526u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08003527{
Yu Zhang855feb62017-08-24 20:27:55 +08003528 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08003529
Yu Zhang855feb62017-08-24 20:27:55 +08003530 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08003531
Peter Feiner995f00a2017-06-30 17:26:32 -07003532 if (enable_ept_ad_bits &&
3533 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02003534 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08003535 eptp |= (root_hpa & PAGE_MASK);
3536
3537 return eptp;
3538}
3539
Avi Kivity6aa8b732006-12-10 02:21:36 -08003540static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
3541{
Tianyu Lan877ad952018-07-19 08:40:23 +00003542 struct kvm *kvm = vcpu->kvm;
Sheng Yang14394422008-04-28 12:24:45 +08003543 unsigned long guest_cr3;
3544 u64 eptp;
3545
3546 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02003547 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07003548 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08003549 vmcs_write64(EPT_POINTER, eptp);
Tianyu Lan877ad952018-07-19 08:40:23 +00003550
3551 if (kvm_x86_ops->tlb_remote_flush) {
3552 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
3553 to_vmx(vcpu)->ept_pointer = eptp;
3554 to_kvm_vmx(kvm)->ept_pointers_match
3555 = EPT_POINTERS_CHECK;
3556 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
3557 }
3558
Sean Christophersone90008d2018-03-05 12:04:37 -08003559 if (enable_unrestricted_guest || is_paging(vcpu) ||
3560 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02003561 guest_cr3 = kvm_read_cr3(vcpu);
3562 else
Tianyu Lan877ad952018-07-19 08:40:23 +00003563 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02003564 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003565 }
3566
Sheng Yang14394422008-04-28 12:24:45 +08003567 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003568}
3569
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003570static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003571{
Ben Serebrin085e68e2015-04-16 11:58:05 -07003572 /*
3573 * Pass through host's Machine Check Enable value to hw_cr4, which
3574 * is in force while we are in guest mode. Do not let guests control
3575 * this bit, even if host CR4.MCE == 0.
3576 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08003577 unsigned long hw_cr4;
3578
3579 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
3580 if (enable_unrestricted_guest)
3581 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
3582 else if (to_vmx(vcpu)->rmode.vm86_active)
3583 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
3584 else
3585 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08003586
Sean Christopherson64f7a112018-04-30 10:01:06 -07003587 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
3588 if (cr4 & X86_CR4_UMIP) {
3589 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02003590 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07003591 hw_cr4 &= ~X86_CR4_UMIP;
3592 } else if (!is_guest_mode(vcpu) ||
3593 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
3594 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
3595 SECONDARY_EXEC_DESC);
3596 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02003597
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003598 if (cr4 & X86_CR4_VMXE) {
3599 /*
3600 * To use VMXON (and later other VMX instructions), a guest
3601 * must first be able to turn on cr4.VMXE (see handle_vmon()).
3602 * So basically the check on whether to allow nested VMX
Paolo Bonzini5bea5122018-09-18 15:19:17 +02003603 * is here. We operate under the default treatment of SMM,
3604 * so VMX cannot be enabled under SMM.
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003605 */
Paolo Bonzini5bea5122018-09-18 15:19:17 +02003606 if (!nested_vmx_allowed(vcpu) || is_smm(vcpu))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003607 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01003608 }
David Matlack38991522016-11-29 18:14:08 -08003609
3610 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003611 return 1;
3612
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003613 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08003614
Sean Christopherson5dc1f042018-03-05 12:04:39 -08003615 if (!enable_unrestricted_guest) {
3616 if (enable_ept) {
3617 if (!is_paging(vcpu)) {
3618 hw_cr4 &= ~X86_CR4_PAE;
3619 hw_cr4 |= X86_CR4_PSE;
3620 } else if (!(cr4 & X86_CR4_PAE)) {
3621 hw_cr4 &= ~X86_CR4_PAE;
3622 }
3623 }
3624
Radim Krčmář656ec4a2015-11-02 22:20:00 +01003625 /*
Huaitong Handdba2622016-03-22 16:51:15 +08003626 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
3627 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
3628 * to be manually disabled when guest switches to non-paging
3629 * mode.
3630 *
3631 * If !enable_unrestricted_guest, the CPU is always running
3632 * with CR0.PG=1 and CR4 needs to be modified.
3633 * If enable_unrestricted_guest, the CPU automatically
3634 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01003635 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08003636 if (!is_paging(vcpu))
3637 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
3638 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01003639
Sheng Yang14394422008-04-28 12:24:45 +08003640 vmcs_writel(CR4_READ_SHADOW, cr4);
3641 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003642 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003643}
3644
Avi Kivity6aa8b732006-12-10 02:21:36 -08003645static void vmx_get_segment(struct kvm_vcpu *vcpu,
3646 struct kvm_segment *var, int seg)
3647{
Avi Kivitya9179492011-01-03 14:28:52 +02003648 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003649 u32 ar;
3650
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003651 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003652 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02003653 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03003654 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003655 return;
Avi Kivity1390a282012-08-21 17:07:08 +03003656 var->base = vmx_read_guest_seg_base(vmx, seg);
3657 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3658 return;
Avi Kivitya9179492011-01-03 14:28:52 +02003659 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03003660 var->base = vmx_read_guest_seg_base(vmx, seg);
3661 var->limit = vmx_read_guest_seg_limit(vmx, seg);
3662 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3663 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03003664 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003665 var->type = ar & 15;
3666 var->s = (ar >> 4) & 1;
3667 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03003668 /*
3669 * Some userspaces do not preserve unusable property. Since usable
3670 * segment has to be present according to VMX spec we can use present
3671 * property to amend userspace bug by making unusable segment always
3672 * nonpresent. vmx_segment_access_rights() already marks nonpresent
3673 * segment as unusable.
3674 */
3675 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003676 var->avl = (ar >> 12) & 1;
3677 var->l = (ar >> 13) & 1;
3678 var->db = (ar >> 14) & 1;
3679 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003680}
3681
Avi Kivitya9179492011-01-03 14:28:52 +02003682static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
3683{
Avi Kivitya9179492011-01-03 14:28:52 +02003684 struct kvm_segment s;
3685
3686 if (to_vmx(vcpu)->rmode.vm86_active) {
3687 vmx_get_segment(vcpu, &s, seg);
3688 return s.base;
3689 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03003690 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02003691}
3692
Marcelo Tosattib09408d2013-01-07 19:27:06 -02003693static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02003694{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02003695 struct vcpu_vmx *vmx = to_vmx(vcpu);
3696
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02003697 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02003698 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02003699 else {
3700 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003701 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02003702 }
Avi Kivity69c73022011-03-07 15:26:44 +02003703}
3704
Avi Kivity653e3102007-05-07 10:55:37 +03003705static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003706{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003707 u32 ar;
3708
Avi Kivityf0495f92012-06-07 17:06:10 +03003709 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003710 ar = 1 << 16;
3711 else {
3712 ar = var->type & 15;
3713 ar |= (var->s & 1) << 4;
3714 ar |= (var->dpl & 3) << 5;
3715 ar |= (var->present & 1) << 7;
3716 ar |= (var->avl & 1) << 12;
3717 ar |= (var->l & 1) << 13;
3718 ar |= (var->db & 1) << 14;
3719 ar |= (var->g & 1) << 15;
3720 }
Avi Kivity653e3102007-05-07 10:55:37 +03003721
3722 return ar;
3723}
3724
3725static void vmx_set_segment(struct kvm_vcpu *vcpu,
3726 struct kvm_segment *var, int seg)
3727{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003728 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02003729 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03003730
Avi Kivity2fb92db2011-04-27 19:42:18 +03003731 vmx_segment_cache_clear(vmx);
3732
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02003733 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
3734 vmx->rmode.segs[seg] = *var;
3735 if (seg == VCPU_SREG_TR)
3736 vmcs_write16(sf->selector, var->selector);
3737 else if (var->s)
3738 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02003739 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03003740 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02003741
Avi Kivity653e3102007-05-07 10:55:37 +03003742 vmcs_writel(sf->base, var->base);
3743 vmcs_write32(sf->limit, var->limit);
3744 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003745
3746 /*
3747 * Fix the "Accessed" bit in AR field of segment registers for older
3748 * qemu binaries.
3749 * IA32 arch specifies that at the time of processor reset the
3750 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08003751 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003752 * state vmexit when "unrestricted guest" mode is turned on.
3753 * Fix for this setup issue in cpu_reset is being pushed in the qemu
3754 * tree. Newer qemu binaries with that qemu fix would not need this
3755 * kvm hack.
3756 */
3757 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02003758 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003759
Gleb Natapovf924d662012-12-12 19:10:55 +02003760 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02003761
3762out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01003763 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003764}
3765
Avi Kivity6aa8b732006-12-10 02:21:36 -08003766static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
3767{
Avi Kivity2fb92db2011-04-27 19:42:18 +03003768 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003769
3770 *db = (ar >> 14) & 1;
3771 *l = (ar >> 13) & 1;
3772}
3773
Gleb Natapov89a27f42010-02-16 10:51:48 +02003774static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003775{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003776 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
3777 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003778}
3779
Gleb Natapov89a27f42010-02-16 10:51:48 +02003780static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003781{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003782 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
3783 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003784}
3785
Gleb Natapov89a27f42010-02-16 10:51:48 +02003786static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003787{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003788 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
3789 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003790}
3791
Gleb Natapov89a27f42010-02-16 10:51:48 +02003792static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003793{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003794 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
3795 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003796}
3797
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003798static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
3799{
3800 struct kvm_segment var;
3801 u32 ar;
3802
3803 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02003804 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02003805 if (seg == VCPU_SREG_CS)
3806 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003807 ar = vmx_segment_access_rights(&var);
3808
3809 if (var.base != (var.selector << 4))
3810 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02003811 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003812 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02003813 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003814 return false;
3815
3816 return true;
3817}
3818
3819static bool code_segment_valid(struct kvm_vcpu *vcpu)
3820{
3821 struct kvm_segment cs;
3822 unsigned int cs_rpl;
3823
3824 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03003825 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003826
Avi Kivity1872a3f2009-01-04 23:26:52 +02003827 if (cs.unusable)
3828 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003829 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003830 return false;
3831 if (!cs.s)
3832 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003833 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003834 if (cs.dpl > cs_rpl)
3835 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003836 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003837 if (cs.dpl != cs_rpl)
3838 return false;
3839 }
3840 if (!cs.present)
3841 return false;
3842
3843 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
3844 return true;
3845}
3846
3847static bool stack_segment_valid(struct kvm_vcpu *vcpu)
3848{
3849 struct kvm_segment ss;
3850 unsigned int ss_rpl;
3851
3852 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03003853 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003854
Avi Kivity1872a3f2009-01-04 23:26:52 +02003855 if (ss.unusable)
3856 return true;
3857 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003858 return false;
3859 if (!ss.s)
3860 return false;
3861 if (ss.dpl != ss_rpl) /* DPL != RPL */
3862 return false;
3863 if (!ss.present)
3864 return false;
3865
3866 return true;
3867}
3868
3869static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
3870{
3871 struct kvm_segment var;
3872 unsigned int rpl;
3873
3874 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03003875 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003876
Avi Kivity1872a3f2009-01-04 23:26:52 +02003877 if (var.unusable)
3878 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003879 if (!var.s)
3880 return false;
3881 if (!var.present)
3882 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003883 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003884 if (var.dpl < rpl) /* DPL < RPL */
3885 return false;
3886 }
3887
3888 /* TODO: Add other members to kvm_segment_field to allow checking for other access
3889 * rights flags
3890 */
3891 return true;
3892}
3893
3894static bool tr_valid(struct kvm_vcpu *vcpu)
3895{
3896 struct kvm_segment tr;
3897
3898 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
3899
Avi Kivity1872a3f2009-01-04 23:26:52 +02003900 if (tr.unusable)
3901 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03003902 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003903 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003904 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003905 return false;
3906 if (!tr.present)
3907 return false;
3908
3909 return true;
3910}
3911
3912static bool ldtr_valid(struct kvm_vcpu *vcpu)
3913{
3914 struct kvm_segment ldtr;
3915
3916 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
3917
Avi Kivity1872a3f2009-01-04 23:26:52 +02003918 if (ldtr.unusable)
3919 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03003920 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003921 return false;
3922 if (ldtr.type != 2)
3923 return false;
3924 if (!ldtr.present)
3925 return false;
3926
3927 return true;
3928}
3929
3930static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
3931{
3932 struct kvm_segment cs, ss;
3933
3934 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
3935 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
3936
Nadav Amitb32a9912015-03-29 16:33:04 +03003937 return ((cs.selector & SEGMENT_RPL_MASK) ==
3938 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003939}
3940
3941/*
3942 * Check if guest state is valid. Returns true if valid, false if
3943 * not.
3944 * We assume that registers are always usable
3945 */
3946static bool guest_state_valid(struct kvm_vcpu *vcpu)
3947{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02003948 if (enable_unrestricted_guest)
3949 return true;
3950
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003951 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03003952 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003953 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
3954 return false;
3955 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
3956 return false;
3957 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
3958 return false;
3959 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
3960 return false;
3961 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
3962 return false;
3963 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
3964 return false;
3965 } else {
3966 /* protected mode guest state checks */
3967 if (!cs_ss_rpl_check(vcpu))
3968 return false;
3969 if (!code_segment_valid(vcpu))
3970 return false;
3971 if (!stack_segment_valid(vcpu))
3972 return false;
3973 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
3974 return false;
3975 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
3976 return false;
3977 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
3978 return false;
3979 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
3980 return false;
3981 if (!tr_valid(vcpu))
3982 return false;
3983 if (!ldtr_valid(vcpu))
3984 return false;
3985 }
3986 /* TODO:
3987 * - Add checks on RIP
3988 * - Add checks on RFLAGS
3989 */
3990
3991 return true;
3992}
3993
Jim Mattson5fa99cb2017-07-06 16:33:07 -07003994static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
3995{
3996 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
3997}
3998
Mike Dayd77c26f2007-10-08 09:02:08 -04003999static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004000{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004001 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02004002 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004003 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004004
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004005 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004006 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02004007 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4008 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004009 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004010 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08004011 r = kvm_write_guest_page(kvm, fn++, &data,
4012 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02004013 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004014 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004015 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
4016 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004017 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004018 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4019 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004020 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004021 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004022 r = kvm_write_guest_page(kvm, fn, &data,
4023 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
4024 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004025out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004026 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004027 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004028}
4029
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004030static int init_rmode_identity_map(struct kvm *kvm)
4031{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004032 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08004033 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08004034 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004035 u32 tmp;
4036
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004037 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08004038 mutex_lock(&kvm->slots_lock);
4039
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004040 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08004041 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08004042
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004043 if (!kvm_vmx->ept_identity_map_addr)
4044 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
4045 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08004046
David Hildenbrandd8a6e362017-08-24 20:51:34 +02004047 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004048 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08004049 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08004050 goto out2;
4051
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004052 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004053 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
4054 if (r < 0)
4055 goto out;
4056 /* Set up identity-mapping pagetable for EPT in real mode */
4057 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
4058 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
4059 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
4060 r = kvm_write_guest_page(kvm, identity_map_pfn,
4061 &tmp, i * sizeof(tmp), sizeof(tmp));
4062 if (r < 0)
4063 goto out;
4064 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004065 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08004066
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004067out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004068 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08004069
4070out2:
4071 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08004072 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004073}
4074
Avi Kivity6aa8b732006-12-10 02:21:36 -08004075static void seg_setup(int seg)
4076{
Mathias Krause772e0312012-08-30 01:30:19 +02004077 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004078 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004079
4080 vmcs_write16(sf->selector, 0);
4081 vmcs_writel(sf->base, 0);
4082 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02004083 ar = 0x93;
4084 if (seg == VCPU_SREG_CS)
4085 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004086
4087 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004088}
4089
Sheng Yangf78e0e22007-10-29 09:40:42 +08004090static int alloc_apic_access_page(struct kvm *kvm)
4091{
Xiao Guangrong44841412012-09-07 14:14:20 +08004092 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004093 int r = 0;
4094
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004095 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08004096 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08004097 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004098 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
4099 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004100 if (r)
4101 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02004102
Tang Chen73a6d942014-09-11 13:38:00 +08004103 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08004104 if (is_error_page(page)) {
4105 r = -EFAULT;
4106 goto out;
4107 }
4108
Tang Chenc24ae0d2014-09-24 15:57:58 +08004109 /*
4110 * Do not pin the page in memory, so that memory hot-unplug
4111 * is able to migrate it.
4112 */
4113 put_page(page);
4114 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004115out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004116 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004117 return r;
4118}
4119
Wanpeng Li991e7a02015-09-16 17:30:05 +08004120static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004121{
4122 int vpid;
4123
Avi Kivity919818a2009-03-23 18:01:29 +02004124 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08004125 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004126 spin_lock(&vmx_vpid_lock);
4127 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004128 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004129 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004130 else
4131 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004132 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004133 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004134}
4135
Wanpeng Li991e7a02015-09-16 17:30:05 +08004136static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004137{
Wanpeng Li991e7a02015-09-16 17:30:05 +08004138 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004139 return;
4140 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004141 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004142 spin_unlock(&vmx_vpid_lock);
4143}
4144
Yi Wang1e4329ee2018-11-08 11:22:21 +08004145static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004146 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08004147{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004148 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08004149
4150 if (!cpu_has_vmx_msr_bitmap())
4151 return;
4152
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004153 if (static_branch_unlikely(&enable_evmcs))
4154 evmcs_touch_msr_bitmap();
4155
Sheng Yang25c5f222008-03-28 13:18:56 +08004156 /*
4157 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4158 * have the write-low and read-high bitmap offsets the wrong way round.
4159 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4160 */
Sheng Yang25c5f222008-03-28 13:18:56 +08004161 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08004162 if (type & MSR_TYPE_R)
4163 /* read-low */
4164 __clear_bit(msr, msr_bitmap + 0x000 / f);
4165
4166 if (type & MSR_TYPE_W)
4167 /* write-low */
4168 __clear_bit(msr, msr_bitmap + 0x800 / f);
4169
Sheng Yang25c5f222008-03-28 13:18:56 +08004170 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4171 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08004172 if (type & MSR_TYPE_R)
4173 /* read-high */
4174 __clear_bit(msr, msr_bitmap + 0x400 / f);
4175
4176 if (type & MSR_TYPE_W)
4177 /* write-high */
4178 __clear_bit(msr, msr_bitmap + 0xc00 / f);
4179
4180 }
4181}
4182
Yi Wang1e4329ee2018-11-08 11:22:21 +08004183static __always_inline void vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004184 u32 msr, int type)
4185{
4186 int f = sizeof(unsigned long);
4187
4188 if (!cpu_has_vmx_msr_bitmap())
4189 return;
4190
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004191 if (static_branch_unlikely(&enable_evmcs))
4192 evmcs_touch_msr_bitmap();
4193
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004194 /*
4195 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4196 * have the write-low and read-high bitmap offsets the wrong way round.
4197 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4198 */
4199 if (msr <= 0x1fff) {
4200 if (type & MSR_TYPE_R)
4201 /* read-low */
4202 __set_bit(msr, msr_bitmap + 0x000 / f);
4203
4204 if (type & MSR_TYPE_W)
4205 /* write-low */
4206 __set_bit(msr, msr_bitmap + 0x800 / f);
4207
4208 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4209 msr &= 0x1fff;
4210 if (type & MSR_TYPE_R)
4211 /* read-high */
4212 __set_bit(msr, msr_bitmap + 0x400 / f);
4213
4214 if (type & MSR_TYPE_W)
4215 /* write-high */
4216 __set_bit(msr, msr_bitmap + 0xc00 / f);
4217
4218 }
4219}
4220
Yi Wang1e4329ee2018-11-08 11:22:21 +08004221static __always_inline void vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004222 u32 msr, int type, bool value)
4223{
4224 if (value)
4225 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
4226 else
4227 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
4228}
4229
Wincy Vanf2b93282015-02-03 23:56:03 +08004230/*
4231 * If a msr is allowed by L0, we should check whether it is allowed by L1.
4232 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
4233 */
4234static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
4235 unsigned long *msr_bitmap_nested,
4236 u32 msr, int type)
4237{
4238 int f = sizeof(unsigned long);
4239
Wincy Vanf2b93282015-02-03 23:56:03 +08004240 /*
4241 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4242 * have the write-low and read-high bitmap offsets the wrong way round.
4243 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4244 */
4245 if (msr <= 0x1fff) {
4246 if (type & MSR_TYPE_R &&
4247 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
4248 /* read-low */
4249 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
4250
4251 if (type & MSR_TYPE_W &&
4252 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
4253 /* write-low */
4254 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
4255
4256 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4257 msr &= 0x1fff;
4258 if (type & MSR_TYPE_R &&
4259 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
4260 /* read-high */
4261 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
4262
4263 if (type & MSR_TYPE_W &&
4264 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
4265 /* write-high */
4266 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
4267
4268 }
4269}
4270
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004271static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02004272{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004273 u8 mode = 0;
4274
4275 if (cpu_has_secondary_exec_ctrls() &&
4276 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
4277 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
4278 mode |= MSR_BITMAP_MODE_X2APIC;
4279 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
4280 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
4281 }
4282
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004283 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08004284}
4285
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004286static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
4287 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08004288{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004289 int msr;
4290
4291 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
4292 unsigned word = msr / BITS_PER_LONG;
4293 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
4294 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004295 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004296
4297 if (mode & MSR_BITMAP_MODE_X2APIC) {
4298 /*
4299 * TPR reads and writes can be virtualized even if virtual interrupt
4300 * delivery is not in use.
4301 */
4302 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
4303 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
4304 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
4305 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
4306 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
4307 }
4308 }
4309}
4310
4311static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
4312{
4313 struct vcpu_vmx *vmx = to_vmx(vcpu);
4314 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
4315 u8 mode = vmx_msr_bitmap_mode(vcpu);
4316 u8 changed = mode ^ vmx->msr_bitmap_mode;
4317
4318 if (!changed)
4319 return;
4320
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004321 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
4322 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
4323
4324 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02004325}
4326
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05004327static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02004328{
Andrey Smetanind62caab2015-11-10 15:36:33 +03004329 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02004330}
4331
David Matlackc9f04402017-08-01 14:00:40 -07004332static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
4333{
4334 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4335 gfn_t gfn;
4336
4337 /*
4338 * Don't need to mark the APIC access page dirty; it is never
4339 * written to by the CPU during APIC virtualization.
4340 */
4341
4342 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
4343 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
4344 kvm_vcpu_mark_page_dirty(vcpu, gfn);
4345 }
4346
4347 if (nested_cpu_has_posted_intr(vmcs12)) {
4348 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
4349 kvm_vcpu_mark_page_dirty(vcpu, gfn);
4350 }
4351}
4352
4353
David Hildenbrand6342c502017-01-25 11:58:58 +01004354static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08004355{
4356 struct vcpu_vmx *vmx = to_vmx(vcpu);
4357 int max_irr;
4358 void *vapic_page;
4359 u16 status;
4360
David Matlackc9f04402017-08-01 14:00:40 -07004361 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
4362 return;
Wincy Van705699a2015-02-03 23:58:17 +08004363
David Matlackc9f04402017-08-01 14:00:40 -07004364 vmx->nested.pi_pending = false;
4365 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
4366 return;
Wincy Van705699a2015-02-03 23:58:17 +08004367
David Matlackc9f04402017-08-01 14:00:40 -07004368 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
4369 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08004370 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02004371 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
4372 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08004373 kunmap(vmx->nested.virtual_apic_page);
4374
4375 status = vmcs_read16(GUEST_INTR_STATUS);
4376 if ((u8)max_irr > ((u8)status & 0xff)) {
4377 status &= ~0xff;
4378 status |= (u8)max_irr;
4379 vmcs_write16(GUEST_INTR_STATUS, status);
4380 }
4381 }
David Matlackc9f04402017-08-01 14:00:40 -07004382
4383 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08004384}
4385
Liran Alone6c67d82018-09-04 10:56:52 +03004386static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
4387{
4388 struct vcpu_vmx *vmx = to_vmx(vcpu);
4389 void *vapic_page;
4390 u32 vppr;
4391 int rvi;
4392
4393 if (WARN_ON_ONCE(!is_guest_mode(vcpu)) ||
4394 !nested_cpu_has_vid(get_vmcs12(vcpu)) ||
4395 WARN_ON_ONCE(!vmx->nested.virtual_apic_page))
4396 return false;
4397
Paolo Bonzini7e712682018-10-03 13:44:26 +02004398 rvi = vmx_get_rvi();
Liran Alone6c67d82018-09-04 10:56:52 +03004399
4400 vapic_page = kmap(vmx->nested.virtual_apic_page);
4401 vppr = *((u32 *)(vapic_page + APIC_PROCPRI));
4402 kunmap(vmx->nested.virtual_apic_page);
4403
4404 return ((rvi & 0xf0) > (vppr & 0xf0));
4405}
4406
Wincy Van06a55242017-04-28 13:13:59 +08004407static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
4408 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004409{
4410#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08004411 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
4412
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004413 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08004414 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08004415 * The vector of interrupt to be delivered to vcpu had
4416 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08004417 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08004418 * Following cases will be reached in this block, and
4419 * we always send a notification event in all cases as
4420 * explained below.
4421 *
4422 * Case 1: vcpu keeps in non-root mode. Sending a
4423 * notification event posts the interrupt to vcpu.
4424 *
4425 * Case 2: vcpu exits to root mode and is still
4426 * runnable. PIR will be synced to vIRR before the
4427 * next vcpu entry. Sending a notification event in
4428 * this case has no effect, as vcpu is not in root
4429 * mode.
4430 *
4431 * Case 3: vcpu exits to root mode and is blocked.
4432 * vcpu_block() has already synced PIR to vIRR and
4433 * never blocks vcpu if vIRR is not cleared. Therefore,
4434 * a blocked vcpu here does not wait for any requested
4435 * interrupts in PIR, and sending a notification event
4436 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08004437 */
Feng Wu28b835d2015-09-18 22:29:54 +08004438
Wincy Van06a55242017-04-28 13:13:59 +08004439 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004440 return true;
4441 }
4442#endif
4443 return false;
4444}
4445
Wincy Van705699a2015-02-03 23:58:17 +08004446static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
4447 int vector)
4448{
4449 struct vcpu_vmx *vmx = to_vmx(vcpu);
4450
4451 if (is_guest_mode(vcpu) &&
4452 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08004453 /*
4454 * If a posted intr is not recognized by hardware,
4455 * we will accomplish it in the next vmentry.
4456 */
4457 vmx->nested.pi_pending = true;
4458 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02004459 /* the PIR and ON have been set by L1. */
4460 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
4461 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08004462 return 0;
4463 }
4464 return -1;
4465}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004466/*
Yang Zhanga20ed542013-04-11 19:25:15 +08004467 * Send interrupt to vcpu via posted interrupt way.
4468 * 1. If target vcpu is running(non-root mode), send posted interrupt
4469 * notification to vcpu and hardware will sync PIR to vIRR atomically.
4470 * 2. If target vcpu isn't running(root mode), kick it to pick up the
4471 * interrupt from PIR in next vmentry.
4472 */
4473static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
4474{
4475 struct vcpu_vmx *vmx = to_vmx(vcpu);
4476 int r;
4477
Wincy Van705699a2015-02-03 23:58:17 +08004478 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
4479 if (!r)
4480 return;
4481
Yang Zhanga20ed542013-04-11 19:25:15 +08004482 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
4483 return;
4484
Paolo Bonzinib95234c2016-12-19 13:57:33 +01004485 /* If a previous notification has sent the IPI, nothing to do. */
4486 if (pi_test_and_set_on(&vmx->pi_desc))
4487 return;
4488
Wincy Van06a55242017-04-28 13:13:59 +08004489 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08004490 kvm_vcpu_kick(vcpu);
4491}
4492
Avi Kivity6aa8b732006-12-10 02:21:36 -08004493/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004494 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
4495 * will not change in the lifetime of the guest.
4496 * Note that host-state that does change is set elsewhere. E.g., host-state
4497 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
4498 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004499static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004500{
4501 u32 low32, high32;
4502 unsigned long tmpl;
4503 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07004504 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004505
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07004506 cr0 = read_cr0();
4507 WARN_ON(cr0 & X86_CR0_TS);
4508 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07004509
4510 /*
4511 * Save the most likely value for this task's CR3 in the VMCS.
4512 * We can't use __get_current_cr3_fast() because we're not atomic.
4513 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07004514 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07004515 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Sean Christophersond7ee0392018-07-23 12:32:47 -07004516 vmx->loaded_vmcs->host_state.cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004517
Andy Lutomirskid974baa2014-10-08 09:02:13 -07004518 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004519 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07004520 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07004521 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07004522
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004523 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03004524#ifdef CONFIG_X86_64
4525 /*
4526 * Load null selectors, so we can avoid reloading them in
Sean Christopherson6d6095b2018-07-23 12:32:44 -07004527 * vmx_prepare_switch_to_host(), in case userspace uses
4528 * the null selectors too (the expected case).
Avi Kivityb2da15a2012-05-13 19:53:24 +03004529 */
4530 vmcs_write16(HOST_DS_SELECTOR, 0);
4531 vmcs_write16(HOST_ES_SELECTOR, 0);
4532#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004533 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4534 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03004535#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004536 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4537 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
4538
Juergen Gross87930012017-09-04 12:25:27 +02004539 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004540 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004541 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004542
Avi Kivity83287ea422012-09-16 15:10:57 +03004543 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004544
4545 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
4546 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
4547 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
4548 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
4549
4550 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
4551 rdmsr(MSR_IA32_CR_PAT, low32, high32);
4552 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
4553 }
Sean Christopherson5a5e8a12018-09-26 09:23:56 -07004554
Sean Christophersonc73da3f2018-12-03 13:53:00 -08004555 if (cpu_has_load_ia32_efer())
Sean Christopherson5a5e8a12018-09-26 09:23:56 -07004556 vmcs_write64(HOST_IA32_EFER, host_efer);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004557}
4558
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004559static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
4560{
4561 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
4562 if (enable_ept)
4563 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03004564 if (is_guest_mode(&vmx->vcpu))
4565 vmx->vcpu.arch.cr4_guest_owned_bits &=
4566 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004567 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
4568}
4569
Yang Zhang01e439b2013-04-11 19:25:12 +08004570static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
4571{
4572 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
4573
Andrey Smetanind62caab2015-11-10 15:36:33 +03004574 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08004575 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004576
4577 if (!enable_vnmi)
4578 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
4579
Yunhong Jiang64672c92016-06-13 14:19:59 -07004580 /* Enable the preemption timer dynamically */
4581 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004582 return pin_based_exec_ctrl;
4583}
4584
Andrey Smetanind62caab2015-11-10 15:36:33 +03004585static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
4586{
4587 struct vcpu_vmx *vmx = to_vmx(vcpu);
4588
4589 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03004590 if (cpu_has_secondary_exec_ctrls()) {
4591 if (kvm_vcpu_apicv_active(vcpu))
4592 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
4593 SECONDARY_EXEC_APIC_REGISTER_VIRT |
4594 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4595 else
4596 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
4597 SECONDARY_EXEC_APIC_REGISTER_VIRT |
4598 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4599 }
4600
4601 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004602 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03004603}
4604
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08004605u32 vmx_exec_control(struct vcpu_vmx *vmx)
4606{
4607 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
4608
4609 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
4610 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
4611
4612 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
4613 exec_control &= ~CPU_BASED_TPR_SHADOW;
4614#ifdef CONFIG_X86_64
4615 exec_control |= CPU_BASED_CR8_STORE_EXITING |
4616 CPU_BASED_CR8_LOAD_EXITING;
4617#endif
4618 }
4619 if (!enable_ept)
4620 exec_control |= CPU_BASED_CR3_STORE_EXITING |
4621 CPU_BASED_CR3_LOAD_EXITING |
4622 CPU_BASED_INVLPG_EXITING;
4623 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
4624 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
4625 CPU_BASED_MONITOR_EXITING);
4626 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
4627 exec_control &= ~CPU_BASED_HLT_EXITING;
4628 return exec_control;
4629}
4630
4631
Paolo Bonzini80154d72017-08-24 13:55:35 +02004632static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004633{
Paolo Bonzini80154d72017-08-24 13:55:35 +02004634 struct kvm_vcpu *vcpu = &vmx->vcpu;
4635
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004636 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02004637
Paolo Bonzini80154d72017-08-24 13:55:35 +02004638 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004639 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
4640 if (vmx->vpid == 0)
4641 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
4642 if (!enable_ept) {
4643 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
4644 enable_unrestricted_guest = 0;
4645 }
4646 if (!enable_unrestricted_guest)
4647 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07004648 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004649 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02004650 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08004651 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
4652 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08004653 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02004654
4655 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
4656 * in vmx_set_cr4. */
4657 exec_control &= ~SECONDARY_EXEC_DESC;
4658
Abel Gordonabc4fc52013-04-18 14:35:25 +03004659 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
4660 (handle_vmptrld).
4661 We can NOT enable shadow_vmcs here because we don't have yet
4662 a current VMCS12
4663 */
4664 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08004665
4666 if (!enable_pml)
4667 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08004668
Paolo Bonzini3db13482017-08-24 14:48:03 +02004669 if (vmx_xsaves_supported()) {
4670 /* Exposing XSAVES only when XSAVE is exposed */
4671 bool xsaves_enabled =
4672 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
4673 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
4674
4675 if (!xsaves_enabled)
4676 exec_control &= ~SECONDARY_EXEC_XSAVES;
4677
4678 if (nested) {
4679 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004680 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02004681 SECONDARY_EXEC_XSAVES;
4682 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004683 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02004684 ~SECONDARY_EXEC_XSAVES;
4685 }
4686 }
4687
Paolo Bonzini80154d72017-08-24 13:55:35 +02004688 if (vmx_rdtscp_supported()) {
4689 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
4690 if (!rdtscp_enabled)
4691 exec_control &= ~SECONDARY_EXEC_RDTSCP;
4692
4693 if (nested) {
4694 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004695 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02004696 SECONDARY_EXEC_RDTSCP;
4697 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004698 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02004699 ~SECONDARY_EXEC_RDTSCP;
4700 }
4701 }
4702
4703 if (vmx_invpcid_supported()) {
4704 /* Exposing INVPCID only when PCID is exposed */
4705 bool invpcid_enabled =
4706 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
4707 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
4708
4709 if (!invpcid_enabled) {
4710 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
4711 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
4712 }
4713
4714 if (nested) {
4715 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004716 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02004717 SECONDARY_EXEC_ENABLE_INVPCID;
4718 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004719 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02004720 ~SECONDARY_EXEC_ENABLE_INVPCID;
4721 }
4722 }
4723
Jim Mattson45ec3682017-08-23 16:32:04 -07004724 if (vmx_rdrand_supported()) {
4725 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
4726 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02004727 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07004728
4729 if (nested) {
4730 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004731 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02004732 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07004733 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004734 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02004735 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07004736 }
4737 }
4738
Jim Mattson75f4fc82017-08-23 16:32:03 -07004739 if (vmx_rdseed_supported()) {
4740 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
4741 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02004742 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07004743
4744 if (nested) {
4745 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004746 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02004747 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07004748 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004749 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02004750 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07004751 }
4752 }
4753
Paolo Bonzini80154d72017-08-24 13:55:35 +02004754 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004755}
4756
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004757static void ept_set_mmio_spte_mask(void)
4758{
4759 /*
4760 * EPT Misconfigurations can be generated if the value of bits 2:0
4761 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004762 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07004763 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
4764 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004765}
4766
Wanpeng Lif53cd632014-12-02 19:14:58 +08004767#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004768/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08004769 * Sets up the vmcs for emulated real mode.
4770 */
David Hildenbrand12d79912017-08-24 20:51:26 +02004771static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004772{
Avi Kivity6aa8b732006-12-10 02:21:36 -08004773 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004774
Abel Gordon4607c2d2013-04-18 14:35:55 +03004775 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07004776 /*
4777 * At vCPU creation, "VMWRITE to any supported field
4778 * in the VMCS" is supported, so use the more
4779 * permissive vmx_vmread_bitmap to specify both read
4780 * and write permissions for the shadow VMCS.
4781 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03004782 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07004783 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03004784 }
Sheng Yang25c5f222008-03-28 13:18:56 +08004785 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004786 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08004787
Avi Kivity6aa8b732006-12-10 02:21:36 -08004788 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
4789
Avi Kivity6aa8b732006-12-10 02:21:36 -08004790 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08004791 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07004792 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004793
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004794 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004795
Dan Williamsdfa169b2016-06-02 11:17:24 -07004796 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02004797 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004798 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02004799 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07004800 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08004801
Andrey Smetanind62caab2015-11-10 15:36:33 +03004802 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08004803 vmcs_write64(EOI_EXIT_BITMAP0, 0);
4804 vmcs_write64(EOI_EXIT_BITMAP1, 0);
4805 vmcs_write64(EOI_EXIT_BITMAP2, 0);
4806 vmcs_write64(EOI_EXIT_BITMAP3, 0);
4807
4808 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08004809
Li RongQing0bcf2612015-12-03 13:29:34 +08004810 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08004811 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08004812 }
4813
Wanpeng Lib31c1142018-03-12 04:53:04 -07004814 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004815 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02004816 vmx->ple_window = ple_window;
4817 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004818 }
4819
Xiao Guangrongc3707952011-07-12 03:28:04 +08004820 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
4821 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004822 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
4823
Avi Kivity9581d442010-10-19 16:46:55 +02004824 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
4825 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004826 vmx_set_constant_host_state(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004827 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
4828 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004829
Bandan Das2a499e42017-08-03 15:54:41 -04004830 if (cpu_has_vmx_vmfunc())
4831 vmcs_write64(VM_FUNCTION_CONTROL, 0);
4832
Eddie Dong2cc51562007-05-21 07:28:09 +03004833 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
4834 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04004835 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03004836 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04004837 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004838
Radim Krčmář74545702015-04-27 15:11:25 +02004839 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
4840 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08004841
Paolo Bonzini03916db2014-07-24 14:21:57 +02004842 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004843 u32 index = vmx_msr_index[i];
4844 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004845 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004846
4847 if (rdmsr_safe(index, &data_low, &data_high) < 0)
4848 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08004849 if (wrmsr_safe(index, data_low, data_high) < 0)
4850 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03004851 vmx->guest_msrs[j].index = i;
4852 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02004853 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004854 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004855 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004856
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02004857 vmx->arch_capabilities = kvm_get_arch_capabilities();
Gleb Natapov2961e8762013-11-25 15:37:13 +02004858
Sean Christophersonc73da3f2018-12-03 13:53:00 -08004859 vm_exit_controls_init(vmx, vmx_vmexit_ctrl());
Avi Kivity6aa8b732006-12-10 02:21:36 -08004860
4861 /* 22.2.1, 20.8.1 */
Sean Christophersonc73da3f2018-12-03 13:53:00 -08004862 vm_entry_controls_init(vmx, vmx_vmentry_ctrl());
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004863
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08004864 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
4865 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
4866
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004867 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004868
Wanpeng Lif53cd632014-12-02 19:14:58 +08004869 if (vmx_xsaves_supported())
4870 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
4871
Peter Feiner4e595162016-07-07 14:49:58 -07004872 if (enable_pml) {
Peter Feiner4e595162016-07-07 14:49:58 -07004873 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
4874 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
4875 }
Sean Christopherson0b665d32018-08-14 09:33:34 -07004876
4877 if (cpu_has_vmx_encls_vmexit())
4878 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004879}
4880
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004881static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004882{
4883 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01004884 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004885 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004886
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004887 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004888 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004889
Wanpeng Li518e7b92018-02-28 14:03:31 +08004890 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004891 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004892 kvm_set_cr8(vcpu, 0);
4893
4894 if (!init_event) {
4895 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
4896 MSR_IA32_APICBASE_ENABLE;
4897 if (kvm_vcpu_is_reset_bsp(vcpu))
4898 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
4899 apic_base_msr.host_initiated = true;
4900 kvm_set_apic_base(vcpu, &apic_base_msr);
4901 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004902
Avi Kivity2fb92db2011-04-27 19:42:18 +03004903 vmx_segment_cache_clear(vmx);
4904
Avi Kivity5706be02008-08-20 15:07:31 +03004905 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01004906 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01004907 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004908
4909 seg_setup(VCPU_SREG_DS);
4910 seg_setup(VCPU_SREG_ES);
4911 seg_setup(VCPU_SREG_FS);
4912 seg_setup(VCPU_SREG_GS);
4913 seg_setup(VCPU_SREG_SS);
4914
4915 vmcs_write16(GUEST_TR_SELECTOR, 0);
4916 vmcs_writel(GUEST_TR_BASE, 0);
4917 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
4918 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4919
4920 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
4921 vmcs_writel(GUEST_LDTR_BASE, 0);
4922 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
4923 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
4924
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004925 if (!init_event) {
4926 vmcs_write32(GUEST_SYSENTER_CS, 0);
4927 vmcs_writel(GUEST_SYSENTER_ESP, 0);
4928 vmcs_writel(GUEST_SYSENTER_EIP, 0);
4929 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
4930 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004931
Wanpeng Lic37c2872017-11-20 14:52:21 -08004932 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01004933 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004934
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004935 vmcs_writel(GUEST_GDTR_BASE, 0);
4936 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
4937
4938 vmcs_writel(GUEST_IDTR_BASE, 0);
4939 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
4940
Anthony Liguori443381a2010-12-06 10:53:38 -06004941 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004942 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01004943 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07004944 if (kvm_mpx_supported())
4945 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004946
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004947 setup_msrs(vmx);
4948
Avi Kivity6aa8b732006-12-10 02:21:36 -08004949 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
4950
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004951 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08004952 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02004953 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08004954 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004955 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08004956 vmcs_write32(TPR_THRESHOLD, 0);
4957 }
4958
Paolo Bonzinia73896c2014-11-02 07:54:30 +01004959 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004960
Sheng Yang2384d2b2008-01-17 15:14:33 +08004961 if (vmx->vpid != 0)
4962 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
4963
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004964 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004965 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06004966 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004967 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02004968 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08004969
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004970 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004971
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004972 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07004973 if (init_event)
4974 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004975}
4976
Nadav Har'Elb6f12502011-05-25 23:13:06 +03004977/*
4978 * In nested virtualization, check if L1 asked to exit on external interrupts.
4979 * For most existing hypervisors, this will always return true.
4980 */
4981static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
4982{
4983 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
4984 PIN_BASED_EXT_INTR_MASK;
4985}
4986
Bandan Das77b0f5d2014-04-19 18:17:45 -04004987/*
4988 * In nested virtualization, check if L1 has set
4989 * VM_EXIT_ACK_INTR_ON_EXIT
4990 */
4991static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
4992{
4993 return get_vmcs12(vcpu)->vm_exit_controls &
4994 VM_EXIT_ACK_INTR_ON_EXIT;
4995}
4996
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02004997static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
4998{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05004999 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005000}
5001
Jan Kiszkac9a79532014-03-07 20:03:15 +01005002static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005003{
Paolo Bonzini47c01522016-12-19 11:44:07 +01005004 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
5005 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005006}
5007
Jan Kiszkac9a79532014-03-07 20:03:15 +01005008static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005009{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005010 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005011 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01005012 enable_irq_window(vcpu);
5013 return;
5014 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02005015
Paolo Bonzini47c01522016-12-19 11:44:07 +01005016 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
5017 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005018}
5019
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005020static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03005021{
Avi Kivity9c8cba32007-11-22 11:42:59 +02005022 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005023 uint32_t intr;
5024 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02005025
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005026 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005027
Avi Kivityfa89a812008-09-01 15:57:51 +03005028 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005029 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005030 int inc_eip = 0;
5031 if (vcpu->arch.interrupt.soft)
5032 inc_eip = vcpu->arch.event_exit_inst_len;
5033 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005034 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005035 return;
5036 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005037 intr = irq | INTR_INFO_VALID_MASK;
5038 if (vcpu->arch.interrupt.soft) {
5039 intr |= INTR_TYPE_SOFT_INTR;
5040 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
5041 vmx->vcpu.arch.event_exit_inst_len);
5042 } else
5043 intr |= INTR_TYPE_EXT_INTR;
5044 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07005045
5046 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005047}
5048
Sheng Yangf08864b2008-05-15 18:23:25 +08005049static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
5050{
Jan Kiszka66a5a342008-09-26 09:30:51 +02005051 struct vcpu_vmx *vmx = to_vmx(vcpu);
5052
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005053 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005054 /*
5055 * Tracking the NMI-blocked state in software is built upon
5056 * finding the next open IRQ window. This, in turn, depends on
5057 * well-behaving guests: They have to keep IRQs disabled at
5058 * least as long as the NMI handler runs. Otherwise we may
5059 * cause NMI nesting, maybe breaking the guest. But as this is
5060 * highly unlikely, we can live with the residual risk.
5061 */
5062 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
5063 vmx->loaded_vmcs->vnmi_blocked_time = 0;
5064 }
5065
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02005066 ++vcpu->stat.nmi_injections;
5067 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005068
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005069 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005070 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005071 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02005072 return;
5073 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005074
Sheng Yangf08864b2008-05-15 18:23:25 +08005075 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
5076 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07005077
5078 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08005079}
5080
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005081static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
5082{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02005083 struct vcpu_vmx *vmx = to_vmx(vcpu);
5084 bool masked;
5085
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005086 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005087 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02005088 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02005089 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02005090 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
5091 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
5092 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005093}
5094
5095static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5096{
5097 struct vcpu_vmx *vmx = to_vmx(vcpu);
5098
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005099 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005100 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
5101 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
5102 vmx->loaded_vmcs->vnmi_blocked_time = 0;
5103 }
5104 } else {
5105 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
5106 if (masked)
5107 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5108 GUEST_INTR_STATE_NMI);
5109 else
5110 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
5111 GUEST_INTR_STATE_NMI);
5112 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005113}
5114
Jan Kiszka2505dc92013-04-14 12:12:47 +02005115static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
5116{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005117 if (to_vmx(vcpu)->nested.nested_run_pending)
5118 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005119
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005120 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005121 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
5122 return 0;
5123
Jan Kiszka2505dc92013-04-14 12:12:47 +02005124 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5125 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
5126 | GUEST_INTR_STATE_NMI));
5127}
5128
Gleb Natapov78646122009-03-23 12:12:11 +02005129static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
5130{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005131 return (!to_vmx(vcpu)->nested.nested_run_pending &&
5132 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03005133 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5134 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02005135}
5136
Izik Eiduscbc94022007-10-25 00:29:55 +02005137static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
5138{
5139 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02005140
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08005141 if (enable_unrestricted_guest)
5142 return 0;
5143
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005144 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
5145 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02005146 if (ret)
5147 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005148 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005149 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02005150}
5151
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07005152static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
5153{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005154 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07005155 return 0;
5156}
5157
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005158static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005159{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005160 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005161 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01005162 /*
5163 * Update instruction length as we may reinject the exception
5164 * from user space while in guest debugging mode.
5165 */
5166 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
5167 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005168 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005169 return false;
5170 /* fall through */
5171 case DB_VECTOR:
5172 if (vcpu->guest_debug &
5173 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5174 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005175 /* fall through */
5176 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005177 case OF_VECTOR:
5178 case BR_VECTOR:
5179 case UD_VECTOR:
5180 case DF_VECTOR:
5181 case SS_VECTOR:
5182 case GP_VECTOR:
5183 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005184 return true;
5185 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005186 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005187 return false;
5188}
5189
5190static int handle_rmode_exception(struct kvm_vcpu *vcpu,
5191 int vec, u32 err_code)
5192{
5193 /*
5194 * Instruction with address size override prefix opcode 0x67
5195 * Cause the #SS fault with 0 error code in VM86 mode.
5196 */
5197 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
Sean Christopherson0ce97a22018-08-23 13:56:52 -07005198 if (kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE) {
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005199 if (vcpu->arch.halt_request) {
5200 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06005201 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005202 }
5203 return 1;
5204 }
5205 return 0;
5206 }
5207
5208 /*
5209 * Forward all other exceptions that are valid in real mode.
5210 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
5211 * the required debugging infrastructure rework.
5212 */
5213 kvm_queue_exception(vcpu, vec);
5214 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005215}
5216
Andi Kleena0861c02009-06-08 17:37:09 +08005217/*
5218 * Trigger machine check on the host. We assume all the MSRs are already set up
5219 * by the CPU and that we still run on the same CPU as the MCE occurred on.
5220 * We pass a fake environment to the machine check handler because we want
5221 * the guest to be always treated like user space, no matter what context
5222 * it used internally.
5223 */
5224static void kvm_machine_check(void)
5225{
5226#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
5227 struct pt_regs regs = {
5228 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
5229 .flags = X86_EFLAGS_IF,
5230 };
5231
5232 do_machine_check(&regs, 0);
5233#endif
5234}
5235
Avi Kivity851ba692009-08-24 11:10:17 +03005236static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08005237{
5238 /* already handled by vcpu_run */
5239 return 1;
5240}
5241
Avi Kivity851ba692009-08-24 11:10:17 +03005242static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005243{
Avi Kivity1155f762007-11-22 11:30:47 +02005244 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005245 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005246 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005247 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005248 u32 vect_info;
5249 enum emulation_result er;
5250
Avi Kivity1155f762007-11-22 11:30:47 +02005251 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02005252 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005253
Andi Kleena0861c02009-06-08 17:37:09 +08005254 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03005255 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08005256
Jim Mattsonef85b672016-12-12 11:01:37 -08005257 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02005258 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005259
Wanpeng Li082d06e2018-04-03 16:28:48 -07005260 if (is_invalid_opcode(intr_info))
5261 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005262
Avi Kivity6aa8b732006-12-10 02:21:36 -08005263 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06005264 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005265 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005266
Liran Alon9e869482018-03-12 13:12:51 +02005267 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
5268 WARN_ON_ONCE(!enable_vmware_backdoor);
Sean Christopherson0ce97a22018-08-23 13:56:52 -07005269 er = kvm_emulate_instruction(vcpu,
Liran Alon9e869482018-03-12 13:12:51 +02005270 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
5271 if (er == EMULATE_USER_EXIT)
5272 return 0;
5273 else if (er != EMULATE_DONE)
5274 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
5275 return 1;
5276 }
5277
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005278 /*
5279 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
5280 * MMIO, it is better to report an internal error.
5281 * See the comments in vmx_handle_exit.
5282 */
5283 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
5284 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
5285 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5286 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005287 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005288 vcpu->run->internal.data[0] = vect_info;
5289 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005290 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005291 return 0;
5292 }
5293
Avi Kivity6aa8b732006-12-10 02:21:36 -08005294 if (is_page_fault(intr_info)) {
5295 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07005296 /* EPT won't cause page fault directly */
5297 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02005298 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005299 }
5300
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005301 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005302
5303 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
5304 return handle_rmode_exception(vcpu, ex_no, error_code);
5305
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005306 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01005307 case AC_VECTOR:
5308 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
5309 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005310 case DB_VECTOR:
5311 dr6 = vmcs_readl(EXIT_QUALIFICATION);
5312 if (!(vcpu->guest_debug &
5313 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01005314 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005315 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07005316 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01005317 skip_emulated_instruction(vcpu);
5318
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005319 kvm_queue_exception(vcpu, DB_VECTOR);
5320 return 1;
5321 }
5322 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
5323 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
5324 /* fall through */
5325 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01005326 /*
5327 * Update instruction length as we may reinject #BP from
5328 * user space while in guest debugging mode. Reading it for
5329 * #DB as well causes no harm, it is not used in that case.
5330 */
5331 vmx->vcpu.arch.event_exit_inst_len =
5332 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005333 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03005334 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005335 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
5336 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005337 break;
5338 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005339 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
5340 kvm_run->ex.exception = ex_no;
5341 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005342 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005343 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005344 return 0;
5345}
5346
Avi Kivity851ba692009-08-24 11:10:17 +03005347static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005348{
Avi Kivity1165f5f2007-04-19 17:27:43 +03005349 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005350 return 1;
5351}
5352
Avi Kivity851ba692009-08-24 11:10:17 +03005353static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08005354{
Avi Kivity851ba692009-08-24 11:10:17 +03005355 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07005356 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08005357 return 0;
5358}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005359
Avi Kivity851ba692009-08-24 11:10:17 +03005360static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005361{
He, Qingbfdaab02007-09-12 14:18:28 +08005362 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08005363 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02005364 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005365
He, Qingbfdaab02007-09-12 14:18:28 +08005366 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02005367 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005368
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005369 ++vcpu->stat.io_exits;
5370
Sean Christopherson432baf62018-03-08 08:57:26 -08005371 if (string)
Sean Christopherson0ce97a22018-08-23 13:56:52 -07005372 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005373
5374 port = exit_qualification >> 16;
5375 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08005376 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005377
Sean Christophersondca7f122018-03-08 08:57:27 -08005378 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005379}
5380
Ingo Molnar102d8322007-02-19 14:37:47 +02005381static void
5382vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5383{
5384 /*
5385 * Patch in the VMCALL instruction:
5386 */
5387 hypercall[0] = 0x0f;
5388 hypercall[1] = 0x01;
5389 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02005390}
5391
Guo Chao0fa06072012-06-28 15:16:19 +08005392/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005393static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
5394{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005395 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005396 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5397 unsigned long orig_val = val;
5398
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005399 /*
5400 * We get here when L2 changed cr0 in a way that did not change
5401 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005402 * but did change L0 shadowed bits. So we first calculate the
5403 * effective cr0 value that L1 would like to write into the
5404 * hardware. It consists of the L2-owned bits from the new
5405 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005406 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005407 val = (val & ~vmcs12->cr0_guest_host_mask) |
5408 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
5409
David Matlack38991522016-11-29 18:14:08 -08005410 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005411 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005412
5413 if (kvm_set_cr0(vcpu, val))
5414 return 1;
5415 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005416 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005417 } else {
5418 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08005419 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005420 return 1;
David Matlack38991522016-11-29 18:14:08 -08005421
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005422 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005423 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005424}
5425
5426static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
5427{
5428 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005429 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5430 unsigned long orig_val = val;
5431
5432 /* analogously to handle_set_cr0 */
5433 val = (val & ~vmcs12->cr4_guest_host_mask) |
5434 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
5435 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005436 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005437 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005438 return 0;
5439 } else
5440 return kvm_set_cr4(vcpu, val);
5441}
5442
Paolo Bonzini0367f202016-07-12 10:44:55 +02005443static int handle_desc(struct kvm_vcpu *vcpu)
5444{
5445 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
Sean Christopherson0ce97a22018-08-23 13:56:52 -07005446 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02005447}
5448
Avi Kivity851ba692009-08-24 11:10:17 +03005449static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005450{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005451 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005452 int cr;
5453 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03005454 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08005455 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005456
He, Qingbfdaab02007-09-12 14:18:28 +08005457 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005458 cr = exit_qualification & 15;
5459 reg = (exit_qualification >> 8) & 15;
5460 switch ((exit_qualification >> 4) & 3) {
5461 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03005462 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005463 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005464 switch (cr) {
5465 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005466 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005467 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005468 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08005469 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03005470 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005471 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005472 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005473 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005474 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005475 case 8: {
5476 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03005477 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01005478 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005479 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005480 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08005481 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005482 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08005483 return ret;
5484 /*
5485 * TODO: we might be squashing a
5486 * KVM_GUESTDBG_SINGLESTEP-triggered
5487 * KVM_EXIT_DEBUG here.
5488 */
Avi Kivity851ba692009-08-24 11:10:17 +03005489 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005490 return 0;
5491 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02005492 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005493 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03005494 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08005495 WARN_ONCE(1, "Guest should always own CR0.TS");
5496 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02005497 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08005498 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005499 case 1: /*mov from cr*/
5500 switch (cr) {
5501 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08005502 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02005503 val = kvm_read_cr3(vcpu);
5504 kvm_register_write(vcpu, reg, val);
5505 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005506 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005507 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005508 val = kvm_get_cr8(vcpu);
5509 kvm_register_write(vcpu, reg, val);
5510 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005511 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005512 }
5513 break;
5514 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02005515 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02005516 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02005517 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005518
Kyle Huey6affcbe2016-11-29 12:40:40 -08005519 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005520 default:
5521 break;
5522 }
Avi Kivity851ba692009-08-24 11:10:17 +03005523 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03005524 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08005525 (int)(exit_qualification >> 4) & 3, cr);
5526 return 0;
5527}
5528
Avi Kivity851ba692009-08-24 11:10:17 +03005529static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005530{
He, Qingbfdaab02007-09-12 14:18:28 +08005531 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005532 int dr, dr7, reg;
5533
5534 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5535 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
5536
5537 /* First, if DR does not exist, trigger UD */
5538 if (!kvm_require_dr(vcpu, dr))
5539 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005540
Jan Kiszkaf2483412010-01-20 18:20:20 +01005541 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03005542 if (!kvm_require_cpl(vcpu, 0))
5543 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005544 dr7 = vmcs_readl(GUEST_DR7);
5545 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005546 /*
5547 * As the vm-exit takes precedence over the debug trap, we
5548 * need to emulate the latter, either for the host or the
5549 * guest debugging itself.
5550 */
5551 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03005552 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005553 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02005554 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005555 vcpu->run->debug.arch.exception = DB_VECTOR;
5556 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005557 return 0;
5558 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02005559 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005560 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005561 kvm_queue_exception(vcpu, DB_VECTOR);
5562 return 1;
5563 }
5564 }
5565
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005566 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01005567 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
5568 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005569
5570 /*
5571 * No more DR vmexits; force a reload of the debug registers
5572 * and reenter on this instruction. The next vmexit will
5573 * retrieve the full state of the debug registers.
5574 */
5575 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
5576 return 1;
5577 }
5578
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005579 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
5580 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03005581 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005582
5583 if (kvm_get_dr(vcpu, dr, &val))
5584 return 1;
5585 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03005586 } else
Nadav Amit57773922014-06-18 17:19:23 +03005587 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005588 return 1;
5589
Kyle Huey6affcbe2016-11-29 12:40:40 -08005590 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005591}
5592
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01005593static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
5594{
5595 return vcpu->arch.dr6;
5596}
5597
5598static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
5599{
5600}
5601
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005602static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
5603{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005604 get_debugreg(vcpu->arch.db[0], 0);
5605 get_debugreg(vcpu->arch.db[1], 1);
5606 get_debugreg(vcpu->arch.db[2], 2);
5607 get_debugreg(vcpu->arch.db[3], 3);
5608 get_debugreg(vcpu->arch.dr6, 6);
5609 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
5610
5611 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01005612 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005613}
5614
Gleb Natapov020df072010-04-13 10:05:23 +03005615static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
5616{
5617 vmcs_writel(GUEST_DR7, val);
5618}
5619
Avi Kivity851ba692009-08-24 11:10:17 +03005620static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005621{
Kyle Huey6a908b62016-11-29 12:40:37 -08005622 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005623}
5624
Avi Kivity851ba692009-08-24 11:10:17 +03005625static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005626{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005627 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005628 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005629
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005630 msr_info.index = ecx;
5631 msr_info.host_initiated = false;
5632 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02005633 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02005634 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005635 return 1;
5636 }
5637
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005638 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005639
Avi Kivity6aa8b732006-12-10 02:21:36 -08005640 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005641 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
5642 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08005643 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005644}
5645
Avi Kivity851ba692009-08-24 11:10:17 +03005646static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005647{
Will Auld8fe8ab42012-11-29 12:42:12 -08005648 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005649 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
5650 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
5651 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005652
Will Auld8fe8ab42012-11-29 12:42:12 -08005653 msr.data = data;
5654 msr.index = ecx;
5655 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03005656 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02005657 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02005658 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005659 return 1;
5660 }
5661
Avi Kivity59200272010-01-25 19:47:02 +02005662 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005663 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005664}
5665
Avi Kivity851ba692009-08-24 11:10:17 +03005666static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005667{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01005668 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005669 return 1;
5670}
5671
Avi Kivity851ba692009-08-24 11:10:17 +03005672static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005673{
Paolo Bonzini47c01522016-12-19 11:44:07 +01005674 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
5675 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005676
Avi Kivity3842d132010-07-27 12:30:24 +03005677 kvm_make_request(KVM_REQ_EVENT, vcpu);
5678
Jan Kiszkaa26bf122008-09-26 09:30:45 +02005679 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005680 return 1;
5681}
5682
Avi Kivity851ba692009-08-24 11:10:17 +03005683static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005684{
Avi Kivityd3bef152007-06-05 15:53:05 +03005685 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005686}
5687
Avi Kivity851ba692009-08-24 11:10:17 +03005688static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02005689{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03005690 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02005691}
5692
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005693static int handle_invd(struct kvm_vcpu *vcpu)
5694{
Sean Christopherson0ce97a22018-08-23 13:56:52 -07005695 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005696}
5697
Avi Kivity851ba692009-08-24 11:10:17 +03005698static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03005699{
Sheng Yangf9c617f2009-03-25 10:08:52 +08005700 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03005701
5702 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005703 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03005704}
5705
Avi Kivityfee84b02011-11-10 14:57:25 +02005706static int handle_rdpmc(struct kvm_vcpu *vcpu)
5707{
5708 int err;
5709
5710 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005711 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02005712}
5713
Avi Kivity851ba692009-08-24 11:10:17 +03005714static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02005715{
Kyle Huey6affcbe2016-11-29 12:40:40 -08005716 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02005717}
5718
Dexuan Cui2acf9232010-06-10 11:27:12 +08005719static int handle_xsetbv(struct kvm_vcpu *vcpu)
5720{
5721 u64 new_bv = kvm_read_edx_eax(vcpu);
5722 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
5723
5724 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08005725 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08005726 return 1;
5727}
5728
Wanpeng Lif53cd632014-12-02 19:14:58 +08005729static int handle_xsaves(struct kvm_vcpu *vcpu)
5730{
Kyle Huey6affcbe2016-11-29 12:40:40 -08005731 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08005732 WARN(1, "this should never happen\n");
5733 return 1;
5734}
5735
5736static int handle_xrstors(struct kvm_vcpu *vcpu)
5737{
Kyle Huey6affcbe2016-11-29 12:40:40 -08005738 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08005739 WARN(1, "this should never happen\n");
5740 return 1;
5741}
5742
Avi Kivity851ba692009-08-24 11:10:17 +03005743static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005744{
Kevin Tian58fbbf22011-08-30 13:56:17 +03005745 if (likely(fasteoi)) {
5746 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5747 int access_type, offset;
5748
5749 access_type = exit_qualification & APIC_ACCESS_TYPE;
5750 offset = exit_qualification & APIC_ACCESS_OFFSET;
5751 /*
5752 * Sane guest uses MOV to write EOI, with written value
5753 * not cared. So make a short-circuit here by avoiding
5754 * heavy instruction emulation.
5755 */
5756 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
5757 (offset == APIC_EOI)) {
5758 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005759 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03005760 }
5761 }
Sean Christopherson0ce97a22018-08-23 13:56:52 -07005762 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005763}
5764
Yang Zhangc7c9c562013-01-25 10:18:51 +08005765static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
5766{
5767 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5768 int vector = exit_qualification & 0xff;
5769
5770 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
5771 kvm_apic_set_eoi_accelerated(vcpu, vector);
5772 return 1;
5773}
5774
Yang Zhang83d4c282013-01-25 10:18:49 +08005775static int handle_apic_write(struct kvm_vcpu *vcpu)
5776{
5777 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5778 u32 offset = exit_qualification & 0xfff;
5779
5780 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
5781 kvm_apic_write_nodecode(vcpu, offset);
5782 return 1;
5783}
5784
Avi Kivity851ba692009-08-24 11:10:17 +03005785static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02005786{
Jan Kiszka60637aa2008-09-26 09:30:47 +02005787 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02005788 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02005789 bool has_error_code = false;
5790 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02005791 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005792 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005793
5794 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005795 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005796 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02005797
5798 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5799
5800 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005801 if (reason == TASK_SWITCH_GATE && idt_v) {
5802 switch (type) {
5803 case INTR_TYPE_NMI_INTR:
5804 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02005805 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005806 break;
5807 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005808 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005809 kvm_clear_interrupt_queue(vcpu);
5810 break;
5811 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02005812 if (vmx->idt_vectoring_info &
5813 VECTORING_INFO_DELIVER_CODE_MASK) {
5814 has_error_code = true;
5815 error_code =
5816 vmcs_read32(IDT_VECTORING_ERROR_CODE);
5817 }
5818 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005819 case INTR_TYPE_SOFT_EXCEPTION:
5820 kvm_clear_exception_queue(vcpu);
5821 break;
5822 default:
5823 break;
5824 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02005825 }
Izik Eidus37817f22008-03-24 23:14:53 +02005826 tss_selector = exit_qualification;
5827
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005828 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
5829 type != INTR_TYPE_EXT_INTR &&
5830 type != INTR_TYPE_NMI_INTR))
5831 skip_emulated_instruction(vcpu);
5832
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005833 if (kvm_task_switch(vcpu, tss_selector,
5834 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
5835 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03005836 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5837 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
5838 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005839 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03005840 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005841
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005842 /*
5843 * TODO: What about debug traps on tss switch?
5844 * Are we supposed to inject them and update dr6?
5845 */
5846
5847 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02005848}
5849
Avi Kivity851ba692009-08-24 11:10:17 +03005850static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08005851{
Sheng Yangf9c617f2009-03-25 10:08:52 +08005852 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08005853 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01005854 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08005855
Sheng Yangf9c617f2009-03-25 10:08:52 +08005856 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08005857
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03005858 /*
5859 * EPT violation happened while executing iret from NMI,
5860 * "blocked by NMI" bit has to be set before next VM entry.
5861 * There are errata that may cause this bit to not be set:
5862 * AAK134, BY25.
5863 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03005864 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005865 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03005866 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03005867 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
5868
Sheng Yang14394422008-04-28 12:24:45 +08005869 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005870 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005871
Junaid Shahid27959a42016-12-06 16:46:10 -08005872 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08005873 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08005874 ? PFERR_USER_MASK : 0;
5875 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08005876 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08005877 ? PFERR_WRITE_MASK : 0;
5878 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08005879 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08005880 ? PFERR_FETCH_MASK : 0;
5881 /* ept page table entry is present? */
5882 error_code |= (exit_qualification &
5883 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
5884 EPT_VIOLATION_EXECUTABLE))
5885 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005886
Paolo Bonzinieebed242016-11-28 14:39:58 +01005887 error_code |= (exit_qualification & 0x100) != 0 ?
5888 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03005889
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005890 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005891 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08005892}
5893
Avi Kivity851ba692009-08-24 11:10:17 +03005894static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005895{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005896 gpa_t gpa;
5897
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02005898 /*
5899 * A nested guest cannot optimize MMIO vmexits, because we have an
5900 * nGPA here instead of the required GPA.
5901 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005902 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02005903 if (!is_guest_mode(vcpu) &&
5904 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08005905 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01005906 /*
5907 * Doing kvm_skip_emulated_instruction() depends on undefined
5908 * behavior: Intel's manual doesn't mandate
5909 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
5910 * occurs and while on real hardware it was observed to be set,
5911 * other hypervisors (namely Hyper-V) don't set it, we end up
5912 * advancing IP with some random value. Disable fast mmio when
5913 * running nested and keep it for real hardware in hope that
5914 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
5915 */
5916 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
5917 return kvm_skip_emulated_instruction(vcpu);
5918 else
Sean Christopherson0ce97a22018-08-23 13:56:52 -07005919 return kvm_emulate_instruction(vcpu, EMULTYPE_SKIP) ==
Sean Christophersonc4409902018-08-23 13:56:46 -07005920 EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03005921 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005922
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07005923 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005924}
5925
Avi Kivity851ba692009-08-24 11:10:17 +03005926static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08005927{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005928 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01005929 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
5930 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08005931 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03005932 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08005933
5934 return 1;
5935}
5936
Mohammed Gamal80ced182009-09-01 12:48:18 +02005937static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005938{
Avi Kivity8b3079a2009-01-05 12:10:54 +02005939 struct vcpu_vmx *vmx = to_vmx(vcpu);
5940 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02005941 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02005942 u32 cpu_exec_ctrl;
5943 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03005944 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02005945
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07005946 /*
5947 * We should never reach the point where we are emulating L2
5948 * due to invalid guest state as that means we incorrectly
5949 * allowed a nested VMEntry with an invalid vmcs12.
5950 */
5951 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
5952
Avi Kivity49e9d552010-09-19 14:34:08 +02005953 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5954 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005955
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005956 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03005957 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02005958 return handle_interrupt_window(&vmx->vcpu);
5959
Radim Krčmář72875d82017-04-26 22:32:19 +02005960 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03005961 return 1;
5962
Sean Christopherson0ce97a22018-08-23 13:56:52 -07005963 err = kvm_emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005964
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02005965 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02005966 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02005967 ret = 0;
5968 goto out;
5969 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01005970
Sean Christophersonadd5ff72018-03-23 09:34:00 -07005971 if (err != EMULATE_DONE)
5972 goto emulation_error;
5973
5974 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
5975 vcpu->arch.exception.pending)
5976 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005977
Gleb Natapov8d76c492013-05-08 18:38:44 +03005978 if (vcpu->arch.halt_request) {
5979 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06005980 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03005981 goto out;
5982 }
5983
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005984 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02005985 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005986 if (need_resched())
5987 schedule();
5988 }
5989
Mohammed Gamal80ced182009-09-01 12:48:18 +02005990out:
5991 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005992
Sean Christophersonadd5ff72018-03-23 09:34:00 -07005993emulation_error:
5994 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5995 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
5996 vcpu->run->internal.ndata = 0;
5997 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005998}
5999
6000static void grow_ple_window(struct kvm_vcpu *vcpu)
6001{
6002 struct vcpu_vmx *vmx = to_vmx(vcpu);
6003 int old = vmx->ple_window;
6004
Babu Mogerc8e88712018-03-16 16:37:24 -04006005 vmx->ple_window = __grow_ple_window(old, ple_window,
6006 ple_window_grow,
6007 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006008
6009 if (vmx->ple_window != old)
6010 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006011
6012 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006013}
6014
6015static void shrink_ple_window(struct kvm_vcpu *vcpu)
6016{
6017 struct vcpu_vmx *vmx = to_vmx(vcpu);
6018 int old = vmx->ple_window;
6019
Babu Mogerc8e88712018-03-16 16:37:24 -04006020 vmx->ple_window = __shrink_ple_window(old, ple_window,
6021 ple_window_shrink,
6022 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006023
6024 if (vmx->ple_window != old)
6025 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006026
6027 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006028}
6029
6030/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08006031 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
6032 */
6033static void wakeup_handler(void)
6034{
6035 struct kvm_vcpu *vcpu;
6036 int cpu = smp_processor_id();
6037
6038 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6039 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
6040 blocked_vcpu_list) {
6041 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6042
6043 if (pi_test_on(pi_desc) == 1)
6044 kvm_vcpu_kick(vcpu);
6045 }
6046 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6047}
6048
Peng Haoe01bca22018-04-07 05:47:32 +08006049static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08006050{
6051 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
6052 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
6053 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
6054 0ull, VMX_EPT_EXECUTABLE_MASK,
6055 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05006056 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08006057
6058 ept_set_mmio_spte_mask();
6059 kvm_enable_tdp();
6060}
6061
Tiejun Chenf2c76482014-10-28 10:14:47 +08006062static __init int hardware_setup(void)
6063{
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07006064 unsigned long host_bndcfgs;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006065 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006066
6067 rdmsrl_safe(MSR_EFER, &host_efer);
6068
6069 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
6070 kvm_define_shared_msr(i, vmx_msr_index[i]);
6071
Sean Christopherson7caaa712018-12-03 13:53:01 -08006072 if (setup_vmcs_config(&vmcs_config, &vmx_capability) < 0)
Sean Christophersondfae3c02018-12-03 13:52:52 -08006073 return -EIO;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006074
6075 if (boot_cpu_has(X86_FEATURE_NX))
6076 kvm_enable_efer_bits(EFER_NX);
6077
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07006078 if (boot_cpu_has(X86_FEATURE_MPX)) {
6079 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
6080 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
6081 }
6082
Sean Christopherson71d94092018-12-03 13:52:59 -08006083 if (boot_cpu_has(X86_FEATURE_XSAVES))
6084 rdmsrl(MSR_IA32_XSS, host_xss);
6085
Wanpeng Li08d839c2017-03-23 05:30:08 -07006086 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
6087 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08006088 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07006089
Tiejun Chenf2c76482014-10-28 10:14:47 +08006090 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02006091 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02006092 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07006093 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08006094 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006095
Wanpeng Lifce6ac42017-05-11 02:58:56 -07006096 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08006097 enable_ept_ad_bits = 0;
6098
Wanpeng Li8ad81822017-10-09 15:51:53 -07006099 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08006100 enable_unrestricted_guest = 0;
6101
Paolo Bonziniad15a292015-01-30 16:18:49 +01006102 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08006103 flexpriority_enabled = 0;
6104
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006105 if (!cpu_has_virtual_nmis())
6106 enable_vnmi = 0;
6107
Paolo Bonziniad15a292015-01-30 16:18:49 +01006108 /*
6109 * set_apic_access_page_addr() is used to reload apic access
6110 * page upon invalidation. No need to do anything if not
6111 * using the APIC_ACCESS_ADDR VMCS field.
6112 */
6113 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08006114 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006115
6116 if (!cpu_has_vmx_tpr_shadow())
6117 kvm_x86_ops->update_cr8_intercept = NULL;
6118
6119 if (enable_ept && !cpu_has_vmx_ept_2m_page())
6120 kvm_disable_largepages();
6121
Tianyu Lan877ad952018-07-19 08:40:23 +00006122#if IS_ENABLED(CONFIG_HYPERV)
6123 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
6124 && enable_ept)
6125 kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb;
6126#endif
6127
Wanpeng Li0f107682017-09-28 18:06:24 -07006128 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08006129 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07006130 ple_window = 0;
6131 ple_window_grow = 0;
6132 ple_window_max = 0;
6133 ple_window_shrink = 0;
6134 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006135
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006136 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08006137 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006138 kvm_x86_ops->sync_pir_to_irr = NULL;
6139 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006140
Haozhong Zhang64903d62015-10-20 15:39:09 +08006141 if (cpu_has_vmx_tsc_scaling()) {
6142 kvm_has_tsc_control = true;
6143 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
6144 kvm_tsc_scaling_ratio_frac_bits = 48;
6145 }
6146
Wanpeng Li04bb92e2015-09-16 19:31:11 +08006147 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
6148
Junaid Shahidf160c7b2016-12-06 16:46:16 -08006149 if (enable_ept)
6150 vmx_enable_tdp();
6151 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08006152 kvm_disable_tdp();
6153
Jim Mattson8fcc4b52018-07-10 11:27:20 +02006154 if (!nested) {
6155 kvm_x86_ops->get_nested_state = NULL;
6156 kvm_x86_ops->set_nested_state = NULL;
6157 }
6158
Kai Huang843e4332015-01-28 10:54:28 +08006159 /*
6160 * Only enable PML when hardware supports PML feature, and both EPT
6161 * and EPT A/D bit features are enabled -- PML depends on them to work.
6162 */
6163 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
6164 enable_pml = 0;
6165
6166 if (!enable_pml) {
6167 kvm_x86_ops->slot_enable_log_dirty = NULL;
6168 kvm_x86_ops->slot_disable_log_dirty = NULL;
6169 kvm_x86_ops->flush_log_dirty = NULL;
6170 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
6171 }
6172
Sean Christophersond264ee02018-08-27 15:21:12 -07006173 if (!cpu_has_vmx_preemption_timer())
6174 kvm_x86_ops->request_immediate_exit = __kvm_request_immediate_exit;
6175
Yunhong Jiang64672c92016-06-13 14:19:59 -07006176 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
6177 u64 vmx_msr;
6178
6179 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
6180 cpu_preemption_timer_multi =
6181 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
6182 } else {
6183 kvm_x86_ops->set_hv_timer = NULL;
6184 kvm_x86_ops->cancel_hv_timer = NULL;
6185 }
6186
Sean Christophersondfae3c02018-12-03 13:52:52 -08006187 if (!cpu_has_vmx_shadow_vmcs() || !nested)
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01006188 enable_shadow_vmcs = 0;
Sean Christophersondfae3c02018-12-03 13:52:52 -08006189 if (enable_shadow_vmcs) {
6190 for (i = 0; i < VMX_BITMAP_NR; i++) {
6191 vmx_bitmap[i] = (unsigned long *)
6192 __get_free_page(GFP_KERNEL);
6193 if (!vmx_bitmap[i])
6194 goto out;
6195 }
6196
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01006197 init_vmcs_shadow_fields();
Sean Christophersondfae3c02018-12-03 13:52:52 -08006198 }
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01006199
Feng Wubf9f6ac2015-09-18 22:29:55 +08006200 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Sean Christopherson7caaa712018-12-03 13:53:01 -08006201 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, vmx_capability.ept,
6202 enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08006203
Ashok Rajc45dcc72016-06-22 14:59:56 +08006204 kvm_mce_cap_supported |= MCG_LMCE_P;
6205
Sean Christopherson1b3ab5a2018-12-03 13:52:51 -08006206 r = alloc_kvm_area();
6207 if (r)
6208 goto out;
6209 return 0;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006210
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006211out:
Sean Christophersondfae3c02018-12-03 13:52:52 -08006212 if (enable_shadow_vmcs) {
6213 for (i = 0; i < VMX_BITMAP_NR; i++)
6214 free_page((unsigned long)vmx_bitmap[i]);
6215 }
Sean Christopherson1b3ab5a2018-12-03 13:52:51 -08006216 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006217}
6218
6219static __exit void hardware_unsetup(void)
6220{
Radim Krčmář23611332016-09-29 22:41:33 +02006221 int i;
6222
Sean Christophersondfae3c02018-12-03 13:52:52 -08006223 if (enable_shadow_vmcs) {
6224 for (i = 0; i < VMX_BITMAP_NR; i++)
6225 free_page((unsigned long)vmx_bitmap[i]);
6226 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006227
Tiejun Chenf2c76482014-10-28 10:14:47 +08006228 free_kvm_area();
6229}
6230
Avi Kivity6aa8b732006-12-10 02:21:36 -08006231/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006232 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
6233 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
6234 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03006235static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006236{
Wanpeng Lib31c1142018-03-12 04:53:04 -07006237 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006238 grow_ple_window(vcpu);
6239
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08006240 /*
6241 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
6242 * VM-execution control is ignored if CPL > 0. OTOH, KVM
6243 * never set PAUSE_EXITING and just set PLE if supported,
6244 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
6245 */
6246 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006247 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006248}
6249
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006250static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08006251{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006252 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08006253}
6254
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006255static int handle_mwait(struct kvm_vcpu *vcpu)
6256{
6257 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
6258 return handle_nop(vcpu);
6259}
6260
Jim Mattson45ec3682017-08-23 16:32:04 -07006261static int handle_invalid_op(struct kvm_vcpu *vcpu)
6262{
6263 kvm_queue_exception(vcpu, UD_VECTOR);
6264 return 1;
6265}
6266
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03006267static int handle_monitor_trap(struct kvm_vcpu *vcpu)
6268{
6269 return 1;
6270}
6271
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006272static int handle_monitor(struct kvm_vcpu *vcpu)
6273{
6274 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
6275 return handle_nop(vcpu);
6276}
6277
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006278/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006279 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
Sean Christopherson09abb5e2018-09-26 09:23:55 -07006280 * set the success or error code of an emulated VMX instruction (as specified
6281 * by Vol 2B, VMX Instruction Reference, "Conventions"), and skip the emulated
6282 * instruction.
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006283 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07006284static int nested_vmx_succeed(struct kvm_vcpu *vcpu)
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006285{
6286 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
6287 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6288 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
Sean Christopherson09abb5e2018-09-26 09:23:55 -07006289 return kvm_skip_emulated_instruction(vcpu);
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006290}
6291
Sean Christopherson09abb5e2018-09-26 09:23:55 -07006292static int nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006293{
6294 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6295 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
6296 X86_EFLAGS_SF | X86_EFLAGS_OF))
6297 | X86_EFLAGS_CF);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07006298 return kvm_skip_emulated_instruction(vcpu);
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006299}
6300
Sean Christopherson09abb5e2018-09-26 09:23:55 -07006301static int nested_vmx_failValid(struct kvm_vcpu *vcpu,
6302 u32 vm_instruction_error)
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006303{
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02006304 struct vcpu_vmx *vmx = to_vmx(vcpu);
6305
Sean Christopherson09abb5e2018-09-26 09:23:55 -07006306 /*
6307 * failValid writes the error number to the current VMCS, which
6308 * can't be done if there isn't a current VMCS.
6309 */
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02006310 if (vmx->nested.current_vmptr == -1ull && !vmx->nested.hv_evmcs)
Sean Christopherson09abb5e2018-09-26 09:23:55 -07006311 return nested_vmx_failInvalid(vcpu);
6312
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006313 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6314 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6315 X86_EFLAGS_SF | X86_EFLAGS_OF))
6316 | X86_EFLAGS_ZF);
6317 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
6318 /*
6319 * We don't need to force a shadow sync because
6320 * VM_INSTRUCTION_ERROR is not shadowed
6321 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07006322 return kvm_skip_emulated_instruction(vcpu);
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006323}
Abel Gordon145c28d2013-04-18 14:36:55 +03006324
Wincy Vanff651cb2014-12-11 08:52:58 +03006325static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
6326{
6327 /* TODO: not to reset guest simply here. */
6328 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02006329 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03006330}
6331
Jan Kiszkaf41245002014-03-07 20:03:13 +01006332static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
6333{
6334 struct vcpu_vmx *vmx =
6335 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
6336
6337 vmx->nested.preemption_timer_expired = true;
6338 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
6339 kvm_vcpu_kick(&vmx->vcpu);
6340
6341 return HRTIMER_NORESTART;
6342}
6343
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006344/*
Bandan Das19677e32014-05-06 02:19:15 -04006345 * Decode the memory-address operand of a vmx instruction, as recorded on an
6346 * exit caused by such an instruction (run by a guest hypervisor).
6347 * On success, returns 0. When the operand is invalid, returns 1 and throws
6348 * #UD or #GP.
6349 */
6350static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
6351 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006352 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04006353{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006354 gva_t off;
6355 bool exn;
6356 struct kvm_segment s;
6357
Bandan Das19677e32014-05-06 02:19:15 -04006358 /*
6359 * According to Vol. 3B, "Information for VM Exits Due to Instruction
6360 * Execution", on an exit, vmx_instruction_info holds most of the
6361 * addressing components of the operand. Only the displacement part
6362 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
6363 * For how an actual address is calculated from all these components,
6364 * refer to Vol. 1, "Operand Addressing".
6365 */
6366 int scaling = vmx_instruction_info & 3;
6367 int addr_size = (vmx_instruction_info >> 7) & 7;
6368 bool is_reg = vmx_instruction_info & (1u << 10);
6369 int seg_reg = (vmx_instruction_info >> 15) & 7;
6370 int index_reg = (vmx_instruction_info >> 18) & 0xf;
6371 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
6372 int base_reg = (vmx_instruction_info >> 23) & 0xf;
6373 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
6374
6375 if (is_reg) {
6376 kvm_queue_exception(vcpu, UD_VECTOR);
6377 return 1;
6378 }
6379
6380 /* Addr = segment_base + offset */
6381 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006382 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04006383 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006384 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04006385 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006386 off += kvm_register_read(vcpu, index_reg)<<scaling;
6387 vmx_get_segment(vcpu, &s, seg_reg);
6388 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04006389
6390 if (addr_size == 1) /* 32 bit */
6391 *ret &= 0xffffffff;
6392
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006393 /* Checks for #GP/#SS exceptions. */
6394 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02006395 if (is_long_mode(vcpu)) {
6396 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
6397 * non-canonical form. This is the only check on the memory
6398 * destination for long mode!
6399 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08006400 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02006401 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006402 /* Protected mode: apply checks for segment validity in the
6403 * following order:
6404 * - segment type check (#GP(0) may be thrown)
6405 * - usability check (#GP(0)/#SS(0))
6406 * - limit check (#GP(0)/#SS(0))
6407 */
6408 if (wr)
6409 /* #GP(0) if the destination operand is located in a
6410 * read-only data segment or any code segment.
6411 */
6412 exn = ((s.type & 0xa) == 0 || (s.type & 8));
6413 else
6414 /* #GP(0) if the source operand is located in an
6415 * execute-only code segment
6416 */
6417 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02006418 if (exn) {
6419 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
6420 return 1;
6421 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006422 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
6423 */
6424 exn = (s.unusable != 0);
6425 /* Protected mode: #GP(0)/#SS(0) if the memory
6426 * operand is outside the segment limit.
6427 */
6428 exn = exn || (off + sizeof(u64) > s.limit);
6429 }
6430 if (exn) {
6431 kvm_queue_exception_e(vcpu,
6432 seg_reg == VCPU_SREG_SS ?
6433 SS_VECTOR : GP_VECTOR,
6434 0);
6435 return 1;
6436 }
6437
Bandan Das19677e32014-05-06 02:19:15 -04006438 return 0;
6439}
6440
Radim Krčmářcbf71272017-05-19 15:48:51 +02006441static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04006442{
6443 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04006444 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04006445
6446 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006447 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04006448 return 1;
6449
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02006450 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04006451 kvm_inject_page_fault(vcpu, &e);
6452 return 1;
6453 }
6454
Bandan Das3573e222014-05-06 02:19:16 -04006455 return 0;
6456}
6457
Liran Alonabfc52c2018-06-23 02:35:13 +03006458/*
6459 * Allocate a shadow VMCS and associate it with the currently loaded
6460 * VMCS, unless such a shadow VMCS already exists. The newly allocated
6461 * VMCS is also VMCLEARed, so that it is ready for use.
6462 */
6463static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
6464{
6465 struct vcpu_vmx *vmx = to_vmx(vcpu);
6466 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
6467
6468 /*
6469 * We should allocate a shadow vmcs for vmcs01 only when L1
6470 * executes VMXON and free it when L1 executes VMXOFF.
6471 * As it is invalid to execute VMXON twice, we shouldn't reach
6472 * here when vmcs01 already have an allocated shadow vmcs.
6473 */
6474 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
6475
6476 if (!loaded_vmcs->shadow_vmcs) {
6477 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
6478 if (loaded_vmcs->shadow_vmcs)
6479 vmcs_clear(loaded_vmcs->shadow_vmcs);
6480 }
6481 return loaded_vmcs->shadow_vmcs;
6482}
6483
Jim Mattsone29acc52016-11-30 12:03:43 -08006484static int enter_vmx_operation(struct kvm_vcpu *vcpu)
6485{
6486 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01006487 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08006488
Paolo Bonzinif21f1652018-01-11 12:16:15 +01006489 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
6490 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06006491 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08006492
6493 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
6494 if (!vmx->nested.cached_vmcs12)
6495 goto out_cached_vmcs12;
6496
Liran Alon61ada742018-06-23 02:35:08 +03006497 vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
6498 if (!vmx->nested.cached_shadow_vmcs12)
6499 goto out_cached_shadow_vmcs12;
6500
Liran Alonabfc52c2018-06-23 02:35:13 +03006501 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
6502 goto out_shadow_vmcs;
Jim Mattsone29acc52016-11-30 12:03:43 -08006503
Jim Mattsone29acc52016-11-30 12:03:43 -08006504 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
6505 HRTIMER_MODE_REL_PINNED);
6506 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
6507
Roman Kagan63aff652018-07-19 21:59:07 +03006508 vmx->nested.vpid02 = allocate_vpid();
6509
Sean Christopherson9d6105b2018-09-26 09:23:51 -07006510 vmx->nested.vmcs02_initialized = false;
Jim Mattsone29acc52016-11-30 12:03:43 -08006511 vmx->nested.vmxon = true;
6512 return 0;
6513
6514out_shadow_vmcs:
Liran Alon61ada742018-06-23 02:35:08 +03006515 kfree(vmx->nested.cached_shadow_vmcs12);
6516
6517out_cached_shadow_vmcs12:
Jim Mattsone29acc52016-11-30 12:03:43 -08006518 kfree(vmx->nested.cached_vmcs12);
6519
6520out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06006521 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08006522
Jim Mattsonde3a0022017-11-27 17:22:25 -06006523out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08006524 return -ENOMEM;
6525}
6526
Bandan Das3573e222014-05-06 02:19:16 -04006527/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006528 * Emulate the VMXON instruction.
6529 * Currently, we just remember that VMX is active, and do not save or even
6530 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
6531 * do not currently need to store anything in that guest-allocated memory
6532 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
6533 * argument is different from the VMXON pointer (which the spec says they do).
6534 */
6535static int handle_vmon(struct kvm_vcpu *vcpu)
6536{
Jim Mattsone29acc52016-11-30 12:03:43 -08006537 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02006538 gpa_t vmptr;
6539 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006540 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08006541 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
6542 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006543
Jim Mattson70f3aac2017-04-26 08:53:46 -07006544 /*
6545 * The Intel VMX Instruction Reference lists a bunch of bits that are
6546 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
6547 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
6548 * Otherwise, we should fail with #UD. But most faulting conditions
6549 * have already been checked by hardware, prior to the VM-exit for
6550 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
6551 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006552 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07006553 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006554 kvm_queue_exception(vcpu, UD_VECTOR);
6555 return 1;
6556 }
6557
Felix Wilhelm727ba742018-06-11 09:43:44 +02006558 /* CPL=0 must be checked manually. */
6559 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07006560 kvm_inject_gp(vcpu, 0);
Felix Wilhelm727ba742018-06-11 09:43:44 +02006561 return 1;
6562 }
6563
Sean Christopherson09abb5e2018-09-26 09:23:55 -07006564 if (vmx->nested.vmxon)
6565 return nested_vmx_failValid(vcpu,
6566 VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08006567
Haozhong Zhang3b840802016-06-22 14:59:54 +08006568 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08006569 != VMXON_NEEDED_FEATURES) {
6570 kvm_inject_gp(vcpu, 0);
6571 return 1;
6572 }
6573
Radim Krčmářcbf71272017-05-19 15:48:51 +02006574 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08006575 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02006576
6577 /*
6578 * SDM 3: 24.11.5
6579 * The first 4 bytes of VMXON region contain the supported
6580 * VMCS revision identifier
6581 *
6582 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
6583 * which replaces physical address width with 32
6584 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07006585 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu)))
6586 return nested_vmx_failInvalid(vcpu);
Radim Krčmářcbf71272017-05-19 15:48:51 +02006587
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02006588 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07006589 if (is_error_page(page))
6590 return nested_vmx_failInvalid(vcpu);
6591
Radim Krčmářcbf71272017-05-19 15:48:51 +02006592 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
6593 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02006594 kvm_release_page_clean(page);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07006595 return nested_vmx_failInvalid(vcpu);
Radim Krčmářcbf71272017-05-19 15:48:51 +02006596 }
6597 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02006598 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02006599
6600 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08006601 ret = enter_vmx_operation(vcpu);
6602 if (ret)
6603 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006604
Sean Christopherson09abb5e2018-09-26 09:23:55 -07006605 return nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006606}
6607
6608/*
6609 * Intel's VMX Instruction Reference specifies a common set of prerequisites
6610 * for running VMX instructions (except VMXON, whose prerequisites are
6611 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07006612 * Note that many of these exceptions have priority over VM exits, so they
6613 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006614 */
6615static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
6616{
Jim Mattson70f3aac2017-04-26 08:53:46 -07006617 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006618 kvm_queue_exception(vcpu, UD_VECTOR);
6619 return 0;
6620 }
Jim Mattsone49fcb82018-07-27 13:44:45 -07006621
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006622 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07006623 kvm_inject_gp(vcpu, 0);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006624 return 0;
6625 }
6626
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006627 return 1;
6628}
6629
David Matlack8ca44e82017-08-01 14:00:39 -07006630static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
6631{
6632 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
6633 vmcs_write64(VMCS_LINK_POINTER, -1ull);
6634}
6635
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02006636static inline void nested_release_evmcs(struct kvm_vcpu *vcpu)
Abel Gordone7953d72013-04-18 14:37:55 +03006637{
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02006638 struct vcpu_vmx *vmx = to_vmx(vcpu);
6639
6640 if (!vmx->nested.hv_evmcs)
6641 return;
6642
6643 kunmap(vmx->nested.hv_evmcs_page);
6644 kvm_release_page_dirty(vmx->nested.hv_evmcs_page);
6645 vmx->nested.hv_evmcs_vmptr = -1ull;
6646 vmx->nested.hv_evmcs_page = NULL;
6647 vmx->nested.hv_evmcs = NULL;
6648}
6649
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02006650static inline void nested_release_vmcs12(struct kvm_vcpu *vcpu)
Abel Gordone7953d72013-04-18 14:37:55 +03006651{
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02006652 struct vcpu_vmx *vmx = to_vmx(vcpu);
6653
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02006654 if (vmx->nested.current_vmptr == -1ull)
6655 return;
6656
Abel Gordon012f83c2013-04-18 14:39:25 +03006657 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02006658 /* copy to memory all shadowed fields in case
6659 they were modified */
6660 copy_shadow_to_vmcs12(vmx);
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +02006661 vmx->nested.need_vmcs12_sync = false;
David Matlack8ca44e82017-08-01 14:00:39 -07006662 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03006663 }
Wincy Van705699a2015-02-03 23:58:17 +08006664 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07006665
6666 /* Flush VMCS12 to guest memory */
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02006667 kvm_vcpu_write_guest_page(vcpu,
Paolo Bonzini9f744c52017-07-27 15:54:46 +02006668 vmx->nested.current_vmptr >> PAGE_SHIFT,
6669 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07006670
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02006671 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
6672
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02006673 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03006674}
6675
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006676/*
6677 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
6678 * just stops using VMX.
6679 */
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02006680static void free_nested(struct kvm_vcpu *vcpu)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006681{
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02006682 struct vcpu_vmx *vmx = to_vmx(vcpu);
6683
Wanpeng Lib7455822017-11-22 14:04:00 -08006684 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006685 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02006686
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006687 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08006688 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07006689 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07006690 vmx->nested.posted_intr_nv = -1;
6691 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07006692 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07006693 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07006694 vmcs_clear(vmx->vmcs01.shadow_vmcs);
6695 free_vmcs(vmx->vmcs01.shadow_vmcs);
6696 vmx->vmcs01.shadow_vmcs = NULL;
6697 }
David Matlack4f2777b2016-07-13 17:16:37 -07006698 kfree(vmx->nested.cached_vmcs12);
Liran Alon61ada742018-06-23 02:35:08 +03006699 kfree(vmx->nested.cached_shadow_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06006700 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006701 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02006702 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02006703 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006704 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08006705 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02006706 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02006707 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08006708 }
Wincy Van705699a2015-02-03 23:58:17 +08006709 if (vmx->nested.pi_desc_page) {
6710 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02006711 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08006712 vmx->nested.pi_desc_page = NULL;
6713 vmx->nested.pi_desc = NULL;
6714 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006715
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02006716 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
6717
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02006718 nested_release_evmcs(vcpu);
6719
Jim Mattsonde3a0022017-11-27 17:22:25 -06006720 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006721}
6722
6723/* Emulate the VMXOFF instruction */
6724static int handle_vmoff(struct kvm_vcpu *vcpu)
6725{
6726 if (!nested_vmx_check_permission(vcpu))
6727 return 1;
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02006728 free_nested(vcpu);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07006729 return nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006730}
6731
Nadav Har'El27d6c862011-05-25 23:06:59 +03006732/* Emulate the VMCLEAR instruction */
6733static int handle_vmclear(struct kvm_vcpu *vcpu)
6734{
6735 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08006736 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03006737 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03006738
6739 if (!nested_vmx_check_permission(vcpu))
6740 return 1;
6741
Radim Krčmářcbf71272017-05-19 15:48:51 +02006742 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03006743 return 1;
6744
Sean Christopherson09abb5e2018-09-26 09:23:55 -07006745 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu)))
6746 return nested_vmx_failValid(vcpu,
6747 VMXERR_VMCLEAR_INVALID_ADDRESS);
Radim Krčmářcbf71272017-05-19 15:48:51 +02006748
Sean Christopherson09abb5e2018-09-26 09:23:55 -07006749 if (vmptr == vmx->nested.vmxon_ptr)
6750 return nested_vmx_failValid(vcpu,
6751 VMXERR_VMCLEAR_VMXON_POINTER);
Radim Krčmářcbf71272017-05-19 15:48:51 +02006752
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02006753 if (vmx->nested.hv_evmcs_page) {
6754 if (vmptr == vmx->nested.hv_evmcs_vmptr)
6755 nested_release_evmcs(vcpu);
6756 } else {
6757 if (vmptr == vmx->nested.current_vmptr)
6758 nested_release_vmcs12(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03006759
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02006760 kvm_vcpu_write_guest(vcpu,
6761 vmptr + offsetof(struct vmcs12,
6762 launch_state),
6763 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03006764 }
6765
Sean Christopherson09abb5e2018-09-26 09:23:55 -07006766 return nested_vmx_succeed(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03006767}
6768
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03006769static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
6770
6771/* Emulate the VMLAUNCH instruction */
6772static int handle_vmlaunch(struct kvm_vcpu *vcpu)
6773{
6774 return nested_vmx_run(vcpu, true);
6775}
6776
6777/* Emulate the VMRESUME instruction */
6778static int handle_vmresume(struct kvm_vcpu *vcpu)
6779{
6780
6781 return nested_vmx_run(vcpu, false);
6782}
6783
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +02006784static int copy_enlightened_to_vmcs12(struct vcpu_vmx *vmx)
6785{
6786 struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
6787 struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
6788
6789 /* HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE */
6790 vmcs12->tpr_threshold = evmcs->tpr_threshold;
6791 vmcs12->guest_rip = evmcs->guest_rip;
6792
6793 if (unlikely(!(evmcs->hv_clean_fields &
6794 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC))) {
6795 vmcs12->guest_rsp = evmcs->guest_rsp;
6796 vmcs12->guest_rflags = evmcs->guest_rflags;
6797 vmcs12->guest_interruptibility_info =
6798 evmcs->guest_interruptibility_info;
6799 }
6800
6801 if (unlikely(!(evmcs->hv_clean_fields &
6802 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) {
6803 vmcs12->cpu_based_vm_exec_control =
6804 evmcs->cpu_based_vm_exec_control;
6805 }
6806
6807 if (unlikely(!(evmcs->hv_clean_fields &
6808 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) {
6809 vmcs12->exception_bitmap = evmcs->exception_bitmap;
6810 }
6811
6812 if (unlikely(!(evmcs->hv_clean_fields &
6813 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_ENTRY))) {
6814 vmcs12->vm_entry_controls = evmcs->vm_entry_controls;
6815 }
6816
6817 if (unlikely(!(evmcs->hv_clean_fields &
6818 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT))) {
6819 vmcs12->vm_entry_intr_info_field =
6820 evmcs->vm_entry_intr_info_field;
6821 vmcs12->vm_entry_exception_error_code =
6822 evmcs->vm_entry_exception_error_code;
6823 vmcs12->vm_entry_instruction_len =
6824 evmcs->vm_entry_instruction_len;
6825 }
6826
6827 if (unlikely(!(evmcs->hv_clean_fields &
6828 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) {
6829 vmcs12->host_ia32_pat = evmcs->host_ia32_pat;
6830 vmcs12->host_ia32_efer = evmcs->host_ia32_efer;
6831 vmcs12->host_cr0 = evmcs->host_cr0;
6832 vmcs12->host_cr3 = evmcs->host_cr3;
6833 vmcs12->host_cr4 = evmcs->host_cr4;
6834 vmcs12->host_ia32_sysenter_esp = evmcs->host_ia32_sysenter_esp;
6835 vmcs12->host_ia32_sysenter_eip = evmcs->host_ia32_sysenter_eip;
6836 vmcs12->host_rip = evmcs->host_rip;
6837 vmcs12->host_ia32_sysenter_cs = evmcs->host_ia32_sysenter_cs;
6838 vmcs12->host_es_selector = evmcs->host_es_selector;
6839 vmcs12->host_cs_selector = evmcs->host_cs_selector;
6840 vmcs12->host_ss_selector = evmcs->host_ss_selector;
6841 vmcs12->host_ds_selector = evmcs->host_ds_selector;
6842 vmcs12->host_fs_selector = evmcs->host_fs_selector;
6843 vmcs12->host_gs_selector = evmcs->host_gs_selector;
6844 vmcs12->host_tr_selector = evmcs->host_tr_selector;
6845 }
6846
6847 if (unlikely(!(evmcs->hv_clean_fields &
6848 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) {
6849 vmcs12->pin_based_vm_exec_control =
6850 evmcs->pin_based_vm_exec_control;
6851 vmcs12->vm_exit_controls = evmcs->vm_exit_controls;
6852 vmcs12->secondary_vm_exec_control =
6853 evmcs->secondary_vm_exec_control;
6854 }
6855
6856 if (unlikely(!(evmcs->hv_clean_fields &
6857 HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP))) {
6858 vmcs12->io_bitmap_a = evmcs->io_bitmap_a;
6859 vmcs12->io_bitmap_b = evmcs->io_bitmap_b;
6860 }
6861
6862 if (unlikely(!(evmcs->hv_clean_fields &
6863 HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP))) {
6864 vmcs12->msr_bitmap = evmcs->msr_bitmap;
6865 }
6866
6867 if (unlikely(!(evmcs->hv_clean_fields &
6868 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2))) {
6869 vmcs12->guest_es_base = evmcs->guest_es_base;
6870 vmcs12->guest_cs_base = evmcs->guest_cs_base;
6871 vmcs12->guest_ss_base = evmcs->guest_ss_base;
6872 vmcs12->guest_ds_base = evmcs->guest_ds_base;
6873 vmcs12->guest_fs_base = evmcs->guest_fs_base;
6874 vmcs12->guest_gs_base = evmcs->guest_gs_base;
6875 vmcs12->guest_ldtr_base = evmcs->guest_ldtr_base;
6876 vmcs12->guest_tr_base = evmcs->guest_tr_base;
6877 vmcs12->guest_gdtr_base = evmcs->guest_gdtr_base;
6878 vmcs12->guest_idtr_base = evmcs->guest_idtr_base;
6879 vmcs12->guest_es_limit = evmcs->guest_es_limit;
6880 vmcs12->guest_cs_limit = evmcs->guest_cs_limit;
6881 vmcs12->guest_ss_limit = evmcs->guest_ss_limit;
6882 vmcs12->guest_ds_limit = evmcs->guest_ds_limit;
6883 vmcs12->guest_fs_limit = evmcs->guest_fs_limit;
6884 vmcs12->guest_gs_limit = evmcs->guest_gs_limit;
6885 vmcs12->guest_ldtr_limit = evmcs->guest_ldtr_limit;
6886 vmcs12->guest_tr_limit = evmcs->guest_tr_limit;
6887 vmcs12->guest_gdtr_limit = evmcs->guest_gdtr_limit;
6888 vmcs12->guest_idtr_limit = evmcs->guest_idtr_limit;
6889 vmcs12->guest_es_ar_bytes = evmcs->guest_es_ar_bytes;
6890 vmcs12->guest_cs_ar_bytes = evmcs->guest_cs_ar_bytes;
6891 vmcs12->guest_ss_ar_bytes = evmcs->guest_ss_ar_bytes;
6892 vmcs12->guest_ds_ar_bytes = evmcs->guest_ds_ar_bytes;
6893 vmcs12->guest_fs_ar_bytes = evmcs->guest_fs_ar_bytes;
6894 vmcs12->guest_gs_ar_bytes = evmcs->guest_gs_ar_bytes;
6895 vmcs12->guest_ldtr_ar_bytes = evmcs->guest_ldtr_ar_bytes;
6896 vmcs12->guest_tr_ar_bytes = evmcs->guest_tr_ar_bytes;
6897 vmcs12->guest_es_selector = evmcs->guest_es_selector;
6898 vmcs12->guest_cs_selector = evmcs->guest_cs_selector;
6899 vmcs12->guest_ss_selector = evmcs->guest_ss_selector;
6900 vmcs12->guest_ds_selector = evmcs->guest_ds_selector;
6901 vmcs12->guest_fs_selector = evmcs->guest_fs_selector;
6902 vmcs12->guest_gs_selector = evmcs->guest_gs_selector;
6903 vmcs12->guest_ldtr_selector = evmcs->guest_ldtr_selector;
6904 vmcs12->guest_tr_selector = evmcs->guest_tr_selector;
6905 }
6906
6907 if (unlikely(!(evmcs->hv_clean_fields &
6908 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2))) {
6909 vmcs12->tsc_offset = evmcs->tsc_offset;
6910 vmcs12->virtual_apic_page_addr = evmcs->virtual_apic_page_addr;
6911 vmcs12->xss_exit_bitmap = evmcs->xss_exit_bitmap;
6912 }
6913
6914 if (unlikely(!(evmcs->hv_clean_fields &
6915 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR))) {
6916 vmcs12->cr0_guest_host_mask = evmcs->cr0_guest_host_mask;
6917 vmcs12->cr4_guest_host_mask = evmcs->cr4_guest_host_mask;
6918 vmcs12->cr0_read_shadow = evmcs->cr0_read_shadow;
6919 vmcs12->cr4_read_shadow = evmcs->cr4_read_shadow;
6920 vmcs12->guest_cr0 = evmcs->guest_cr0;
6921 vmcs12->guest_cr3 = evmcs->guest_cr3;
6922 vmcs12->guest_cr4 = evmcs->guest_cr4;
6923 vmcs12->guest_dr7 = evmcs->guest_dr7;
6924 }
6925
6926 if (unlikely(!(evmcs->hv_clean_fields &
6927 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER))) {
6928 vmcs12->host_fs_base = evmcs->host_fs_base;
6929 vmcs12->host_gs_base = evmcs->host_gs_base;
6930 vmcs12->host_tr_base = evmcs->host_tr_base;
6931 vmcs12->host_gdtr_base = evmcs->host_gdtr_base;
6932 vmcs12->host_idtr_base = evmcs->host_idtr_base;
6933 vmcs12->host_rsp = evmcs->host_rsp;
6934 }
6935
6936 if (unlikely(!(evmcs->hv_clean_fields &
6937 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT))) {
6938 vmcs12->ept_pointer = evmcs->ept_pointer;
6939 vmcs12->virtual_processor_id = evmcs->virtual_processor_id;
6940 }
6941
6942 if (unlikely(!(evmcs->hv_clean_fields &
6943 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1))) {
6944 vmcs12->vmcs_link_pointer = evmcs->vmcs_link_pointer;
6945 vmcs12->guest_ia32_debugctl = evmcs->guest_ia32_debugctl;
6946 vmcs12->guest_ia32_pat = evmcs->guest_ia32_pat;
6947 vmcs12->guest_ia32_efer = evmcs->guest_ia32_efer;
6948 vmcs12->guest_pdptr0 = evmcs->guest_pdptr0;
6949 vmcs12->guest_pdptr1 = evmcs->guest_pdptr1;
6950 vmcs12->guest_pdptr2 = evmcs->guest_pdptr2;
6951 vmcs12->guest_pdptr3 = evmcs->guest_pdptr3;
6952 vmcs12->guest_pending_dbg_exceptions =
6953 evmcs->guest_pending_dbg_exceptions;
6954 vmcs12->guest_sysenter_esp = evmcs->guest_sysenter_esp;
6955 vmcs12->guest_sysenter_eip = evmcs->guest_sysenter_eip;
6956 vmcs12->guest_bndcfgs = evmcs->guest_bndcfgs;
6957 vmcs12->guest_activity_state = evmcs->guest_activity_state;
6958 vmcs12->guest_sysenter_cs = evmcs->guest_sysenter_cs;
6959 }
6960
6961 /*
6962 * Not used?
6963 * vmcs12->vm_exit_msr_store_addr = evmcs->vm_exit_msr_store_addr;
6964 * vmcs12->vm_exit_msr_load_addr = evmcs->vm_exit_msr_load_addr;
6965 * vmcs12->vm_entry_msr_load_addr = evmcs->vm_entry_msr_load_addr;
6966 * vmcs12->cr3_target_value0 = evmcs->cr3_target_value0;
6967 * vmcs12->cr3_target_value1 = evmcs->cr3_target_value1;
6968 * vmcs12->cr3_target_value2 = evmcs->cr3_target_value2;
6969 * vmcs12->cr3_target_value3 = evmcs->cr3_target_value3;
6970 * vmcs12->page_fault_error_code_mask =
6971 * evmcs->page_fault_error_code_mask;
6972 * vmcs12->page_fault_error_code_match =
6973 * evmcs->page_fault_error_code_match;
6974 * vmcs12->cr3_target_count = evmcs->cr3_target_count;
6975 * vmcs12->vm_exit_msr_store_count = evmcs->vm_exit_msr_store_count;
6976 * vmcs12->vm_exit_msr_load_count = evmcs->vm_exit_msr_load_count;
6977 * vmcs12->vm_entry_msr_load_count = evmcs->vm_entry_msr_load_count;
6978 */
6979
6980 /*
6981 * Read only fields:
6982 * vmcs12->guest_physical_address = evmcs->guest_physical_address;
6983 * vmcs12->vm_instruction_error = evmcs->vm_instruction_error;
6984 * vmcs12->vm_exit_reason = evmcs->vm_exit_reason;
6985 * vmcs12->vm_exit_intr_info = evmcs->vm_exit_intr_info;
6986 * vmcs12->vm_exit_intr_error_code = evmcs->vm_exit_intr_error_code;
6987 * vmcs12->idt_vectoring_info_field = evmcs->idt_vectoring_info_field;
6988 * vmcs12->idt_vectoring_error_code = evmcs->idt_vectoring_error_code;
6989 * vmcs12->vm_exit_instruction_len = evmcs->vm_exit_instruction_len;
6990 * vmcs12->vmx_instruction_info = evmcs->vmx_instruction_info;
6991 * vmcs12->exit_qualification = evmcs->exit_qualification;
6992 * vmcs12->guest_linear_address = evmcs->guest_linear_address;
6993 *
6994 * Not present in struct vmcs12:
6995 * vmcs12->exit_io_instruction_ecx = evmcs->exit_io_instruction_ecx;
6996 * vmcs12->exit_io_instruction_esi = evmcs->exit_io_instruction_esi;
6997 * vmcs12->exit_io_instruction_edi = evmcs->exit_io_instruction_edi;
6998 * vmcs12->exit_io_instruction_eip = evmcs->exit_io_instruction_eip;
6999 */
7000
7001 return 0;
7002}
7003
7004static int copy_vmcs12_to_enlightened(struct vcpu_vmx *vmx)
7005{
7006 struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
7007 struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
7008
7009 /*
7010 * Should not be changed by KVM:
7011 *
7012 * evmcs->host_es_selector = vmcs12->host_es_selector;
7013 * evmcs->host_cs_selector = vmcs12->host_cs_selector;
7014 * evmcs->host_ss_selector = vmcs12->host_ss_selector;
7015 * evmcs->host_ds_selector = vmcs12->host_ds_selector;
7016 * evmcs->host_fs_selector = vmcs12->host_fs_selector;
7017 * evmcs->host_gs_selector = vmcs12->host_gs_selector;
7018 * evmcs->host_tr_selector = vmcs12->host_tr_selector;
7019 * evmcs->host_ia32_pat = vmcs12->host_ia32_pat;
7020 * evmcs->host_ia32_efer = vmcs12->host_ia32_efer;
7021 * evmcs->host_cr0 = vmcs12->host_cr0;
7022 * evmcs->host_cr3 = vmcs12->host_cr3;
7023 * evmcs->host_cr4 = vmcs12->host_cr4;
7024 * evmcs->host_ia32_sysenter_esp = vmcs12->host_ia32_sysenter_esp;
7025 * evmcs->host_ia32_sysenter_eip = vmcs12->host_ia32_sysenter_eip;
7026 * evmcs->host_rip = vmcs12->host_rip;
7027 * evmcs->host_ia32_sysenter_cs = vmcs12->host_ia32_sysenter_cs;
7028 * evmcs->host_fs_base = vmcs12->host_fs_base;
7029 * evmcs->host_gs_base = vmcs12->host_gs_base;
7030 * evmcs->host_tr_base = vmcs12->host_tr_base;
7031 * evmcs->host_gdtr_base = vmcs12->host_gdtr_base;
7032 * evmcs->host_idtr_base = vmcs12->host_idtr_base;
7033 * evmcs->host_rsp = vmcs12->host_rsp;
7034 * sync_vmcs12() doesn't read these:
7035 * evmcs->io_bitmap_a = vmcs12->io_bitmap_a;
7036 * evmcs->io_bitmap_b = vmcs12->io_bitmap_b;
7037 * evmcs->msr_bitmap = vmcs12->msr_bitmap;
7038 * evmcs->ept_pointer = vmcs12->ept_pointer;
7039 * evmcs->xss_exit_bitmap = vmcs12->xss_exit_bitmap;
7040 * evmcs->vm_exit_msr_store_addr = vmcs12->vm_exit_msr_store_addr;
7041 * evmcs->vm_exit_msr_load_addr = vmcs12->vm_exit_msr_load_addr;
7042 * evmcs->vm_entry_msr_load_addr = vmcs12->vm_entry_msr_load_addr;
7043 * evmcs->cr3_target_value0 = vmcs12->cr3_target_value0;
7044 * evmcs->cr3_target_value1 = vmcs12->cr3_target_value1;
7045 * evmcs->cr3_target_value2 = vmcs12->cr3_target_value2;
7046 * evmcs->cr3_target_value3 = vmcs12->cr3_target_value3;
7047 * evmcs->tpr_threshold = vmcs12->tpr_threshold;
7048 * evmcs->virtual_processor_id = vmcs12->virtual_processor_id;
7049 * evmcs->exception_bitmap = vmcs12->exception_bitmap;
7050 * evmcs->vmcs_link_pointer = vmcs12->vmcs_link_pointer;
7051 * evmcs->pin_based_vm_exec_control = vmcs12->pin_based_vm_exec_control;
7052 * evmcs->vm_exit_controls = vmcs12->vm_exit_controls;
7053 * evmcs->secondary_vm_exec_control = vmcs12->secondary_vm_exec_control;
7054 * evmcs->page_fault_error_code_mask =
7055 * vmcs12->page_fault_error_code_mask;
7056 * evmcs->page_fault_error_code_match =
7057 * vmcs12->page_fault_error_code_match;
7058 * evmcs->cr3_target_count = vmcs12->cr3_target_count;
7059 * evmcs->virtual_apic_page_addr = vmcs12->virtual_apic_page_addr;
7060 * evmcs->tsc_offset = vmcs12->tsc_offset;
7061 * evmcs->guest_ia32_debugctl = vmcs12->guest_ia32_debugctl;
7062 * evmcs->cr0_guest_host_mask = vmcs12->cr0_guest_host_mask;
7063 * evmcs->cr4_guest_host_mask = vmcs12->cr4_guest_host_mask;
7064 * evmcs->cr0_read_shadow = vmcs12->cr0_read_shadow;
7065 * evmcs->cr4_read_shadow = vmcs12->cr4_read_shadow;
7066 * evmcs->vm_exit_msr_store_count = vmcs12->vm_exit_msr_store_count;
7067 * evmcs->vm_exit_msr_load_count = vmcs12->vm_exit_msr_load_count;
7068 * evmcs->vm_entry_msr_load_count = vmcs12->vm_entry_msr_load_count;
7069 *
7070 * Not present in struct vmcs12:
7071 * evmcs->exit_io_instruction_ecx = vmcs12->exit_io_instruction_ecx;
7072 * evmcs->exit_io_instruction_esi = vmcs12->exit_io_instruction_esi;
7073 * evmcs->exit_io_instruction_edi = vmcs12->exit_io_instruction_edi;
7074 * evmcs->exit_io_instruction_eip = vmcs12->exit_io_instruction_eip;
7075 */
7076
7077 evmcs->guest_es_selector = vmcs12->guest_es_selector;
7078 evmcs->guest_cs_selector = vmcs12->guest_cs_selector;
7079 evmcs->guest_ss_selector = vmcs12->guest_ss_selector;
7080 evmcs->guest_ds_selector = vmcs12->guest_ds_selector;
7081 evmcs->guest_fs_selector = vmcs12->guest_fs_selector;
7082 evmcs->guest_gs_selector = vmcs12->guest_gs_selector;
7083 evmcs->guest_ldtr_selector = vmcs12->guest_ldtr_selector;
7084 evmcs->guest_tr_selector = vmcs12->guest_tr_selector;
7085
7086 evmcs->guest_es_limit = vmcs12->guest_es_limit;
7087 evmcs->guest_cs_limit = vmcs12->guest_cs_limit;
7088 evmcs->guest_ss_limit = vmcs12->guest_ss_limit;
7089 evmcs->guest_ds_limit = vmcs12->guest_ds_limit;
7090 evmcs->guest_fs_limit = vmcs12->guest_fs_limit;
7091 evmcs->guest_gs_limit = vmcs12->guest_gs_limit;
7092 evmcs->guest_ldtr_limit = vmcs12->guest_ldtr_limit;
7093 evmcs->guest_tr_limit = vmcs12->guest_tr_limit;
7094 evmcs->guest_gdtr_limit = vmcs12->guest_gdtr_limit;
7095 evmcs->guest_idtr_limit = vmcs12->guest_idtr_limit;
7096
7097 evmcs->guest_es_ar_bytes = vmcs12->guest_es_ar_bytes;
7098 evmcs->guest_cs_ar_bytes = vmcs12->guest_cs_ar_bytes;
7099 evmcs->guest_ss_ar_bytes = vmcs12->guest_ss_ar_bytes;
7100 evmcs->guest_ds_ar_bytes = vmcs12->guest_ds_ar_bytes;
7101 evmcs->guest_fs_ar_bytes = vmcs12->guest_fs_ar_bytes;
7102 evmcs->guest_gs_ar_bytes = vmcs12->guest_gs_ar_bytes;
7103 evmcs->guest_ldtr_ar_bytes = vmcs12->guest_ldtr_ar_bytes;
7104 evmcs->guest_tr_ar_bytes = vmcs12->guest_tr_ar_bytes;
7105
7106 evmcs->guest_es_base = vmcs12->guest_es_base;
7107 evmcs->guest_cs_base = vmcs12->guest_cs_base;
7108 evmcs->guest_ss_base = vmcs12->guest_ss_base;
7109 evmcs->guest_ds_base = vmcs12->guest_ds_base;
7110 evmcs->guest_fs_base = vmcs12->guest_fs_base;
7111 evmcs->guest_gs_base = vmcs12->guest_gs_base;
7112 evmcs->guest_ldtr_base = vmcs12->guest_ldtr_base;
7113 evmcs->guest_tr_base = vmcs12->guest_tr_base;
7114 evmcs->guest_gdtr_base = vmcs12->guest_gdtr_base;
7115 evmcs->guest_idtr_base = vmcs12->guest_idtr_base;
7116
7117 evmcs->guest_ia32_pat = vmcs12->guest_ia32_pat;
7118 evmcs->guest_ia32_efer = vmcs12->guest_ia32_efer;
7119
7120 evmcs->guest_pdptr0 = vmcs12->guest_pdptr0;
7121 evmcs->guest_pdptr1 = vmcs12->guest_pdptr1;
7122 evmcs->guest_pdptr2 = vmcs12->guest_pdptr2;
7123 evmcs->guest_pdptr3 = vmcs12->guest_pdptr3;
7124
7125 evmcs->guest_pending_dbg_exceptions =
7126 vmcs12->guest_pending_dbg_exceptions;
7127 evmcs->guest_sysenter_esp = vmcs12->guest_sysenter_esp;
7128 evmcs->guest_sysenter_eip = vmcs12->guest_sysenter_eip;
7129
7130 evmcs->guest_activity_state = vmcs12->guest_activity_state;
7131 evmcs->guest_sysenter_cs = vmcs12->guest_sysenter_cs;
7132
7133 evmcs->guest_cr0 = vmcs12->guest_cr0;
7134 evmcs->guest_cr3 = vmcs12->guest_cr3;
7135 evmcs->guest_cr4 = vmcs12->guest_cr4;
7136 evmcs->guest_dr7 = vmcs12->guest_dr7;
7137
7138 evmcs->guest_physical_address = vmcs12->guest_physical_address;
7139
7140 evmcs->vm_instruction_error = vmcs12->vm_instruction_error;
7141 evmcs->vm_exit_reason = vmcs12->vm_exit_reason;
7142 evmcs->vm_exit_intr_info = vmcs12->vm_exit_intr_info;
7143 evmcs->vm_exit_intr_error_code = vmcs12->vm_exit_intr_error_code;
7144 evmcs->idt_vectoring_info_field = vmcs12->idt_vectoring_info_field;
7145 evmcs->idt_vectoring_error_code = vmcs12->idt_vectoring_error_code;
7146 evmcs->vm_exit_instruction_len = vmcs12->vm_exit_instruction_len;
7147 evmcs->vmx_instruction_info = vmcs12->vmx_instruction_info;
7148
7149 evmcs->exit_qualification = vmcs12->exit_qualification;
7150
7151 evmcs->guest_linear_address = vmcs12->guest_linear_address;
7152 evmcs->guest_rsp = vmcs12->guest_rsp;
7153 evmcs->guest_rflags = vmcs12->guest_rflags;
7154
7155 evmcs->guest_interruptibility_info =
7156 vmcs12->guest_interruptibility_info;
7157 evmcs->cpu_based_vm_exec_control = vmcs12->cpu_based_vm_exec_control;
7158 evmcs->vm_entry_controls = vmcs12->vm_entry_controls;
7159 evmcs->vm_entry_intr_info_field = vmcs12->vm_entry_intr_info_field;
7160 evmcs->vm_entry_exception_error_code =
7161 vmcs12->vm_entry_exception_error_code;
7162 evmcs->vm_entry_instruction_len = vmcs12->vm_entry_instruction_len;
7163
7164 evmcs->guest_rip = vmcs12->guest_rip;
7165
7166 evmcs->guest_bndcfgs = vmcs12->guest_bndcfgs;
7167
7168 return 0;
7169}
7170
Jim Mattsonf4160e42018-05-29 09:11:33 -07007171/*
7172 * Copy the writable VMCS shadow fields back to the VMCS12, in case
7173 * they have been modified by the L1 guest. Note that the "read-only"
7174 * VM-exit information fields are actually writable if the vCPU is
7175 * configured to support "VMWRITE to any supported field in the VMCS."
7176 */
Abel Gordon16f5b902013-04-18 14:38:25 +03007177static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7178{
Jim Mattsonf4160e42018-05-29 09:11:33 -07007179 const u16 *fields[] = {
7180 shadow_read_write_fields,
7181 shadow_read_only_fields
7182 };
7183 const int max_fields[] = {
7184 max_shadow_read_write_fields,
7185 max_shadow_read_only_fields
7186 };
7187 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03007188 unsigned long field;
7189 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007190 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03007191
Jan Kiszka282da872014-10-08 18:05:39 +02007192 preempt_disable();
7193
Abel Gordon16f5b902013-04-18 14:38:25 +03007194 vmcs_load(shadow_vmcs);
7195
Jim Mattsonf4160e42018-05-29 09:11:33 -07007196 for (q = 0; q < ARRAY_SIZE(fields); q++) {
7197 for (i = 0; i < max_fields[q]; i++) {
7198 field = fields[q][i];
7199 field_value = __vmcs_readl(field);
Liran Alone2536742018-06-23 02:35:02 +03007200 vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
Jim Mattsonf4160e42018-05-29 09:11:33 -07007201 }
7202 /*
7203 * Skip the VM-exit information fields if they are read-only.
7204 */
7205 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
7206 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03007207 }
7208
7209 vmcs_clear(shadow_vmcs);
7210 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02007211
7212 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03007213}
7214
Abel Gordonc3114422013-04-18 14:38:55 +03007215static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7216{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007217 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02007218 shadow_read_write_fields,
7219 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03007220 };
Mathias Krausec2bae892013-06-26 20:36:21 +02007221 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03007222 max_shadow_read_write_fields,
7223 max_shadow_read_only_fields
7224 };
7225 int i, q;
7226 unsigned long field;
7227 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007228 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03007229
7230 vmcs_load(shadow_vmcs);
7231
Mathias Krausec2bae892013-06-26 20:36:21 +02007232 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03007233 for (i = 0; i < max_fields[q]; i++) {
7234 field = fields[q][i];
Liran Alone2536742018-06-23 02:35:02 +03007235 vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007236 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03007237 }
7238 }
7239
7240 vmcs_clear(shadow_vmcs);
7241 vmcs_load(vmx->loaded_vmcs->vmcs);
7242}
7243
Nadav Har'El49f705c2011-05-25 23:08:30 +03007244static int handle_vmread(struct kvm_vcpu *vcpu)
7245{
7246 unsigned long field;
7247 u64 field_value;
7248 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7249 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7250 gva_t gva = 0;
Liran Alon6d894f42018-06-23 02:35:09 +03007251 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007252
Kyle Hueyeb277562016-11-29 12:40:39 -08007253 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007254 return 1;
7255
Sean Christopherson09abb5e2018-09-26 09:23:55 -07007256 if (to_vmx(vcpu)->nested.current_vmptr == -1ull)
7257 return nested_vmx_failInvalid(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08007258
Liran Alon6d894f42018-06-23 02:35:09 +03007259 if (!is_guest_mode(vcpu))
7260 vmcs12 = get_vmcs12(vcpu);
7261 else {
7262 /*
7263 * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
7264 * to shadowed-field sets the ALU flags for VMfailInvalid.
7265 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07007266 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull)
7267 return nested_vmx_failInvalid(vcpu);
Liran Alon6d894f42018-06-23 02:35:09 +03007268 vmcs12 = get_shadow_vmcs12(vcpu);
7269 }
7270
Nadav Har'El49f705c2011-05-25 23:08:30 +03007271 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03007272 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007273 /* Read the field, zero-extended to a u64 field_value */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07007274 if (vmcs12_read_any(vmcs12, field, &field_value) < 0)
7275 return nested_vmx_failValid(vcpu,
7276 VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7277
Nadav Har'El49f705c2011-05-25 23:08:30 +03007278 /*
7279 * Now copy part of this value to register or memory, as requested.
7280 * Note that the number of bits actually copied is 32 or 64 depending
7281 * on the guest's mode (32 or 64 bit), not on the given field's length.
7282 */
7283 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03007284 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03007285 field_value);
7286 } else {
7287 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007288 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007289 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02007290 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02007291 kvm_write_guest_virt_system(vcpu, gva, &field_value,
7292 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007293 }
7294
Sean Christopherson09abb5e2018-09-26 09:23:55 -07007295 return nested_vmx_succeed(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007296}
7297
7298
7299static int handle_vmwrite(struct kvm_vcpu *vcpu)
7300{
7301 unsigned long field;
7302 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01007303 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007304 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7305 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01007306
Nadav Har'El49f705c2011-05-25 23:08:30 +03007307 /* The value to write might be 32 or 64 bits, depending on L1's long
7308 * mode, and eventually we need to write that into a field of several
7309 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08007310 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03007311 * bits into the vmcs12 field.
7312 */
7313 u64 field_value = 0;
7314 struct x86_exception e;
Liran Alon6d894f42018-06-23 02:35:09 +03007315 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007316
Kyle Hueyeb277562016-11-29 12:40:39 -08007317 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007318 return 1;
7319
Sean Christopherson09abb5e2018-09-26 09:23:55 -07007320 if (vmx->nested.current_vmptr == -1ull)
7321 return nested_vmx_failInvalid(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08007322
Nadav Har'El49f705c2011-05-25 23:08:30 +03007323 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03007324 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007325 (((vmx_instruction_info) >> 3) & 0xf));
7326 else {
7327 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007328 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007329 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02007330 if (kvm_read_guest_virt(vcpu, gva, &field_value,
7331 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007332 kvm_inject_page_fault(vcpu, &e);
7333 return 1;
7334 }
7335 }
7336
7337
Nadav Amit27e6fb52014-06-18 17:19:26 +03007338 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07007339 /*
7340 * If the vCPU supports "VMWRITE to any supported field in the
7341 * VMCS," then the "read-only" fields are actually read/write.
7342 */
7343 if (vmcs_field_readonly(field) &&
Sean Christopherson09abb5e2018-09-26 09:23:55 -07007344 !nested_cpu_has_vmwrite_any_field(vcpu))
7345 return nested_vmx_failValid(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007346 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007347
Liran Alon6d894f42018-06-23 02:35:09 +03007348 if (!is_guest_mode(vcpu))
7349 vmcs12 = get_vmcs12(vcpu);
7350 else {
7351 /*
7352 * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
7353 * to shadowed-field sets the ALU flags for VMfailInvalid.
7354 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07007355 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull)
7356 return nested_vmx_failInvalid(vcpu);
Liran Alon6d894f42018-06-23 02:35:09 +03007357 vmcs12 = get_shadow_vmcs12(vcpu);
Liran Alon6d894f42018-06-23 02:35:09 +03007358 }
7359
Sean Christopherson09abb5e2018-09-26 09:23:55 -07007360 if (vmcs12_write_any(vmcs12, field, field_value) < 0)
7361 return nested_vmx_failValid(vcpu,
7362 VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007363
Liran Alon6d894f42018-06-23 02:35:09 +03007364 /*
7365 * Do not track vmcs12 dirty-state if in guest-mode
7366 * as we actually dirty shadow vmcs12 instead of vmcs12.
7367 */
7368 if (!is_guest_mode(vcpu)) {
7369 switch (field) {
Paolo Bonzini74a497f2017-12-20 13:55:39 +01007370#define SHADOW_FIELD_RW(x) case x:
Sean Christophersone0123112018-12-03 13:52:57 -08007371#include "vmcs_shadow_fields.h"
Liran Alon6d894f42018-06-23 02:35:09 +03007372 /*
7373 * The fields that can be updated by L1 without a vmexit are
7374 * always updated in the vmcs02, the others go down the slow
7375 * path of prepare_vmcs02.
7376 */
7377 break;
7378 default:
7379 vmx->nested.dirty_vmcs12 = true;
7380 break;
7381 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01007382 }
7383
Sean Christopherson09abb5e2018-09-26 09:23:55 -07007384 return nested_vmx_succeed(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007385}
7386
Jim Mattsona8bc2842016-11-30 12:03:44 -08007387static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
7388{
7389 vmx->nested.current_vmptr = vmptr;
7390 if (enable_shadow_vmcs) {
7391 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
7392 SECONDARY_EXEC_SHADOW_VMCS);
7393 vmcs_write64(VMCS_LINK_POINTER,
7394 __pa(vmx->vmcs01.shadow_vmcs));
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +02007395 vmx->nested.need_vmcs12_sync = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08007396 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01007397 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08007398}
7399
Nadav Har'El63846662011-05-25 23:07:29 +03007400/* Emulate the VMPTRLD instruction */
7401static int handle_vmptrld(struct kvm_vcpu *vcpu)
7402{
7403 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007404 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03007405
7406 if (!nested_vmx_check_permission(vcpu))
7407 return 1;
7408
Radim Krčmářcbf71272017-05-19 15:48:51 +02007409 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03007410 return 1;
7411
Sean Christopherson09abb5e2018-09-26 09:23:55 -07007412 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu)))
7413 return nested_vmx_failValid(vcpu,
7414 VMXERR_VMPTRLD_INVALID_ADDRESS);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007415
Sean Christopherson09abb5e2018-09-26 09:23:55 -07007416 if (vmptr == vmx->nested.vmxon_ptr)
7417 return nested_vmx_failValid(vcpu,
7418 VMXERR_VMPTRLD_VMXON_POINTER);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007419
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02007420 /* Forbid normal VMPTRLD if Enlightened version was used */
7421 if (vmx->nested.hv_evmcs)
7422 return 1;
Nadav Har'El0140cae2011-05-25 23:06:28 +03007423
Nadav Har'El63846662011-05-25 23:07:29 +03007424 if (vmx->nested.current_vmptr != vmptr) {
7425 struct vmcs12 *new_vmcs12;
7426 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02007427 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
Jim Mattsonfca91f62017-03-15 07:40:55 -07007428 if (is_error_page(page)) {
7429 /*
7430 * Reads from an unbacked page return all 1s,
7431 * which means that the 32 bits located at the
7432 * given physical address won't match the required
7433 * VMCS12_REVISION identifier.
7434 */
7435 nested_vmx_failValid(vcpu,
7436 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
7437 return kvm_skip_emulated_instruction(vcpu);
7438 }
Nadav Har'El63846662011-05-25 23:07:29 +03007439 new_vmcs12 = kmap(page);
Liran Alon392b2f22018-06-23 02:35:01 +03007440 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
Liran Alonfa97d7d2018-07-18 14:07:59 +02007441 (new_vmcs12->hdr.shadow_vmcs &&
7442 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
Nadav Har'El63846662011-05-25 23:07:29 +03007443 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007444 kvm_release_page_clean(page);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07007445 return nested_vmx_failValid(vcpu,
Nadav Har'El63846662011-05-25 23:07:29 +03007446 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Nadav Har'El63846662011-05-25 23:07:29 +03007447 }
Nadav Har'El63846662011-05-25 23:07:29 +03007448
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02007449 nested_release_vmcs12(vcpu);
7450
David Matlack4f2777b2016-07-13 17:16:37 -07007451 /*
7452 * Load VMCS12 from guest memory since it is not already
7453 * cached.
7454 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02007455 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
7456 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007457 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02007458
Jim Mattsona8bc2842016-11-30 12:03:44 -08007459 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03007460 }
7461
Sean Christopherson09abb5e2018-09-26 09:23:55 -07007462 return nested_vmx_succeed(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007463}
7464
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02007465/*
7466 * This is an equivalent of the nested hypervisor executing the vmptrld
7467 * instruction.
7468 */
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02007469static int nested_vmx_handle_enlightened_vmptrld(struct kvm_vcpu *vcpu,
7470 bool from_launch)
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02007471{
7472 struct vcpu_vmx *vmx = to_vmx(vcpu);
7473 struct hv_vp_assist_page assist_page;
7474
7475 if (likely(!vmx->nested.enlightened_vmcs_enabled))
7476 return 1;
7477
7478 if (unlikely(!kvm_hv_get_assist_page(vcpu, &assist_page)))
7479 return 1;
7480
7481 if (unlikely(!assist_page.enlighten_vmentry))
7482 return 1;
7483
7484 if (unlikely(assist_page.current_nested_vmcs !=
7485 vmx->nested.hv_evmcs_vmptr)) {
7486
7487 if (!vmx->nested.hv_evmcs)
7488 vmx->nested.current_vmptr = -1ull;
7489
7490 nested_release_evmcs(vcpu);
7491
7492 vmx->nested.hv_evmcs_page = kvm_vcpu_gpa_to_page(
7493 vcpu, assist_page.current_nested_vmcs);
7494
7495 if (unlikely(is_error_page(vmx->nested.hv_evmcs_page)))
7496 return 0;
7497
7498 vmx->nested.hv_evmcs = kmap(vmx->nested.hv_evmcs_page);
7499
Liran Alon72aeb602018-11-01 10:57:39 +02007500 /*
7501 * Currently, KVM only supports eVMCS version 1
7502 * (== KVM_EVMCS_VERSION) and thus we expect guest to set this
7503 * value to first u32 field of eVMCS which should specify eVMCS
7504 * VersionNumber.
7505 *
7506 * Guest should be aware of supported eVMCS versions by host by
7507 * examining CPUID.0x4000000A.EAX[0:15]. Host userspace VMM is
7508 * expected to set this CPUID leaf according to the value
7509 * returned in vmcs_version from nested_enable_evmcs().
7510 *
7511 * However, it turns out that Microsoft Hyper-V fails to comply
7512 * to their own invented interface: When Hyper-V use eVMCS, it
7513 * just sets first u32 field of eVMCS to revision_id specified
7514 * in MSR_IA32_VMX_BASIC. Instead of used eVMCS version number
7515 * which is one of the supported versions specified in
7516 * CPUID.0x4000000A.EAX[0:15].
7517 *
7518 * To overcome Hyper-V bug, we accept here either a supported
7519 * eVMCS version or VMCS12 revision_id as valid values for first
7520 * u32 field of eVMCS.
7521 */
7522 if ((vmx->nested.hv_evmcs->revision_id != KVM_EVMCS_VERSION) &&
7523 (vmx->nested.hv_evmcs->revision_id != VMCS12_REVISION)) {
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02007524 nested_release_evmcs(vcpu);
7525 return 0;
7526 }
7527
7528 vmx->nested.dirty_vmcs12 = true;
7529 /*
7530 * As we keep L2 state for one guest only 'hv_clean_fields' mask
7531 * can't be used when we switch between them. Reset it here for
7532 * simplicity.
7533 */
7534 vmx->nested.hv_evmcs->hv_clean_fields &=
7535 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
7536 vmx->nested.hv_evmcs_vmptr = assist_page.current_nested_vmcs;
7537
7538 /*
7539 * Unlike normal vmcs12, enlightened vmcs12 is not fully
7540 * reloaded from guest's memory (read only fields, fields not
7541 * present in struct hv_enlightened_vmcs, ...). Make sure there
7542 * are no leftovers.
7543 */
Liran Alon52ad7eb2018-11-13 17:44:46 +02007544 if (from_launch) {
7545 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7546 memset(vmcs12, 0, sizeof(*vmcs12));
7547 vmcs12->hdr.revision_id = VMCS12_REVISION;
7548 }
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02007549
7550 }
7551 return 1;
Nadav Har'El0140cae2011-05-25 23:06:28 +03007552}
7553
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007554/* Emulate the VMPTRST instruction */
7555static int handle_vmptrst(struct kvm_vcpu *vcpu)
7556{
Sean Christopherson0a06d422018-07-19 10:31:00 -07007557 unsigned long exit_qual = vmcs_readl(EXIT_QUALIFICATION);
7558 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7559 gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007560 struct x86_exception e;
Sean Christopherson0a06d422018-07-19 10:31:00 -07007561 gva_t gva;
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007562
7563 if (!nested_vmx_check_permission(vcpu))
7564 return 1;
7565
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02007566 if (unlikely(to_vmx(vcpu)->nested.hv_evmcs))
7567 return 1;
7568
Sean Christopherson0a06d422018-07-19 10:31:00 -07007569 if (get_vmx_mem_address(vcpu, exit_qual, instr_info, true, &gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007570 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02007571 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Sean Christopherson0a06d422018-07-19 10:31:00 -07007572 if (kvm_write_guest_virt_system(vcpu, gva, (void *)&current_vmptr,
7573 sizeof(gpa_t), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007574 kvm_inject_page_fault(vcpu, &e);
7575 return 1;
7576 }
Sean Christopherson09abb5e2018-09-26 09:23:55 -07007577 return nested_vmx_succeed(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007578}
7579
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007580/* Emulate the INVEPT instruction */
7581static int handle_invept(struct kvm_vcpu *vcpu)
7582{
Wincy Vanb9c237b2015-02-03 23:56:30 +08007583 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007584 u32 vmx_instruction_info, types;
7585 unsigned long type;
7586 gva_t gva;
7587 struct x86_exception e;
7588 struct {
7589 u64 eptp, gpa;
7590 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007591
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01007592 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08007593 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01007594 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007595 kvm_queue_exception(vcpu, UD_VECTOR);
7596 return 1;
7597 }
7598
7599 if (!nested_vmx_check_permission(vcpu))
7600 return 1;
7601
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007602 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03007603 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007604
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01007605 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007606
Sean Christopherson09abb5e2018-09-26 09:23:55 -07007607 if (type >= 32 || !(types & (1 << type)))
7608 return nested_vmx_failValid(vcpu,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007609 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007610
7611 /* According to the Intel VMX instruction reference, the memory
7612 * operand is read even if it isn't needed (e.g., for type==global)
7613 */
7614 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007615 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007616 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02007617 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007618 kvm_inject_page_fault(vcpu, &e);
7619 return 1;
7620 }
7621
7622 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007623 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04007624 /*
7625 * TODO: track mappings and invalidate
7626 * single context requests appropriately
7627 */
7628 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007629 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04007630 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007631 break;
7632 default:
7633 BUG_ON(1);
7634 break;
7635 }
7636
Sean Christopherson09abb5e2018-09-26 09:23:55 -07007637 return nested_vmx_succeed(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007638}
7639
Liran Alon3d5bdae2018-10-08 23:42:18 +03007640static u16 nested_get_vpid02(struct kvm_vcpu *vcpu)
7641{
7642 struct vcpu_vmx *vmx = to_vmx(vcpu);
7643
7644 return vmx->nested.vpid02 ? vmx->nested.vpid02 : vmx->vpid;
Nadav Har'El0140cae2011-05-25 23:06:28 +03007645}
7646
Petr Matouseka642fc32014-09-23 20:22:30 +02007647static int handle_invvpid(struct kvm_vcpu *vcpu)
7648{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007649 struct vcpu_vmx *vmx = to_vmx(vcpu);
7650 u32 vmx_instruction_info;
7651 unsigned long type, types;
7652 gva_t gva;
7653 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07007654 struct {
7655 u64 vpid;
7656 u64 gla;
7657 } operand;
Liran Alon3d5bdae2018-10-08 23:42:18 +03007658 u16 vpid02;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007659
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01007660 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007661 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01007662 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007663 kvm_queue_exception(vcpu, UD_VECTOR);
7664 return 1;
7665 }
7666
7667 if (!nested_vmx_check_permission(vcpu))
7668 return 1;
7669
7670 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7671 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
7672
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01007673 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007674 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007675
Sean Christopherson09abb5e2018-09-26 09:23:55 -07007676 if (type >= 32 || !(types & (1 << type)))
7677 return nested_vmx_failValid(vcpu,
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007678 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007679
7680 /* according to the intel vmx instruction reference, the memory
7681 * operand is read even if it isn't needed (e.g., for type==global)
7682 */
7683 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7684 vmx_instruction_info, false, &gva))
7685 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02007686 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007687 kvm_inject_page_fault(vcpu, &e);
7688 return 1;
7689 }
Sean Christopherson09abb5e2018-09-26 09:23:55 -07007690 if (operand.vpid >> 16)
7691 return nested_vmx_failValid(vcpu,
Jim Mattson40352602017-06-28 09:37:37 -07007692 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007693
Liran Alon3d5bdae2018-10-08 23:42:18 +03007694 vpid02 = nested_get_vpid02(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007695 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007696 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03007697 if (!operand.vpid ||
Sean Christopherson09abb5e2018-09-26 09:23:55 -07007698 is_noncanonical_address(operand.gla, vcpu))
7699 return nested_vmx_failValid(vcpu,
Jim Mattson40352602017-06-28 09:37:37 -07007700 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Liran Alon3d5bdae2018-10-08 23:42:18 +03007701 if (cpu_has_vmx_invvpid_individual_addr()) {
Liran Aloncd9a4912018-05-22 17:16:15 +03007702 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
Liran Alon3d5bdae2018-10-08 23:42:18 +03007703 vpid02, operand.gla);
Liran Aloncd9a4912018-05-22 17:16:15 +03007704 } else
Liran Alon327c0722018-10-08 23:42:19 +03007705 __vmx_flush_tlb(vcpu, vpid02, false);
Liran Aloncd9a4912018-05-22 17:16:15 +03007706 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01007707 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007708 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Sean Christopherson09abb5e2018-09-26 09:23:55 -07007709 if (!operand.vpid)
7710 return nested_vmx_failValid(vcpu,
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007711 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Liran Alon327c0722018-10-08 23:42:19 +03007712 __vmx_flush_tlb(vcpu, vpid02, false);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007713 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007714 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Alon327c0722018-10-08 23:42:19 +03007715 __vmx_flush_tlb(vcpu, vpid02, false);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007716 break;
7717 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007718 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007719 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007720 }
7721
Sean Christopherson09abb5e2018-09-26 09:23:55 -07007722 return nested_vmx_succeed(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02007723}
7724
Junaid Shahideb4b2482018-06-27 14:59:14 -07007725static int handle_invpcid(struct kvm_vcpu *vcpu)
7726{
7727 u32 vmx_instruction_info;
7728 unsigned long type;
7729 bool pcid_enabled;
7730 gva_t gva;
7731 struct x86_exception e;
Junaid Shahidb94742c2018-06-27 14:59:20 -07007732 unsigned i;
7733 unsigned long roots_to_free = 0;
Junaid Shahideb4b2482018-06-27 14:59:14 -07007734 struct {
7735 u64 pcid;
7736 u64 gla;
7737 } operand;
7738
7739 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
7740 kvm_queue_exception(vcpu, UD_VECTOR);
7741 return 1;
7742 }
7743
7744 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7745 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
7746
7747 if (type > 3) {
7748 kvm_inject_gp(vcpu, 0);
7749 return 1;
7750 }
7751
7752 /* According to the Intel instruction reference, the memory operand
7753 * is read even if it isn't needed (e.g., for type==all)
7754 */
7755 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7756 vmx_instruction_info, false, &gva))
7757 return 1;
7758
7759 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
7760 kvm_inject_page_fault(vcpu, &e);
7761 return 1;
7762 }
7763
7764 if (operand.pcid >> 12 != 0) {
7765 kvm_inject_gp(vcpu, 0);
7766 return 1;
7767 }
7768
7769 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
7770
7771 switch (type) {
7772 case INVPCID_TYPE_INDIV_ADDR:
7773 if ((!pcid_enabled && (operand.pcid != 0)) ||
7774 is_noncanonical_address(operand.gla, vcpu)) {
7775 kvm_inject_gp(vcpu, 0);
7776 return 1;
7777 }
7778 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
7779 return kvm_skip_emulated_instruction(vcpu);
7780
7781 case INVPCID_TYPE_SINGLE_CTXT:
7782 if (!pcid_enabled && (operand.pcid != 0)) {
7783 kvm_inject_gp(vcpu, 0);
7784 return 1;
7785 }
7786
7787 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
7788 kvm_mmu_sync_roots(vcpu);
7789 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
7790 }
7791
Junaid Shahidb94742c2018-06-27 14:59:20 -07007792 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02007793 if (kvm_get_pcid(vcpu, vcpu->arch.mmu->prev_roots[i].cr3)
Junaid Shahidb94742c2018-06-27 14:59:20 -07007794 == operand.pcid)
7795 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
Junaid Shahidade61e22018-06-27 14:59:15 -07007796
Vitaly Kuznetsov6a82cd12018-10-08 21:28:07 +02007797 kvm_mmu_free_roots(vcpu, vcpu->arch.mmu, roots_to_free);
Junaid Shahideb4b2482018-06-27 14:59:14 -07007798 /*
Junaid Shahidb94742c2018-06-27 14:59:20 -07007799 * If neither the current cr3 nor any of the prev_roots use the
Junaid Shahidade61e22018-06-27 14:59:15 -07007800 * given PCID, then nothing needs to be done here because a
7801 * resync will happen anyway before switching to any other CR3.
Junaid Shahideb4b2482018-06-27 14:59:14 -07007802 */
7803
7804 return kvm_skip_emulated_instruction(vcpu);
7805
7806 case INVPCID_TYPE_ALL_NON_GLOBAL:
7807 /*
7808 * Currently, KVM doesn't mark global entries in the shadow
7809 * page tables, so a non-global flush just degenerates to a
7810 * global flush. If needed, we could optimize this later by
7811 * keeping track of global entries in shadow page tables.
7812 */
7813
7814 /* fall-through */
7815 case INVPCID_TYPE_ALL_INCL_GLOBAL:
7816 kvm_mmu_unload(vcpu);
7817 return kvm_skip_emulated_instruction(vcpu);
7818
7819 default:
7820 BUG(); /* We have already checked above that type <= 3 */
7821 }
7822}
7823
Kai Huang843e4332015-01-28 10:54:28 +08007824static int handle_pml_full(struct kvm_vcpu *vcpu)
7825{
7826 unsigned long exit_qualification;
7827
7828 trace_kvm_pml_full(vcpu->vcpu_id);
7829
7830 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7831
7832 /*
7833 * PML buffer FULL happened while executing iret from NMI,
7834 * "blocked by NMI" bit has to be set before next VM entry.
7835 */
7836 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007837 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08007838 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
7839 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
7840 GUEST_INTR_STATE_NMI);
7841
7842 /*
7843 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
7844 * here.., and there's no userspace involvement needed for PML.
7845 */
7846 return 1;
7847}
7848
Yunhong Jiang64672c92016-06-13 14:19:59 -07007849static int handle_preemption_timer(struct kvm_vcpu *vcpu)
7850{
Sean Christophersond264ee02018-08-27 15:21:12 -07007851 if (!to_vmx(vcpu)->req_immediate_exit)
7852 kvm_lapic_expired_hv_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -07007853 return 1;
7854}
7855
Bandan Das41ab9372017-08-03 15:54:43 -04007856static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
7857{
7858 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04007859 int maxphyaddr = cpuid_maxphyaddr(vcpu);
7860
7861 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02007862 switch (address & VMX_EPTP_MT_MASK) {
7863 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01007864 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04007865 return false;
7866 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02007867 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01007868 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04007869 return false;
7870 break;
7871 default:
7872 return false;
7873 }
7874
David Hildenbrandbb97a012017-08-10 23:15:28 +02007875 /* only 4 levels page-walk length are valid */
7876 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04007877 return false;
7878
7879 /* Reserved bits should not be set */
7880 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
7881 return false;
7882
7883 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02007884 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01007885 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04007886 return false;
7887 }
7888
7889 return true;
7890}
7891
7892static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
7893 struct vmcs12 *vmcs12)
7894{
7895 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
7896 u64 address;
7897 bool accessed_dirty;
7898 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
7899
7900 if (!nested_cpu_has_eptp_switching(vmcs12) ||
7901 !nested_cpu_has_ept(vmcs12))
7902 return 1;
7903
7904 if (index >= VMFUNC_EPTP_ENTRIES)
7905 return 1;
7906
7907
7908 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
7909 &address, index * 8, 8))
7910 return 1;
7911
David Hildenbrandbb97a012017-08-10 23:15:28 +02007912 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04007913
7914 /*
7915 * If the (L2) guest does a vmfunc to the currently
7916 * active ept pointer, we don't have to do anything else
7917 */
7918 if (vmcs12->ept_pointer != address) {
7919 if (!valid_ept_address(vcpu, address))
7920 return 1;
7921
7922 kvm_mmu_unload(vcpu);
7923 mmu->ept_ad = accessed_dirty;
Vitaly Kuznetsov36d9594d2018-10-08 21:28:10 +02007924 mmu->mmu_role.base.ad_disabled = !accessed_dirty;
Bandan Das41ab9372017-08-03 15:54:43 -04007925 vmcs12->ept_pointer = address;
7926 /*
7927 * TODO: Check what's the correct approach in case
7928 * mmu reload fails. Currently, we just let the next
7929 * reload potentially fail
7930 */
7931 kvm_mmu_reload(vcpu);
7932 }
7933
7934 return 0;
7935}
7936
Bandan Das2a499e42017-08-03 15:54:41 -04007937static int handle_vmfunc(struct kvm_vcpu *vcpu)
7938{
Bandan Das27c42a12017-08-03 15:54:42 -04007939 struct vcpu_vmx *vmx = to_vmx(vcpu);
7940 struct vmcs12 *vmcs12;
7941 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
7942
7943 /*
7944 * VMFUNC is only supported for nested guests, but we always enable the
7945 * secondary control for simplicity; for non-nested mode, fake that we
7946 * didn't by injecting #UD.
7947 */
7948 if (!is_guest_mode(vcpu)) {
7949 kvm_queue_exception(vcpu, UD_VECTOR);
7950 return 1;
7951 }
7952
7953 vmcs12 = get_vmcs12(vcpu);
7954 if ((vmcs12->vm_function_control & (1 << function)) == 0)
7955 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04007956
7957 switch (function) {
7958 case 0:
7959 if (nested_vmx_eptp_switching(vcpu, vmcs12))
7960 goto fail;
7961 break;
7962 default:
7963 goto fail;
7964 }
7965 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04007966
7967fail:
7968 nested_vmx_vmexit(vcpu, vmx->exit_reason,
7969 vmcs_read32(VM_EXIT_INTR_INFO),
7970 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04007971 return 1;
7972}
7973
Sean Christopherson0b665d32018-08-14 09:33:34 -07007974static int handle_encls(struct kvm_vcpu *vcpu)
7975{
7976 /*
7977 * SGX virtualization is not yet supported. There is no software
7978 * enable bit for SGX, so we have to trap ENCLS and inject a #UD
7979 * to prevent the guest from executing ENCLS.
7980 */
7981 kvm_queue_exception(vcpu, UD_VECTOR);
7982 return 1;
7983}
7984
Nadav Har'El0140cae2011-05-25 23:06:28 +03007985/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08007986 * The exit handlers return 1 if the exit was handled fully and guest execution
7987 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
7988 * to be done to userspace and return 0.
7989 */
Mathias Krause772e0312012-08-30 01:30:19 +02007990static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007991 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
7992 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08007993 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08007994 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007995 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007996 [EXIT_REASON_CR_ACCESS] = handle_cr,
7997 [EXIT_REASON_DR_ACCESS] = handle_dr,
7998 [EXIT_REASON_CPUID] = handle_cpuid,
7999 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8000 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8001 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8002 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02008003 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03008004 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02008005 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02008006 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03008007 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008008 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03008009 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008010 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008011 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008012 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008013 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008014 [EXIT_REASON_VMOFF] = handle_vmoff,
8015 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08008016 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8017 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08008018 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008019 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02008020 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08008021 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02008022 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08008023 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02008024 [EXIT_REASON_GDTR_IDTR] = handle_desc,
8025 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03008026 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8027 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008028 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008029 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008030 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008031 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008032 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02008033 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07008034 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07008035 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08008036 [EXIT_REASON_XSAVES] = handle_xsaves,
8037 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08008038 [EXIT_REASON_PML_FULL] = handle_pml_full,
Junaid Shahideb4b2482018-06-27 14:59:14 -07008039 [EXIT_REASON_INVPCID] = handle_invpcid,
Bandan Das2a499e42017-08-03 15:54:41 -04008040 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07008041 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Sean Christopherson0b665d32018-08-14 09:33:34 -07008042 [EXIT_REASON_ENCLS] = handle_encls,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008043};
8044
8045static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04008046 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008047
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008048static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8049 struct vmcs12 *vmcs12)
8050{
8051 unsigned long exit_qualification;
8052 gpa_t bitmap, last_bitmap;
8053 unsigned int port;
8054 int size;
8055 u8 b;
8056
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008057 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05008058 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008059
8060 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8061
8062 port = exit_qualification >> 16;
8063 size = (exit_qualification & 7) + 1;
8064
8065 last_bitmap = (gpa_t)-1;
8066 b = -1;
8067
8068 while (size > 0) {
8069 if (port < 0x8000)
8070 bitmap = vmcs12->io_bitmap_a;
8071 else if (port < 0x10000)
8072 bitmap = vmcs12->io_bitmap_b;
8073 else
Joe Perches1d804d02015-03-30 16:46:09 -07008074 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008075 bitmap += (port & 0x7fff) / 8;
8076
8077 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008078 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008079 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008080 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07008081 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008082
8083 port++;
8084 size--;
8085 last_bitmap = bitmap;
8086 }
8087
Joe Perches1d804d02015-03-30 16:46:09 -07008088 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008089}
8090
Nadav Har'El644d7112011-05-25 23:12:35 +03008091/*
8092 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8093 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8094 * disinterest in the current event (read or write a specific MSR) by using an
8095 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8096 */
8097static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8098 struct vmcs12 *vmcs12, u32 exit_reason)
8099{
8100 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8101 gpa_t bitmap;
8102
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01008103 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07008104 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008105
8106 /*
8107 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8108 * for the four combinations of read/write and low/high MSR numbers.
8109 * First we need to figure out which of the four to use:
8110 */
8111 bitmap = vmcs12->msr_bitmap;
8112 if (exit_reason == EXIT_REASON_MSR_WRITE)
8113 bitmap += 2048;
8114 if (msr_index >= 0xc0000000) {
8115 msr_index -= 0xc0000000;
8116 bitmap += 1024;
8117 }
8118
8119 /* Then read the msr_index'th bit from this bitmap: */
8120 if (msr_index < 1024*8) {
8121 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008122 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008123 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008124 return 1 & (b >> (msr_index & 7));
8125 } else
Joe Perches1d804d02015-03-30 16:46:09 -07008126 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03008127}
8128
8129/*
8130 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8131 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8132 * intercept (via guest_host_mask etc.) the current event.
8133 */
8134static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8135 struct vmcs12 *vmcs12)
8136{
8137 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8138 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008139 int reg;
8140 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03008141
8142 switch ((exit_qualification >> 4) & 3) {
8143 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008144 reg = (exit_qualification >> 8) & 15;
8145 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03008146 switch (cr) {
8147 case 0:
8148 if (vmcs12->cr0_guest_host_mask &
8149 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008150 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008151 break;
8152 case 3:
8153 if ((vmcs12->cr3_target_count >= 1 &&
8154 vmcs12->cr3_target_value0 == val) ||
8155 (vmcs12->cr3_target_count >= 2 &&
8156 vmcs12->cr3_target_value1 == val) ||
8157 (vmcs12->cr3_target_count >= 3 &&
8158 vmcs12->cr3_target_value2 == val) ||
8159 (vmcs12->cr3_target_count >= 4 &&
8160 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07008161 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008162 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008163 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008164 break;
8165 case 4:
8166 if (vmcs12->cr4_guest_host_mask &
8167 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07008168 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008169 break;
8170 case 8:
8171 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008172 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008173 break;
8174 }
8175 break;
8176 case 2: /* clts */
8177 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
8178 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008179 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008180 break;
8181 case 1: /* mov from cr */
8182 switch (cr) {
8183 case 3:
8184 if (vmcs12->cpu_based_vm_exec_control &
8185 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008186 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008187 break;
8188 case 8:
8189 if (vmcs12->cpu_based_vm_exec_control &
8190 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008191 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008192 break;
8193 }
8194 break;
8195 case 3: /* lmsw */
8196 /*
8197 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
8198 * cr0. Other attempted changes are ignored, with no exit.
8199 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008200 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03008201 if (vmcs12->cr0_guest_host_mask & 0xe &
8202 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008203 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008204 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
8205 !(vmcs12->cr0_read_shadow & 0x1) &&
8206 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07008207 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008208 break;
8209 }
Joe Perches1d804d02015-03-30 16:46:09 -07008210 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008211}
8212
Liran Alona7cde482018-06-23 02:35:10 +03008213static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
8214 struct vmcs12 *vmcs12, gpa_t bitmap)
8215{
8216 u32 vmx_instruction_info;
8217 unsigned long field;
8218 u8 b;
8219
8220 if (!nested_cpu_has_shadow_vmcs(vmcs12))
8221 return true;
8222
8223 /* Decode instruction info and find the field to access */
8224 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8225 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
8226
8227 /* Out-of-range fields always cause a VM exit from L2 to L1 */
8228 if (field >> 15)
8229 return true;
8230
8231 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
8232 return true;
8233
8234 return 1 & (b >> (field & 7));
8235}
8236
Nadav Har'El644d7112011-05-25 23:12:35 +03008237/*
8238 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8239 * should handle it ourselves in L0 (and then continue L2). Only call this
8240 * when in is_guest_mode (L2).
8241 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02008242static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03008243{
Nadav Har'El644d7112011-05-25 23:12:35 +03008244 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8245 struct vcpu_vmx *vmx = to_vmx(vcpu);
8246 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8247
Jim Mattson4f350c62017-09-14 16:31:44 -07008248 if (vmx->nested.nested_run_pending)
8249 return false;
8250
8251 if (unlikely(vmx->fail)) {
8252 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8253 vmcs_read32(VM_INSTRUCTION_ERROR));
8254 return true;
8255 }
Jan Kiszka542060e2014-01-04 18:47:21 +01008256
David Matlackc9f04402017-08-01 14:00:40 -07008257 /*
8258 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06008259 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
8260 * Page). The CPU may write to these pages via their host
8261 * physical address while L2 is running, bypassing any
8262 * address-translation-based dirty tracking (e.g. EPT write
8263 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07008264 *
8265 * Mark them dirty on every exit from L2 to prevent them from
8266 * getting out of sync with dirty tracking.
8267 */
8268 nested_mark_vmcs12_pages_dirty(vcpu);
8269
Jim Mattson4f350c62017-09-14 16:31:44 -07008270 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
8271 vmcs_readl(EXIT_QUALIFICATION),
8272 vmx->idt_vectoring_info,
8273 intr_info,
8274 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8275 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03008276
8277 switch (exit_reason) {
8278 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08008279 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07008280 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008281 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07008282 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Jan Kiszka6f054852016-02-09 20:15:18 +01008283 else if (is_debug(intr_info) &&
8284 vcpu->guest_debug &
8285 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8286 return false;
8287 else if (is_breakpoint(intr_info) &&
8288 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8289 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008290 return vmcs12->exception_bitmap &
8291 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8292 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07008293 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008294 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07008295 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008296 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008297 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008298 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008299 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008300 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07008301 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008302 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07008303 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008304 case EXIT_REASON_HLT:
8305 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8306 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07008307 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008308 case EXIT_REASON_INVLPG:
8309 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8310 case EXIT_REASON_RDPMC:
8311 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02008312 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02008313 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02008314 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02008315 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01008316 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03008317 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
Liran Alona7cde482018-06-23 02:35:10 +03008318 case EXIT_REASON_VMREAD:
8319 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
8320 vmcs12->vmread_bitmap);
8321 case EXIT_REASON_VMWRITE:
8322 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
8323 vmcs12->vmwrite_bitmap);
Nadav Har'El644d7112011-05-25 23:12:35 +03008324 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8325 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
Liran Alona7cde482018-06-23 02:35:10 +03008326 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
Nadav Har'El644d7112011-05-25 23:12:35 +03008327 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02008328 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03008329 /*
8330 * VMX instructions trap unconditionally. This allows L1 to
8331 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8332 */
Joe Perches1d804d02015-03-30 16:46:09 -07008333 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008334 case EXIT_REASON_CR_ACCESS:
8335 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8336 case EXIT_REASON_DR_ACCESS:
8337 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8338 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008339 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02008340 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
8341 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03008342 case EXIT_REASON_MSR_READ:
8343 case EXIT_REASON_MSR_WRITE:
8344 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8345 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07008346 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008347 case EXIT_REASON_MWAIT_INSTRUCTION:
8348 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008349 case EXIT_REASON_MONITOR_TRAP_FLAG:
8350 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03008351 case EXIT_REASON_MONITOR_INSTRUCTION:
8352 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8353 case EXIT_REASON_PAUSE_INSTRUCTION:
8354 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8355 nested_cpu_has2(vmcs12,
8356 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8357 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07008358 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008359 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008360 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03008361 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08008362 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08008363 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04008364 /*
8365 * The controls for "virtualize APIC accesses," "APIC-
8366 * register virtualization," and "virtual-interrupt
8367 * delivery" only come from vmcs12.
8368 */
Joe Perches1d804d02015-03-30 16:46:09 -07008369 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008370 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008371 /*
8372 * L0 always deals with the EPT violation. If nested EPT is
8373 * used, and the nested mmu code discovers that the address is
8374 * missing in the guest EPT table (EPT12), the EPT violation
8375 * will be injected with nested_ept_inject_page_fault()
8376 */
Joe Perches1d804d02015-03-30 16:46:09 -07008377 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008378 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008379 /*
8380 * L2 never uses directly L1's EPT, but rather L0's own EPT
8381 * table (shadow on EPT) or a merged EPT table that L0 built
8382 * (EPT on EPT). So any problems with the structure of the
8383 * table is L0's fault.
8384 */
Joe Perches1d804d02015-03-30 16:46:09 -07008385 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02008386 case EXIT_REASON_INVPCID:
8387 return
8388 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
8389 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008390 case EXIT_REASON_WBINVD:
8391 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8392 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07008393 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08008394 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8395 /*
8396 * This should never happen, since it is not possible to
8397 * set XSS to a non-zero value---neither in L1 nor in L2.
8398 * If if it were, XSS would have to be checked against
8399 * the XSS exit bitmap in vmcs12.
8400 */
8401 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08008402 case EXIT_REASON_PREEMPTION_TIMER:
8403 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02008404 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04008405 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02008406 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04008407 case EXIT_REASON_VMFUNC:
8408 /* VM functions are emulated through L2->L0 vmexits. */
8409 return false;
Sean Christopherson0b665d32018-08-14 09:33:34 -07008410 case EXIT_REASON_ENCLS:
8411 /* SGX is never exposed to L1 */
8412 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008413 default:
Joe Perches1d804d02015-03-30 16:46:09 -07008414 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008415 }
8416}
8417
Paolo Bonzini7313c692017-07-27 10:31:25 +02008418static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
8419{
8420 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8421
8422 /*
8423 * At this point, the exit interruption info in exit_intr_info
8424 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
8425 * we need to query the in-kernel LAPIC.
8426 */
8427 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
8428 if ((exit_intr_info &
8429 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
8430 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
8431 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8432 vmcs12->vm_exit_intr_error_code =
8433 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
8434 }
8435
8436 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
8437 vmcs_readl(EXIT_QUALIFICATION));
8438 return 1;
8439}
8440
Avi Kivity586f9602010-11-18 13:09:54 +02008441static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8442{
8443 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8444 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8445}
8446
Kai Huanga3eaa862015-11-04 13:46:05 +08008447static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08008448{
Kai Huanga3eaa862015-11-04 13:46:05 +08008449 if (vmx->pml_pg) {
8450 __free_page(vmx->pml_pg);
8451 vmx->pml_pg = NULL;
8452 }
Kai Huang843e4332015-01-28 10:54:28 +08008453}
8454
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008455static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08008456{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008457 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008458 u64 *pml_buf;
8459 u16 pml_idx;
8460
8461 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8462
8463 /* Do nothing if PML buffer is empty */
8464 if (pml_idx == (PML_ENTITY_NUM - 1))
8465 return;
8466
8467 /* PML index always points to next available PML buffer entity */
8468 if (pml_idx >= PML_ENTITY_NUM)
8469 pml_idx = 0;
8470 else
8471 pml_idx++;
8472
8473 pml_buf = page_address(vmx->pml_pg);
8474 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8475 u64 gpa;
8476
8477 gpa = pml_buf[pml_idx];
8478 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008479 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08008480 }
8481
8482 /* reset PML index */
8483 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8484}
8485
8486/*
8487 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8488 * Called before reporting dirty_bitmap to userspace.
8489 */
8490static void kvm_flush_pml_buffers(struct kvm *kvm)
8491{
8492 int i;
8493 struct kvm_vcpu *vcpu;
8494 /*
8495 * We only need to kick vcpu out of guest mode here, as PML buffer
8496 * is flushed at beginning of all VMEXITs, and it's obvious that only
8497 * vcpus running in guest are possible to have unflushed GPAs in PML
8498 * buffer.
8499 */
8500 kvm_for_each_vcpu(i, vcpu, kvm)
8501 kvm_vcpu_kick(vcpu);
8502}
8503
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008504static void vmx_dump_sel(char *name, uint32_t sel)
8505{
8506 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05008507 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008508 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8509 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
8510 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
8511}
8512
8513static void vmx_dump_dtsel(char *name, uint32_t limit)
8514{
8515 pr_err("%s limit=0x%08x, base=0x%016lx\n",
8516 name, vmcs_read32(limit),
8517 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
8518}
8519
8520static void dump_vmcs(void)
8521{
8522 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
8523 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
8524 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
8525 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
8526 u32 secondary_exec_control = 0;
8527 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01008528 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008529 int i, n;
8530
8531 if (cpu_has_secondary_exec_ctrls())
8532 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8533
8534 pr_err("*** Guest State ***\n");
8535 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8536 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
8537 vmcs_readl(CR0_GUEST_HOST_MASK));
8538 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8539 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
8540 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
8541 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
8542 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
8543 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008544 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
8545 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
8546 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
8547 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008548 }
8549 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
8550 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
8551 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
8552 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
8553 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8554 vmcs_readl(GUEST_SYSENTER_ESP),
8555 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
8556 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
8557 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
8558 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
8559 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
8560 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
8561 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
8562 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
8563 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
8564 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
8565 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
8566 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
8567 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008568 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8569 efer, vmcs_read64(GUEST_IA32_PAT));
8570 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
8571 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008572 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Sean Christophersonc73da3f2018-12-03 13:53:00 -08008573 if (cpu_has_load_perf_global_ctrl() &&
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01008574 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008575 pr_err("PerfGlobCtl = 0x%016llx\n",
8576 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008577 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008578 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008579 pr_err("Interruptibility = %08x ActivityState = %08x\n",
8580 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
8581 vmcs_read32(GUEST_ACTIVITY_STATE));
8582 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
8583 pr_err("InterruptStatus = %04x\n",
8584 vmcs_read16(GUEST_INTR_STATUS));
8585
8586 pr_err("*** Host State ***\n");
8587 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
8588 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
8589 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
8590 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
8591 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
8592 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
8593 vmcs_read16(HOST_TR_SELECTOR));
8594 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
8595 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
8596 vmcs_readl(HOST_TR_BASE));
8597 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
8598 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
8599 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
8600 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
8601 vmcs_readl(HOST_CR4));
8602 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8603 vmcs_readl(HOST_IA32_SYSENTER_ESP),
8604 vmcs_read32(HOST_IA32_SYSENTER_CS),
8605 vmcs_readl(HOST_IA32_SYSENTER_EIP));
8606 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008607 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8608 vmcs_read64(HOST_IA32_EFER),
8609 vmcs_read64(HOST_IA32_PAT));
Sean Christophersonc73da3f2018-12-03 13:53:00 -08008610 if (cpu_has_load_perf_global_ctrl() &&
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01008611 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008612 pr_err("PerfGlobCtl = 0x%016llx\n",
8613 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008614
8615 pr_err("*** Control State ***\n");
8616 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
8617 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
8618 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
8619 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
8620 vmcs_read32(EXCEPTION_BITMAP),
8621 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
8622 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
8623 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
8624 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8625 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
8626 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
8627 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
8628 vmcs_read32(VM_EXIT_INTR_INFO),
8629 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8630 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
8631 pr_err(" reason=%08x qualification=%016lx\n",
8632 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
8633 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
8634 vmcs_read32(IDT_VECTORING_INFO_FIELD),
8635 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008636 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08008637 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008638 pr_err("TSC Multiplier = 0x%016llx\n",
8639 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008640 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
8641 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
8642 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
8643 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
8644 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008645 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008646 n = vmcs_read32(CR3_TARGET_COUNT);
8647 for (i = 0; i + 1 < n; i += 4)
8648 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
8649 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
8650 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
8651 if (i < n)
8652 pr_err("CR3 target%u=%016lx\n",
8653 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
8654 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
8655 pr_err("PLE Gap=%08x Window=%08x\n",
8656 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
8657 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
8658 pr_err("Virtual processor ID = 0x%04x\n",
8659 vmcs_read16(VIRTUAL_PROCESSOR_ID));
8660}
8661
Avi Kivity6aa8b732006-12-10 02:21:36 -08008662/*
8663 * The guest has exited. See if we can fix it or if we need userspace
8664 * assistance.
8665 */
Avi Kivity851ba692009-08-24 11:10:17 +03008666static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008667{
Avi Kivity29bd8a72007-09-10 17:27:03 +03008668 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08008669 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02008670 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03008671
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008672 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
8673
Kai Huang843e4332015-01-28 10:54:28 +08008674 /*
8675 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
8676 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
8677 * querying dirty_bitmap, we only need to kick all vcpus out of guest
8678 * mode as if vcpus is in root mode, the PML buffer must has been
8679 * flushed already.
8680 */
8681 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008682 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008683
Mohammed Gamal80ced182009-09-01 12:48:18 +02008684 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02008685 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02008686 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01008687
Paolo Bonzini7313c692017-07-27 10:31:25 +02008688 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
8689 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03008690
Mohammed Gamal51207022010-05-31 22:40:54 +03008691 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008692 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03008693 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8694 vcpu->run->fail_entry.hardware_entry_failure_reason
8695 = exit_reason;
8696 return 0;
8697 }
8698
Avi Kivity29bd8a72007-09-10 17:27:03 +03008699 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008700 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8701 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03008702 = vmcs_read32(VM_INSTRUCTION_ERROR);
8703 return 0;
8704 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008705
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008706 /*
8707 * Note:
8708 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
8709 * delivery event since it indicates guest is accessing MMIO.
8710 * The vm-exit can be triggered again after return to guest that
8711 * will cause infinite loop.
8712 */
Mike Dayd77c26f2007-10-08 09:02:08 -04008713 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08008714 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02008715 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00008716 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008717 exit_reason != EXIT_REASON_TASK_SWITCH)) {
8718 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
8719 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02008720 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008721 vcpu->run->internal.data[0] = vectoring_info;
8722 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02008723 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
8724 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
8725 vcpu->run->internal.ndata++;
8726 vcpu->run->internal.data[3] =
8727 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
8728 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008729 return 0;
8730 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008731
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008732 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01008733 vmx->loaded_vmcs->soft_vnmi_blocked)) {
8734 if (vmx_interrupt_allowed(vcpu)) {
8735 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
8736 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
8737 vcpu->arch.nmi_pending) {
8738 /*
8739 * This CPU don't support us in finding the end of an
8740 * NMI-blocked window if the guest runs with IRQs
8741 * disabled. So we pull the trigger after 1 s of
8742 * futile waiting, but inform the user about this.
8743 */
8744 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
8745 "state on VCPU %d after 1 s timeout\n",
8746 __func__, vcpu->vcpu_id);
8747 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
8748 }
8749 }
8750
Avi Kivity6aa8b732006-12-10 02:21:36 -08008751 if (exit_reason < kvm_vmx_max_exit_handlers
8752 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03008753 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008754 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01008755 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
8756 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03008757 kvm_queue_exception(vcpu, UD_VECTOR);
8758 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008759 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008760}
8761
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02008762/*
8763 * Software based L1D cache flush which is used when microcode providing
8764 * the cache control MSR is not loaded.
8765 *
8766 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
8767 * flush it is required to read in 64 KiB because the replacement algorithm
8768 * is not exactly LRU. This could be sized at runtime via topology
8769 * information but as all relevant affected CPUs have 32KiB L1D cache size
8770 * there is no point in doing so.
8771 */
Paolo Bonzinic595cee2018-07-02 13:07:14 +02008772static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02008773{
8774 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +02008775
8776 /*
Thomas Gleixner2f055942018-07-13 16:23:17 +02008777 * This code is only executed when the the flush mode is 'cond' or
8778 * 'always'
Paolo Bonzinic595cee2018-07-02 13:07:14 +02008779 */
Nicolai Stange427362a2018-07-21 22:25:00 +02008780 if (static_branch_likely(&vmx_l1d_flush_cond)) {
Nicolai Stange45b575c2018-07-27 13:22:16 +02008781 bool flush_l1d;
Nicolai Stange5b6ccc62018-07-21 22:35:28 +02008782
Nicolai Stange379fd0c2018-07-21 22:16:56 +02008783 /*
Nicolai Stange45b575c2018-07-27 13:22:16 +02008784 * Clear the per-vcpu flush bit, it gets set again
8785 * either from vcpu_run() or from one of the unsafe
8786 * VMEXIT handlers.
Nicolai Stange379fd0c2018-07-21 22:16:56 +02008787 */
Nicolai Stange45b575c2018-07-27 13:22:16 +02008788 flush_l1d = vcpu->arch.l1tf_flush_l1d;
Thomas Gleixner4c6523e2018-07-13 16:23:20 +02008789 vcpu->arch.l1tf_flush_l1d = false;
Nicolai Stange45b575c2018-07-27 13:22:16 +02008790
8791 /*
8792 * Clear the per-cpu flush bit, it gets set again from
8793 * the interrupt handlers.
8794 */
8795 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
8796 kvm_clear_cpu_l1tf_flush_l1d();
8797
Nicolai Stange5b6ccc62018-07-21 22:35:28 +02008798 if (!flush_l1d)
8799 return;
Nicolai Stange379fd0c2018-07-21 22:16:56 +02008800 }
Paolo Bonzinic595cee2018-07-02 13:07:14 +02008801
8802 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02008803
Paolo Bonzini3fa045b2018-07-02 13:03:48 +02008804 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
8805 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
8806 return;
8807 }
8808
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02008809 asm volatile(
8810 /* First ensure the pages are in the TLB */
8811 "xorl %%eax, %%eax\n"
8812 ".Lpopulate_tlb:\n\t"
Nicolai Stange288d1522018-07-18 19:07:38 +02008813 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02008814 "addl $4096, %%eax\n\t"
8815 "cmpl %%eax, %[size]\n\t"
8816 "jne .Lpopulate_tlb\n\t"
8817 "xorl %%eax, %%eax\n\t"
8818 "cpuid\n\t"
8819 /* Now fill the cache */
8820 "xorl %%eax, %%eax\n"
8821 ".Lfill_cache:\n"
Nicolai Stange288d1522018-07-18 19:07:38 +02008822 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02008823 "addl $64, %%eax\n\t"
8824 "cmpl %%eax, %[size]\n\t"
8825 "jne .Lfill_cache\n\t"
8826 "lfence\n"
Nicolai Stange288d1522018-07-18 19:07:38 +02008827 :: [flush_pages] "r" (vmx_l1d_flush_pages),
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02008828 [size] "r" (size)
8829 : "eax", "ebx", "ecx", "edx");
8830}
8831
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008832static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008833{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008834 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8835
8836 if (is_guest_mode(vcpu) &&
8837 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
8838 return;
8839
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008840 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008841 vmcs_write32(TPR_THRESHOLD, 0);
8842 return;
8843 }
8844
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008845 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008846}
8847
Jim Mattson8d860bb2018-05-09 16:56:05 -04008848static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08008849{
8850 u32 sec_exec_control;
8851
Jim Mattson8d860bb2018-05-09 16:56:05 -04008852 if (!lapic_in_kernel(vcpu))
8853 return;
8854
Sean Christophersonfd6b6d92018-10-01 14:25:34 -07008855 if (!flexpriority_enabled &&
8856 !cpu_has_vmx_virtualize_x2apic_mode())
8857 return;
8858
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02008859 /* Postpone execution until vmcs01 is the current VMCS. */
8860 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -04008861 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02008862 return;
8863 }
8864
Yang Zhang8d146952013-01-25 10:18:50 +08008865 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -04008866 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
8867 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08008868
Jim Mattson8d860bb2018-05-09 16:56:05 -04008869 switch (kvm_get_apic_mode(vcpu)) {
8870 case LAPIC_MODE_INVALID:
8871 WARN_ONCE(true, "Invalid local APIC state");
8872 case LAPIC_MODE_DISABLED:
8873 break;
8874 case LAPIC_MODE_XAPIC:
8875 if (flexpriority_enabled) {
8876 sec_exec_control |=
8877 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8878 vmx_flush_tlb(vcpu, true);
8879 }
8880 break;
8881 case LAPIC_MODE_X2APIC:
8882 if (cpu_has_vmx_virtualize_x2apic_mode())
8883 sec_exec_control |=
8884 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8885 break;
Yang Zhang8d146952013-01-25 10:18:50 +08008886 }
8887 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
8888
Paolo Bonzini904e14f2018-01-16 16:51:18 +01008889 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08008890}
8891
Tang Chen38b99172014-09-24 15:57:54 +08008892static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
8893{
Jim Mattsonab5df312018-05-09 17:02:03 -04008894 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +08008895 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -07008896 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07008897 }
Tang Chen38b99172014-09-24 15:57:54 +08008898}
8899
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008900static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008901{
8902 u16 status;
8903 u8 old;
8904
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008905 if (max_isr == -1)
8906 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008907
8908 status = vmcs_read16(GUEST_INTR_STATUS);
8909 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008910 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08008911 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008912 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008913 vmcs_write16(GUEST_INTR_STATUS, status);
8914 }
8915}
8916
8917static void vmx_set_rvi(int vector)
8918{
8919 u16 status;
8920 u8 old;
8921
Wei Wang4114c272014-11-05 10:53:43 +08008922 if (vector == -1)
8923 vector = 0;
8924
Yang Zhangc7c9c562013-01-25 10:18:51 +08008925 status = vmcs_read16(GUEST_INTR_STATUS);
8926 old = (u8)status & 0xff;
8927 if ((u8)vector != old) {
8928 status &= ~0xff;
8929 status |= (u8)vector;
8930 vmcs_write16(GUEST_INTR_STATUS, status);
8931 }
8932}
8933
8934static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
8935{
Liran Alon851c1a182017-12-24 18:12:56 +02008936 /*
8937 * When running L2, updating RVI is only relevant when
8938 * vmcs12 virtual-interrupt-delivery enabled.
8939 * However, it can be enabled only when L1 also
8940 * intercepts external-interrupts and in that case
8941 * we should not update vmcs02 RVI but instead intercept
8942 * interrupt. Therefore, do nothing when running L2.
8943 */
8944 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08008945 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08008946}
8947
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01008948static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01008949{
8950 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01008951 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02008952 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01008953
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01008954 WARN_ON(!vcpu->arch.apicv_active);
8955 if (pi_test_on(&vmx->pi_desc)) {
8956 pi_clear_on(&vmx->pi_desc);
8957 /*
8958 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
8959 * But on x86 this is just a compiler barrier anyway.
8960 */
8961 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02008962 max_irr_updated =
8963 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
8964
8965 /*
8966 * If we are running L2 and L1 has a new pending interrupt
8967 * which can be injected, we should re-evaluate
8968 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02008969 * If L1 intercepts external-interrupts, we should
8970 * exit from L2 to L1. Otherwise, interrupt should be
8971 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02008972 */
Liran Alon851c1a182017-12-24 18:12:56 +02008973 if (is_guest_mode(vcpu) && max_irr_updated) {
8974 if (nested_exit_on_intr(vcpu))
8975 kvm_vcpu_exiting_guest_mode(vcpu);
8976 else
8977 kvm_make_request(KVM_REQ_EVENT, vcpu);
8978 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01008979 } else {
8980 max_irr = kvm_lapic_find_highest_irr(vcpu);
8981 }
8982 vmx_hwapic_irr_update(vcpu, max_irr);
8983 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01008984}
8985
Paolo Bonzini7e712682018-10-03 13:44:26 +02008986static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu)
8987{
8988 u8 rvi = vmx_get_rvi();
8989 u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI);
8990
8991 return ((rvi & 0xf0) > (vppr & 0xf0));
8992}
8993
Andrey Smetanin63086302015-11-10 15:36:32 +03008994static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008995{
Andrey Smetanind62caab2015-11-10 15:36:33 +03008996 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08008997 return;
8998
Yang Zhangc7c9c562013-01-25 10:18:51 +08008999 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9000 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9001 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9002 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9003}
9004
Paolo Bonzini967235d2016-12-19 14:03:45 +01009005static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9006{
9007 struct vcpu_vmx *vmx = to_vmx(vcpu);
9008
9009 pi_clear_on(&vmx->pi_desc);
9010 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9011}
9012
Avi Kivity51aa01d2010-07-20 14:31:20 +03009013static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03009014{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009015 u32 exit_intr_info = 0;
9016 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02009017
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009018 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9019 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02009020 return;
9021
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009022 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9023 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9024 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08009025
Wanpeng Li1261bfa2017-07-13 18:30:40 -07009026 /* if exit due to PF check for async PF */
9027 if (is_page_fault(exit_intr_info))
9028 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9029
Andi Kleena0861c02009-06-08 17:37:09 +08009030 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009031 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9032 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08009033 kvm_machine_check();
9034
Gleb Natapov20f65982009-05-11 13:35:55 +03009035 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08009036 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -07009037 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03009038 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -07009039 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009040 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03009041}
Gleb Natapov20f65982009-05-11 13:35:55 +03009042
Yang Zhanga547c6d2013-04-11 19:25:10 +08009043static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9044{
9045 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9046
Yang Zhanga547c6d2013-04-11 19:25:10 +08009047 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9048 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9049 unsigned int vector;
9050 unsigned long entry;
9051 gate_desc *desc;
9052 struct vcpu_vmx *vmx = to_vmx(vcpu);
9053#ifdef CONFIG_X86_64
9054 unsigned long tmp;
9055#endif
9056
9057 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9058 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02009059 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009060 asm volatile(
9061#ifdef CONFIG_X86_64
9062 "mov %%" _ASM_SP ", %[sp]\n\t"
9063 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
9064 "push $%c[ss]\n\t"
9065 "push %[sp]\n\t"
9066#endif
9067 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08009068 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009069 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08009070 :
9071#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06009072 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009073#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -05009074 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +08009075 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009076 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009077 [ss]"i"(__KERNEL_DS),
9078 [cs]"i"(__KERNEL_CS)
9079 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02009080 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08009081}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009082STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009083
Tom Lendackybc226f02018-05-10 22:06:39 +02009084static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009085{
Tom Lendackybc226f02018-05-10 22:06:39 +02009086 switch (index) {
9087 case MSR_IA32_SMBASE:
9088 /*
9089 * We cannot do SMM unless we can run the guest in big
9090 * real mode.
9091 */
9092 return enable_unrestricted_guest || emulate_invalid_guest_state;
9093 case MSR_AMD64_VIRT_SPEC_CTRL:
9094 /* This is AMD only. */
9095 return false;
9096 default:
9097 return true;
9098 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009099}
9100
Avi Kivity51aa01d2010-07-20 14:31:20 +03009101static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9102{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02009103 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03009104 bool unblock_nmi;
9105 u8 vector;
9106 bool idtv_info_valid;
9107
9108 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03009109
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009110 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009111 if (vmx->loaded_vmcs->nmi_known_unmasked)
9112 return;
9113 /*
9114 * Can't use vmx->exit_intr_info since we're not sure what
9115 * the exit reason is.
9116 */
9117 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9118 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9119 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9120 /*
9121 * SDM 3: 27.7.1.2 (September 2008)
9122 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9123 * a guest IRET fault.
9124 * SDM 3: 23.2.2 (September 2008)
9125 * Bit 12 is undefined in any of the following cases:
9126 * If the VM exit sets the valid bit in the IDT-vectoring
9127 * information field.
9128 * If the VM exit is due to a double fault.
9129 */
9130 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9131 vector != DF_VECTOR && !idtv_info_valid)
9132 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9133 GUEST_INTR_STATE_NMI);
9134 else
9135 vmx->loaded_vmcs->nmi_known_unmasked =
9136 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9137 & GUEST_INTR_STATE_NMI);
9138 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
9139 vmx->loaded_vmcs->vnmi_blocked_time +=
9140 ktime_to_ns(ktime_sub(ktime_get(),
9141 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03009142}
9143
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009144static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03009145 u32 idt_vectoring_info,
9146 int instr_len_field,
9147 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03009148{
Avi Kivity51aa01d2010-07-20 14:31:20 +03009149 u8 vector;
9150 int type;
9151 bool idtv_info_valid;
9152
9153 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03009154
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009155 vcpu->arch.nmi_injected = false;
9156 kvm_clear_exception_queue(vcpu);
9157 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009158
9159 if (!idtv_info_valid)
9160 return;
9161
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009162 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03009163
Avi Kivity668f6122008-07-02 09:28:55 +03009164 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
9165 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009166
Gleb Natapov64a7ec02009-03-30 16:03:29 +03009167 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03009168 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009169 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03009170 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009171 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03009172 * Clear bit "block by NMI" before VM entry if a NMI
9173 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03009174 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009175 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009176 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009177 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009178 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009179 /* fall through */
9180 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03009181 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03009182 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03009183 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03009184 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03009185 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009186 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009187 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009188 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009189 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03009190 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009191 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009192 break;
9193 default:
9194 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03009195 }
Avi Kivitycf393f72008-07-01 16:20:21 +03009196}
9197
Avi Kivity83422e12010-07-20 14:43:23 +03009198static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
9199{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009200 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03009201 VM_EXIT_INSTRUCTION_LEN,
9202 IDT_VECTORING_ERROR_CODE);
9203}
9204
Avi Kivityb463a6f2010-07-20 15:06:17 +03009205static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
9206{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009207 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03009208 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9209 VM_ENTRY_INSTRUCTION_LEN,
9210 VM_ENTRY_EXCEPTION_ERROR_CODE);
9211
9212 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
9213}
9214
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009215static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
9216{
9217 int i, nr_msrs;
9218 struct perf_guest_switch_msr *msrs;
9219
9220 msrs = perf_guest_get_msrs(&nr_msrs);
9221
9222 if (!msrs)
9223 return;
9224
9225 for (i = 0; i < nr_msrs; i++)
9226 if (msrs[i].host == msrs[i].guest)
9227 clear_atomic_switch_msr(vmx, msrs[i].msr);
9228 else
9229 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04009230 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009231}
9232
Sean Christophersonf459a702018-08-27 15:21:11 -07009233static void vmx_arm_hv_timer(struct vcpu_vmx *vmx, u32 val)
9234{
9235 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, val);
9236 if (!vmx->loaded_vmcs->hv_timer_armed)
9237 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
9238 PIN_BASED_VMX_PREEMPTION_TIMER);
9239 vmx->loaded_vmcs->hv_timer_armed = true;
9240}
9241
9242static void vmx_update_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07009243{
9244 struct vcpu_vmx *vmx = to_vmx(vcpu);
9245 u64 tscl;
9246 u32 delta_tsc;
9247
Sean Christophersond264ee02018-08-27 15:21:12 -07009248 if (vmx->req_immediate_exit) {
9249 vmx_arm_hv_timer(vmx, 0);
9250 return;
9251 }
9252
Sean Christophersonf459a702018-08-27 15:21:11 -07009253 if (vmx->hv_deadline_tsc != -1) {
9254 tscl = rdtsc();
9255 if (vmx->hv_deadline_tsc > tscl)
9256 /* set_hv_timer ensures the delta fits in 32-bits */
9257 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
9258 cpu_preemption_timer_multi);
9259 else
9260 delta_tsc = 0;
9261
9262 vmx_arm_hv_timer(vmx, delta_tsc);
Yunhong Jiang64672c92016-06-13 14:19:59 -07009263 return;
Sean Christophersonf459a702018-08-27 15:21:11 -07009264 }
Yunhong Jiang64672c92016-06-13 14:19:59 -07009265
Sean Christophersonf459a702018-08-27 15:21:11 -07009266 if (vmx->loaded_vmcs->hv_timer_armed)
9267 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
9268 PIN_BASED_VMX_PREEMPTION_TIMER);
9269 vmx->loaded_vmcs->hv_timer_armed = false;
Yunhong Jiang64672c92016-06-13 14:19:59 -07009270}
9271
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08009272static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009273{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009274 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009275 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +02009276
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009277 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009278 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009279 vmx->loaded_vmcs->soft_vnmi_blocked))
9280 vmx->loaded_vmcs->entry_time = ktime_get();
9281
Avi Kivity104f2262010-11-18 13:12:52 +02009282 /* Don't enter VMX if guest state is invalid, let the exit handler
9283 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02009284 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02009285 return;
9286
Radim Krčmářa7653ec2014-08-21 18:08:07 +02009287 if (vmx->ple_window_dirty) {
9288 vmx->ple_window_dirty = false;
9289 vmcs_write32(PLE_WINDOW, vmx->ple_window);
9290 }
9291
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +02009292 if (vmx->nested.need_vmcs12_sync) {
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02009293 /*
9294 * hv_evmcs may end up being not mapped after migration (when
9295 * L2 was running), map it here to make sure vmcs12 changes are
9296 * properly reflected.
9297 */
9298 if (vmx->nested.enlightened_vmcs_enabled &&
9299 !vmx->nested.hv_evmcs)
9300 nested_vmx_handle_enlightened_vmptrld(vcpu, false);
9301
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +02009302 if (vmx->nested.hv_evmcs) {
9303 copy_vmcs12_to_enlightened(vmx);
9304 /* All fields are clean */
9305 vmx->nested.hv_evmcs->hv_clean_fields |=
9306 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
9307 } else {
9308 copy_vmcs12_to_shadow(vmx);
9309 }
9310 vmx->nested.need_vmcs12_sync = false;
Abel Gordon012f83c2013-04-18 14:39:25 +03009311 }
9312
Avi Kivity104f2262010-11-18 13:12:52 +02009313 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
9314 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
9315 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
9316 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
9317
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009318 cr3 = __get_current_cr3_fast();
Sean Christophersond7ee0392018-07-23 12:32:47 -07009319 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009320 vmcs_writel(HOST_CR3, cr3);
Sean Christophersond7ee0392018-07-23 12:32:47 -07009321 vmx->loaded_vmcs->host_state.cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009322 }
9323
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07009324 cr4 = cr4_read_shadow();
Sean Christophersond7ee0392018-07-23 12:32:47 -07009325 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009326 vmcs_writel(HOST_CR4, cr4);
Sean Christophersond7ee0392018-07-23 12:32:47 -07009327 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009328 }
9329
Avi Kivity104f2262010-11-18 13:12:52 +02009330 /* When single-stepping over STI and MOV SS, we must clear the
9331 * corresponding interruptibility bits in the guest state. Otherwise
9332 * vmentry fails as it then expects bit 14 (BS) in pending debug
9333 * exceptions being set, but that's not correct for the guest debugging
9334 * case. */
9335 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9336 vmx_set_interrupt_shadow(vcpu, 0);
9337
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02009338 if (static_cpu_has(X86_FEATURE_PKU) &&
9339 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
9340 vcpu->arch.pkru != vmx->host_pkru)
9341 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009342
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009343 atomic_switch_perf_msrs(vmx);
9344
Sean Christophersonf459a702018-08-27 15:21:11 -07009345 vmx_update_hv_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -07009346
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009347 /*
9348 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
9349 * it's non-zero. Since vmentry is serialising on affected CPUs, there
9350 * is no need to worry about the conditional branch over the wrmsr
9351 * being speculatively taken.
9352 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +02009353 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009354
Nadav Har'Eld462b812011-05-24 15:26:10 +03009355 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009356
9357 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
9358 (unsigned long)&current_evmcs->host_rsp : 0;
9359
Nicolai Stange5b6ccc62018-07-21 22:35:28 +02009360 if (static_branch_unlikely(&vmx_l1d_should_flush))
9361 vmx_l1d_flush(vcpu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009362
Avi Kivity104f2262010-11-18 13:12:52 +02009363 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08009364 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009365 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
9366 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
9367 "push %%" _ASM_CX " \n\t"
9368 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03009369 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009370 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009371 /* Avoid VMWRITE when Enlightened VMCS is in use */
9372 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
9373 "jz 2f \n\t"
9374 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
9375 "jmp 1f \n\t"
9376 "2: \n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +02009377 __ex("vmwrite %%" _ASM_SP ", %%" _ASM_DX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03009378 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009379 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009380 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
9381 "mov %%cr2, %%" _ASM_DX " \n\t"
9382 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009383 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009384 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009385 "3: \n\t"
Uros Bizjak00df9182018-10-23 00:09:11 +02009386 /* Check if vmlaunch or vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02009387 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009388 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009389 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
9390 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
9391 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
9392 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
9393 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
9394 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009395#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009396 "mov %c[r8](%0), %%r8 \n\t"
9397 "mov %c[r9](%0), %%r9 \n\t"
9398 "mov %c[r10](%0), %%r10 \n\t"
9399 "mov %c[r11](%0), %%r11 \n\t"
9400 "mov %c[r12](%0), %%r12 \n\t"
9401 "mov %c[r13](%0), %%r13 \n\t"
9402 "mov %c[r14](%0), %%r14 \n\t"
9403 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009404#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009405 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03009406
Avi Kivity6aa8b732006-12-10 02:21:36 -08009407 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03009408 "jne 1f \n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +02009409 __ex("vmlaunch") "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009410 "jmp 2f \n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +02009411 "1: " __ex("vmresume") "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009412 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08009413 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009414 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02009415 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -08009416 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009417 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
9418 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
9419 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
9420 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
9421 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
9422 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
9423 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009424#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009425 "mov %%r8, %c[r8](%0) \n\t"
9426 "mov %%r9, %c[r9](%0) \n\t"
9427 "mov %%r10, %c[r10](%0) \n\t"
9428 "mov %%r11, %c[r11](%0) \n\t"
9429 "mov %%r12, %c[r12](%0) \n\t"
9430 "mov %%r13, %c[r13](%0) \n\t"
9431 "mov %%r14, %c[r14](%0) \n\t"
9432 "mov %%r15, %c[r15](%0) \n\t"
Uros Bizjak43ce76c2018-10-17 16:46:57 +02009433 /*
9434 * Clear host registers marked as clobbered to prevent
9435 * speculative use.
9436 */
Jim Mattson0cb5b302018-01-03 14:31:38 -08009437 "xor %%r8d, %%r8d \n\t"
9438 "xor %%r9d, %%r9d \n\t"
9439 "xor %%r10d, %%r10d \n\t"
9440 "xor %%r11d, %%r11d \n\t"
9441 "xor %%r12d, %%r12d \n\t"
9442 "xor %%r13d, %%r13d \n\t"
9443 "xor %%r14d, %%r14d \n\t"
9444 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009445#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009446 "mov %%cr2, %%" _ASM_AX " \n\t"
9447 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03009448
Jim Mattson0cb5b302018-01-03 14:31:38 -08009449 "xor %%eax, %%eax \n\t"
9450 "xor %%ebx, %%ebx \n\t"
9451 "xor %%esi, %%esi \n\t"
9452 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009453 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009454 ".pushsection .rodata \n\t"
9455 ".global vmx_return \n\t"
9456 "vmx_return: " _ASM_PTR " 2b \n\t"
9457 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009458 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +03009459 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02009460 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +03009461 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009462 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
9463 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
9464 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
9465 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
9466 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
9467 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
9468 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009469#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009470 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
9471 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
9472 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
9473 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
9474 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
9475 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
9476 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
9477 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08009478#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02009479 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
9480 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02009481 : "cc", "memory"
9482#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009483 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009484 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009485#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009486 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009487#endif
9488 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08009489
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009490 /*
9491 * We do not use IBRS in the kernel. If this vCPU has used the
9492 * SPEC_CTRL MSR it may have left it on; save the value and
9493 * turn it off. This is much more efficient than blindly adding
9494 * it to the atomic save/restore list. Especially as the former
9495 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
9496 *
9497 * For non-nested case:
9498 * If the L01 MSR bitmap does not intercept the MSR, then we need to
9499 * save it.
9500 *
9501 * For nested case:
9502 * If the L02 MSR bitmap does not intercept the MSR, then we need to
9503 * save it.
9504 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +01009505 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +01009506 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009507
Thomas Gleixnerccbcd262018-05-09 23:01:01 +02009508 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009509
David Woodhouse117cc7a2018-01-12 11:11:27 +00009510 /* Eliminate branch target predictions from guest mode */
9511 vmexit_fill_RSB();
9512
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009513 /* All fields are clean at this point */
9514 if (static_branch_unlikely(&enable_evmcs))
9515 current_evmcs->hv_clean_fields |=
9516 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
9517
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009518 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -08009519 if (vmx->host_debugctlmsr)
9520 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009521
Avi Kivityaa67f602012-08-01 16:48:03 +03009522#ifndef CONFIG_X86_64
9523 /*
9524 * The sysexit path does not restore ds/es, so we must set them to
9525 * a reasonable value ourselves.
9526 *
Sean Christopherson6d6095b2018-07-23 12:32:44 -07009527 * We can't defer this to vmx_prepare_switch_to_host() since that
9528 * function may be executed in interrupt context, which saves and
9529 * restore segments around it, nullifying its effect.
Avi Kivityaa67f602012-08-01 16:48:03 +03009530 */
9531 loadsegment(ds, __USER_DS);
9532 loadsegment(es, __USER_DS);
9533#endif
9534
Avi Kivity6de4f3a2009-05-31 22:58:47 +03009535 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02009536 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009537 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03009538 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009539 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009540 vcpu->arch.regs_dirty = 0;
9541
Gleb Natapove0b890d2013-09-25 12:51:33 +03009542 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009543 * eager fpu is enabled if PKEY is supported and CR4 is switched
9544 * back on host, so it is safe to read guest PKRU from current
9545 * XSAVE.
9546 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02009547 if (static_cpu_has(X86_FEATURE_PKU) &&
9548 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
9549 vcpu->arch.pkru = __read_pkru();
9550 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009551 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009552 }
9553
Gleb Natapove0b890d2013-09-25 12:51:33 +03009554 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -07009555 vmx->idt_vectoring_info = 0;
9556
9557 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
9558 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9559 return;
9560
9561 vmx->loaded_vmcs->launched = 1;
9562 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +03009563
Avi Kivity51aa01d2010-07-20 14:31:20 +03009564 vmx_complete_atomic_exit(vmx);
9565 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03009566 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009567}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009568STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009569
Sean Christopherson434a1e92018-03-20 12:17:18 -07009570static struct kvm *vmx_vm_alloc(void)
9571{
Marc Orrd1e5b0e2018-05-15 04:37:37 -07009572 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07009573 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -07009574}
9575
9576static void vmx_vm_free(struct kvm *kvm)
9577{
Marc Orrd1e5b0e2018-05-15 04:37:37 -07009578 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -07009579}
9580
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009581static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009582{
9583 struct vcpu_vmx *vmx = to_vmx(vcpu);
9584 int cpu;
9585
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009586 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009587 return;
9588
9589 cpu = get_cpu();
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009590 vmx_vcpu_put(vcpu);
Sean Christophersonbd9966d2018-07-23 12:32:42 -07009591 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009592 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009593 put_cpu();
Sean Christophersonb7031fd2018-09-26 09:23:42 -07009594
9595 vm_entry_controls_reset_shadow(vmx);
9596 vm_exit_controls_reset_shadow(vmx);
9597 vmx_segment_cache_clear(vmx);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009598}
9599
Jim Mattson2f1fe812016-07-08 15:36:06 -07009600/*
9601 * Ensure that the current vmcs of the logical processor is the
9602 * vmcs01 of the vcpu before calling free_nested().
9603 */
9604static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9605{
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02009606 vcpu_load(vcpu);
9607 vmx_switch_vmcs(vcpu, &to_vmx(vcpu)->vmcs01);
9608 free_nested(vcpu);
9609 vcpu_put(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009610}
9611
Avi Kivity6aa8b732006-12-10 02:21:36 -08009612static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9613{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009614 struct vcpu_vmx *vmx = to_vmx(vcpu);
9615
Kai Huang843e4332015-01-28 10:54:28 +08009616 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08009617 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08009618 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009619 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009620 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009621 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009622 kfree(vmx->guest_msrs);
9623 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10009624 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009625}
9626
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009627static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009628{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009629 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10009630 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009631 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +03009632 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009633
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009634 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009635 return ERR_PTR(-ENOMEM);
9636
Wanpeng Li991e7a02015-09-16 17:30:05 +08009637 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +08009638
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009639 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
9640 if (err)
9641 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009642
Peter Feiner4e595162016-07-07 14:49:58 -07009643 err = -ENOMEM;
9644
9645 /*
9646 * If PML is turned on, failure on enabling PML just results in failure
9647 * of creating the vcpu, therefore we can simplify PML logic (by
9648 * avoiding dealing with cases, such as enabling PML partially on vcpus
9649 * for the guest, etc.
9650 */
9651 if (enable_pml) {
9652 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
9653 if (!vmx->pml_pg)
9654 goto uninit_vcpu;
9655 }
9656
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009657 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +02009658 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
9659 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +03009660
Peter Feiner4e595162016-07-07 14:49:58 -07009661 if (!vmx->guest_msrs)
9662 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009663
Paolo Bonzinif21f1652018-01-11 12:16:15 +01009664 err = alloc_loaded_vmcs(&vmx->vmcs01);
9665 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009666 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009667
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009668 msr_bitmap = vmx->vmcs01.msr_bitmap;
9669 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
9670 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
9671 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
9672 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
9673 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
9674 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
9675 vmx->msr_bitmap_mode = 0;
9676
Paolo Bonzinif21f1652018-01-11 12:16:15 +01009677 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +03009678 cpu = get_cpu();
9679 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10009680 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +02009681 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009682 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03009683 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +02009684 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02009685 err = alloc_apic_access_page(kvm);
9686 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02009687 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02009688 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08009689
Sean Christophersone90008d2018-03-05 12:04:37 -08009690 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +08009691 err = init_rmode_identity_map(kvm);
9692 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02009693 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08009694 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08009695
Roman Kagan63aff652018-07-19 21:59:07 +03009696 if (nested)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009697 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
Sean Christopherson7caaa712018-12-03 13:53:01 -08009698 vmx_capability.ept,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009699 kvm_vcpu_apicv_active(&vmx->vcpu));
Wincy Vanb9c237b2015-02-03 23:56:30 +08009700
Wincy Van705699a2015-02-03 23:58:17 +08009701 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009702 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009703
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009704 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
9705
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02009706 /*
9707 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
9708 * or POSTED_INTR_WAKEUP_VECTOR.
9709 */
9710 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
9711 vmx->pi_desc.sn = 1;
9712
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009713 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009714
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009715free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08009716 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009717free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009718 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -07009719free_pml:
9720 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009721uninit_vcpu:
9722 kvm_vcpu_uninit(&vmx->vcpu);
9723free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +08009724 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +10009725 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009726 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009727}
9728
Jiri Kosinad90a7a02018-07-13 16:23:25 +02009729#define L1TF_MSG_SMT "L1TF CPU bug present and SMT on, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/l1tf.html for details.\n"
9730#define L1TF_MSG_L1D "L1TF CPU bug present and virtualization mitigation disabled, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/l1tf.html for details.\n"
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04009731
Wanpeng Lib31c1142018-03-12 04:53:04 -07009732static int vmx_vm_init(struct kvm *kvm)
9733{
Tianyu Lan877ad952018-07-19 08:40:23 +00009734 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
9735
Wanpeng Lib31c1142018-03-12 04:53:04 -07009736 if (!ple_gap)
9737 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04009738
Jiri Kosinad90a7a02018-07-13 16:23:25 +02009739 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
9740 switch (l1tf_mitigation) {
9741 case L1TF_MITIGATION_OFF:
9742 case L1TF_MITIGATION_FLUSH_NOWARN:
9743 /* 'I explicitly don't care' is set */
9744 break;
9745 case L1TF_MITIGATION_FLUSH:
9746 case L1TF_MITIGATION_FLUSH_NOSMT:
9747 case L1TF_MITIGATION_FULL:
9748 /*
9749 * Warn upon starting the first VM in a potentially
9750 * insecure environment.
9751 */
9752 if (cpu_smt_control == CPU_SMT_ENABLED)
9753 pr_warn_once(L1TF_MSG_SMT);
9754 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
9755 pr_warn_once(L1TF_MSG_L1D);
9756 break;
9757 case L1TF_MITIGATION_FULL_FORCE:
9758 /* Flush is enforced */
9759 break;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04009760 }
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04009761 }
Wanpeng Lib31c1142018-03-12 04:53:04 -07009762 return 0;
9763}
9764
Yang, Sheng002c7f72007-07-31 14:23:01 +03009765static void __init vmx_check_processor_compat(void *rtn)
9766{
9767 struct vmcs_config vmcs_conf;
Sean Christopherson7caaa712018-12-03 13:53:01 -08009768 struct vmx_capability vmx_cap;
Yang, Sheng002c7f72007-07-31 14:23:01 +03009769
9770 *(int *)rtn = 0;
Sean Christopherson7caaa712018-12-03 13:53:01 -08009771 if (setup_vmcs_config(&vmcs_conf, &vmx_cap) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03009772 *(int *)rtn = -EIO;
Sean Christopherson7caaa712018-12-03 13:53:01 -08009773 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, vmx_cap.ept, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +03009774 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
9775 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
9776 smp_processor_id());
9777 *(int *)rtn = -EIO;
9778 }
9779}
9780
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009781static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08009782{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009783 u8 cache;
9784 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009785
Sheng Yang522c68c2009-04-27 20:35:43 +08009786 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +02009787 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +08009788 * 2. EPT with VT-d:
9789 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +02009790 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +08009791 * b. VT-d with snooping control feature: snooping control feature of
9792 * VT-d engine can guarantee the cache correctness. Just set it
9793 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08009794 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08009795 * consistent with host MTRR
9796 */
Paolo Bonzini606decd2015-10-01 13:12:47 +02009797 if (is_mmio) {
9798 cache = MTRR_TYPE_UNCACHABLE;
9799 goto exit;
9800 }
9801
9802 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009803 ipat = VMX_EPT_IPAT_BIT;
9804 cache = MTRR_TYPE_WRBACK;
9805 goto exit;
9806 }
9807
9808 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
9809 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02009810 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08009811 cache = MTRR_TYPE_WRBACK;
9812 else
9813 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009814 goto exit;
9815 }
9816
Xiao Guangrongff536042015-06-15 16:55:22 +08009817 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009818
9819exit:
9820 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08009821}
9822
Sheng Yang17cc3932010-01-05 19:02:27 +08009823static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02009824{
Sheng Yang878403b2010-01-05 19:02:29 +08009825 if (enable_ept && !cpu_has_vmx_ept_1g_page())
9826 return PT_DIRECTORY_LEVEL;
9827 else
9828 /* For shadow and EPT supported 1GB page */
9829 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02009830}
9831
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009832static void vmcs_set_secondary_exec_control(u32 new_ctl)
9833{
9834 /*
9835 * These bits in the secondary execution controls field
9836 * are dynamic, the others are mostly based on the hypervisor
9837 * architecture and the guest's CPUID. Do not touch the
9838 * dynamic bits.
9839 */
9840 u32 mask =
9841 SECONDARY_EXEC_SHADOW_VMCS |
9842 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +02009843 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9844 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009845
9846 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9847
9848 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
9849 (new_ctl & ~mask) | (cur_ctl & mask));
9850}
9851
David Matlack8322ebb2016-11-29 18:14:09 -08009852/*
9853 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
9854 * (indicating "allowed-1") if they are supported in the guest's CPUID.
9855 */
9856static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
9857{
9858 struct vcpu_vmx *vmx = to_vmx(vcpu);
9859 struct kvm_cpuid_entry2 *entry;
9860
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009861 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
9862 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -08009863
9864#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
9865 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009866 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -08009867} while (0)
9868
9869 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
9870 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
9871 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
9872 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
9873 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
9874 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
9875 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
9876 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
9877 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
9878 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
9879 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
9880 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
9881 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
9882 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
9883 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
9884
9885 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
9886 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
9887 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
9888 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
9889 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +01009890 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -08009891
9892#undef cr4_fixed1_update
9893}
9894
Liran Alon5f76f6f2018-09-14 03:25:52 +03009895static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
9896{
9897 struct vcpu_vmx *vmx = to_vmx(vcpu);
9898
9899 if (kvm_mpx_supported()) {
9900 bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX);
9901
9902 if (mpx_enabled) {
9903 vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
9904 vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
9905 } else {
9906 vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS;
9907 vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS;
9908 }
9909 }
9910}
9911
Sheng Yang0e851882009-12-18 16:48:46 +08009912static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
9913{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009914 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009915
Paolo Bonzini80154d72017-08-24 13:55:35 +02009916 if (cpu_has_secondary_exec_ctrls()) {
9917 vmx_compute_secondary_exec_control(vmx);
9918 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009919 }
Mao, Junjiead756a12012-07-02 01:18:48 +00009920
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009921 if (nested_vmx_allowed(vcpu))
9922 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
9923 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
9924 else
9925 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
9926 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -08009927
Liran Alon5f76f6f2018-09-14 03:25:52 +03009928 if (nested_vmx_allowed(vcpu)) {
David Matlack8322ebb2016-11-29 18:14:09 -08009929 nested_vmx_cr_fixed1_bits_update(vcpu);
Liran Alon5f76f6f2018-09-14 03:25:52 +03009930 nested_vmx_entry_exit_ctls_update(vcpu);
9931 }
Sheng Yang0e851882009-12-18 16:48:46 +08009932}
9933
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009934static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
9935{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03009936 if (func == 1 && nested)
9937 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009938}
9939
Yang Zhang25d92082013-08-06 12:00:32 +03009940static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
9941 struct x86_exception *fault)
9942{
Jan Kiszka533558b2014-01-04 18:47:20 +01009943 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -04009944 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +01009945 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -04009946 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +03009947
Bandan Dasc5f983f2017-05-05 15:25:14 -04009948 if (vmx->nested.pml_full) {
9949 exit_reason = EXIT_REASON_PML_FULL;
9950 vmx->nested.pml_full = false;
9951 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
9952 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +01009953 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +03009954 else
Jan Kiszka533558b2014-01-04 18:47:20 +01009955 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -04009956
9957 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +03009958 vmcs12->guest_physical_address = fault->address;
9959}
9960
Peter Feiner995f00a2017-06-30 17:26:32 -07009961static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
9962{
David Hildenbrandbb97a012017-08-10 23:15:28 +02009963 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -07009964}
9965
Nadav Har'El155a97a2013-08-05 11:07:16 +03009966/* Callbacks for nested_ept_init_mmu_context: */
9967
9968static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
9969{
9970 /* return the page table to be shadowed - in our case, EPT12 */
9971 return get_vmcs12(vcpu)->ept_pointer;
9972}
9973
Sean Christopherson5b8ba412018-09-26 09:23:40 -07009974static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +03009975{
Paolo Bonziniad896af2013-10-02 16:56:14 +02009976 WARN_ON(mmu_is_nested(vcpu));
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02009977
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02009978 vcpu->arch.mmu = &vcpu->arch.guest_mmu;
Paolo Bonziniad896af2013-10-02 16:56:14 +02009979 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009980 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02009981 VMX_EPT_EXECUTE_ONLY_BIT,
Junaid Shahid50c28f22018-06-27 14:59:11 -07009982 nested_ept_ad_enabled(vcpu),
9983 nested_ept_get_cr3(vcpu));
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02009984 vcpu->arch.mmu->set_cr3 = vmx_set_cr3;
9985 vcpu->arch.mmu->get_cr3 = nested_ept_get_cr3;
9986 vcpu->arch.mmu->inject_page_fault = nested_ept_inject_page_fault;
Vitaly Kuznetsov3dc773e2018-10-08 21:28:06 +02009987 vcpu->arch.mmu->get_pdptr = kvm_pdptr_read;
Nadav Har'El155a97a2013-08-05 11:07:16 +03009988
9989 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +03009990}
9991
9992static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
9993{
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02009994 vcpu->arch.mmu = &vcpu->arch.root_mmu;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02009995 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +03009996}
9997
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009998static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
9999 u16 error_code)
10000{
10001 bool inequality, bit;
10002
10003 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10004 inequality =
10005 (error_code & vmcs12->page_fault_error_code_mask) !=
10006 vmcs12->page_fault_error_code_match;
10007 return inequality ^ bit;
10008}
10009
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010010static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10011 struct x86_exception *fault)
10012{
10013 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10014
10015 WARN_ON(!is_guest_mode(vcpu));
10016
Wanpeng Li305d0ab2017-09-28 18:16:44 -070010017 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10018 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020010019 vmcs12->vm_exit_intr_error_code = fault->error_code;
10020 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10021 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10022 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10023 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010024 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010025 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010026 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010027}
10028
Paolo Bonzinic9923842017-12-13 14:16:30 +010010029static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10030 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010031
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020010032static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010033{
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020010034 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010035 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010036 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010037 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010038
10039 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010040 /*
10041 * Translate L1 physical address to host physical
10042 * address for vmcs02. Keep the page pinned, so this
10043 * physical address remains valid. We keep a reference
10044 * to it so we can release it later.
10045 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010046 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010047 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010048 vmx->nested.apic_access_page = NULL;
10049 }
10050 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010051 /*
10052 * If translation failed, no matter: This feature asks
10053 * to exit when accessing the given address, and if it
10054 * can never be accessed, this feature won't do
10055 * anything anyway.
10056 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010057 if (!is_error_page(page)) {
10058 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010059 hpa = page_to_phys(vmx->nested.apic_access_page);
10060 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10061 } else {
10062 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
10063 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10064 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010065 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010066
10067 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010068 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010069 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010070 vmx->nested.virtual_apic_page = NULL;
10071 }
10072 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010073
10074 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010075 * If translation failed, VM entry will fail because
10076 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10077 * Failing the vm entry is _not_ what the processor
10078 * does but it's basically the only possibility we
10079 * have. We could still enter the guest if CR8 load
10080 * exits are enabled, CR8 store exits are enabled, and
10081 * virtualize APIC access is disabled; in this case
10082 * the processor would never use the TPR shadow and we
10083 * could simply clear the bit from the execution
10084 * control. But such a configuration is useless, so
10085 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010086 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010087 if (!is_error_page(page)) {
10088 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010089 hpa = page_to_phys(vmx->nested.virtual_apic_page);
10090 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
10091 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010092 }
10093
Wincy Van705699a2015-02-03 23:58:17 +080010094 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010095 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
10096 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010097 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010098 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080010099 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010100 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
10101 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010102 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010103 vmx->nested.pi_desc_page = page;
10104 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080010105 vmx->nested.pi_desc =
10106 (struct pi_desc *)((void *)vmx->nested.pi_desc +
10107 (unsigned long)(vmcs12->posted_intr_desc_addr &
10108 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010109 vmcs_write64(POSTED_INTR_DESC_ADDR,
10110 page_to_phys(vmx->nested.pi_desc_page) +
10111 (unsigned long)(vmcs12->posted_intr_desc_addr &
10112 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080010113 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080010114 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000010115 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
10116 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010117 else
10118 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
10119 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010120}
10121
Jan Kiszkaf41245002014-03-07 20:03:13 +010010122static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
10123{
10124 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
10125 struct vcpu_vmx *vmx = to_vmx(vcpu);
10126
Sean Christopherson4c008122018-08-27 15:21:10 -070010127 /*
10128 * A timer value of zero is architecturally guaranteed to cause
10129 * a VMExit prior to executing any instructions in the guest.
10130 */
10131 if (preemption_timeout == 0) {
Jan Kiszkaf41245002014-03-07 20:03:13 +010010132 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
10133 return;
10134 }
10135
Sean Christopherson4c008122018-08-27 15:21:10 -070010136 if (vcpu->arch.virtual_tsc_khz == 0)
10137 return;
10138
Jan Kiszkaf41245002014-03-07 20:03:13 +010010139 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10140 preemption_timeout *= 1000000;
10141 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
10142 hrtimer_start(&vmx->nested.preemption_timer,
10143 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
10144}
10145
Jim Mattson56a20512017-07-06 16:33:06 -070010146static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
10147 struct vmcs12 *vmcs12)
10148{
10149 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
10150 return 0;
10151
10152 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
10153 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
10154 return -EINVAL;
10155
10156 return 0;
10157}
10158
Wincy Van3af18d92015-02-03 23:49:31 +080010159static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
10160 struct vmcs12 *vmcs12)
10161{
Wincy Van3af18d92015-02-03 23:49:31 +080010162 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10163 return 0;
10164
Jim Mattson5fa99cb2017-07-06 16:33:07 -070010165 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080010166 return -EINVAL;
10167
10168 return 0;
10169}
10170
Jim Mattson712b12d2017-08-24 13:24:47 -070010171static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
10172 struct vmcs12 *vmcs12)
10173{
10174 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10175 return 0;
10176
10177 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
10178 return -EINVAL;
10179
10180 return 0;
10181}
10182
Wincy Van3af18d92015-02-03 23:49:31 +080010183/*
10184 * Merge L0's and L1's MSR bitmap, return false to indicate that
10185 * we do not use the hardware.
10186 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010010187static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10188 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080010189{
Wincy Van82f0dd42015-02-03 23:57:18 +080010190 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080010191 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020010192 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010193 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010010194 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010195 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010010196 *
10197 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
10198 * ensures that we do not accidentally generate an L02 MSR bitmap
10199 * from the L12 MSR bitmap that is too permissive.
10200 * 2. That L1 or L2s have actually used the MSR. This avoids
10201 * unnecessarily merging of the bitmap if the MSR is unused. This
10202 * works properly because we only update the L01 MSR bitmap lazily.
10203 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
10204 * updated to reflect this when L1 (or its L2s) actually write to
10205 * the MSR.
10206 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000010207 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
10208 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080010209
Paolo Bonzinic9923842017-12-13 14:16:30 +010010210 /* Nothing to do if the MSR bitmap is not in use. */
10211 if (!cpu_has_vmx_msr_bitmap() ||
10212 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10213 return false;
10214
Ashok Raj15d45072018-02-01 22:59:43 +010010215 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010216 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080010217 return false;
10218
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010219 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
10220 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080010221 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010010222
Radim Krčmářd048c092016-08-08 20:16:22 +020010223 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010010224 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
10225 /*
10226 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
10227 * just lets the processor take the value from the virtual-APIC page;
10228 * take those 256 bits directly from the L1 bitmap.
10229 */
10230 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10231 unsigned word = msr / BITS_PER_LONG;
10232 msr_bitmap_l0[word] = msr_bitmap_l1[word];
10233 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080010234 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010010235 } else {
10236 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10237 unsigned word = msr / BITS_PER_LONG;
10238 msr_bitmap_l0[word] = ~0;
10239 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
10240 }
10241 }
10242
10243 nested_vmx_disable_intercept_for_msr(
10244 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010245 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010246 MSR_TYPE_W);
10247
10248 if (nested_cpu_has_vid(vmcs12)) {
10249 nested_vmx_disable_intercept_for_msr(
10250 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010251 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010252 MSR_TYPE_W);
10253 nested_vmx_disable_intercept_for_msr(
10254 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010255 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010256 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080010257 }
Ashok Raj15d45072018-02-01 22:59:43 +010010258
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010259 if (spec_ctrl)
10260 nested_vmx_disable_intercept_for_msr(
10261 msr_bitmap_l1, msr_bitmap_l0,
10262 MSR_IA32_SPEC_CTRL,
10263 MSR_TYPE_R | MSR_TYPE_W);
10264
Ashok Raj15d45072018-02-01 22:59:43 +010010265 if (pred_cmd)
10266 nested_vmx_disable_intercept_for_msr(
10267 msr_bitmap_l1, msr_bitmap_l0,
10268 MSR_IA32_PRED_CMD,
10269 MSR_TYPE_W);
10270
Wincy Vanf2b93282015-02-03 23:56:03 +080010271 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010272 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080010273
10274 return true;
10275}
10276
Liran Alon61ada742018-06-23 02:35:08 +030010277static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
10278 struct vmcs12 *vmcs12)
10279{
10280 struct vmcs12 *shadow;
10281 struct page *page;
10282
10283 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
10284 vmcs12->vmcs_link_pointer == -1ull)
10285 return;
10286
10287 shadow = get_shadow_vmcs12(vcpu);
10288 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
10289
10290 memcpy(shadow, kmap(page), VMCS12_SIZE);
10291
10292 kunmap(page);
10293 kvm_release_page_clean(page);
10294}
10295
10296static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
10297 struct vmcs12 *vmcs12)
10298{
10299 struct vcpu_vmx *vmx = to_vmx(vcpu);
10300
10301 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
10302 vmcs12->vmcs_link_pointer == -1ull)
10303 return;
10304
10305 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
10306 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
10307}
10308
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040010309static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
10310 struct vmcs12 *vmcs12)
10311{
10312 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
10313 !page_address_valid(vcpu, vmcs12->apic_access_addr))
10314 return -EINVAL;
10315 else
10316 return 0;
10317}
10318
Wincy Vanf2b93282015-02-03 23:56:03 +080010319static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
10320 struct vmcs12 *vmcs12)
10321{
Wincy Van82f0dd42015-02-03 23:57:18 +080010322 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080010323 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080010324 !nested_cpu_has_vid(vmcs12) &&
10325 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080010326 return 0;
10327
10328 /*
10329 * If virtualize x2apic mode is enabled,
10330 * virtualize apic access must be disabled.
10331 */
Wincy Van82f0dd42015-02-03 23:57:18 +080010332 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10333 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080010334 return -EINVAL;
10335
Wincy Van608406e2015-02-03 23:57:51 +080010336 /*
10337 * If virtual interrupt delivery is enabled,
10338 * we must exit on external interrupts.
10339 */
10340 if (nested_cpu_has_vid(vmcs12) &&
10341 !nested_exit_on_intr(vcpu))
10342 return -EINVAL;
10343
Wincy Van705699a2015-02-03 23:58:17 +080010344 /*
10345 * bits 15:8 should be zero in posted_intr_nv,
10346 * the descriptor address has been already checked
10347 * in nested_get_vmcs12_pages.
Krish Sadhukhan6de84e52018-08-23 20:03:03 -040010348 *
10349 * bits 5:0 of posted_intr_desc_addr should be zero.
Wincy Van705699a2015-02-03 23:58:17 +080010350 */
10351 if (nested_cpu_has_posted_intr(vmcs12) &&
10352 (!nested_cpu_has_vid(vmcs12) ||
10353 !nested_exit_intr_ack_set(vcpu) ||
Krish Sadhukhan6de84e52018-08-23 20:03:03 -040010354 (vmcs12->posted_intr_nv & 0xff00) ||
10355 (vmcs12->posted_intr_desc_addr & 0x3f) ||
KarimAllah Ahmed22a7cdc2018-10-20 23:42:59 +020010356 (vmcs12->posted_intr_desc_addr >> cpuid_maxphyaddr(vcpu))))
Wincy Van705699a2015-02-03 23:58:17 +080010357 return -EINVAL;
10358
Wincy Vanf2b93282015-02-03 23:56:03 +080010359 /* tpr shadow is needed by all apicv features. */
10360 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10361 return -EINVAL;
10362
10363 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080010364}
10365
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010366static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
10367 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010368 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030010369{
Liran Alone2536742018-06-23 02:35:02 +030010370 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010371 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010372 u64 count, addr;
10373
Liran Alone2536742018-06-23 02:35:02 +030010374 if (vmcs12_read_any(vmcs12, count_field, &count) ||
10375 vmcs12_read_any(vmcs12, addr_field, &addr)) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010376 WARN_ON(1);
10377 return -EINVAL;
10378 }
10379 if (count == 0)
10380 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010381 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010382 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
10383 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010384 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010385 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
10386 addr_field, maxphyaddr, count, addr);
10387 return -EINVAL;
10388 }
10389 return 0;
10390}
10391
10392static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
10393 struct vmcs12 *vmcs12)
10394{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010395 if (vmcs12->vm_exit_msr_load_count == 0 &&
10396 vmcs12->vm_exit_msr_store_count == 0 &&
10397 vmcs12->vm_entry_msr_load_count == 0)
10398 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010399 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010400 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010401 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010402 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010403 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010404 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030010405 return -EINVAL;
10406 return 0;
10407}
10408
Bandan Dasc5f983f2017-05-05 15:25:14 -040010409static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
10410 struct vmcs12 *vmcs12)
10411{
Krish Sadhukhan55c1dcd2018-09-27 14:33:27 -040010412 if (!nested_cpu_has_pml(vmcs12))
10413 return 0;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010414
Krish Sadhukhan55c1dcd2018-09-27 14:33:27 -040010415 if (!nested_cpu_has_ept(vmcs12) ||
10416 !page_address_valid(vcpu, vmcs12->pml_address))
10417 return -EINVAL;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010418
10419 return 0;
10420}
10421
Jim Mattson88656042018-09-24 11:05:43 -070010422static int nested_vmx_check_unrestricted_guest_controls(struct kvm_vcpu *vcpu,
10423 struct vmcs12 *vmcs12)
10424{
10425 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST) &&
10426 !nested_cpu_has_ept(vmcs12))
10427 return -EINVAL;
10428 return 0;
10429}
10430
10431static int nested_vmx_check_mode_based_ept_exec_controls(struct kvm_vcpu *vcpu,
10432 struct vmcs12 *vmcs12)
10433{
10434 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_MODE_BASED_EPT_EXEC) &&
10435 !nested_cpu_has_ept(vmcs12))
10436 return -EINVAL;
10437 return 0;
10438}
10439
Liran Alona8a7c022018-06-23 02:35:06 +030010440static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
10441 struct vmcs12 *vmcs12)
10442{
10443 if (!nested_cpu_has_shadow_vmcs(vmcs12))
10444 return 0;
10445
10446 if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
10447 !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
10448 return -EINVAL;
10449
10450 return 0;
10451}
10452
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010453static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
10454 struct vmx_msr_entry *e)
10455{
10456 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020010457 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010458 return -EINVAL;
10459 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
10460 e->index == MSR_IA32_UCODE_REV)
10461 return -EINVAL;
10462 if (e->reserved != 0)
10463 return -EINVAL;
10464 return 0;
10465}
10466
10467static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
10468 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030010469{
10470 if (e->index == MSR_FS_BASE ||
10471 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010472 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
10473 nested_vmx_msr_check_common(vcpu, e))
10474 return -EINVAL;
10475 return 0;
10476}
10477
10478static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
10479 struct vmx_msr_entry *e)
10480{
10481 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
10482 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030010483 return -EINVAL;
10484 return 0;
10485}
10486
10487/*
10488 * Load guest's/host's msr at nested entry/exit.
10489 * return 0 for success, entry index for failure.
10490 */
10491static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10492{
10493 u32 i;
10494 struct vmx_msr_entry e;
10495 struct msr_data msr;
10496
10497 msr.host_initiated = false;
10498 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010499 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
10500 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010501 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010502 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10503 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010504 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010505 }
10506 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010507 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010508 "%s check failed (%u, 0x%x, 0x%x)\n",
10509 __func__, i, e.index, e.reserved);
10510 goto fail;
10511 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010512 msr.index = e.index;
10513 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010514 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010515 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010516 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10517 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030010518 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010519 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010520 }
10521 return 0;
10522fail:
10523 return i + 1;
10524}
10525
10526static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10527{
10528 u32 i;
10529 struct vmx_msr_entry e;
10530
10531 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010532 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010533 if (kvm_vcpu_read_guest(vcpu,
10534 gpa + i * sizeof(e),
10535 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010536 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010537 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10538 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010539 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010540 }
10541 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010542 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010543 "%s check failed (%u, 0x%x, 0x%x)\n",
10544 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030010545 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010546 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010547 msr_info.host_initiated = false;
10548 msr_info.index = e.index;
10549 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010550 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010551 "%s cannot read MSR (%u, 0x%x)\n",
10552 __func__, i, e.index);
10553 return -EINVAL;
10554 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010555 if (kvm_vcpu_write_guest(vcpu,
10556 gpa + i * sizeof(e) +
10557 offsetof(struct vmx_msr_entry, value),
10558 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010559 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010560 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010561 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010562 return -EINVAL;
10563 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010564 }
10565 return 0;
10566}
10567
Ladi Prosek1dc35da2016-11-30 16:03:11 +010010568static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
10569{
10570 unsigned long invalid_mask;
10571
10572 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
10573 return (val & invalid_mask) == 0;
10574}
10575
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010576/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010577 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
10578 * emulating VM entry into a guest with EPT enabled.
10579 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10580 * is assigned to entry_failure_code on failure.
10581 */
10582static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080010583 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010584{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010585 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010010586 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010587 *entry_failure_code = ENTRY_FAIL_DEFAULT;
10588 return 1;
10589 }
10590
10591 /*
10592 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
10593 * must not be dereferenced.
10594 */
10595 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
10596 !nested_ept) {
10597 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
10598 *entry_failure_code = ENTRY_FAIL_PDPTE;
10599 return 1;
10600 }
10601 }
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010602 }
10603
Junaid Shahid50c28f22018-06-27 14:59:11 -070010604 if (!nested_ept)
Junaid Shahidade61e22018-06-27 14:59:15 -070010605 kvm_mmu_new_cr3(vcpu, cr3, false);
Junaid Shahid50c28f22018-06-27 14:59:11 -070010606
10607 vcpu->arch.cr3 = cr3;
10608 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
10609
10610 kvm_init_mmu(vcpu, false);
10611
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010612 return 0;
10613}
10614
Liran Alonefebf0a2018-10-08 23:42:20 +030010615/*
10616 * Returns if KVM is able to config CPU to tag TLB entries
10617 * populated by L2 differently than TLB entries populated
10618 * by L1.
10619 *
10620 * If L1 uses EPT, then TLB entries are tagged with different EPTP.
10621 *
10622 * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged
10623 * with different VPID (L1 entries are tagged with vmx->vpid
10624 * while L2 entries are tagged with vmx->nested.vpid02).
10625 */
10626static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010010627{
Liran Alonefebf0a2018-10-08 23:42:20 +030010628 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010010629
Liran Alonefebf0a2018-10-08 23:42:20 +030010630 return nested_cpu_has_ept(vmcs12) ||
10631 (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02);
10632}
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010010633
Sean Christopherson3df5c372018-09-26 09:23:44 -070010634static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
10635{
10636 if (vmx->nested.nested_run_pending &&
10637 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
10638 return vmcs12->guest_ia32_efer;
10639 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
10640 return vmx->vcpu.arch.efer | (EFER_LMA | EFER_LME);
10641 else
10642 return vmx->vcpu.arch.efer & ~(EFER_LMA | EFER_LME);
10643}
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010010644
Sean Christopherson09abe322018-09-26 09:23:50 -070010645static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010010646{
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010010647 /*
Sean Christopherson9d6105b2018-09-26 09:23:51 -070010648 * If vmcs02 hasn't been initialized, set the constant vmcs02 state
Sean Christopherson09abe322018-09-26 09:23:50 -070010649 * according to L0's settings (vmcs12 is irrelevant here). Host
10650 * fields that come from L0 and are not constant, e.g. HOST_CR3,
10651 * will be set as needed prior to VMLAUNCH/VMRESUME.
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010010652 */
Sean Christopherson9d6105b2018-09-26 09:23:51 -070010653 if (vmx->nested.vmcs02_initialized)
Sean Christopherson09abe322018-09-26 09:23:50 -070010654 return;
Sean Christopherson9d6105b2018-09-26 09:23:51 -070010655 vmx->nested.vmcs02_initialized = true;
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010010656
10657 /*
Sean Christopherson52017602018-09-26 09:23:57 -070010658 * We don't care what the EPTP value is we just need to guarantee
10659 * it's valid so we don't get a false positive when doing early
10660 * consistency checks.
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010010661 */
Sean Christopherson52017602018-09-26 09:23:57 -070010662 if (enable_ept && nested_early_check)
10663 vmcs_write64(EPT_POINTER, construct_eptp(&vmx->vcpu, 0));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010010664
10665 /* All VMFUNCs are currently emulated through L0 vmexits. */
10666 if (cpu_has_vmx_vmfunc())
10667 vmcs_write64(VM_FUNCTION_CONTROL, 0);
10668
Sean Christopherson09abe322018-09-26 09:23:50 -070010669 if (cpu_has_vmx_posted_intr())
10670 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
10671
10672 if (cpu_has_vmx_msr_bitmap())
10673 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
10674
10675 if (enable_pml)
10676 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010010677
10678 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070010679 * Set the MSR load/store lists to match L0's settings. Only the
10680 * addresses are constant (for vmcs02), the counts can change based
10681 * on L2's behavior, e.g. switching to/from long mode.
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010010682 */
10683 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040010684 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040010685 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010010686
Sean Christopherson09abe322018-09-26 09:23:50 -070010687 vmx_set_constant_host_state(vmx);
10688}
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010010689
Sean Christopherson09abe322018-09-26 09:23:50 -070010690static void prepare_vmcs02_early_full(struct vcpu_vmx *vmx,
10691 struct vmcs12 *vmcs12)
10692{
10693 prepare_vmcs02_constant_state(vmx);
10694
10695 vmcs_write64(VMCS_LINK_POINTER, -1ull);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010010696
10697 if (enable_vpid) {
10698 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
10699 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
10700 else
10701 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
10702 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +010010703}
10704
Sean Christopherson09abe322018-09-26 09:23:50 -070010705static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010706{
Bandan Das03efce62017-05-05 15:25:15 -040010707 u32 exec_control, vmcs12_exec_ctrl;
Sean Christopherson09abe322018-09-26 09:23:50 -070010708 u64 guest_efer = nested_vmx_calc_efer(vmx, vmcs12);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010709
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020010710 if (vmx->nested.dirty_vmcs12 || vmx->nested.hv_evmcs)
Sean Christopherson09abe322018-09-26 09:23:50 -070010711 prepare_vmcs02_early_full(vmx, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080010712
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010010713 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070010714 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
10715 * entry, but only if the current (host) sp changed from the value
10716 * we wrote last (vmx->host_rsp). This cache is no longer relevant
10717 * if we switch vmcs, and rather than hold a separate cache per vmcs,
Sean Christopherson52017602018-09-26 09:23:57 -070010718 * here we just force the write to happen on entry. host_rsp will
10719 * also be written unconditionally by nested_vmx_check_vmentry_hw()
10720 * if we are doing early consistency checks via hardware.
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010010721 */
Sean Christopherson09abe322018-09-26 09:23:50 -070010722 vmx->host_rsp = 0;
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010010723
Sean Christopherson09abe322018-09-26 09:23:50 -070010724 /*
10725 * PIN CONTROLS
10726 */
Jan Kiszkaf41245002014-03-07 20:03:13 +010010727 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080010728
Sean Christophersonf459a702018-08-27 15:21:11 -070010729 /* Preemption timer setting is computed directly in vmx_vcpu_run. */
Paolo Bonzini9314006db2016-07-06 13:23:51 +020010730 exec_control |= vmcs_config.pin_based_exec_ctrl;
Sean Christophersonf459a702018-08-27 15:21:11 -070010731 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10732 vmx->loaded_vmcs->hv_timer_armed = false;
Paolo Bonzini9314006db2016-07-06 13:23:51 +020010733
10734 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080010735 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010736 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
10737 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010738 } else {
Wincy Van705699a2015-02-03 23:58:17 +080010739 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010740 }
Jan Kiszkaf41245002014-03-07 20:03:13 +010010741 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010742
Sean Christopherson09abe322018-09-26 09:23:50 -070010743 /*
10744 * EXEC CONTROLS
10745 */
10746 exec_control = vmx_exec_control(vmx); /* L0's desires */
10747 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
10748 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
10749 exec_control &= ~CPU_BASED_TPR_SHADOW;
10750 exec_control |= vmcs12->cpu_based_vm_exec_control;
Jan Kiszka0238ea92013-03-13 11:31:24 +010010751
Sean Christopherson09abe322018-09-26 09:23:50 -070010752 /*
10753 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
10754 * nested_get_vmcs12_pages can't fix it up, the illegal value
10755 * will result in a VM entry failure.
10756 */
10757 if (exec_control & CPU_BASED_TPR_SHADOW) {
10758 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
10759 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
10760 } else {
10761#ifdef CONFIG_X86_64
10762 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
10763 CPU_BASED_CR8_STORE_EXITING;
10764#endif
10765 }
10766
10767 /*
10768 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
10769 * for I/O port accesses.
10770 */
10771 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
10772 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
10773 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
10774
10775 /*
10776 * SECONDARY EXEC CONTROLS
10777 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010778 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020010779 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080010780
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010781 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010782 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020010783 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010010784 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020010785 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010786 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040010787 SECONDARY_EXEC_APIC_REGISTER_VIRT |
10788 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010789 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040010790 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
10791 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
10792 ~SECONDARY_EXEC_ENABLE_PML;
10793 exec_control |= vmcs12_exec_ctrl;
10794 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010795
Liran Alon32c7acf2018-06-23 02:35:11 +030010796 /* VMCS shadowing for L2 is emulated for now */
10797 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
10798
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010010799 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080010800 vmcs_write16(GUEST_INTR_STATUS,
10801 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080010802
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010803 /*
10804 * Write an illegal value to APIC_ACCESS_ADDR. Later,
10805 * nested_get_vmcs12_pages will either fix it up or
10806 * remove the VM execution control.
10807 */
10808 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
10809 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
10810
Sean Christopherson0b665d32018-08-14 09:33:34 -070010811 if (exec_control & SECONDARY_EXEC_ENCLS_EXITING)
10812 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
10813
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010814 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
10815 }
10816
Jim Mattson83bafef2016-10-04 10:48:38 -070010817 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070010818 * ENTRY CONTROLS
10819 *
Sean Christopherson3df5c372018-09-26 09:23:44 -070010820 * vmcs12's VM_{ENTRY,EXIT}_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE
Sean Christopherson09abe322018-09-26 09:23:50 -070010821 * are emulated by vmx_set_efer() in prepare_vmcs02(), but speculate
10822 * on the related bits (if supported by the CPU) in the hope that
10823 * we can avoid VMWrites during vmx_set_efer().
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010824 */
Sean Christophersonc73da3f2018-12-03 13:53:00 -080010825 exec_control = (vmcs12->vm_entry_controls | vmx_vmentry_ctrl()) &
Sean Christopherson3df5c372018-09-26 09:23:44 -070010826 ~VM_ENTRY_IA32E_MODE & ~VM_ENTRY_LOAD_IA32_EFER;
Sean Christophersonc73da3f2018-12-03 13:53:00 -080010827 if (cpu_has_load_ia32_efer()) {
Sean Christopherson3df5c372018-09-26 09:23:44 -070010828 if (guest_efer & EFER_LMA)
10829 exec_control |= VM_ENTRY_IA32E_MODE;
10830 if (guest_efer != host_efer)
10831 exec_control |= VM_ENTRY_LOAD_IA32_EFER;
10832 }
10833 vm_entry_controls_init(vmx, exec_control);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010834
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010835 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070010836 * EXIT CONTROLS
10837 *
10838 * L2->L1 exit controls are emulated - the hardware exit is to L0 so
10839 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
10840 * bits may be modified by vmx_set_efer() in prepare_vmcs02().
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010841 */
Sean Christophersonc73da3f2018-12-03 13:53:00 -080010842 exec_control = vmx_vmexit_ctrl();
10843 if (cpu_has_load_ia32_efer() && guest_efer != host_efer)
Sean Christopherson09abe322018-09-26 09:23:50 -070010844 exec_control |= VM_EXIT_LOAD_IA32_EFER;
10845 vm_exit_controls_init(vmx, exec_control);
10846
10847 /*
10848 * Conceptually we want to copy the PML address and index from
10849 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
10850 * since we always flush the log on each vmexit and never change
10851 * the PML address (once set), this happens to be equivalent to
10852 * simply resetting the index in vmcs02.
10853 */
10854 if (enable_pml)
10855 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
10856
10857 /*
10858 * Interrupt/Exception Fields
10859 */
10860 if (vmx->nested.nested_run_pending) {
10861 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
10862 vmcs12->vm_entry_intr_info_field);
10863 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
10864 vmcs12->vm_entry_exception_error_code);
10865 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
10866 vmcs12->vm_entry_instruction_len);
10867 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
10868 vmcs12->guest_interruptibility_info);
10869 vmx->loaded_vmcs->nmi_known_unmasked =
10870 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattson51aa68e2017-09-12 13:02:54 -070010871 } else {
Sean Christopherson09abe322018-09-26 09:23:50 -070010872 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10873 }
10874}
10875
10876static void prepare_vmcs02_full(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
10877{
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020010878 struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
Sean Christopherson09abe322018-09-26 09:23:50 -070010879
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020010880 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
10881 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) {
10882 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
Vitaly Kuznetsovcbe3f892018-10-19 16:16:03 +020010883 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020010884 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
10885 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
10886 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
10887 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
10888 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
10889 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
10890 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
Vitaly Kuznetsovcbe3f892018-10-19 16:16:03 +020010891 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020010892 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
10893 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
10894 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
10895 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
10896 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
10897 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
10898 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
10899 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
10900 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020010901 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
10902 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
10903 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
10904 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
10905 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
Vitaly Kuznetsovcbe3f892018-10-19 16:16:03 +020010906 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
10907 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020010908 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
10909 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
10910 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
10911 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
10912 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
10913 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
10914 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
10915 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
10916 }
10917
10918 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
10919 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1)) {
10920 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
10921 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
10922 vmcs12->guest_pending_dbg_exceptions);
10923 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
10924 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
10925
10926 /*
10927 * L1 may access the L2's PDPTR, so save them to construct
10928 * vmcs12
10929 */
10930 if (enable_ept) {
10931 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
10932 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
10933 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
10934 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
10935 }
10936 }
Sean Christopherson09abe322018-09-26 09:23:50 -070010937
10938 if (nested_cpu_has_xsaves(vmcs12))
10939 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
10940
10941 /*
10942 * Whether page-faults are trapped is determined by a combination of
10943 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
10944 * If enable_ept, L0 doesn't care about page faults and we should
10945 * set all of these to L1's desires. However, if !enable_ept, L0 does
10946 * care about (at least some) page faults, and because it is not easy
10947 * (if at all possible?) to merge L0 and L1's desires, we simply ask
10948 * to exit on each and every L2 page fault. This is done by setting
10949 * MASK=MATCH=0 and (see below) EB.PF=1.
10950 * Note that below we don't need special code to set EB.PF beyond the
10951 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
10952 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
10953 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
10954 */
10955 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
10956 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
10957 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
10958 enable_ept ? vmcs12->page_fault_error_code_match : 0);
10959
10960 if (cpu_has_vmx_apicv()) {
10961 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
10962 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
10963 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
10964 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
10965 }
10966
10967 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
10968 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
10969
10970 set_cr4_guest_host_mask(vmx);
10971
10972 if (kvm_mpx_supported()) {
10973 if (vmx->nested.nested_run_pending &&
10974 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
10975 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
10976 else
10977 vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs);
10978 }
Sean Christopherson09abe322018-09-26 09:23:50 -070010979}
10980
10981/*
10982 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
10983 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
10984 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
10985 * guest in a way that will both be appropriate to L1's requests, and our
10986 * needs. In addition to modifying the active vmcs (which is vmcs02), this
10987 * function also has additional necessary side-effects, like setting various
10988 * vcpu->arch fields.
10989 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10990 * is assigned to entry_failure_code on failure.
10991 */
10992static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10993 u32 *entry_failure_code)
10994{
10995 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020010996 struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
Sean Christopherson09abe322018-09-26 09:23:50 -070010997
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020010998 if (vmx->nested.dirty_vmcs12 || vmx->nested.hv_evmcs) {
Sean Christopherson09abe322018-09-26 09:23:50 -070010999 prepare_vmcs02_full(vmx, vmcs12);
11000 vmx->nested.dirty_vmcs12 = false;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011001 }
11002
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011003 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070011004 * First, the fields that are shadowed. This must be kept in sync
Sean Christophersone0123112018-12-03 13:52:57 -080011005 * with vmcs_shadow_fields.h.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011006 */
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020011007 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
11008 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) {
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020011009 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Vitaly Kuznetsovcbe3f892018-10-19 16:16:03 +020011010 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020011011 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011012
Sean Christopherson09abe322018-09-26 09:23:50 -070011013 if (vmx->nested.nested_run_pending &&
11014 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
11015 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
11016 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
11017 } else {
11018 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
11019 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
11020 }
11021 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
11022
11023 vmx->nested.preemption_timer_expired = false;
11024 if (nested_cpu_has_preemption_timer(vmcs12))
11025 vmx_start_preemption_timer(vcpu);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011026
11027 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
11028 * bitwise-or of what L1 wants to trap for L2, and what we want to
11029 * trap. Note that CR0.TS also needs updating - we do this later.
11030 */
11031 update_exception_bitmap(vcpu);
11032 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
11033 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11034
Jim Mattson6514dc32018-04-26 16:09:12 -070011035 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011036 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011037 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011038 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011039 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011040 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011041 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011042
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011043 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11044
Peter Feinerc95ba922016-08-17 09:36:47 -070011045 if (kvm_has_tsc_control)
11046 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011047
11048 if (enable_vpid) {
11049 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070011050 * There is no direct mapping between vpid02 and vpid12, the
11051 * vpid02 is per-vCPU for L0 and reused while the value of
11052 * vpid12 is changed w/ one invvpid during nested vmentry.
11053 * The vpid12 is allocated by L1 for L2, so it will not
11054 * influence global bitmap(for vpid01 and vpid02 allocation)
11055 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011056 */
Liran Alonefebf0a2018-10-08 23:42:20 +030011057 if (nested_cpu_has_vpid(vmcs12) && nested_has_guest_tlb_tag(vcpu)) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070011058 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
11059 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alonefebf0a2018-10-08 23:42:20 +030011060 __vmx_flush_tlb(vcpu, nested_get_vpid02(vcpu), false);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011061 }
11062 } else {
Liran Alon14389212018-10-08 23:42:17 +030011063 /*
11064 * If L1 use EPT, then L0 needs to execute INVEPT on
11065 * EPTP02 instead of EPTP01. Therefore, delay TLB
11066 * flush until vmcs02->eptp is fully updated by
11067 * KVM_REQ_LOAD_CR3. Note that this assumes
11068 * KVM_REQ_TLB_FLUSH is evaluated after
11069 * KVM_REQ_LOAD_CR3 in vcpu_enter_guest().
11070 */
11071 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011072 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011073 }
11074
Sean Christopherson5b8ba412018-09-26 09:23:40 -070011075 if (nested_cpu_has_ept(vmcs12))
11076 nested_ept_init_mmu_context(vcpu);
11077 else if (nested_cpu_has2(vmcs12,
11078 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Junaid Shahida468f2d2018-04-26 13:09:50 -070011079 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011080
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011081 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011082 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11083 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011084 * The CR0_READ_SHADOW is what L2 should have expected to read given
11085 * the specifications by L1; It's not enough to take
11086 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11087 * have more bits than L1 expected.
11088 */
11089 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11090 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11091
11092 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11093 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11094
Sean Christopherson09abe322018-09-26 09:23:50 -070011095 vcpu->arch.efer = nested_vmx_calc_efer(vmx, vmcs12);
Sean Christopherson3df5c372018-09-26 09:23:44 -070011096 /* Note: may modify VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
David Matlack5a6a9742016-11-29 18:14:10 -080011097 vmx_set_efer(vcpu, vcpu->arch.efer);
11098
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011099 /*
11100 * Guest state is invalid and unrestricted guest is disabled,
11101 * which means L1 attempted VMEntry to L2 with invalid state.
11102 * Fail the VMEntry.
11103 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010011104 if (vmx->emulation_required) {
11105 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011106 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010011107 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011108
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011109 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010011110 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011111 entry_failure_code))
11112 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010011113
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011114 if (!enable_ept)
11115 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11116
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011117 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11118 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010011119 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011120}
11121
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011122static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11123{
11124 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11125 nested_cpu_has_virtual_nmis(vmcs12))
11126 return -EINVAL;
11127
11128 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11129 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
11130 return -EINVAL;
11131
11132 return 0;
11133}
11134
Jim Mattsonca0bde22016-11-30 12:03:46 -080011135static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11136{
11137 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson64a919f2018-09-26 09:23:39 -070011138 bool ia32e;
Jim Mattsonca0bde22016-11-30 12:03:46 -080011139
11140 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
11141 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11142 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11143
Krish Sadhukhanba8e23d2018-09-04 14:42:58 -040011144 if (nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id)
11145 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11146
Jim Mattson56a20512017-07-06 16:33:06 -070011147 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11148 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11149
Jim Mattsonca0bde22016-11-30 12:03:46 -080011150 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11151 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11152
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011153 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
11154 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11155
Jim Mattson712b12d2017-08-24 13:24:47 -070011156 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11157 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11158
Jim Mattsonca0bde22016-11-30 12:03:46 -080011159 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
11160 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11161
11162 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11163 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11164
Krish Sadhukhan14aa61d2018-11-01 01:21:58 -040011165 if (!nested_cpu_has_preemption_timer(vmcs12) &&
11166 nested_cpu_has_save_preemption_timer(vmcs12))
11167 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11168
Bandan Dasc5f983f2017-05-05 15:25:14 -040011169 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
11170 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11171
Jim Mattson88656042018-09-24 11:05:43 -070011172 if (nested_vmx_check_unrestricted_guest_controls(vcpu, vmcs12))
11173 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11174
11175 if (nested_vmx_check_mode_based_ept_exec_controls(vcpu, vmcs12))
11176 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11177
Liran Alona8a7c022018-06-23 02:35:06 +030011178 if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
11179 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11180
Jim Mattsonca0bde22016-11-30 12:03:46 -080011181 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011182 vmx->nested.msrs.procbased_ctls_low,
11183 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070011184 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
11185 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011186 vmx->nested.msrs.secondary_ctls_low,
11187 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011188 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011189 vmx->nested.msrs.pinbased_ctls_low,
11190 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011191 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011192 vmx->nested.msrs.exit_ctls_low,
11193 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011194 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011195 vmx->nested.msrs.entry_ctls_low,
11196 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011197 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11198
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011199 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011200 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11201
Bandan Das41ab9372017-08-03 15:54:43 -040011202 if (nested_cpu_has_vmfunc(vmcs12)) {
11203 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011204 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040011205 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11206
11207 if (nested_cpu_has_eptp_switching(vmcs12)) {
11208 if (!nested_cpu_has_ept(vmcs12) ||
11209 !page_address_valid(vcpu, vmcs12->eptp_list_address))
11210 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11211 }
11212 }
Bandan Das27c42a12017-08-03 15:54:42 -040011213
Jim Mattsonc7c2c7092017-05-05 11:28:09 -070011214 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
11215 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11216
Jim Mattsonca0bde22016-11-30 12:03:46 -080011217 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
11218 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
11219 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
11220 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11221
Marc Orr04473782018-06-20 17:21:29 -070011222 /*
Sean Christopherson64a919f2018-09-26 09:23:39 -070011223 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
11224 * IA32_EFER MSR must be 0 in the field for that register. In addition,
11225 * the values of the LMA and LME bits in the field must each be that of
11226 * the host address-space size VM-exit control.
11227 */
11228 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
11229 ia32e = (vmcs12->vm_exit_controls &
11230 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
11231 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
11232 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
11233 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
11234 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11235 }
11236
11237 /*
Marc Orr04473782018-06-20 17:21:29 -070011238 * From the Intel SDM, volume 3:
11239 * Fields relevant to VM-entry event injection must be set properly.
11240 * These fields are the VM-entry interruption-information field, the
11241 * VM-entry exception error code, and the VM-entry instruction length.
11242 */
11243 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
11244 u32 intr_info = vmcs12->vm_entry_intr_info_field;
11245 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
11246 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
11247 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
11248 bool should_have_error_code;
11249 bool urg = nested_cpu_has2(vmcs12,
11250 SECONDARY_EXEC_UNRESTRICTED_GUEST);
11251 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
11252
11253 /* VM-entry interruption-info field: interruption type */
11254 if (intr_type == INTR_TYPE_RESERVED ||
11255 (intr_type == INTR_TYPE_OTHER_EVENT &&
11256 !nested_cpu_supports_monitor_trap_flag(vcpu)))
11257 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11258
11259 /* VM-entry interruption-info field: vector */
11260 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
11261 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
11262 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
11263 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11264
11265 /* VM-entry interruption-info field: deliver error code */
11266 should_have_error_code =
11267 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
11268 x86_exception_has_error_code(vector);
11269 if (has_error_code != should_have_error_code)
11270 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11271
11272 /* VM-entry exception error code */
11273 if (has_error_code &&
11274 vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
11275 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11276
11277 /* VM-entry interruption-info field: reserved bits */
11278 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
11279 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11280
11281 /* VM-entry instruction length */
11282 switch (intr_type) {
11283 case INTR_TYPE_SOFT_EXCEPTION:
11284 case INTR_TYPE_SOFT_INTR:
11285 case INTR_TYPE_PRIV_SW_EXCEPTION:
11286 if ((vmcs12->vm_entry_instruction_len > 15) ||
11287 (vmcs12->vm_entry_instruction_len == 0 &&
11288 !nested_cpu_has_zero_length_injection(vcpu)))
11289 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11290 }
11291 }
11292
Sean Christopherson5b8ba412018-09-26 09:23:40 -070011293 if (nested_cpu_has_ept(vmcs12) &&
11294 !valid_ept_address(vcpu, vmcs12->ept_pointer))
11295 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11296
Jim Mattsonca0bde22016-11-30 12:03:46 -080011297 return 0;
11298}
11299
Liran Alonf145d902018-06-23 02:35:07 +030011300static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
11301 struct vmcs12 *vmcs12)
11302{
11303 int r;
11304 struct page *page;
11305 struct vmcs12 *shadow;
11306
11307 if (vmcs12->vmcs_link_pointer == -1ull)
11308 return 0;
11309
11310 if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
11311 return -EINVAL;
11312
11313 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
11314 if (is_error_page(page))
11315 return -EINVAL;
11316
11317 r = 0;
11318 shadow = kmap(page);
11319 if (shadow->hdr.revision_id != VMCS12_REVISION ||
11320 shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
11321 r = -EINVAL;
11322 kunmap(page);
11323 kvm_release_page_clean(page);
11324 return r;
11325}
11326
Jim Mattsonca0bde22016-11-30 12:03:46 -080011327static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11328 u32 *exit_qual)
11329{
11330 bool ia32e;
11331
11332 *exit_qual = ENTRY_FAIL_DEFAULT;
11333
11334 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
11335 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
11336 return 1;
11337
Liran Alonf145d902018-06-23 02:35:07 +030011338 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Jim Mattsonca0bde22016-11-30 12:03:46 -080011339 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
11340 return 1;
11341 }
11342
11343 /*
11344 * If the load IA32_EFER VM-entry control is 1, the following checks
11345 * are performed on the field for the IA32_EFER MSR:
11346 * - Bits reserved in the IA32_EFER MSR must be 0.
11347 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
11348 * the IA-32e mode guest VM-exit control. It must also be identical
11349 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
11350 * CR0.PG) is 1.
11351 */
11352 if (to_vmx(vcpu)->nested.nested_run_pending &&
11353 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
11354 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
11355 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
11356 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
11357 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
11358 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
11359 return 1;
11360 }
11361
Wanpeng Lif1b026a2017-11-05 16:54:48 -080011362 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
11363 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
11364 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
11365 return 1;
11366
Jim Mattsonca0bde22016-11-30 12:03:46 -080011367 return 0;
11368}
11369
Sean Christopherson52017602018-09-26 09:23:57 -070011370static int __noclone nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu)
11371{
11372 struct vcpu_vmx *vmx = to_vmx(vcpu);
11373 unsigned long cr3, cr4;
11374
11375 if (!nested_early_check)
11376 return 0;
11377
11378 if (vmx->msr_autoload.host.nr)
11379 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
11380 if (vmx->msr_autoload.guest.nr)
11381 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
11382
11383 preempt_disable();
11384
11385 vmx_prepare_switch_to_guest(vcpu);
11386
11387 /*
11388 * Induce a consistency check VMExit by clearing bit 1 in GUEST_RFLAGS,
11389 * which is reserved to '1' by hardware. GUEST_RFLAGS is guaranteed to
11390 * be written (by preparve_vmcs02()) before the "real" VMEnter, i.e.
11391 * there is no need to preserve other bits or save/restore the field.
11392 */
11393 vmcs_writel(GUEST_RFLAGS, 0);
11394
11395 vmcs_writel(HOST_RIP, vmx_early_consistency_check_return);
11396
11397 cr3 = __get_current_cr3_fast();
11398 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
11399 vmcs_writel(HOST_CR3, cr3);
11400 vmx->loaded_vmcs->host_state.cr3 = cr3;
11401 }
11402
11403 cr4 = cr4_read_shadow();
11404 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
11405 vmcs_writel(HOST_CR4, cr4);
11406 vmx->loaded_vmcs->host_state.cr4 = cr4;
11407 }
11408
11409 vmx->__launched = vmx->loaded_vmcs->launched;
11410
11411 asm(
11412 /* Set HOST_RSP */
Uros Bizjak4b1e5472018-10-11 19:40:44 +020011413 __ex("vmwrite %%" _ASM_SP ", %%" _ASM_DX) "\n\t"
Sean Christopherson52017602018-09-26 09:23:57 -070011414 "mov %%" _ASM_SP ", %c[host_rsp](%0)\n\t"
11415
Uros Bizjak00df9182018-10-23 00:09:11 +020011416 /* Check if vmlaunch or vmresume is needed */
Sean Christopherson52017602018-09-26 09:23:57 -070011417 "cmpl $0, %c[launched](%0)\n\t"
Uros Bizjak00df9182018-10-23 00:09:11 +020011418 "jne 1f\n\t"
11419 __ex("vmlaunch") "\n\t"
Sean Christopherson52017602018-09-26 09:23:57 -070011420 "jmp 2f\n\t"
Uros Bizjak00df9182018-10-23 00:09:11 +020011421 "1: " __ex("vmresume") "\n\t"
Sean Christopherson52017602018-09-26 09:23:57 -070011422 "2: "
Sean Christopherson52017602018-09-26 09:23:57 -070011423 /* Set vmx->fail accordingly */
11424 "setbe %c[fail](%0)\n\t"
11425
11426 ".pushsection .rodata\n\t"
11427 ".global vmx_early_consistency_check_return\n\t"
11428 "vmx_early_consistency_check_return: " _ASM_PTR " 2b\n\t"
11429 ".popsection"
11430 :
11431 : "c"(vmx), "d"((unsigned long)HOST_RSP),
11432 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
11433 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
11434 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp))
11435 : "rax", "cc", "memory"
11436 );
11437
11438 vmcs_writel(HOST_RIP, vmx_return);
11439
11440 preempt_enable();
11441
11442 if (vmx->msr_autoload.host.nr)
11443 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
11444 if (vmx->msr_autoload.guest.nr)
11445 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
11446
11447 if (vmx->fail) {
11448 WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
11449 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
11450 vmx->fail = 0;
11451 return 1;
11452 }
11453
11454 /*
11455 * VMExit clears RFLAGS.IF and DR7, even on a consistency check.
11456 */
11457 local_irq_enable();
11458 if (hw_breakpoint_active())
11459 set_debugreg(__this_cpu_read(cpu_dr7), 7);
11460
11461 /*
11462 * A non-failing VMEntry means we somehow entered guest mode with
11463 * an illegal RIP, and that's just the tip of the iceberg. There
11464 * is no telling what memory has been modified or what state has
11465 * been exposed to unknown code. Hitting this all but guarantees
11466 * a (very critical) hardware issue.
11467 */
11468 WARN_ON(!(vmcs_read32(VM_EXIT_REASON) &
11469 VMX_EXIT_REASONS_FAILED_VMENTRY));
11470
11471 return 0;
11472}
11473STACK_FRAME_NON_STANDARD(nested_vmx_check_vmentry_hw);
11474
Sean Christophersona633e412018-09-26 09:23:47 -070011475static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
11476 struct vmcs12 *vmcs12);
11477
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011478/*
Sean Christophersona633e412018-09-26 09:23:47 -070011479 * If from_vmentry is false, this is being called from state restore (either RSM
Jim Mattson8fcc4b52018-07-10 11:27:20 +020011480 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Sean Christopherson52017602018-09-26 09:23:57 -070011481+ *
11482+ * Returns:
11483+ * 0 - success, i.e. proceed with actual VMEnter
11484+ * 1 - consistency check VMExit
11485+ * -1 - consistency check VMFail
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011486 */
Sean Christophersona633e412018-09-26 09:23:47 -070011487static int nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu,
11488 bool from_vmentry)
Jim Mattson858e25c2016-11-30 12:03:47 -080011489{
11490 struct vcpu_vmx *vmx = to_vmx(vcpu);
11491 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzini7e712682018-10-03 13:44:26 +020011492 bool evaluate_pending_interrupts;
Sean Christophersona633e412018-09-26 09:23:47 -070011493 u32 exit_reason = EXIT_REASON_INVALID_STATE;
11494 u32 exit_qual;
Jim Mattson858e25c2016-11-30 12:03:47 -080011495
Paolo Bonzini7e712682018-10-03 13:44:26 +020011496 evaluate_pending_interrupts = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
11497 (CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_VIRTUAL_NMI_PENDING);
11498 if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu))
11499 evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu);
Liran Alonb5861e52018-09-03 15:20:22 +030011500
Jim Mattson858e25c2016-11-30 12:03:47 -080011501 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
11502 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
Liran Alon62cf9bd812018-09-14 03:25:54 +030011503 if (kvm_mpx_supported() &&
11504 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
11505 vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattson858e25c2016-11-30 12:03:47 -080011506
Jim Mattsonde3a0022017-11-27 17:22:25 -060011507 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080011508
Sean Christopherson16fb9a42018-09-26 09:23:52 -070011509 prepare_vmcs02_early(vmx, vmcs12);
Jim Mattson858e25c2016-11-30 12:03:47 -080011510
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011511 if (from_vmentry) {
11512 nested_get_vmcs12_pages(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080011513
Sean Christopherson52017602018-09-26 09:23:57 -070011514 if (nested_vmx_check_vmentry_hw(vcpu)) {
11515 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
11516 return -1;
11517 }
11518
Sean Christopherson16fb9a42018-09-26 09:23:52 -070011519 if (check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
11520 goto vmentry_fail_vmexit;
11521 }
11522
11523 enter_guest_mode(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080011524 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11525 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
11526
Sean Christophersona633e412018-09-26 09:23:47 -070011527 if (prepare_vmcs02(vcpu, vmcs12, &exit_qual))
Sean Christopherson39f9c382018-09-26 09:23:48 -070011528 goto vmentry_fail_vmexit_guest_mode;
Jim Mattson858e25c2016-11-30 12:03:47 -080011529
11530 if (from_vmentry) {
Sean Christophersona633e412018-09-26 09:23:47 -070011531 exit_reason = EXIT_REASON_MSR_LOAD_FAIL;
11532 exit_qual = nested_vmx_load_msr(vcpu,
11533 vmcs12->vm_entry_msr_load_addr,
11534 vmcs12->vm_entry_msr_load_count);
11535 if (exit_qual)
Sean Christopherson39f9c382018-09-26 09:23:48 -070011536 goto vmentry_fail_vmexit_guest_mode;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011537 } else {
11538 /*
11539 * The MMU is not initialized to point at the right entities yet and
11540 * "get pages" would need to read data from the guest (i.e. we will
11541 * need to perform gpa to hpa translation). Request a call
11542 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
11543 * have already been set at vmentry time and should not be reset.
11544 */
11545 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
11546 }
Jim Mattson858e25c2016-11-30 12:03:47 -080011547
Jim Mattson858e25c2016-11-30 12:03:47 -080011548 /*
Liran Alonb5861e52018-09-03 15:20:22 +030011549 * If L1 had a pending IRQ/NMI until it executed
11550 * VMLAUNCH/VMRESUME which wasn't delivered because it was
11551 * disallowed (e.g. interrupts disabled), L0 needs to
11552 * evaluate if this pending event should cause an exit from L2
11553 * to L1 or delivered directly to L2 (e.g. In case L1 don't
11554 * intercept EXTERNAL_INTERRUPT).
11555 *
Paolo Bonzini7e712682018-10-03 13:44:26 +020011556 * Usually this would be handled by the processor noticing an
11557 * IRQ/NMI window request, or checking RVI during evaluation of
11558 * pending virtual interrupts. However, this setting was done
11559 * on VMCS01 and now VMCS02 is active instead. Thus, we force L0
11560 * to perform pending event evaluation by requesting a KVM_REQ_EVENT.
Liran Alonb5861e52018-09-03 15:20:22 +030011561 */
Paolo Bonzini7e712682018-10-03 13:44:26 +020011562 if (unlikely(evaluate_pending_interrupts))
Liran Alonb5861e52018-09-03 15:20:22 +030011563 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alonb5861e52018-09-03 15:20:22 +030011564
11565 /*
Jim Mattson858e25c2016-11-30 12:03:47 -080011566 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
11567 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
11568 * returned as far as L1 is concerned. It will only return (and set
11569 * the success flag) when L2 exits (see nested_vmx_vmexit()).
11570 */
11571 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011572
Sean Christophersona633e412018-09-26 09:23:47 -070011573 /*
11574 * A failed consistency check that leads to a VMExit during L1's
11575 * VMEnter to L2 is a variation of a normal VMexit, as explained in
11576 * 26.7 "VM-entry failures during or after loading guest state".
11577 */
Sean Christopherson39f9c382018-09-26 09:23:48 -070011578vmentry_fail_vmexit_guest_mode:
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011579 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11580 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
11581 leave_guest_mode(vcpu);
Sean Christopherson16fb9a42018-09-26 09:23:52 -070011582
11583vmentry_fail_vmexit:
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011584 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Sean Christophersona633e412018-09-26 09:23:47 -070011585
11586 if (!from_vmentry)
11587 return 1;
11588
Sean Christophersona633e412018-09-26 09:23:47 -070011589 load_vmcs12_host_state(vcpu, vmcs12);
11590 vmcs12->vm_exit_reason = exit_reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
11591 vmcs12->exit_qualification = exit_qual;
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020011592 if (enable_shadow_vmcs || vmx->nested.hv_evmcs)
11593 vmx->nested.need_vmcs12_sync = true;
Sean Christophersona633e412018-09-26 09:23:47 -070011594 return 1;
Jim Mattson858e25c2016-11-30 12:03:47 -080011595}
11596
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011597/*
11598 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
11599 * for running an L2 nested guest.
11600 */
11601static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
11602{
11603 struct vmcs12 *vmcs12;
11604 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011605 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080011606 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011607
Kyle Hueyeb277562016-11-29 12:40:39 -080011608 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011609 return 1;
11610
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020011611 if (!nested_vmx_handle_enlightened_vmptrld(vcpu, true))
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +020011612 return 1;
11613
11614 if (!vmx->nested.hv_evmcs && vmx->nested.current_vmptr == -1ull)
Sean Christopherson09abb5e2018-09-26 09:23:55 -070011615 return nested_vmx_failInvalid(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -080011616
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011617 vmcs12 = get_vmcs12(vcpu);
11618
Liran Alona6192d42018-06-23 02:35:04 +030011619 /*
11620 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
11621 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
11622 * rather than RFLAGS.ZF, and no error number is stored to the
11623 * VM-instruction error field.
11624 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -070011625 if (vmcs12->hdr.shadow_vmcs)
11626 return nested_vmx_failInvalid(vcpu);
Liran Alona6192d42018-06-23 02:35:04 +030011627
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020011628 if (vmx->nested.hv_evmcs) {
11629 copy_enlightened_to_vmcs12(vmx);
11630 /* Enlightened VMCS doesn't have launch state */
11631 vmcs12->launch_state = !launch;
11632 } else if (enable_shadow_vmcs) {
Abel Gordon012f83c2013-04-18 14:39:25 +030011633 copy_shadow_to_vmcs12(vmx);
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020011634 }
Abel Gordon012f83c2013-04-18 14:39:25 +030011635
Nadav Har'El7c177932011-05-25 23:12:04 +030011636 /*
11637 * The nested entry process starts with enforcing various prerequisites
11638 * on vmcs12 as required by the Intel SDM, and act appropriately when
11639 * they fail: As the SDM explains, some conditions should cause the
11640 * instruction to fail, while others will cause the instruction to seem
11641 * to succeed, but return an EXIT_REASON_INVALID_STATE.
11642 * To speed up the normal (success) code path, we should avoid checking
11643 * for misconfigurations which will anyway be caught by the processor
11644 * when using the merged vmcs02.
11645 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -070011646 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS)
11647 return nested_vmx_failValid(vcpu,
11648 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011649
Sean Christopherson09abb5e2018-09-26 09:23:55 -070011650 if (vmcs12->launch_state == launch)
11651 return nested_vmx_failValid(vcpu,
Nadav Har'El7c177932011-05-25 23:12:04 +030011652 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
11653 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Nadav Har'El7c177932011-05-25 23:12:04 +030011654
Jim Mattsonca0bde22016-11-30 12:03:46 -080011655 ret = check_vmentry_prereqs(vcpu, vmcs12);
Sean Christopherson09abb5e2018-09-26 09:23:55 -070011656 if (ret)
11657 return nested_vmx_failValid(vcpu, ret);
Jan Kiszka384bb782013-04-20 10:52:36 +020011658
11659 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030011660 * We're finally done with prerequisite checking, and can start with
11661 * the nested entry.
11662 */
Jim Mattson6514dc32018-04-26 16:09:12 -070011663 vmx->nested.nested_run_pending = 1;
Sean Christophersona633e412018-09-26 09:23:47 -070011664 ret = nested_vmx_enter_non_root_mode(vcpu, true);
Sean Christopherson52017602018-09-26 09:23:57 -070011665 vmx->nested.nested_run_pending = !ret;
11666 if (ret > 0)
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011667 return 1;
Sean Christopherson52017602018-09-26 09:23:57 -070011668 else if (ret)
11669 return nested_vmx_failValid(vcpu,
11670 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wincy Vanff651cb2014-12-11 08:52:58 +030011671
Paolo Bonzinic595cee2018-07-02 13:07:14 +020011672 /* Hide L1D cache contents from the nested guest. */
11673 vmx->vcpu.arch.l1tf_flush_l1d = true;
11674
Chao Gao135a06c2018-02-11 10:06:30 +080011675 /*
Sean Christophersond63907d2018-09-26 09:23:45 -070011676 * Must happen outside of nested_vmx_enter_non_root_mode() as it will
Liran Alon61ada742018-06-23 02:35:08 +030011677 * also be used as part of restoring nVMX state for
11678 * snapshot restore (migration).
11679 *
11680 * In this flow, it is assumed that vmcs12 cache was
11681 * trasferred as part of captured nVMX state and should
11682 * therefore not be read from guest memory (which may not
11683 * exist on destination host yet).
11684 */
11685 nested_cache_shadow_vmcs12(vcpu, vmcs12);
11686
11687 /*
Chao Gao135a06c2018-02-11 10:06:30 +080011688 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
11689 * by event injection, halt vcpu.
11690 */
11691 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070011692 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
11693 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060011694 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070011695 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011696 return 1;
11697}
11698
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011699/*
11700 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
11701 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
11702 * This function returns the new value we should put in vmcs12.guest_cr0.
11703 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
11704 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
11705 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
11706 * didn't trap the bit, because if L1 did, so would L0).
11707 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
11708 * been modified by L2, and L1 knows it. So just leave the old value of
11709 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
11710 * isn't relevant, because if L0 traps this bit it can set it to anything.
11711 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
11712 * changed these bits, and therefore they need to be updated, but L0
11713 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
11714 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
11715 */
11716static inline unsigned long
11717vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11718{
11719 return
11720 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
11721 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
11722 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
11723 vcpu->arch.cr0_guest_owned_bits));
11724}
11725
11726static inline unsigned long
11727vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11728{
11729 return
11730 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
11731 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
11732 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
11733 vcpu->arch.cr4_guest_owned_bits));
11734}
11735
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011736static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
11737 struct vmcs12 *vmcs12)
11738{
11739 u32 idt_vectoring;
11740 unsigned int nr;
11741
Wanpeng Li664f8e22017-08-24 03:35:09 -070011742 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011743 nr = vcpu->arch.exception.nr;
11744 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11745
11746 if (kvm_exception_is_soft(nr)) {
11747 vmcs12->vm_exit_instruction_len =
11748 vcpu->arch.event_exit_inst_len;
11749 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
11750 } else
11751 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
11752
11753 if (vcpu->arch.exception.has_error_code) {
11754 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
11755 vmcs12->idt_vectoring_error_code =
11756 vcpu->arch.exception.error_code;
11757 }
11758
11759 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010011760 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011761 vmcs12->idt_vectoring_info_field =
11762 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030011763 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011764 nr = vcpu->arch.interrupt.nr;
11765 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11766
11767 if (vcpu->arch.interrupt.soft) {
11768 idt_vectoring |= INTR_TYPE_SOFT_INTR;
11769 vmcs12->vm_entry_instruction_len =
11770 vcpu->arch.event_exit_inst_len;
11771 } else
11772 idt_vectoring |= INTR_TYPE_EXT_INTR;
11773
11774 vmcs12->idt_vectoring_info_field = idt_vectoring;
11775 }
11776}
11777
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011778static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
11779{
11780 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011781 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020011782 bool block_nested_events =
11783 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080011784
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011785 if (vcpu->arch.exception.pending &&
11786 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020011787 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011788 return -EBUSY;
11789 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011790 return 0;
11791 }
11792
Jan Kiszkaf41245002014-03-07 20:03:13 +010011793 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
11794 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020011795 if (block_nested_events)
Jan Kiszkaf41245002014-03-07 20:03:13 +010011796 return -EBUSY;
11797 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
11798 return 0;
11799 }
11800
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011801 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020011802 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011803 return -EBUSY;
11804 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11805 NMI_VECTOR | INTR_TYPE_NMI_INTR |
11806 INTR_INFO_VALID_MASK, 0);
11807 /*
11808 * The NMI-triggered VM exit counts as injection:
11809 * clear this one and block further NMIs.
11810 */
11811 vcpu->arch.nmi_pending = 0;
11812 vmx_set_nmi_mask(vcpu, true);
11813 return 0;
11814 }
11815
11816 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
11817 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020011818 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011819 return -EBUSY;
11820 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080011821 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011822 }
11823
David Hildenbrand6342c502017-01-25 11:58:58 +010011824 vmx_complete_nested_posted_interrupt(vcpu);
11825 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011826}
11827
Sean Christophersond264ee02018-08-27 15:21:12 -070011828static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)
11829{
11830 to_vmx(vcpu)->req_immediate_exit = true;
11831}
11832
Jan Kiszkaf41245002014-03-07 20:03:13 +010011833static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
11834{
11835 ktime_t remaining =
11836 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
11837 u64 value;
11838
11839 if (ktime_to_ns(remaining) <= 0)
11840 return 0;
11841
11842 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
11843 do_div(value, 1000000);
11844 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11845}
11846
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011847/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011848 * Update the guest state fields of vmcs12 to reflect changes that
11849 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
11850 * VM-entry controls is also updated, since this is really a guest
11851 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011852 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011853static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011854{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011855 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
11856 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
11857
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011858 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
11859 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
11860 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
11861
11862 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
11863 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
11864 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
11865 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
11866 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
11867 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
11868 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
11869 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
11870 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
11871 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
11872 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
11873 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
11874 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
11875 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
11876 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
11877 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
11878 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
11879 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
11880 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
11881 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
11882 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
11883 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
11884 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
11885 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
11886 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
11887 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
11888 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
11889 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
11890 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
11891 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
11892 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
11893 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
11894 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
11895 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
11896 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
11897 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
11898
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011899 vmcs12->guest_interruptibility_info =
11900 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
11901 vmcs12->guest_pending_dbg_exceptions =
11902 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010011903 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
11904 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
11905 else
11906 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011907
Jan Kiszkaf41245002014-03-07 20:03:13 +010011908 if (nested_cpu_has_preemption_timer(vmcs12)) {
11909 if (vmcs12->vm_exit_controls &
11910 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
11911 vmcs12->vmx_preemption_timer_value =
11912 vmx_get_preemption_timer_value(vcpu);
11913 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
11914 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080011915
Nadav Har'El3633cfc2013-08-05 11:07:07 +030011916 /*
11917 * In some cases (usually, nested EPT), L2 is allowed to change its
11918 * own CR3 without exiting. If it has changed it, we must keep it.
11919 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
11920 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
11921 *
11922 * Additionally, restore L2's PDPTR to vmcs12.
11923 */
11924 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010011925 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030011926 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
11927 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
11928 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
11929 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
11930 }
11931
Jim Mattsond281e132017-06-01 12:44:46 -070011932 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030011933
Wincy Van608406e2015-02-03 23:57:51 +080011934 if (nested_cpu_has_vid(vmcs12))
11935 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
11936
Jan Kiszkac18911a2013-03-13 16:06:41 +010011937 vmcs12->vm_entry_controls =
11938 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020011939 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010011940
Jan Kiszka2996fca2014-06-16 13:59:43 +020011941 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
11942 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
11943 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11944 }
11945
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011946 /* TODO: These cannot have changed unless we have MSR bitmaps and
11947 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020011948 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011949 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020011950 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
11951 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011952 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
11953 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
11954 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010011955 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011956 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011957}
11958
11959/*
11960 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
11961 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
11962 * and this function updates it to reflect the changes to the guest state while
11963 * L2 was running (and perhaps made some exits which were handled directly by L0
11964 * without going back to L1), and to reflect the exit reason.
11965 * Note that we do not have to copy here all VMCS fields, just those that
11966 * could have changed by the L2 guest or the exit - i.e., the guest-state and
11967 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
11968 * which already writes to vmcs12 directly.
11969 */
11970static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11971 u32 exit_reason, u32 exit_intr_info,
11972 unsigned long exit_qualification)
11973{
11974 /* update guest state fields: */
11975 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011976
11977 /* update exit information fields: */
11978
Jan Kiszka533558b2014-01-04 18:47:20 +010011979 vmcs12->vm_exit_reason = exit_reason;
11980 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010011981 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020011982
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011983 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011984 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
11985 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
11986
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011987 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070011988 vmcs12->launch_state = 1;
11989
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011990 /* vm_entry_intr_info_field is cleared on exit. Emulate this
11991 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011992 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011993
11994 /*
11995 * Transfer the event that L0 or L1 may wanted to inject into
11996 * L2 to IDT_VECTORING_INFO_FIELD.
11997 */
11998 vmcs12_save_pending_event(vcpu, vmcs12);
11999 }
12000
12001 /*
12002 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
12003 * preserved above and would only end up incorrectly in L1.
12004 */
12005 vcpu->arch.nmi_injected = false;
12006 kvm_clear_exception_queue(vcpu);
12007 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012008}
12009
12010/*
12011 * A part of what we need to when the nested L2 guest exits and we want to
12012 * run its L1 parent, is to reset L1's guest state to the host state specified
12013 * in vmcs12.
12014 * This function is to be called not only on normal nested exit, but also on
12015 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
12016 * Failures During or After Loading Guest State").
12017 * This function should be called when the active VMCS is L1's (vmcs01).
12018 */
Jan Kiszka733568f2013-02-23 15:07:47 +010012019static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
12020 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012021{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012022 struct kvm_segment seg;
Sean Christophersonbd18bff2018-08-22 14:57:07 -070012023 u32 entry_failure_code;
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012024
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012025 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
12026 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020012027 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012028 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
12029 else
12030 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
12031 vmx_set_efer(vcpu, vcpu->arch.efer);
12032
12033 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
12034 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070012035 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Sean Christophersoncb61de22018-09-26 09:23:53 -070012036 vmx_set_interrupt_shadow(vcpu, 0);
12037
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012038 /*
12039 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012040 * actually changed, because vmx_set_cr0 refers to efer set above.
12041 *
12042 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
12043 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012044 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012045 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020012046 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012047
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012048 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012049 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080012050 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012051
Sean Christophersonbd18bff2018-08-22 14:57:07 -070012052 nested_ept_uninit_mmu_context(vcpu);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012053
Liran Alon6f1e03b2018-05-22 17:16:14 +030012054 /*
Sean Christophersonbd18bff2018-08-22 14:57:07 -070012055 * Only PDPTE load can fail as the value of cr3 was checked on entry and
12056 * couldn't have changed.
12057 */
12058 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
12059 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
12060
12061 if (!enable_ept)
12062 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
Jan Kiszka44811c02013-08-04 17:17:27 +020012063
Liran Alon6f1e03b2018-05-22 17:16:14 +030012064 /*
Liran Alonefebf0a2018-10-08 23:42:20 +030012065 * If vmcs01 doesn't use VPID, CPU flushes TLB on every
Liran Alon6f1e03b2018-05-22 17:16:14 +030012066 * VMEntry/VMExit. Thus, no need to flush TLB.
12067 *
Liran Alonefebf0a2018-10-08 23:42:20 +030012068 * If vmcs12 doesn't use VPID, L1 expects TLB to be
12069 * flushed on every VMEntry/VMExit.
Liran Alon6f1e03b2018-05-22 17:16:14 +030012070 *
Liran Alonefebf0a2018-10-08 23:42:20 +030012071 * Otherwise, we can preserve TLB entries as long as we are
12072 * able to tag L1 TLB entries differently than L2 TLB entries.
Liran Alon14389212018-10-08 23:42:17 +030012073 *
12074 * If vmcs12 uses EPT, we need to execute this flush on EPTP01
12075 * and therefore we request the TLB flush to happen only after VMCS EPTP
12076 * has been set by KVM_REQ_LOAD_CR3.
Liran Alon6f1e03b2018-05-22 17:16:14 +030012077 */
12078 if (enable_vpid &&
Liran Alonefebf0a2018-10-08 23:42:20 +030012079 (!nested_cpu_has_vpid(vmcs12) || !nested_has_guest_tlb_tag(vcpu))) {
Liran Alon14389212018-10-08 23:42:17 +030012080 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012081 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012082
12083 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
12084 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
12085 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
12086 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012087 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d5512017-10-11 16:54:42 +020012088 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
12089 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012090
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012091 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
12092 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
12093 vmcs_write64(GUEST_BNDCFGS, 0);
12094
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012095 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
12096 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
12097 vcpu->arch.pat = vmcs12->host_ia32_pat;
12098 }
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012099 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012100 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
12101 vmcs12->host_ia32_perf_global_ctrl);
12102
12103 /* Set L1 segment info according to Intel SDM
12104 27.5.2 Loading Host Segment and Descriptor-Table Registers */
12105 seg = (struct kvm_segment) {
12106 .base = 0,
12107 .limit = 0xFFFFFFFF,
12108 .selector = vmcs12->host_cs_selector,
12109 .type = 11,
12110 .present = 1,
12111 .s = 1,
12112 .g = 1
12113 };
12114 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
12115 seg.l = 1;
12116 else
12117 seg.db = 1;
12118 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
12119 seg = (struct kvm_segment) {
12120 .base = 0,
12121 .limit = 0xFFFFFFFF,
12122 .type = 3,
12123 .present = 1,
12124 .s = 1,
12125 .db = 1,
12126 .g = 1
12127 };
12128 seg.selector = vmcs12->host_ds_selector;
12129 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
12130 seg.selector = vmcs12->host_es_selector;
12131 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
12132 seg.selector = vmcs12->host_ss_selector;
12133 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
12134 seg.selector = vmcs12->host_fs_selector;
12135 seg.base = vmcs12->host_fs_base;
12136 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
12137 seg.selector = vmcs12->host_gs_selector;
Gleb Natapov205befd2013-08-04 15:08:06 +030012138 seg.base = vmcs12->host_gs_base;
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012139 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
12140 seg = (struct kvm_segment) {
12141 .base = vmcs12->host_tr_base,
12142 .limit = 0x67,
12143 .selector = vmcs12->host_tr_selector,
12144 .type = 11,
12145 .present = 1
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012146 };
12147 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012148
12149 kvm_set_dr(vcpu, 7, 0x400);
12150 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030012151
Wincy Van3af18d92015-02-03 23:49:31 +080012152 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010012153 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080012154
Wincy Vanff651cb2014-12-11 08:52:58 +030012155 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
12156 vmcs12->vm_exit_msr_load_count))
12157 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012158}
12159
Sean Christophersonbd18bff2018-08-22 14:57:07 -070012160static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx)
12161{
12162 struct shared_msr_entry *efer_msr;
12163 unsigned int i;
12164
12165 if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER)
12166 return vmcs_read64(GUEST_IA32_EFER);
12167
Sean Christophersonc73da3f2018-12-03 13:53:00 -080012168 if (cpu_has_load_ia32_efer())
Sean Christophersonbd18bff2018-08-22 14:57:07 -070012169 return host_efer;
12170
12171 for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) {
12172 if (vmx->msr_autoload.guest.val[i].index == MSR_EFER)
12173 return vmx->msr_autoload.guest.val[i].value;
12174 }
12175
12176 efer_msr = find_msr_entry(vmx, MSR_EFER);
12177 if (efer_msr)
12178 return efer_msr->data;
12179
12180 return host_efer;
12181}
12182
12183static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu)
12184{
12185 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12186 struct vcpu_vmx *vmx = to_vmx(vcpu);
12187 struct vmx_msr_entry g, h;
12188 struct msr_data msr;
12189 gpa_t gpa;
12190 u32 i, j;
12191
12192 vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT);
12193
12194 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
12195 /*
12196 * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set
12197 * as vmcs01.GUEST_DR7 contains a userspace defined value
12198 * and vcpu->arch.dr7 is not squirreled away before the
12199 * nested VMENTER (not worth adding a variable in nested_vmx).
12200 */
12201 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
12202 kvm_set_dr(vcpu, 7, DR7_FIXED_1);
12203 else
12204 WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7)));
12205 }
12206
12207 /*
12208 * Note that calling vmx_set_{efer,cr0,cr4} is important as they
12209 * handle a variety of side effects to KVM's software model.
12210 */
12211 vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx));
12212
12213 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
12214 vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW));
12215
12216 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
12217 vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW));
12218
12219 nested_ept_uninit_mmu_context(vcpu);
12220 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
12221 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
12222
12223 /*
12224 * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs
12225 * from vmcs01 (if necessary). The PDPTRs are not loaded on
12226 * VMFail, like everything else we just need to ensure our
12227 * software model is up-to-date.
12228 */
12229 ept_save_pdptrs(vcpu);
12230
12231 kvm_mmu_reset_context(vcpu);
12232
12233 if (cpu_has_vmx_msr_bitmap())
12234 vmx_update_msr_bitmap(vcpu);
12235
12236 /*
12237 * This nasty bit of open coding is a compromise between blindly
12238 * loading L1's MSRs using the exit load lists (incorrect emulation
12239 * of VMFail), leaving the nested VM's MSRs in the software model
12240 * (incorrect behavior) and snapshotting the modified MSRs (too
12241 * expensive since the lists are unbound by hardware). For each
12242 * MSR that was (prematurely) loaded from the nested VMEntry load
12243 * list, reload it from the exit load list if it exists and differs
12244 * from the guest value. The intent is to stuff host state as
12245 * silently as possible, not to fully process the exit load list.
12246 */
12247 msr.host_initiated = false;
12248 for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) {
12249 gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g));
12250 if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) {
12251 pr_debug_ratelimited(
12252 "%s read MSR index failed (%u, 0x%08llx)\n",
12253 __func__, i, gpa);
12254 goto vmabort;
12255 }
12256
12257 for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) {
12258 gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h));
12259 if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) {
12260 pr_debug_ratelimited(
12261 "%s read MSR failed (%u, 0x%08llx)\n",
12262 __func__, j, gpa);
12263 goto vmabort;
12264 }
12265 if (h.index != g.index)
12266 continue;
12267 if (h.value == g.value)
12268 break;
12269
12270 if (nested_vmx_load_msr_check(vcpu, &h)) {
12271 pr_debug_ratelimited(
12272 "%s check failed (%u, 0x%x, 0x%x)\n",
12273 __func__, j, h.index, h.reserved);
12274 goto vmabort;
12275 }
12276
12277 msr.index = h.index;
12278 msr.data = h.value;
12279 if (kvm_set_msr(vcpu, &msr)) {
12280 pr_debug_ratelimited(
12281 "%s WRMSR failed (%u, 0x%x, 0x%llx)\n",
12282 __func__, j, h.index, h.value);
12283 goto vmabort;
12284 }
12285 }
12286 }
12287
12288 return;
12289
12290vmabort:
12291 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
12292}
12293
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012294/*
12295 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
12296 * and modify vmcs12 to make it see what it would expect to see there if
12297 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
12298 */
Jan Kiszka533558b2014-01-04 18:47:20 +010012299static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
12300 u32 exit_intr_info,
12301 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012302{
12303 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012304 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12305
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012306 /* trying to cancel vmlaunch/vmresume is a bug */
12307 WARN_ON_ONCE(vmx->nested.nested_run_pending);
12308
Jim Mattson4f350c62017-09-14 16:31:44 -070012309 leave_guest_mode(vcpu);
12310
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012311 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12312 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12313
Jim Mattson4f350c62017-09-14 16:31:44 -070012314 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012315 if (exit_reason == -1)
12316 sync_vmcs12(vcpu, vmcs12);
12317 else
12318 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
12319 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070012320
Liran Alon61ada742018-06-23 02:35:08 +030012321 /*
12322 * Must happen outside of sync_vmcs12() as it will
12323 * also be used to capture vmcs12 cache as part of
12324 * capturing nVMX state for snapshot (migration).
12325 *
12326 * Otherwise, this flush will dirty guest memory at a
12327 * point it is already assumed by user-space to be
12328 * immutable.
12329 */
12330 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
12331
Jim Mattson4f350c62017-09-14 16:31:44 -070012332 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
12333 vmcs12->vm_exit_msr_store_count))
12334 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Sean Christopherson2768c0c2018-09-26 09:23:58 -070012335 } else {
12336 /*
12337 * The only expected VM-instruction error is "VM entry with
12338 * invalid control field(s)." Anything else indicates a
12339 * problem with L0. And we should never get here with a
12340 * VMFail of any type if early consistency checks are enabled.
12341 */
12342 WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
12343 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
12344 WARN_ON_ONCE(nested_early_check);
Bandan Das77b0f5d2014-04-19 18:17:45 -040012345 }
12346
Jim Mattson4f350c62017-09-14 16:31:44 -070012347 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010012348
Paolo Bonzini9314006db2016-07-06 13:23:51 +020012349 /* Update any VMCS fields that might have changed while L2 ran */
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040012350 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
12351 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010012352 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Sean Christophersonf459a702018-08-27 15:21:11 -070012353
Peter Feinerc95ba922016-08-17 09:36:47 -070012354 if (kvm_has_tsc_control)
12355 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012356
Jim Mattson8d860bb2018-05-09 16:56:05 -040012357 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
12358 vmx->nested.change_vmcs01_virtual_apic_mode = false;
12359 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012360 } else if (!nested_cpu_has_ept(vmcs12) &&
12361 nested_cpu_has2(vmcs12,
12362 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070012363 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020012364 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012365
12366 /* This is needed for same reason as it was needed in prepare_vmcs02 */
12367 vmx->host_rsp = 0;
12368
12369 /* Unpin physical memory we referred to in vmcs02 */
12370 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012371 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012372 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012373 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012374 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012375 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012376 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012377 }
Wincy Van705699a2015-02-03 23:58:17 +080012378 if (vmx->nested.pi_desc_page) {
12379 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012380 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080012381 vmx->nested.pi_desc_page = NULL;
12382 vmx->nested.pi_desc = NULL;
12383 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012384
12385 /*
Tang Chen38b99172014-09-24 15:57:54 +080012386 * We are now running in L2, mmu_notifier will force to reload the
12387 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
12388 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080012389 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080012390
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020012391 if ((exit_reason != -1) && (enable_shadow_vmcs || vmx->nested.hv_evmcs))
12392 vmx->nested.need_vmcs12_sync = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012393
12394 /* in case we halted in L2 */
12395 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070012396
12397 if (likely(!vmx->fail)) {
12398 /*
12399 * TODO: SDM says that with acknowledge interrupt on
12400 * exit, bit 31 of the VM-exit interrupt information
12401 * (valid interrupt) is always set to 1 on
12402 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
12403 * need kvm_cpu_has_interrupt(). See the commit
12404 * message for details.
12405 */
12406 if (nested_exit_intr_ack_set(vcpu) &&
12407 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
12408 kvm_cpu_has_interrupt(vcpu)) {
12409 int irq = kvm_cpu_get_interrupt(vcpu);
12410 WARN_ON(irq < 0);
12411 vmcs12->vm_exit_intr_info = irq |
12412 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
12413 }
12414
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012415 if (exit_reason != -1)
12416 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
12417 vmcs12->exit_qualification,
12418 vmcs12->idt_vectoring_info_field,
12419 vmcs12->vm_exit_intr_info,
12420 vmcs12->vm_exit_intr_error_code,
12421 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070012422
12423 load_vmcs12_host_state(vcpu, vmcs12);
12424
12425 return;
12426 }
Sean Christopherson09abb5e2018-09-26 09:23:55 -070012427
Jim Mattson4f350c62017-09-14 16:31:44 -070012428 /*
12429 * After an early L2 VM-entry failure, we're now back
12430 * in L1 which thinks it just finished a VMLAUNCH or
12431 * VMRESUME instruction, so we need to set the failure
12432 * flag and the VM-instruction error field of the VMCS
Sean Christophersoncb61de22018-09-26 09:23:53 -070012433 * accordingly, and skip the emulated instruction.
Jim Mattson4f350c62017-09-14 16:31:44 -070012434 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -070012435 (void)nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080012436
Jim Mattson4f350c62017-09-14 16:31:44 -070012437 /*
Sean Christophersonbd18bff2018-08-22 14:57:07 -070012438 * Restore L1's host state to KVM's software model. We're here
12439 * because a consistency check was caught by hardware, which
12440 * means some amount of guest state has been propagated to KVM's
12441 * model and needs to be unwound to the host's state.
Jim Mattson4f350c62017-09-14 16:31:44 -070012442 */
Sean Christophersonbd18bff2018-08-22 14:57:07 -070012443 nested_vmx_restore_host_state(vcpu);
Jim Mattson4f350c62017-09-14 16:31:44 -070012444
Jim Mattson4f350c62017-09-14 16:31:44 -070012445 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012446}
12447
Nadav Har'El7c177932011-05-25 23:12:04 +030012448/*
Jan Kiszka42124922014-01-04 18:47:19 +010012449 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
12450 */
12451static void vmx_leave_nested(struct kvm_vcpu *vcpu)
12452{
Wanpeng Li2f707d92017-03-06 04:03:28 -080012453 if (is_guest_mode(vcpu)) {
12454 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010012455 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080012456 }
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +020012457 free_nested(vcpu);
Nadav Har'El7c177932011-05-25 23:12:04 +030012458}
12459
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012460static int vmx_check_intercept(struct kvm_vcpu *vcpu,
12461 struct x86_instruction_info *info,
12462 enum x86_intercept_stage stage)
12463{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020012464 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12465 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
12466
12467 /*
12468 * RDPID causes #UD if disabled through secondary execution controls.
12469 * Because it is marked as EmulateOnUD, we need to intercept it here.
12470 */
12471 if (info->intercept == x86_intercept_rdtscp &&
12472 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
12473 ctxt->exception.vector = UD_VECTOR;
12474 ctxt->exception.error_code_valid = false;
12475 return X86EMUL_PROPAGATE_FAULT;
12476 }
12477
12478 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012479 return X86EMUL_CONTINUE;
12480}
12481
Yunhong Jiang64672c92016-06-13 14:19:59 -070012482#ifdef CONFIG_X86_64
12483/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
12484static inline int u64_shl_div_u64(u64 a, unsigned int shift,
12485 u64 divisor, u64 *result)
12486{
12487 u64 low = a << shift, high = a >> (64 - shift);
12488
12489 /* To avoid the overflow on divq */
12490 if (high >= divisor)
12491 return 1;
12492
12493 /* Low hold the result, high hold rem which is discarded */
12494 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
12495 "rm" (divisor), "0" (low), "1" (high));
12496 *result = low;
12497
12498 return 0;
12499}
12500
12501static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
12502{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012503 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012504 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012505
12506 if (kvm_mwait_in_guest(vcpu->kvm))
12507 return -EOPNOTSUPP;
12508
12509 vmx = to_vmx(vcpu);
12510 tscl = rdtsc();
12511 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
12512 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012513 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
12514
12515 if (delta_tsc > lapic_timer_advance_cycles)
12516 delta_tsc -= lapic_timer_advance_cycles;
12517 else
12518 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012519
12520 /* Convert to host delta tsc if tsc scaling is enabled */
12521 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
12522 u64_shl_div_u64(delta_tsc,
12523 kvm_tsc_scaling_ratio_frac_bits,
12524 vcpu->arch.tsc_scaling_ratio,
12525 &delta_tsc))
12526 return -ERANGE;
12527
12528 /*
12529 * If the delta tsc can't fit in the 32 bit after the multi shift,
12530 * we can't use the preemption timer.
12531 * It's possible that it fits on later vmentries, but checking
12532 * on every vmentry is costly so we just use an hrtimer.
12533 */
12534 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
12535 return -ERANGE;
12536
12537 vmx->hv_deadline_tsc = tscl + delta_tsc;
Wanpeng Lic8533542017-06-29 06:28:09 -070012538 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012539}
12540
12541static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
12542{
Sean Christophersonf459a702018-08-27 15:21:11 -070012543 to_vmx(vcpu)->hv_deadline_tsc = -1;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012544}
12545#endif
12546
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012547static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012548{
Wanpeng Lib31c1142018-03-12 04:53:04 -070012549 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020012550 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012551}
12552
Kai Huang843e4332015-01-28 10:54:28 +080012553static void vmx_slot_enable_log_dirty(struct kvm *kvm,
12554 struct kvm_memory_slot *slot)
12555{
12556 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
12557 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
12558}
12559
12560static void vmx_slot_disable_log_dirty(struct kvm *kvm,
12561 struct kvm_memory_slot *slot)
12562{
12563 kvm_mmu_slot_set_dirty(kvm, slot);
12564}
12565
12566static void vmx_flush_log_dirty(struct kvm *kvm)
12567{
12568 kvm_flush_pml_buffers(kvm);
12569}
12570
Bandan Dasc5f983f2017-05-05 15:25:14 -040012571static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
12572{
12573 struct vmcs12 *vmcs12;
12574 struct vcpu_vmx *vmx = to_vmx(vcpu);
12575 gpa_t gpa;
12576 struct page *page = NULL;
12577 u64 *pml_address;
12578
12579 if (is_guest_mode(vcpu)) {
12580 WARN_ON_ONCE(vmx->nested.pml_full);
12581
12582 /*
12583 * Check if PML is enabled for the nested guest.
12584 * Whether eptp bit 6 is set is already checked
12585 * as part of A/D emulation.
12586 */
12587 vmcs12 = get_vmcs12(vcpu);
12588 if (!nested_cpu_has_pml(vmcs12))
12589 return 0;
12590
Dan Carpenter47698862017-05-10 22:43:17 +030012591 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040012592 vmx->nested.pml_full = true;
12593 return 1;
12594 }
12595
12596 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
12597
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020012598 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
12599 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040012600 return 0;
12601
12602 pml_address = kmap(page);
12603 pml_address[vmcs12->guest_pml_index--] = gpa;
12604 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012605 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040012606 }
12607
12608 return 0;
12609}
12610
Kai Huang843e4332015-01-28 10:54:28 +080012611static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
12612 struct kvm_memory_slot *memslot,
12613 gfn_t offset, unsigned long mask)
12614{
12615 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
12616}
12617
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012618static void __pi_post_block(struct kvm_vcpu *vcpu)
12619{
12620 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12621 struct pi_desc old, new;
12622 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012623
12624 do {
12625 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012626 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
12627 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012628
12629 dest = cpu_physical_id(vcpu->cpu);
12630
12631 if (x2apic_enabled())
12632 new.ndst = dest;
12633 else
12634 new.ndst = (dest << 8) & 0xFF00;
12635
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012636 /* set 'NV' to 'notification vector' */
12637 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012638 } while (cmpxchg64(&pi_desc->control, old.control,
12639 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012640
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012641 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
12642 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012643 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012644 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012645 vcpu->pre_pcpu = -1;
12646 }
12647}
12648
Feng Wuefc64402015-09-18 22:29:51 +080012649/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080012650 * This routine does the following things for vCPU which is going
12651 * to be blocked if VT-d PI is enabled.
12652 * - Store the vCPU to the wakeup list, so when interrupts happen
12653 * we can find the right vCPU to wake up.
12654 * - Change the Posted-interrupt descriptor as below:
12655 * 'NDST' <-- vcpu->pre_pcpu
12656 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
12657 * - If 'ON' is set during this process, which means at least one
12658 * interrupt is posted for this vCPU, we cannot block it, in
12659 * this case, return 1, otherwise, return 0.
12660 *
12661 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070012662static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012663{
Feng Wubf9f6ac2015-09-18 22:29:55 +080012664 unsigned int dest;
12665 struct pi_desc old, new;
12666 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12667
12668 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012669 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12670 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080012671 return 0;
12672
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012673 WARN_ON(irqs_disabled());
12674 local_irq_disable();
12675 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
12676 vcpu->pre_pcpu = vcpu->cpu;
12677 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12678 list_add_tail(&vcpu->blocked_vcpu_list,
12679 &per_cpu(blocked_vcpu_on_cpu,
12680 vcpu->pre_pcpu));
12681 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12682 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080012683
12684 do {
12685 old.control = new.control = pi_desc->control;
12686
Feng Wubf9f6ac2015-09-18 22:29:55 +080012687 WARN((pi_desc->sn == 1),
12688 "Warning: SN field of posted-interrupts "
12689 "is set before blocking\n");
12690
12691 /*
12692 * Since vCPU can be preempted during this process,
12693 * vcpu->cpu could be different with pre_pcpu, we
12694 * need to set pre_pcpu as the destination of wakeup
12695 * notification event, then we can find the right vCPU
12696 * to wakeup in wakeup handler if interrupts happen
12697 * when the vCPU is in blocked state.
12698 */
12699 dest = cpu_physical_id(vcpu->pre_pcpu);
12700
12701 if (x2apic_enabled())
12702 new.ndst = dest;
12703 else
12704 new.ndst = (dest << 8) & 0xFF00;
12705
12706 /* set 'NV' to 'wakeup vector' */
12707 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012708 } while (cmpxchg64(&pi_desc->control, old.control,
12709 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012710
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012711 /* We should not block the vCPU if an interrupt is posted for it. */
12712 if (pi_test_on(pi_desc) == 1)
12713 __pi_post_block(vcpu);
12714
12715 local_irq_enable();
12716 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012717}
12718
Yunhong Jiangbc225122016-06-13 14:19:58 -070012719static int vmx_pre_block(struct kvm_vcpu *vcpu)
12720{
12721 if (pi_pre_block(vcpu))
12722 return 1;
12723
Yunhong Jiang64672c92016-06-13 14:19:59 -070012724 if (kvm_lapic_hv_timer_in_use(vcpu))
12725 kvm_lapic_switch_to_sw_timer(vcpu);
12726
Yunhong Jiangbc225122016-06-13 14:19:58 -070012727 return 0;
12728}
12729
12730static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012731{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012732 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012733 return;
12734
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012735 WARN_ON(irqs_disabled());
12736 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012737 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012738 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080012739}
12740
Yunhong Jiangbc225122016-06-13 14:19:58 -070012741static void vmx_post_block(struct kvm_vcpu *vcpu)
12742{
Yunhong Jiang64672c92016-06-13 14:19:59 -070012743 if (kvm_x86_ops->set_hv_timer)
12744 kvm_lapic_switch_to_hv_timer(vcpu);
12745
Yunhong Jiangbc225122016-06-13 14:19:58 -070012746 pi_post_block(vcpu);
12747}
12748
Feng Wubf9f6ac2015-09-18 22:29:55 +080012749/*
Feng Wuefc64402015-09-18 22:29:51 +080012750 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
12751 *
12752 * @kvm: kvm
12753 * @host_irq: host irq of the interrupt
12754 * @guest_irq: gsi of the interrupt
12755 * @set: set or unset PI
12756 * returns 0 on success, < 0 on failure
12757 */
12758static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
12759 uint32_t guest_irq, bool set)
12760{
12761 struct kvm_kernel_irq_routing_entry *e;
12762 struct kvm_irq_routing_table *irq_rt;
12763 struct kvm_lapic_irq irq;
12764 struct kvm_vcpu *vcpu;
12765 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012766 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080012767
12768 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012769 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12770 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080012771 return 0;
12772
12773 idx = srcu_read_lock(&kvm->irq_srcu);
12774 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012775 if (guest_irq >= irq_rt->nr_rt_entries ||
12776 hlist_empty(&irq_rt->map[guest_irq])) {
12777 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
12778 guest_irq, irq_rt->nr_rt_entries);
12779 goto out;
12780 }
Feng Wuefc64402015-09-18 22:29:51 +080012781
12782 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
12783 if (e->type != KVM_IRQ_ROUTING_MSI)
12784 continue;
12785 /*
12786 * VT-d PI cannot support posting multicast/broadcast
12787 * interrupts to a vCPU, we still use interrupt remapping
12788 * for these kind of interrupts.
12789 *
12790 * For lowest-priority interrupts, we only support
12791 * those with single CPU as the destination, e.g. user
12792 * configures the interrupts via /proc/irq or uses
12793 * irqbalance to make the interrupts single-CPU.
12794 *
12795 * We will support full lowest-priority interrupt later.
12796 */
12797
Radim Krčmář371313132016-07-12 22:09:27 +020012798 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080012799 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
12800 /*
12801 * Make sure the IRTE is in remapped mode if
12802 * we don't handle it in posted mode.
12803 */
12804 ret = irq_set_vcpu_affinity(host_irq, NULL);
12805 if (ret < 0) {
12806 printk(KERN_INFO
12807 "failed to back to remapped mode, irq: %u\n",
12808 host_irq);
12809 goto out;
12810 }
12811
Feng Wuefc64402015-09-18 22:29:51 +080012812 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080012813 }
Feng Wuefc64402015-09-18 22:29:51 +080012814
12815 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
12816 vcpu_info.vector = irq.vector;
12817
hu huajun2698d822018-04-11 15:16:40 +080012818 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080012819 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
12820
12821 if (set)
12822 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2eb2017-09-18 09:56:49 +080012823 else
Feng Wuefc64402015-09-18 22:29:51 +080012824 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080012825
12826 if (ret < 0) {
12827 printk(KERN_INFO "%s: failed to update PI IRTE\n",
12828 __func__);
12829 goto out;
12830 }
12831 }
12832
12833 ret = 0;
12834out:
12835 srcu_read_unlock(&kvm->irq_srcu, idx);
12836 return ret;
12837}
12838
Ashok Rajc45dcc72016-06-22 14:59:56 +080012839static void vmx_setup_mce(struct kvm_vcpu *vcpu)
12840{
12841 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
12842 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
12843 FEATURE_CONTROL_LMCE;
12844 else
12845 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
12846 ~FEATURE_CONTROL_LMCE;
12847}
12848
Ladi Prosek72d7b372017-10-11 16:54:41 +020012849static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
12850{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012851 /* we need a nested vmexit to enter SMM, postpone if run is pending */
12852 if (to_vmx(vcpu)->nested.nested_run_pending)
12853 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020012854 return 1;
12855}
12856
Ladi Prosek0234bf82017-10-11 16:54:40 +020012857static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
12858{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012859 struct vcpu_vmx *vmx = to_vmx(vcpu);
12860
12861 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
12862 if (vmx->nested.smm.guest_mode)
12863 nested_vmx_vmexit(vcpu, -1, 0, 0);
12864
12865 vmx->nested.smm.vmxon = vmx->nested.vmxon;
12866 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070012867 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020012868 return 0;
12869}
12870
12871static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
12872{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012873 struct vcpu_vmx *vmx = to_vmx(vcpu);
12874 int ret;
12875
12876 if (vmx->nested.smm.vmxon) {
12877 vmx->nested.vmxon = true;
12878 vmx->nested.smm.vmxon = false;
12879 }
12880
12881 if (vmx->nested.smm.guest_mode) {
12882 vcpu->arch.hflags &= ~HF_SMM_MASK;
Sean Christophersona633e412018-09-26 09:23:47 -070012883 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012884 vcpu->arch.hflags |= HF_SMM_MASK;
12885 if (ret)
12886 return ret;
12887
12888 vmx->nested.smm.guest_mode = false;
12889 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020012890 return 0;
12891}
12892
Ladi Prosekcc3d9672017-10-17 16:02:39 +020012893static int enable_smi_window(struct kvm_vcpu *vcpu)
12894{
12895 return 0;
12896}
12897
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020012898static inline int vmx_has_valid_vmcs12(struct kvm_vcpu *vcpu)
12899{
12900 struct vcpu_vmx *vmx = to_vmx(vcpu);
12901
12902 /*
12903 * In case we do two consecutive get/set_nested_state()s while L2 was
12904 * running hv_evmcs may end up not being mapped (we map it from
12905 * nested_vmx_run()/vmx_vcpu_run()). Check is_guest_mode() as we always
12906 * have vmcs12 if it is true.
12907 */
12908 return is_guest_mode(vcpu) || vmx->nested.current_vmptr != -1ull ||
12909 vmx->nested.hv_evmcs;
12910}
12911
Jim Mattson8fcc4b52018-07-10 11:27:20 +020012912static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
12913 struct kvm_nested_state __user *user_kvm_nested_state,
12914 u32 user_data_size)
12915{
12916 struct vcpu_vmx *vmx;
12917 struct vmcs12 *vmcs12;
12918 struct kvm_nested_state kvm_state = {
12919 .flags = 0,
12920 .format = 0,
12921 .size = sizeof(kvm_state),
12922 .vmx.vmxon_pa = -1ull,
12923 .vmx.vmcs_pa = -1ull,
12924 };
12925
12926 if (!vcpu)
12927 return kvm_state.size + 2 * VMCS12_SIZE;
12928
12929 vmx = to_vmx(vcpu);
12930 vmcs12 = get_vmcs12(vcpu);
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020012931
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020012932 if (nested_vmx_allowed(vcpu) && vmx->nested.enlightened_vmcs_enabled)
12933 kvm_state.flags |= KVM_STATE_NESTED_EVMCS;
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020012934
Jim Mattson8fcc4b52018-07-10 11:27:20 +020012935 if (nested_vmx_allowed(vcpu) &&
12936 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
12937 kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
12938 kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
12939
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020012940 if (vmx_has_valid_vmcs12(vcpu)) {
Jim Mattson8fcc4b52018-07-10 11:27:20 +020012941 kvm_state.size += VMCS12_SIZE;
12942
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020012943 if (is_guest_mode(vcpu) &&
12944 nested_cpu_has_shadow_vmcs(vmcs12) &&
12945 vmcs12->vmcs_link_pointer != -1ull)
12946 kvm_state.size += VMCS12_SIZE;
12947 }
12948
Jim Mattson8fcc4b52018-07-10 11:27:20 +020012949 if (vmx->nested.smm.vmxon)
12950 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
12951
12952 if (vmx->nested.smm.guest_mode)
12953 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
12954
12955 if (is_guest_mode(vcpu)) {
12956 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
12957
12958 if (vmx->nested.nested_run_pending)
12959 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
12960 }
12961 }
12962
12963 if (user_data_size < kvm_state.size)
12964 goto out;
12965
12966 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
12967 return -EFAULT;
12968
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020012969 if (!vmx_has_valid_vmcs12(vcpu))
Jim Mattson8fcc4b52018-07-10 11:27:20 +020012970 goto out;
12971
12972 /*
12973 * When running L2, the authoritative vmcs12 state is in the
12974 * vmcs02. When running L1, the authoritative vmcs12 state is
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020012975 * in the shadow or enlightened vmcs linked to vmcs01, unless
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020012976 * need_vmcs12_sync is set, in which case, the authoritative
Jim Mattson8fcc4b52018-07-10 11:27:20 +020012977 * vmcs12 state is in the vmcs12 already.
12978 */
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020012979 if (is_guest_mode(vcpu)) {
Jim Mattson8fcc4b52018-07-10 11:27:20 +020012980 sync_vmcs12(vcpu, vmcs12);
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020012981 } else if (!vmx->nested.need_vmcs12_sync) {
12982 if (vmx->nested.hv_evmcs)
12983 copy_enlightened_to_vmcs12(vmx);
12984 else if (enable_shadow_vmcs)
12985 copy_shadow_to_vmcs12(vmx);
12986 }
Jim Mattson8fcc4b52018-07-10 11:27:20 +020012987
12988 if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
12989 return -EFAULT;
12990
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020012991 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
12992 vmcs12->vmcs_link_pointer != -1ull) {
12993 if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
12994 get_shadow_vmcs12(vcpu), sizeof(*vmcs12)))
12995 return -EFAULT;
12996 }
12997
Jim Mattson8fcc4b52018-07-10 11:27:20 +020012998out:
12999 return kvm_state.size;
13000}
13001
13002static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
13003 struct kvm_nested_state __user *user_kvm_nested_state,
13004 struct kvm_nested_state *kvm_state)
13005{
13006 struct vcpu_vmx *vmx = to_vmx(vcpu);
13007 struct vmcs12 *vmcs12;
13008 u32 exit_qual;
13009 int ret;
13010
13011 if (kvm_state->format != 0)
13012 return -EINVAL;
13013
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020013014 if (kvm_state->flags & KVM_STATE_NESTED_EVMCS)
13015 nested_enable_evmcs(vcpu, NULL);
13016
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013017 if (!nested_vmx_allowed(vcpu))
13018 return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
13019
13020 if (kvm_state->vmx.vmxon_pa == -1ull) {
13021 if (kvm_state->vmx.smm.flags)
13022 return -EINVAL;
13023
13024 if (kvm_state->vmx.vmcs_pa != -1ull)
13025 return -EINVAL;
13026
13027 vmx_leave_nested(vcpu);
13028 return 0;
13029 }
13030
13031 if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
13032 return -EINVAL;
13033
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013034 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
13035 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
13036 return -EINVAL;
13037
13038 if (kvm_state->vmx.smm.flags &
13039 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
13040 return -EINVAL;
13041
Paolo Bonzini5bea5122018-09-18 15:19:17 +020013042 /*
13043 * SMM temporarily disables VMX, so we cannot be in guest mode,
13044 * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
13045 * must be zero.
13046 */
13047 if (is_smm(vcpu) ? kvm_state->flags : kvm_state->vmx.smm.flags)
13048 return -EINVAL;
13049
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013050 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
13051 !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
13052 return -EINVAL;
13053
13054 vmx_leave_nested(vcpu);
13055 if (kvm_state->vmx.vmxon_pa == -1ull)
13056 return 0;
13057
13058 vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
13059 ret = enter_vmx_operation(vcpu);
13060 if (ret)
13061 return ret;
13062
Vitaly Kuznetsova1b0c1c2018-10-16 18:50:07 +020013063 /* Empty 'VMXON' state is permitted */
13064 if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
13065 return 0;
13066
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020013067 if (kvm_state->vmx.vmcs_pa != -1ull) {
13068 if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
13069 !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
13070 return -EINVAL;
Vitaly Kuznetsova1b0c1c2018-10-16 18:50:07 +020013071
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020013072 set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
13073 } else if (kvm_state->flags & KVM_STATE_NESTED_EVMCS) {
13074 /*
13075 * Sync eVMCS upon entry as we may not have
13076 * HV_X64_MSR_VP_ASSIST_PAGE set up yet.
13077 */
13078 vmx->nested.need_vmcs12_sync = true;
13079 } else {
13080 return -EINVAL;
13081 }
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013082
13083 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
13084 vmx->nested.smm.vmxon = true;
13085 vmx->nested.vmxon = false;
13086
13087 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
13088 vmx->nested.smm.guest_mode = true;
13089 }
13090
13091 vmcs12 = get_vmcs12(vcpu);
13092 if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
13093 return -EFAULT;
13094
Liran Alon392b2f22018-06-23 02:35:01 +030013095 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013096 return -EINVAL;
13097
13098 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
13099 return 0;
13100
13101 vmx->nested.nested_run_pending =
13102 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
13103
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013104 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
13105 vmcs12->vmcs_link_pointer != -1ull) {
13106 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
13107 if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12))
13108 return -EINVAL;
13109
13110 if (copy_from_user(shadow_vmcs12,
13111 user_kvm_nested_state->data + VMCS12_SIZE,
13112 sizeof(*vmcs12)))
13113 return -EFAULT;
13114
13115 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
13116 !shadow_vmcs12->hdr.shadow_vmcs)
13117 return -EINVAL;
13118 }
13119
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013120 if (check_vmentry_prereqs(vcpu, vmcs12) ||
13121 check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
13122 return -EINVAL;
13123
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013124 vmx->nested.dirty_vmcs12 = true;
Sean Christophersona633e412018-09-26 09:23:47 -070013125 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013126 if (ret)
13127 return -EINVAL;
13128
13129 return 0;
13130}
13131
Kees Cook404f6aa2016-08-08 16:29:06 -070013132static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080013133 .cpu_has_kvm_support = cpu_has_kvm_support,
13134 .disabled_by_bios = vmx_disabled_by_bios,
13135 .hardware_setup = hardware_setup,
13136 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030013137 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013138 .hardware_enable = hardware_enable,
13139 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080013140 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020013141 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013142
Wanpeng Lib31c1142018-03-12 04:53:04 -070013143 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070013144 .vm_alloc = vmx_vm_alloc,
13145 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070013146
Avi Kivity6aa8b732006-12-10 02:21:36 -080013147 .vcpu_create = vmx_create_vcpu,
13148 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030013149 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013150
Sean Christopherson6d6095b2018-07-23 12:32:44 -070013151 .prepare_guest_switch = vmx_prepare_switch_to_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013152 .vcpu_load = vmx_vcpu_load,
13153 .vcpu_put = vmx_vcpu_put,
13154
Paolo Bonzinia96036b2015-11-10 11:55:36 +010013155 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060013156 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013157 .get_msr = vmx_get_msr,
13158 .set_msr = vmx_set_msr,
13159 .get_segment_base = vmx_get_segment_base,
13160 .get_segment = vmx_get_segment,
13161 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020013162 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013163 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020013164 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020013165 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030013166 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013167 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013168 .set_cr3 = vmx_set_cr3,
13169 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013170 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013171 .get_idt = vmx_get_idt,
13172 .set_idt = vmx_set_idt,
13173 .get_gdt = vmx_get_gdt,
13174 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010013175 .get_dr6 = vmx_get_dr6,
13176 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030013177 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010013178 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030013179 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013180 .get_rflags = vmx_get_rflags,
13181 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080013182
Avi Kivity6aa8b732006-12-10 02:21:36 -080013183 .tlb_flush = vmx_flush_tlb,
Junaid Shahidfaff8752018-06-29 13:10:05 -070013184 .tlb_flush_gva = vmx_flush_tlb_gva,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013185
Avi Kivity6aa8b732006-12-10 02:21:36 -080013186 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020013187 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013188 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040013189 .set_interrupt_shadow = vmx_set_interrupt_shadow,
13190 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020013191 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030013192 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013193 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020013194 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030013195 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020013196 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013197 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010013198 .get_nmi_mask = vmx_get_nmi_mask,
13199 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013200 .enable_nmi_window = enable_nmi_window,
13201 .enable_irq_window = enable_irq_window,
13202 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040013203 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080013204 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030013205 .get_enable_apicv = vmx_get_enable_apicv,
13206 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013207 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010013208 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013209 .hwapic_irr_update = vmx_hwapic_irr_update,
13210 .hwapic_isr_update = vmx_hwapic_isr_update,
Liran Alone6c67d82018-09-04 10:56:52 +030013211 .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt,
Yang Zhanga20ed542013-04-11 19:25:15 +080013212 .sync_pir_to_irr = vmx_sync_pir_to_irr,
13213 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013214
Izik Eiduscbc94022007-10-25 00:29:55 +020013215 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070013216 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080013217 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080013218 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030013219
Avi Kivity586f9602010-11-18 13:09:54 +020013220 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020013221
Sheng Yang17cc3932010-01-05 19:02:27 +080013222 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080013223
13224 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080013225
13226 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000013227 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020013228
13229 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080013230
13231 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013232
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013233 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Leonid Shatz326e7422018-11-06 12:14:25 +020013234 .write_l1_tsc_offset = vmx_write_l1_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020013235
13236 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013237
13238 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080013239 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000013240 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080013241 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020013242 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013243
13244 .check_nested_events = vmx_check_nested_events,
Sean Christophersond264ee02018-08-27 15:21:12 -070013245 .request_immediate_exit = vmx_request_immediate_exit,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013246
13247 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080013248
13249 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
13250 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
13251 .flush_log_dirty = vmx_flush_log_dirty,
13252 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040013253 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020013254
Feng Wubf9f6ac2015-09-18 22:29:55 +080013255 .pre_block = vmx_pre_block,
13256 .post_block = vmx_post_block,
13257
Wei Huang25462f72015-06-19 15:45:05 +020013258 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080013259
13260 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070013261
13262#ifdef CONFIG_X86_64
13263 .set_hv_timer = vmx_set_hv_timer,
13264 .cancel_hv_timer = vmx_cancel_hv_timer,
13265#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080013266
13267 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013268
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013269 .get_nested_state = vmx_get_nested_state,
13270 .set_nested_state = vmx_set_nested_state,
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013271 .get_vmcs12_pages = nested_get_vmcs12_pages,
13272
Ladi Prosek72d7b372017-10-11 16:54:41 +020013273 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013274 .pre_enter_smm = vmx_pre_enter_smm,
13275 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013276 .enable_smi_window = enable_smi_window,
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +020013277
13278 .nested_enable_evmcs = nested_enable_evmcs,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013279};
13280
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020013281static void vmx_cleanup_l1d_flush(void)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013282{
13283 if (vmx_l1d_flush_pages) {
13284 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
13285 vmx_l1d_flush_pages = NULL;
13286 }
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020013287 /* Restore state so sysfs ignores VMX */
13288 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020013289}
13290
Thomas Gleixnera7b90202018-07-13 16:23:18 +020013291static void vmx_exit(void)
13292{
13293#ifdef CONFIG_KEXEC_CORE
13294 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
13295 synchronize_rcu();
13296#endif
13297
13298 kvm_exit();
13299
13300#if IS_ENABLED(CONFIG_HYPERV)
13301 if (static_branch_unlikely(&enable_evmcs)) {
13302 int cpu;
13303 struct hv_vp_assist_page *vp_ap;
13304 /*
13305 * Reset everything to support using non-enlightened VMCS
13306 * access later (e.g. when we reload the module with
13307 * enlightened_vmcs=0)
13308 */
13309 for_each_online_cpu(cpu) {
13310 vp_ap = hv_get_vp_assist_page(cpu);
13311
13312 if (!vp_ap)
13313 continue;
13314
13315 vp_ap->current_nested_vmcs = 0;
13316 vp_ap->enlighten_vmentry = 0;
13317 }
13318
13319 static_branch_disable(&enable_evmcs);
13320 }
13321#endif
13322 vmx_cleanup_l1d_flush();
13323}
13324module_exit(vmx_exit);
13325
Avi Kivity6aa8b732006-12-10 02:21:36 -080013326static int __init vmx_init(void)
13327{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013328 int r;
13329
13330#if IS_ENABLED(CONFIG_HYPERV)
13331 /*
13332 * Enlightened VMCS usage should be recommended and the host needs
13333 * to support eVMCS v1 or above. We can also disable eVMCS support
13334 * with module parameter.
13335 */
13336 if (enlightened_vmcs &&
13337 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
13338 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
13339 KVM_EVMCS_VERSION) {
13340 int cpu;
13341
13342 /* Check that we have assist pages on all online CPUs */
13343 for_each_online_cpu(cpu) {
13344 if (!hv_get_vp_assist_page(cpu)) {
13345 enlightened_vmcs = false;
13346 break;
13347 }
13348 }
13349
13350 if (enlightened_vmcs) {
13351 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
13352 static_branch_enable(&enable_evmcs);
13353 }
13354 } else {
13355 enlightened_vmcs = false;
13356 }
13357#endif
13358
13359 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Thomas Gleixnera7b90202018-07-13 16:23:18 +020013360 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030013361 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013362 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080013363
Thomas Gleixnera7b90202018-07-13 16:23:18 +020013364 /*
Thomas Gleixner7db92e12018-07-13 16:23:19 +020013365 * Must be called after kvm_init() so enable_ept is properly set
13366 * up. Hand the parameter mitigation value in which was stored in
13367 * the pre module init parser. If no parameter was given, it will
13368 * contain 'auto' which will be turned into the default 'cond'
13369 * mitigation mode.
Thomas Gleixnera7b90202018-07-13 16:23:18 +020013370 */
Thomas Gleixner7db92e12018-07-13 16:23:19 +020013371 if (boot_cpu_has(X86_BUG_L1TF)) {
13372 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
13373 if (r) {
13374 vmx_exit();
13375 return r;
13376 }
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013377 }
13378
Dave Young2965faa2015-09-09 15:38:55 -070013379#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013380 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
13381 crash_vmclear_local_loaded_vmcss);
13382#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070013383 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013384
He, Qingfdef3ad2007-04-30 09:45:24 +030013385 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080013386}
Thomas Gleixnera7b90202018-07-13 16:23:18 +020013387module_init(vmx_init);