blob: 68a0bb7609b868439f42e1ceb11f270ff523b9d2 [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>
28#include <linux/nospec.h>
29#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040031#include <linux/tboot.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080032#include <linux/trace_events.h>
Avi Kivitye4956062007-06-28 14:15:57 -040033
Sean Christopherson199b1182018-12-03 13:52:53 -080034#include <asm/apic.h>
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +020035#include <asm/asm.h>
Feng Wu28b835d2015-09-18 22:29:54 +080036#include <asm/cpu.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010037#include <asm/debugreg.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080038#include <asm/desc.h>
39#include <asm/fpu/internal.h>
40#include <asm/io.h>
Feng Wuefc64402015-09-18 22:29:51 +080041#include <asm/irq_remapping.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080042#include <asm/kexec.h>
43#include <asm/perf_event.h>
44#include <asm/mce.h>
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070045#include <asm/mmu_context.h>
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010046#include <asm/mshyperv.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080047#include <asm/spec-ctrl.h>
48#include <asm/virtext.h>
49#include <asm/vmx.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080050
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"
Wei Huang25462f72015-06-19 15:45:05 +020058#include "pmu.h"
Sean Christopherson199b1182018-12-03 13:52:53 -080059#include "trace.h"
Sean Christopherson199b1182018-12-03 13:52:53 -080060#include "x86.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030061
Avi Kivity4ecac3f2008-05-13 13:23:38 +030062#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040063#define __ex_clear(x, reg) \
Uros Bizjak43ce76c2018-10-17 16:46:57 +020064 ____kvm_handle_fault_on_reboot(x, "xor " reg ", " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030065
Avi Kivity6aa8b732006-12-10 02:21:36 -080066MODULE_AUTHOR("Qumranet");
67MODULE_LICENSE("GPL");
68
Josh Triplette9bda3b2012-03-20 23:33:51 -070069static const struct x86_cpu_id vmx_cpu_id[] = {
70 X86_FEATURE_MATCH(X86_FEATURE_VMX),
71 {}
72};
73MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
74
Rusty Russell476bc002012-01-13 09:32:18 +103075static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020076module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080077
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010078static bool __read_mostly enable_vnmi = 1;
79module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
80
Rusty Russell476bc002012-01-13 09:32:18 +103081static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020082module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020083
Rusty Russell476bc002012-01-13 09:32:18 +103084static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020085module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080086
Rusty Russell476bc002012-01-13 09:32:18 +103087static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070088module_param_named(unrestricted_guest,
89 enable_unrestricted_guest, bool, S_IRUGO);
90
Xudong Hao83c3a332012-05-28 19:33:35 +080091static bool __read_mostly enable_ept_ad_bits = 1;
92module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
93
Avi Kivitya27685c2012-06-12 20:30:18 +030094static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020095module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030096
Rusty Russell476bc002012-01-13 09:32:18 +103097static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030098module_param(fasteoi, bool, S_IRUGO);
99
Yang Zhang5a717852013-04-11 19:25:16 +0800100static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +0800101module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800102
Abel Gordonabc4fc52013-04-18 14:35:25 +0300103static bool __read_mostly enable_shadow_vmcs = 1;
104module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300105/*
106 * If nested=1, nested virtualization is supported, i.e., guests may use
107 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
108 * use VMX instructions.
109 */
Paolo Bonzini1e58e5e2018-10-17 00:55:22 +0200110static bool __read_mostly nested = 1;
Nadav Har'El801d3422011-05-25 23:02:23 +0300111module_param(nested, bool, S_IRUGO);
112
Sean Christopherson52017602018-09-26 09:23:57 -0700113static bool __read_mostly nested_early_check = 0;
114module_param(nested_early_check, bool, S_IRUGO);
115
Wanpeng Li20300092014-12-02 19:14:59 +0800116static u64 __read_mostly host_xss;
117
Kai Huang843e4332015-01-28 10:54:28 +0800118static bool __read_mostly enable_pml = 1;
119module_param_named(pml, enable_pml, bool, S_IRUGO);
120
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100121#define MSR_TYPE_R 1
122#define MSR_TYPE_W 2
123#define MSR_TYPE_RW 3
124
125#define MSR_BITMAP_MODE_X2APIC 1
126#define MSR_BITMAP_MODE_X2APIC_APICV 2
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100127
Haozhong Zhang64903d62015-10-20 15:39:09 +0800128#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
129
Yunhong Jiang64672c92016-06-13 14:19:59 -0700130/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
131static int __read_mostly cpu_preemption_timer_multi;
132static bool __read_mostly enable_preemption_timer = 1;
133#ifdef CONFIG_X86_64
134module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
135#endif
136
Sean Christopherson3de63472018-07-13 08:42:30 -0700137#define KVM_VM_CR0_ALWAYS_OFF (X86_CR0_NW | X86_CR0_CD)
Sean Christopherson1706bd02018-03-05 12:04:38 -0800138#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
139#define KVM_VM_CR0_ALWAYS_ON \
140 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
141 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200142#define KVM_CR4_GUEST_OWNED_BITS \
143 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Yu Zhangfd8cb432017-08-24 20:27:56 +0800144 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200145
Sean Christopherson5dc1f042018-03-05 12:04:39 -0800146#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
Avi Kivitycdc0e242009-12-06 17:21:14 +0200147#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
148#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
149
Avi Kivity78ac8b42010-04-08 18:19:35 +0300150#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
151
Jan Kiszkaf41245002014-03-07 20:03:13 +0100152#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
153
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800154/*
Jan Dakinevich16c2aec2016-10-28 07:00:30 +0300155 * Hyper-V requires all of these, so mark them as supported even though
156 * they are just treated the same as all-context.
157 */
158#define VMX_VPID_EXTENT_SUPPORTED_MASK \
159 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
160 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
161 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
162 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
163
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800164/*
165 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
166 * ple_gap: upper bound on the amount of time between two successive
167 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500168 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800169 * ple_window: upper bound on the amount of time a guest is allowed to execute
170 * in a PAUSE loop. Tests indicate that most spinlocks are held for
171 * less than 2^12 cycles
172 * Time is measured based on a counter that runs at the same rate as the TSC,
173 * refer SDM volume 3b section 21.6.13 & 22.1.3.
174 */
Babu Mogerc8e88712018-03-16 16:37:24 -0400175static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
Luiz Capitulinoa87c99e2018-11-23 12:02:14 -0500176module_param(ple_gap, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200177
Babu Moger7fbc85a2018-03-16 16:37:22 -0400178static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
179module_param(ple_window, uint, 0444);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800180
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200181/* Default doubles per-vcpu window every exit. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400182static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400183module_param(ple_window_grow, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200184
185/* Default resets per-vcpu window every exit to ple_window. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400186static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400187module_param(ple_window_shrink, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200188
189/* Default is to compute the maximum so we can never overflow. */
Babu Moger7fbc85a2018-03-16 16:37:22 -0400190static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
191module_param(ple_window_max, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200192
Avi Kivity83287ea422012-09-16 15:10:57 +0300193extern const ulong vmx_return;
Sean Christopherson52017602018-09-26 09:23:57 -0700194extern const ulong vmx_early_consistency_check_return;
Avi Kivity83287ea422012-09-16 15:10:57 +0300195
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200196static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
Nicolai Stange427362a2018-07-21 22:25:00 +0200197static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200198static DEFINE_MUTEX(vmx_l1d_flush_mutex);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200199
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200200/* Storage for pre module init parameter parsing */
201static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200202
203static const struct {
204 const char *option;
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200205 bool for_parse;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200206} vmentry_l1d_param[] = {
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200207 [VMENTER_L1D_FLUSH_AUTO] = {"auto", true},
208 [VMENTER_L1D_FLUSH_NEVER] = {"never", true},
209 [VMENTER_L1D_FLUSH_COND] = {"cond", true},
210 [VMENTER_L1D_FLUSH_ALWAYS] = {"always", true},
211 [VMENTER_L1D_FLUSH_EPT_DISABLED] = {"EPT disabled", false},
212 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = {"not required", false},
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200213};
214
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200215#define L1D_CACHE_ORDER 4
216static void *vmx_l1d_flush_pages;
217
218static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
219{
220 struct page *page;
Nicolai Stange288d1522018-07-18 19:07:38 +0200221 unsigned int i;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200222
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200223 if (!enable_ept) {
224 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
225 return 0;
226 }
227
Yi Wangd806afa2018-08-16 13:42:39 +0800228 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) {
229 u64 msr;
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200230
Yi Wangd806afa2018-08-16 13:42:39 +0800231 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr);
232 if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) {
233 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
234 return 0;
235 }
236 }
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200237
Jiri Kosinad90a7a02018-07-13 16:23:25 +0200238 /* If set to auto use the default l1tf mitigation method */
239 if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
240 switch (l1tf_mitigation) {
241 case L1TF_MITIGATION_OFF:
242 l1tf = VMENTER_L1D_FLUSH_NEVER;
243 break;
244 case L1TF_MITIGATION_FLUSH_NOWARN:
245 case L1TF_MITIGATION_FLUSH:
246 case L1TF_MITIGATION_FLUSH_NOSMT:
247 l1tf = VMENTER_L1D_FLUSH_COND;
248 break;
249 case L1TF_MITIGATION_FULL:
250 case L1TF_MITIGATION_FULL_FORCE:
251 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
252 break;
253 }
254 } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) {
255 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
256 }
257
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200258 if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
259 !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
260 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
261 if (!page)
262 return -ENOMEM;
263 vmx_l1d_flush_pages = page_address(page);
Nicolai Stange288d1522018-07-18 19:07:38 +0200264
265 /*
266 * Initialize each page with a different pattern in
267 * order to protect against KSM in the nested
268 * virtualization case.
269 */
270 for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) {
271 memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1,
272 PAGE_SIZE);
273 }
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200274 }
275
276 l1tf_vmx_mitigation = l1tf;
277
Thomas Gleixner895ae472018-07-13 16:23:22 +0200278 if (l1tf != VMENTER_L1D_FLUSH_NEVER)
279 static_branch_enable(&vmx_l1d_should_flush);
280 else
281 static_branch_disable(&vmx_l1d_should_flush);
Thomas Gleixner4c6523e2018-07-13 16:23:20 +0200282
Nicolai Stange427362a2018-07-21 22:25:00 +0200283 if (l1tf == VMENTER_L1D_FLUSH_COND)
284 static_branch_enable(&vmx_l1d_flush_cond);
Thomas Gleixner895ae472018-07-13 16:23:22 +0200285 else
Nicolai Stange427362a2018-07-21 22:25:00 +0200286 static_branch_disable(&vmx_l1d_flush_cond);
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200287 return 0;
288}
289
290static int vmentry_l1d_flush_parse(const char *s)
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200291{
292 unsigned int i;
293
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200294 if (s) {
295 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200296 if (vmentry_l1d_param[i].for_parse &&
297 sysfs_streq(s, vmentry_l1d_param[i].option))
298 return i;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200299 }
300 }
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200301 return -EINVAL;
302}
303
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200304static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
305{
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200306 int l1tf, ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200307
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200308 l1tf = vmentry_l1d_flush_parse(s);
309 if (l1tf < 0)
310 return l1tf;
311
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200312 if (!boot_cpu_has(X86_BUG_L1TF))
313 return 0;
314
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200315 /*
316 * Has vmx_init() run already? If not then this is the pre init
317 * parameter parsing. In that case just store the value and let
318 * vmx_init() do the proper setup after enable_ept has been
319 * established.
320 */
321 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
322 vmentry_l1d_flush_param = l1tf;
323 return 0;
324 }
325
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200326 mutex_lock(&vmx_l1d_flush_mutex);
327 ret = vmx_setup_l1d_flush(l1tf);
328 mutex_unlock(&vmx_l1d_flush_mutex);
329 return ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200330}
331
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200332static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
333{
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200334 if (WARN_ON_ONCE(l1tf_vmx_mitigation >= ARRAY_SIZE(vmentry_l1d_param)))
335 return sprintf(s, "???\n");
336
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200337 return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200338}
339
340static const struct kernel_param_ops vmentry_l1d_flush_ops = {
341 .set = vmentry_l1d_flush_set,
342 .get = vmentry_l1d_flush_get,
343};
Thomas Gleixner895ae472018-07-13 16:23:22 +0200344module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200345
Tianyu Lan877ad952018-07-19 08:40:23 +0000346enum ept_pointers_status {
347 EPT_POINTERS_CHECK = 0,
348 EPT_POINTERS_MATCH = 1,
349 EPT_POINTERS_MISMATCH = 2
350};
351
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700352struct kvm_vmx {
353 struct kvm kvm;
354
355 unsigned int tss_addr;
356 bool ept_identity_pagetable_done;
357 gpa_t ept_identity_map_addr;
Tianyu Lan877ad952018-07-19 08:40:23 +0000358
359 enum ept_pointers_status ept_pointers_match;
360 spinlock_t ept_pointer_lock;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700361};
362
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200363#define NR_AUTOLOAD_MSRS 8
Avi Kivity61d2ef22010-04-28 16:40:38 +0300364
Liran Alon392b2f22018-06-23 02:35:01 +0300365struct vmcs_hdr {
366 u32 revision_id:31;
367 u32 shadow_vmcs:1;
368};
369
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400370struct vmcs {
Liran Alon392b2f22018-06-23 02:35:01 +0300371 struct vmcs_hdr hdr;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400372 u32 abort;
373 char data[0];
374};
375
Nadav Har'Eld462b812011-05-24 15:26:10 +0300376/*
Sean Christophersond7ee0392018-07-23 12:32:47 -0700377 * vmcs_host_state tracks registers that are loaded from the VMCS on VMEXIT
378 * and whose values change infrequently, but are not constant. I.e. this is
379 * used as a write-through cache of the corresponding VMCS fields.
380 */
381struct vmcs_host_state {
382 unsigned long cr3; /* May not match real cr3 */
383 unsigned long cr4; /* May not match real cr4 */
Sean Christopherson5e079c72018-07-23 12:32:50 -0700384 unsigned long gs_base;
385 unsigned long fs_base;
Sean Christophersond7ee0392018-07-23 12:32:47 -0700386
387 u16 fs_sel, gs_sel, ldt_sel;
388#ifdef CONFIG_X86_64
389 u16 ds_sel, es_sel;
390#endif
391};
392
393/*
Nadav Har'Eld462b812011-05-24 15:26:10 +0300394 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
395 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
396 * loaded on this CPU (so we can clear them if the CPU goes down).
397 */
398struct loaded_vmcs {
399 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700400 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300401 int cpu;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +0200402 bool launched;
403 bool nmi_known_unmasked;
Sean Christophersonf459a702018-08-27 15:21:11 -0700404 bool hv_timer_armed;
Paolo Bonzini8a1b4392017-11-06 13:31:12 +0100405 /* Support for vnmi-less CPUs */
406 int soft_vnmi_blocked;
407 ktime_t entry_time;
408 s64 vnmi_blocked_time;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100409 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300410 struct list_head loaded_vmcss_on_cpu_link;
Sean Christophersond7ee0392018-07-23 12:32:47 -0700411 struct vmcs_host_state host_state;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300412};
413
Avi Kivity26bb0982009-09-07 11:14:12 +0300414struct shared_msr_entry {
415 unsigned index;
416 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200417 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300418};
419
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300420/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300421 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
422 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
423 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
424 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
425 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
426 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattsonde3a0022017-11-27 17:22:25 -0600427 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300428 * underlying hardware which will be used to run L2.
429 * This structure is packed to ensure that its layout is identical across
430 * machines (necessary for live migration).
Jim Mattsonb348e792018-05-01 15:40:27 -0700431 *
432 * IMPORTANT: Changing the layout of existing fields in this structure
433 * will break save/restore compatibility with older kvm releases. When
434 * adding new fields, either use space in the reserved padding* arrays
435 * or add the new fields to the end of the structure.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300436 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300437typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300438struct __packed vmcs12 {
439 /* According to the Intel spec, a VMCS region must start with the
440 * following two fields. Then follow implementation-specific data.
441 */
Liran Alon392b2f22018-06-23 02:35:01 +0300442 struct vmcs_hdr hdr;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300443 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300444
Nadav Har'El27d6c862011-05-25 23:06:59 +0300445 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
446 u32 padding[7]; /* room for future expansion */
447
Nadav Har'El22bd0352011-05-25 23:05:57 +0300448 u64 io_bitmap_a;
449 u64 io_bitmap_b;
450 u64 msr_bitmap;
451 u64 vm_exit_msr_store_addr;
452 u64 vm_exit_msr_load_addr;
453 u64 vm_entry_msr_load_addr;
454 u64 tsc_offset;
455 u64 virtual_apic_page_addr;
456 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800457 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300458 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800459 u64 eoi_exit_bitmap0;
460 u64 eoi_exit_bitmap1;
461 u64 eoi_exit_bitmap2;
462 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800463 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300464 u64 guest_physical_address;
465 u64 vmcs_link_pointer;
466 u64 guest_ia32_debugctl;
467 u64 guest_ia32_pat;
468 u64 guest_ia32_efer;
469 u64 guest_ia32_perf_global_ctrl;
470 u64 guest_pdptr0;
471 u64 guest_pdptr1;
472 u64 guest_pdptr2;
473 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100474 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300475 u64 host_ia32_pat;
476 u64 host_ia32_efer;
477 u64 host_ia32_perf_global_ctrl;
Jim Mattsonb348e792018-05-01 15:40:27 -0700478 u64 vmread_bitmap;
479 u64 vmwrite_bitmap;
480 u64 vm_function_control;
481 u64 eptp_list_address;
482 u64 pml_address;
483 u64 padding64[3]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300484 /*
485 * To allow migration of L1 (complete with its L2 guests) between
486 * machines of different natural widths (32 or 64 bit), we cannot have
487 * unsigned long fields with no explict size. We use u64 (aliased
488 * natural_width) instead. Luckily, x86 is little-endian.
489 */
490 natural_width cr0_guest_host_mask;
491 natural_width cr4_guest_host_mask;
492 natural_width cr0_read_shadow;
493 natural_width cr4_read_shadow;
494 natural_width cr3_target_value0;
495 natural_width cr3_target_value1;
496 natural_width cr3_target_value2;
497 natural_width cr3_target_value3;
498 natural_width exit_qualification;
499 natural_width guest_linear_address;
500 natural_width guest_cr0;
501 natural_width guest_cr3;
502 natural_width guest_cr4;
503 natural_width guest_es_base;
504 natural_width guest_cs_base;
505 natural_width guest_ss_base;
506 natural_width guest_ds_base;
507 natural_width guest_fs_base;
508 natural_width guest_gs_base;
509 natural_width guest_ldtr_base;
510 natural_width guest_tr_base;
511 natural_width guest_gdtr_base;
512 natural_width guest_idtr_base;
513 natural_width guest_dr7;
514 natural_width guest_rsp;
515 natural_width guest_rip;
516 natural_width guest_rflags;
517 natural_width guest_pending_dbg_exceptions;
518 natural_width guest_sysenter_esp;
519 natural_width guest_sysenter_eip;
520 natural_width host_cr0;
521 natural_width host_cr3;
522 natural_width host_cr4;
523 natural_width host_fs_base;
524 natural_width host_gs_base;
525 natural_width host_tr_base;
526 natural_width host_gdtr_base;
527 natural_width host_idtr_base;
528 natural_width host_ia32_sysenter_esp;
529 natural_width host_ia32_sysenter_eip;
530 natural_width host_rsp;
531 natural_width host_rip;
532 natural_width paddingl[8]; /* room for future expansion */
533 u32 pin_based_vm_exec_control;
534 u32 cpu_based_vm_exec_control;
535 u32 exception_bitmap;
536 u32 page_fault_error_code_mask;
537 u32 page_fault_error_code_match;
538 u32 cr3_target_count;
539 u32 vm_exit_controls;
540 u32 vm_exit_msr_store_count;
541 u32 vm_exit_msr_load_count;
542 u32 vm_entry_controls;
543 u32 vm_entry_msr_load_count;
544 u32 vm_entry_intr_info_field;
545 u32 vm_entry_exception_error_code;
546 u32 vm_entry_instruction_len;
547 u32 tpr_threshold;
548 u32 secondary_vm_exec_control;
549 u32 vm_instruction_error;
550 u32 vm_exit_reason;
551 u32 vm_exit_intr_info;
552 u32 vm_exit_intr_error_code;
553 u32 idt_vectoring_info_field;
554 u32 idt_vectoring_error_code;
555 u32 vm_exit_instruction_len;
556 u32 vmx_instruction_info;
557 u32 guest_es_limit;
558 u32 guest_cs_limit;
559 u32 guest_ss_limit;
560 u32 guest_ds_limit;
561 u32 guest_fs_limit;
562 u32 guest_gs_limit;
563 u32 guest_ldtr_limit;
564 u32 guest_tr_limit;
565 u32 guest_gdtr_limit;
566 u32 guest_idtr_limit;
567 u32 guest_es_ar_bytes;
568 u32 guest_cs_ar_bytes;
569 u32 guest_ss_ar_bytes;
570 u32 guest_ds_ar_bytes;
571 u32 guest_fs_ar_bytes;
572 u32 guest_gs_ar_bytes;
573 u32 guest_ldtr_ar_bytes;
574 u32 guest_tr_ar_bytes;
575 u32 guest_interruptibility_info;
576 u32 guest_activity_state;
577 u32 guest_sysenter_cs;
578 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100579 u32 vmx_preemption_timer_value;
580 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300581 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800582 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300583 u16 guest_es_selector;
584 u16 guest_cs_selector;
585 u16 guest_ss_selector;
586 u16 guest_ds_selector;
587 u16 guest_fs_selector;
588 u16 guest_gs_selector;
589 u16 guest_ldtr_selector;
590 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800591 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300592 u16 host_es_selector;
593 u16 host_cs_selector;
594 u16 host_ss_selector;
595 u16 host_ds_selector;
596 u16 host_fs_selector;
597 u16 host_gs_selector;
598 u16 host_tr_selector;
Jim Mattsonb348e792018-05-01 15:40:27 -0700599 u16 guest_pml_index;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300600};
601
602/*
Jim Mattson21ebf532018-05-01 15:40:28 -0700603 * For save/restore compatibility, the vmcs12 field offsets must not change.
604 */
605#define CHECK_OFFSET(field, loc) \
606 BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
607 "Offset of " #field " in struct vmcs12 has changed.")
608
609static inline void vmx_check_vmcs12_offsets(void) {
Liran Alon392b2f22018-06-23 02:35:01 +0300610 CHECK_OFFSET(hdr, 0);
Jim Mattson21ebf532018-05-01 15:40:28 -0700611 CHECK_OFFSET(abort, 4);
612 CHECK_OFFSET(launch_state, 8);
613 CHECK_OFFSET(io_bitmap_a, 40);
614 CHECK_OFFSET(io_bitmap_b, 48);
615 CHECK_OFFSET(msr_bitmap, 56);
616 CHECK_OFFSET(vm_exit_msr_store_addr, 64);
617 CHECK_OFFSET(vm_exit_msr_load_addr, 72);
618 CHECK_OFFSET(vm_entry_msr_load_addr, 80);
619 CHECK_OFFSET(tsc_offset, 88);
620 CHECK_OFFSET(virtual_apic_page_addr, 96);
621 CHECK_OFFSET(apic_access_addr, 104);
622 CHECK_OFFSET(posted_intr_desc_addr, 112);
623 CHECK_OFFSET(ept_pointer, 120);
624 CHECK_OFFSET(eoi_exit_bitmap0, 128);
625 CHECK_OFFSET(eoi_exit_bitmap1, 136);
626 CHECK_OFFSET(eoi_exit_bitmap2, 144);
627 CHECK_OFFSET(eoi_exit_bitmap3, 152);
628 CHECK_OFFSET(xss_exit_bitmap, 160);
629 CHECK_OFFSET(guest_physical_address, 168);
630 CHECK_OFFSET(vmcs_link_pointer, 176);
631 CHECK_OFFSET(guest_ia32_debugctl, 184);
632 CHECK_OFFSET(guest_ia32_pat, 192);
633 CHECK_OFFSET(guest_ia32_efer, 200);
634 CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
635 CHECK_OFFSET(guest_pdptr0, 216);
636 CHECK_OFFSET(guest_pdptr1, 224);
637 CHECK_OFFSET(guest_pdptr2, 232);
638 CHECK_OFFSET(guest_pdptr3, 240);
639 CHECK_OFFSET(guest_bndcfgs, 248);
640 CHECK_OFFSET(host_ia32_pat, 256);
641 CHECK_OFFSET(host_ia32_efer, 264);
642 CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
643 CHECK_OFFSET(vmread_bitmap, 280);
644 CHECK_OFFSET(vmwrite_bitmap, 288);
645 CHECK_OFFSET(vm_function_control, 296);
646 CHECK_OFFSET(eptp_list_address, 304);
647 CHECK_OFFSET(pml_address, 312);
648 CHECK_OFFSET(cr0_guest_host_mask, 344);
649 CHECK_OFFSET(cr4_guest_host_mask, 352);
650 CHECK_OFFSET(cr0_read_shadow, 360);
651 CHECK_OFFSET(cr4_read_shadow, 368);
652 CHECK_OFFSET(cr3_target_value0, 376);
653 CHECK_OFFSET(cr3_target_value1, 384);
654 CHECK_OFFSET(cr3_target_value2, 392);
655 CHECK_OFFSET(cr3_target_value3, 400);
656 CHECK_OFFSET(exit_qualification, 408);
657 CHECK_OFFSET(guest_linear_address, 416);
658 CHECK_OFFSET(guest_cr0, 424);
659 CHECK_OFFSET(guest_cr3, 432);
660 CHECK_OFFSET(guest_cr4, 440);
661 CHECK_OFFSET(guest_es_base, 448);
662 CHECK_OFFSET(guest_cs_base, 456);
663 CHECK_OFFSET(guest_ss_base, 464);
664 CHECK_OFFSET(guest_ds_base, 472);
665 CHECK_OFFSET(guest_fs_base, 480);
666 CHECK_OFFSET(guest_gs_base, 488);
667 CHECK_OFFSET(guest_ldtr_base, 496);
668 CHECK_OFFSET(guest_tr_base, 504);
669 CHECK_OFFSET(guest_gdtr_base, 512);
670 CHECK_OFFSET(guest_idtr_base, 520);
671 CHECK_OFFSET(guest_dr7, 528);
672 CHECK_OFFSET(guest_rsp, 536);
673 CHECK_OFFSET(guest_rip, 544);
674 CHECK_OFFSET(guest_rflags, 552);
675 CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
676 CHECK_OFFSET(guest_sysenter_esp, 568);
677 CHECK_OFFSET(guest_sysenter_eip, 576);
678 CHECK_OFFSET(host_cr0, 584);
679 CHECK_OFFSET(host_cr3, 592);
680 CHECK_OFFSET(host_cr4, 600);
681 CHECK_OFFSET(host_fs_base, 608);
682 CHECK_OFFSET(host_gs_base, 616);
683 CHECK_OFFSET(host_tr_base, 624);
684 CHECK_OFFSET(host_gdtr_base, 632);
685 CHECK_OFFSET(host_idtr_base, 640);
686 CHECK_OFFSET(host_ia32_sysenter_esp, 648);
687 CHECK_OFFSET(host_ia32_sysenter_eip, 656);
688 CHECK_OFFSET(host_rsp, 664);
689 CHECK_OFFSET(host_rip, 672);
690 CHECK_OFFSET(pin_based_vm_exec_control, 744);
691 CHECK_OFFSET(cpu_based_vm_exec_control, 748);
692 CHECK_OFFSET(exception_bitmap, 752);
693 CHECK_OFFSET(page_fault_error_code_mask, 756);
694 CHECK_OFFSET(page_fault_error_code_match, 760);
695 CHECK_OFFSET(cr3_target_count, 764);
696 CHECK_OFFSET(vm_exit_controls, 768);
697 CHECK_OFFSET(vm_exit_msr_store_count, 772);
698 CHECK_OFFSET(vm_exit_msr_load_count, 776);
699 CHECK_OFFSET(vm_entry_controls, 780);
700 CHECK_OFFSET(vm_entry_msr_load_count, 784);
701 CHECK_OFFSET(vm_entry_intr_info_field, 788);
702 CHECK_OFFSET(vm_entry_exception_error_code, 792);
703 CHECK_OFFSET(vm_entry_instruction_len, 796);
704 CHECK_OFFSET(tpr_threshold, 800);
705 CHECK_OFFSET(secondary_vm_exec_control, 804);
706 CHECK_OFFSET(vm_instruction_error, 808);
707 CHECK_OFFSET(vm_exit_reason, 812);
708 CHECK_OFFSET(vm_exit_intr_info, 816);
709 CHECK_OFFSET(vm_exit_intr_error_code, 820);
710 CHECK_OFFSET(idt_vectoring_info_field, 824);
711 CHECK_OFFSET(idt_vectoring_error_code, 828);
712 CHECK_OFFSET(vm_exit_instruction_len, 832);
713 CHECK_OFFSET(vmx_instruction_info, 836);
714 CHECK_OFFSET(guest_es_limit, 840);
715 CHECK_OFFSET(guest_cs_limit, 844);
716 CHECK_OFFSET(guest_ss_limit, 848);
717 CHECK_OFFSET(guest_ds_limit, 852);
718 CHECK_OFFSET(guest_fs_limit, 856);
719 CHECK_OFFSET(guest_gs_limit, 860);
720 CHECK_OFFSET(guest_ldtr_limit, 864);
721 CHECK_OFFSET(guest_tr_limit, 868);
722 CHECK_OFFSET(guest_gdtr_limit, 872);
723 CHECK_OFFSET(guest_idtr_limit, 876);
724 CHECK_OFFSET(guest_es_ar_bytes, 880);
725 CHECK_OFFSET(guest_cs_ar_bytes, 884);
726 CHECK_OFFSET(guest_ss_ar_bytes, 888);
727 CHECK_OFFSET(guest_ds_ar_bytes, 892);
728 CHECK_OFFSET(guest_fs_ar_bytes, 896);
729 CHECK_OFFSET(guest_gs_ar_bytes, 900);
730 CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
731 CHECK_OFFSET(guest_tr_ar_bytes, 908);
732 CHECK_OFFSET(guest_interruptibility_info, 912);
733 CHECK_OFFSET(guest_activity_state, 916);
734 CHECK_OFFSET(guest_sysenter_cs, 920);
735 CHECK_OFFSET(host_ia32_sysenter_cs, 924);
736 CHECK_OFFSET(vmx_preemption_timer_value, 928);
737 CHECK_OFFSET(virtual_processor_id, 960);
738 CHECK_OFFSET(posted_intr_nv, 962);
739 CHECK_OFFSET(guest_es_selector, 964);
740 CHECK_OFFSET(guest_cs_selector, 966);
741 CHECK_OFFSET(guest_ss_selector, 968);
742 CHECK_OFFSET(guest_ds_selector, 970);
743 CHECK_OFFSET(guest_fs_selector, 972);
744 CHECK_OFFSET(guest_gs_selector, 974);
745 CHECK_OFFSET(guest_ldtr_selector, 976);
746 CHECK_OFFSET(guest_tr_selector, 978);
747 CHECK_OFFSET(guest_intr_status, 980);
748 CHECK_OFFSET(host_es_selector, 982);
749 CHECK_OFFSET(host_cs_selector, 984);
750 CHECK_OFFSET(host_ss_selector, 986);
751 CHECK_OFFSET(host_ds_selector, 988);
752 CHECK_OFFSET(host_fs_selector, 990);
753 CHECK_OFFSET(host_gs_selector, 992);
754 CHECK_OFFSET(host_tr_selector, 994);
755 CHECK_OFFSET(guest_pml_index, 996);
756}
757
758/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300759 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
760 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
761 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
Jim Mattsonb348e792018-05-01 15:40:27 -0700762 *
763 * IMPORTANT: Changing this value will break save/restore compatibility with
764 * older kvm releases.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300765 */
766#define VMCS12_REVISION 0x11e57ed0
767
768/*
769 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
770 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
771 * current implementation, 4K are reserved to avoid future complications.
772 */
773#define VMCS12_SIZE 0x1000
774
775/*
Jim Mattson5b157062017-12-22 12:11:12 -0800776 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
777 * supported VMCS12 field encoding.
778 */
779#define VMCS12_MAX_FIELD_INDEX 0x17
780
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100781struct nested_vmx_msrs {
782 /*
783 * We only store the "true" versions of the VMX capability MSRs. We
784 * generate the "non-true" versions by setting the must-be-1 bits
785 * according to the SDM.
786 */
787 u32 procbased_ctls_low;
788 u32 procbased_ctls_high;
789 u32 secondary_ctls_low;
790 u32 secondary_ctls_high;
791 u32 pinbased_ctls_low;
792 u32 pinbased_ctls_high;
793 u32 exit_ctls_low;
794 u32 exit_ctls_high;
795 u32 entry_ctls_low;
796 u32 entry_ctls_high;
797 u32 misc_low;
798 u32 misc_high;
799 u32 ept_caps;
800 u32 vpid_caps;
801 u64 basic;
802 u64 cr0_fixed0;
803 u64 cr0_fixed1;
804 u64 cr4_fixed0;
805 u64 cr4_fixed1;
806 u64 vmcs_enum;
807 u64 vmfunc_controls;
808};
809
Jim Mattson5b157062017-12-22 12:11:12 -0800810/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300811 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
812 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
813 */
814struct nested_vmx {
815 /* Has the level1 guest done vmxon? */
816 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400817 gpa_t vmxon_ptr;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400818 bool pml_full;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300819
820 /* The guest-physical address of the current VMCS L1 keeps for L2 */
821 gpa_t current_vmptr;
David Matlack4f2777b2016-07-13 17:16:37 -0700822 /*
823 * Cache of the guest's VMCS, existing outside of guest memory.
824 * Loaded from guest memory during VMPTRLD. Flushed to guest
David Matlack8ca44e82017-08-01 14:00:39 -0700825 * memory during VMCLEAR and VMPTRLD.
David Matlack4f2777b2016-07-13 17:16:37 -0700826 */
827 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300828 /*
Liran Alon61ada742018-06-23 02:35:08 +0300829 * Cache of the guest's shadow VMCS, existing outside of guest
830 * memory. Loaded from guest memory during VM entry. Flushed
831 * to guest memory during VM exit.
832 */
833 struct vmcs12 *cached_shadow_vmcs12;
834 /*
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +0200835 * Indicates if the shadow vmcs or enlightened vmcs must be updated
836 * with the data held by struct vmcs12.
Abel Gordon012f83c2013-04-18 14:39:25 +0300837 */
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +0200838 bool need_vmcs12_sync;
Paolo Bonzini74a497f2017-12-20 13:55:39 +0100839 bool dirty_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300840
Sean Christopherson9d6105b2018-09-26 09:23:51 -0700841 /*
842 * vmcs02 has been initialized, i.e. state that is constant for
843 * vmcs02 has been written to the backing VMCS. Initialization
844 * is delayed until L1 actually attempts to run a nested VM.
845 */
846 bool vmcs02_initialized;
847
Jim Mattson8d860bb2018-05-09 16:56:05 -0400848 bool change_vmcs01_virtual_apic_mode;
849
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +0200850 /*
851 * Enlightened VMCS has been enabled. It does not mean that L1 has to
852 * use it. However, VMX features available to L1 will be limited based
853 * on what the enlightened VMCS supports.
854 */
855 bool enlightened_vmcs_enabled;
856
Nadav Har'El644d7112011-05-25 23:12:35 +0300857 /* L2 must run next, and mustn't decide to exit to L1. */
858 bool nested_run_pending;
Jim Mattsonde3a0022017-11-27 17:22:25 -0600859
860 struct loaded_vmcs vmcs02;
861
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300862 /*
Jim Mattsonde3a0022017-11-27 17:22:25 -0600863 * Guest pages referred to in the vmcs02 with host-physical
864 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300865 */
866 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800867 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800868 struct page *pi_desc_page;
869 struct pi_desc *pi_desc;
870 bool pi_pending;
871 u16 posted_intr_nv;
Jan Kiszkaf41245002014-03-07 20:03:13 +0100872
873 struct hrtimer preemption_timer;
874 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200875
876 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
877 u64 vmcs01_debugctl;
Liran Alon62cf9bd812018-09-14 03:25:54 +0300878 u64 vmcs01_guest_bndcfgs;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800879
Wanpeng Li5c614b32015-10-13 09:18:36 -0700880 u16 vpid02;
881 u16 last_vpid;
882
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100883 struct nested_vmx_msrs msrs;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200884
885 /* SMM related state */
886 struct {
887 /* in VMX operation on SMM entry? */
888 bool vmxon;
889 /* in guest mode on SMM entry? */
890 bool guest_mode;
891 } smm;
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +0200892
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +0200893 gpa_t hv_evmcs_vmptr;
894 struct page *hv_evmcs_page;
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +0200895 struct hv_enlightened_vmcs *hv_evmcs;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300896};
897
Yang Zhang01e439b2013-04-11 19:25:12 +0800898#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800899#define POSTED_INTR_SN 1
900
Yang Zhang01e439b2013-04-11 19:25:12 +0800901/* Posted-Interrupt Descriptor */
902struct pi_desc {
903 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800904 union {
905 struct {
906 /* bit 256 - Outstanding Notification */
907 u16 on : 1,
908 /* bit 257 - Suppress Notification */
909 sn : 1,
910 /* bit 271:258 - Reserved */
911 rsvd_1 : 14;
912 /* bit 279:272 - Notification Vector */
913 u8 nv;
914 /* bit 287:280 - Reserved */
915 u8 rsvd_2;
916 /* bit 319:288 - Notification Destination */
917 u32 ndst;
918 };
919 u64 control;
920 };
921 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800922} __aligned(64);
923
Yang Zhanga20ed542013-04-11 19:25:15 +0800924static bool pi_test_and_set_on(struct pi_desc *pi_desc)
925{
926 return test_and_set_bit(POSTED_INTR_ON,
927 (unsigned long *)&pi_desc->control);
928}
929
930static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
931{
932 return test_and_clear_bit(POSTED_INTR_ON,
933 (unsigned long *)&pi_desc->control);
934}
935
936static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
937{
938 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
939}
940
Feng Wuebbfc762015-09-18 22:29:46 +0800941static inline void pi_clear_sn(struct pi_desc *pi_desc)
942{
943 return clear_bit(POSTED_INTR_SN,
944 (unsigned long *)&pi_desc->control);
945}
946
947static inline void pi_set_sn(struct pi_desc *pi_desc)
948{
949 return set_bit(POSTED_INTR_SN,
950 (unsigned long *)&pi_desc->control);
951}
952
Paolo Bonziniad361092016-09-20 16:15:05 +0200953static inline void pi_clear_on(struct pi_desc *pi_desc)
954{
955 clear_bit(POSTED_INTR_ON,
956 (unsigned long *)&pi_desc->control);
957}
958
Feng Wuebbfc762015-09-18 22:29:46 +0800959static inline int pi_test_on(struct pi_desc *pi_desc)
960{
961 return test_bit(POSTED_INTR_ON,
962 (unsigned long *)&pi_desc->control);
963}
964
965static inline int pi_test_sn(struct pi_desc *pi_desc)
966{
967 return test_bit(POSTED_INTR_SN,
968 (unsigned long *)&pi_desc->control);
969}
970
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400971struct vmx_msrs {
972 unsigned int nr;
973 struct vmx_msr_entry val[NR_AUTOLOAD_MSRS];
974};
975
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400976struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000977 struct kvm_vcpu vcpu;
Avi Kivity313dbd42008-07-17 18:04:30 +0300978 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300979 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100980 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300981 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200982 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200983 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300984 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400985 int nmsrs;
986 int save_nmsrs;
Liran Alonf48b4712018-11-20 18:03:25 +0200987 bool guest_msrs_dirty;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800988 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400989#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300990 u64 msr_host_kernel_gs_base;
991 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400992#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100993
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100994 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100995 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100996
Gleb Natapov2961e8762013-11-25 15:37:13 +0200997 u32 vm_entry_controls_shadow;
998 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200999 u32 secondary_exec_control;
1000
Nadav Har'Eld462b812011-05-24 15:26:10 +03001001 /*
1002 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
1003 * non-nested (L1) guest, it always points to vmcs01. For a nested
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001004 * guest (L2), it points to a different VMCS. loaded_cpu_state points
1005 * to the VMCS whose state is loaded into the CPU registers that only
1006 * need to be switched when transitioning to/from the kernel; a NULL
1007 * value indicates that host state is loaded.
Nadav Har'Eld462b812011-05-24 15:26:10 +03001008 */
1009 struct loaded_vmcs vmcs01;
1010 struct loaded_vmcs *loaded_vmcs;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001011 struct loaded_vmcs *loaded_cpu_state;
Nadav Har'Eld462b812011-05-24 15:26:10 +03001012 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +03001013 struct msr_autoload {
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04001014 struct vmx_msrs guest;
1015 struct vmx_msrs host;
Avi Kivity61d2ef22010-04-28 16:40:38 +03001016 } msr_autoload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001017
Avi Kivity9c8cba32007-11-22 11:42:59 +02001018 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001019 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001020 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03001021 struct kvm_segment segs[8];
1022 } rmode;
1023 struct {
1024 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001025 struct kvm_save_segment {
1026 u16 selector;
1027 unsigned long base;
1028 u32 limit;
1029 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03001030 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +03001031 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001032 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +03001033 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02001034
Andi Kleena0861c02009-06-08 17:37:09 +08001035 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001036
Yang Zhang01e439b2013-04-11 19:25:12 +08001037 /* Posted interrupt descriptor */
1038 struct pi_desc pi_desc;
1039
Nadav Har'Elec378ae2011-05-25 23:02:54 +03001040 /* Support for a guest hypervisor (nested VMX) */
1041 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +02001042
1043 /* Dynamic PLE window. */
1044 int ple_window;
1045 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +08001046
Sean Christophersond264ee02018-08-27 15:21:12 -07001047 bool req_immediate_exit;
1048
Kai Huang843e4332015-01-28 10:54:28 +08001049 /* Support for PML */
1050#define PML_ENTITY_NUM 512
1051 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001052
Yunhong Jiang64672c92016-06-13 14:19:59 -07001053 /* apic deadline value in host tsc */
1054 u64 hv_deadline_tsc;
1055
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001056 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +08001057
Xiao Guangrong1be0e612016-03-22 16:51:18 +08001058 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +08001059
Wanpeng Li74c55932017-11-29 01:31:20 -08001060 unsigned long host_debugctlmsr;
1061
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001062 /*
1063 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
1064 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
1065 * in msr_ia32_feature_control_valid_bits.
1066 */
Haozhong Zhang3b840802016-06-22 14:59:54 +08001067 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001068 u64 msr_ia32_feature_control_valid_bits;
Tianyu Lan877ad952018-07-19 08:40:23 +00001069 u64 ept_pointer;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001070};
1071
Avi Kivity2fb92db2011-04-27 19:42:18 +03001072enum segment_cache_field {
1073 SEG_FIELD_SEL = 0,
1074 SEG_FIELD_BASE = 1,
1075 SEG_FIELD_LIMIT = 2,
1076 SEG_FIELD_AR = 3,
1077
1078 SEG_FIELD_NR = 4
1079};
1080
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07001081static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
1082{
1083 return container_of(kvm, struct kvm_vmx, kvm);
1084}
1085
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001086static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
1087{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001088 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001089}
1090
Feng Wuefc64402015-09-18 22:29:51 +08001091static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
1092{
1093 return &(to_vmx(vcpu)->pi_desc);
1094}
1095
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001096#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +03001097#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001098#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
1099#define FIELD64(number, name) \
1100 FIELD(number, name), \
1101 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +03001102
Abel Gordon4607c2d2013-04-18 14:35:55 +03001103
Paolo Bonzini44900ba2017-12-13 12:58:02 +01001104static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +01001105#define SHADOW_FIELD_RO(x) x,
Sean Christophersone0123112018-12-03 13:52:57 -08001106#include "vmcs_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +03001107};
Bandan Dasfe2b2012014-04-21 15:20:14 -04001108static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001109 ARRAY_SIZE(shadow_read_only_fields);
1110
Paolo Bonzini44900ba2017-12-13 12:58:02 +01001111static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +01001112#define SHADOW_FIELD_RW(x) x,
Sean Christophersone0123112018-12-03 13:52:57 -08001113#include "vmcs_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +03001114};
Bandan Dasfe2b2012014-04-21 15:20:14 -04001115static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001116 ARRAY_SIZE(shadow_read_write_fields);
1117
Mathias Krause772e0312012-08-30 01:30:19 +02001118static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +03001119 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +08001120 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001121 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
1122 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
1123 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
1124 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
1125 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
1126 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
1127 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
1128 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +08001129 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -04001130 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001131 FIELD(HOST_ES_SELECTOR, host_es_selector),
1132 FIELD(HOST_CS_SELECTOR, host_cs_selector),
1133 FIELD(HOST_SS_SELECTOR, host_ss_selector),
1134 FIELD(HOST_DS_SELECTOR, host_ds_selector),
1135 FIELD(HOST_FS_SELECTOR, host_fs_selector),
1136 FIELD(HOST_GS_SELECTOR, host_gs_selector),
1137 FIELD(HOST_TR_SELECTOR, host_tr_selector),
1138 FIELD64(IO_BITMAP_A, io_bitmap_a),
1139 FIELD64(IO_BITMAP_B, io_bitmap_b),
1140 FIELD64(MSR_BITMAP, msr_bitmap),
1141 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
1142 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
1143 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
Jim Mattsonb348e792018-05-01 15:40:27 -07001144 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001145 FIELD64(TSC_OFFSET, tsc_offset),
1146 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
1147 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +08001148 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -04001149 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001150 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +08001151 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
1152 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
1153 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
1154 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -04001155 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Jim Mattsonb348e792018-05-01 15:40:27 -07001156 FIELD64(VMREAD_BITMAP, vmread_bitmap),
1157 FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001158 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001159 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
1160 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
1161 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
1162 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
1163 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
1164 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
1165 FIELD64(GUEST_PDPTR0, guest_pdptr0),
1166 FIELD64(GUEST_PDPTR1, guest_pdptr1),
1167 FIELD64(GUEST_PDPTR2, guest_pdptr2),
1168 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +01001169 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001170 FIELD64(HOST_IA32_PAT, host_ia32_pat),
1171 FIELD64(HOST_IA32_EFER, host_ia32_efer),
1172 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
1173 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
1174 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
1175 FIELD(EXCEPTION_BITMAP, exception_bitmap),
1176 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
1177 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
1178 FIELD(CR3_TARGET_COUNT, cr3_target_count),
1179 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
1180 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
1181 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
1182 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
1183 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
1184 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
1185 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
1186 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
1187 FIELD(TPR_THRESHOLD, tpr_threshold),
1188 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
1189 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
1190 FIELD(VM_EXIT_REASON, vm_exit_reason),
1191 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
1192 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
1193 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
1194 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
1195 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
1196 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
1197 FIELD(GUEST_ES_LIMIT, guest_es_limit),
1198 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
1199 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
1200 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
1201 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
1202 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
1203 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
1204 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
1205 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
1206 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
1207 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
1208 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
1209 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
1210 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
1211 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
1212 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
1213 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
1214 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
1215 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
1216 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
1217 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
1218 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +01001219 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001220 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
1221 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
1222 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
1223 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
1224 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
1225 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
1226 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
1227 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
1228 FIELD(EXIT_QUALIFICATION, exit_qualification),
1229 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
1230 FIELD(GUEST_CR0, guest_cr0),
1231 FIELD(GUEST_CR3, guest_cr3),
1232 FIELD(GUEST_CR4, guest_cr4),
1233 FIELD(GUEST_ES_BASE, guest_es_base),
1234 FIELD(GUEST_CS_BASE, guest_cs_base),
1235 FIELD(GUEST_SS_BASE, guest_ss_base),
1236 FIELD(GUEST_DS_BASE, guest_ds_base),
1237 FIELD(GUEST_FS_BASE, guest_fs_base),
1238 FIELD(GUEST_GS_BASE, guest_gs_base),
1239 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1240 FIELD(GUEST_TR_BASE, guest_tr_base),
1241 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1242 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1243 FIELD(GUEST_DR7, guest_dr7),
1244 FIELD(GUEST_RSP, guest_rsp),
1245 FIELD(GUEST_RIP, guest_rip),
1246 FIELD(GUEST_RFLAGS, guest_rflags),
1247 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1248 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1249 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1250 FIELD(HOST_CR0, host_cr0),
1251 FIELD(HOST_CR3, host_cr3),
1252 FIELD(HOST_CR4, host_cr4),
1253 FIELD(HOST_FS_BASE, host_fs_base),
1254 FIELD(HOST_GS_BASE, host_gs_base),
1255 FIELD(HOST_TR_BASE, host_tr_base),
1256 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1257 FIELD(HOST_IDTR_BASE, host_idtr_base),
1258 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1259 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1260 FIELD(HOST_RSP, host_rsp),
1261 FIELD(HOST_RIP, host_rip),
1262};
Nadav Har'El22bd0352011-05-25 23:05:57 +03001263
1264static inline short vmcs_field_to_offset(unsigned long field)
1265{
Dan Williams085331d2018-01-31 17:47:03 -08001266 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1267 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001268 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001269
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001270 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -08001271 return -ENOENT;
1272
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001273 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -08001274 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -08001275 return -ENOENT;
1276
Linus Torvalds15303ba2018-02-10 13:16:35 -08001277 index = array_index_nospec(index, size);
1278 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -08001279 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001280 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -08001281 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +03001282}
1283
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001284static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1285{
David Matlack4f2777b2016-07-13 17:16:37 -07001286 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001287}
1288
Liran Alon61ada742018-06-23 02:35:08 +03001289static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu)
1290{
1291 return to_vmx(vcpu)->nested.cached_shadow_vmcs12;
1292}
1293
Peter Feiner995f00a2017-06-30 17:26:32 -07001294static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03001295static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -07001296static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +08001297static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +03001298static void vmx_set_segment(struct kvm_vcpu *vcpu,
1299 struct kvm_segment *var, int seg);
1300static void vmx_get_segment(struct kvm_vcpu *vcpu,
1301 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +02001302static bool guest_state_valid(struct kvm_vcpu *vcpu);
1303static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +03001304static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001305static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1306static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1307static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1308 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001309static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Yi Wang1e4329ee2018-11-08 11:22:21 +08001310static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
Ashok Raj15d45072018-02-01 22:59:43 +01001311 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +03001312
Avi Kivity6aa8b732006-12-10 02:21:36 -08001313static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1314static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001315/*
1316 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1317 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1318 */
1319static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001320
Feng Wubf9f6ac2015-09-18 22:29:55 +08001321/*
1322 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1323 * can find which vCPU should be waken up.
1324 */
1325static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1326static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1327
Radim Krčmář23611332016-09-29 22:41:33 +02001328enum {
Radim Krčmář23611332016-09-29 22:41:33 +02001329 VMX_VMREAD_BITMAP,
1330 VMX_VMWRITE_BITMAP,
1331 VMX_BITMAP_NR
1332};
1333
1334static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1335
Radim Krčmář23611332016-09-29 22:41:33 +02001336#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1337#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +03001338
Avi Kivity110312c2010-12-21 12:54:20 +02001339static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001340static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001341
Sheng Yang2384d2b2008-01-17 15:14:33 +08001342static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1343static DEFINE_SPINLOCK(vmx_vpid_lock);
1344
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001345static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001346 int size;
1347 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001348 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001349 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001350 u32 pin_based_exec_ctrl;
1351 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001352 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001353 u32 vmexit_ctrl;
1354 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +01001355 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001356} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001357
Hannes Ederefff9e52008-11-28 17:02:06 +01001358static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001359 u32 ept;
1360 u32 vpid;
1361} vmx_capability;
1362
Avi Kivity6aa8b732006-12-10 02:21:36 -08001363#define VMX_SEGMENT_FIELD(seg) \
1364 [VCPU_SREG_##seg] = { \
1365 .selector = GUEST_##seg##_SELECTOR, \
1366 .base = GUEST_##seg##_BASE, \
1367 .limit = GUEST_##seg##_LIMIT, \
1368 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1369 }
1370
Mathias Krause772e0312012-08-30 01:30:19 +02001371static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001372 unsigned selector;
1373 unsigned base;
1374 unsigned limit;
1375 unsigned ar_bytes;
1376} kvm_vmx_segment_fields[] = {
1377 VMX_SEGMENT_FIELD(CS),
1378 VMX_SEGMENT_FIELD(DS),
1379 VMX_SEGMENT_FIELD(ES),
1380 VMX_SEGMENT_FIELD(FS),
1381 VMX_SEGMENT_FIELD(GS),
1382 VMX_SEGMENT_FIELD(SS),
1383 VMX_SEGMENT_FIELD(TR),
1384 VMX_SEGMENT_FIELD(LDTR),
1385};
1386
Avi Kivity26bb0982009-09-07 11:14:12 +03001387static u64 host_efer;
1388
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001389static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1390
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001391/*
Brian Gerst8c065852010-07-17 09:03:26 -04001392 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001393 * away by decrementing the array size.
1394 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001395static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001396#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001397 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001398#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001399 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001400};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001401
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001402DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1403
1404#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1405
1406#define KVM_EVMCS_VERSION 1
1407
Vitaly Kuznetsov5d7a6442018-10-16 18:50:00 +02001408/*
1409 * Enlightened VMCSv1 doesn't support these:
1410 *
1411 * POSTED_INTR_NV = 0x00000002,
1412 * GUEST_INTR_STATUS = 0x00000810,
1413 * APIC_ACCESS_ADDR = 0x00002014,
1414 * POSTED_INTR_DESC_ADDR = 0x00002016,
1415 * EOI_EXIT_BITMAP0 = 0x0000201c,
1416 * EOI_EXIT_BITMAP1 = 0x0000201e,
1417 * EOI_EXIT_BITMAP2 = 0x00002020,
1418 * EOI_EXIT_BITMAP3 = 0x00002022,
1419 * GUEST_PML_INDEX = 0x00000812,
1420 * PML_ADDRESS = 0x0000200e,
1421 * VM_FUNCTION_CONTROL = 0x00002018,
1422 * EPTP_LIST_ADDRESS = 0x00002024,
1423 * VMREAD_BITMAP = 0x00002026,
1424 * VMWRITE_BITMAP = 0x00002028,
1425 *
1426 * TSC_MULTIPLIER = 0x00002032,
1427 * PLE_GAP = 0x00004020,
1428 * PLE_WINDOW = 0x00004022,
1429 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1430 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1431 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1432 *
1433 * Currently unsupported in KVM:
1434 * GUEST_IA32_RTIT_CTL = 0x00002814,
1435 */
1436#define EVMCS1_UNSUPPORTED_PINCTRL (PIN_BASED_POSTED_INTR | \
1437 PIN_BASED_VMX_PREEMPTION_TIMER)
1438#define EVMCS1_UNSUPPORTED_2NDEXEC \
1439 (SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | \
1440 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | \
1441 SECONDARY_EXEC_APIC_REGISTER_VIRT | \
1442 SECONDARY_EXEC_ENABLE_PML | \
1443 SECONDARY_EXEC_ENABLE_VMFUNC | \
1444 SECONDARY_EXEC_SHADOW_VMCS | \
1445 SECONDARY_EXEC_TSC_SCALING | \
1446 SECONDARY_EXEC_PAUSE_LOOP_EXITING)
1447#define EVMCS1_UNSUPPORTED_VMEXIT_CTRL (VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
1448#define EVMCS1_UNSUPPORTED_VMENTRY_CTRL (VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
1449#define EVMCS1_UNSUPPORTED_VMFUNC (VMX_VMFUNC_EPTP_SWITCHING)
1450
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001451#if IS_ENABLED(CONFIG_HYPERV)
1452static bool __read_mostly enlightened_vmcs = true;
1453module_param(enlightened_vmcs, bool, 0444);
1454
1455static inline void evmcs_write64(unsigned long field, u64 value)
1456{
1457 u16 clean_field;
1458 int offset = get_evmcs_offset(field, &clean_field);
1459
1460 if (offset < 0)
1461 return;
1462
1463 *(u64 *)((char *)current_evmcs + offset) = value;
1464
1465 current_evmcs->hv_clean_fields &= ~clean_field;
1466}
1467
1468static inline void evmcs_write32(unsigned long field, u32 value)
1469{
1470 u16 clean_field;
1471 int offset = get_evmcs_offset(field, &clean_field);
1472
1473 if (offset < 0)
1474 return;
1475
1476 *(u32 *)((char *)current_evmcs + offset) = value;
1477 current_evmcs->hv_clean_fields &= ~clean_field;
1478}
1479
1480static inline void evmcs_write16(unsigned long field, u16 value)
1481{
1482 u16 clean_field;
1483 int offset = get_evmcs_offset(field, &clean_field);
1484
1485 if (offset < 0)
1486 return;
1487
1488 *(u16 *)((char *)current_evmcs + offset) = value;
1489 current_evmcs->hv_clean_fields &= ~clean_field;
1490}
1491
1492static inline u64 evmcs_read64(unsigned long field)
1493{
1494 int offset = get_evmcs_offset(field, NULL);
1495
1496 if (offset < 0)
1497 return 0;
1498
1499 return *(u64 *)((char *)current_evmcs + offset);
1500}
1501
1502static inline u32 evmcs_read32(unsigned long field)
1503{
1504 int offset = get_evmcs_offset(field, NULL);
1505
1506 if (offset < 0)
1507 return 0;
1508
1509 return *(u32 *)((char *)current_evmcs + offset);
1510}
1511
1512static inline u16 evmcs_read16(unsigned long field)
1513{
1514 int offset = get_evmcs_offset(field, NULL);
1515
1516 if (offset < 0)
1517 return 0;
1518
1519 return *(u16 *)((char *)current_evmcs + offset);
1520}
1521
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001522static inline void evmcs_touch_msr_bitmap(void)
1523{
1524 if (unlikely(!current_evmcs))
1525 return;
1526
1527 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1528 current_evmcs->hv_clean_fields &=
1529 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1530}
1531
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001532static void evmcs_load(u64 phys_addr)
1533{
1534 struct hv_vp_assist_page *vp_ap =
1535 hv_get_vp_assist_page(smp_processor_id());
1536
1537 vp_ap->current_nested_vmcs = phys_addr;
1538 vp_ap->enlighten_vmentry = 1;
1539}
1540
1541static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1542{
Vitaly Kuznetsov5d7a6442018-10-16 18:50:00 +02001543 vmcs_conf->pin_based_exec_ctrl &= ~EVMCS1_UNSUPPORTED_PINCTRL;
1544 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~EVMCS1_UNSUPPORTED_2NDEXEC;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001545
Vitaly Kuznetsov5d7a6442018-10-16 18:50:00 +02001546 vmcs_conf->vmexit_ctrl &= ~EVMCS1_UNSUPPORTED_VMEXIT_CTRL;
1547 vmcs_conf->vmentry_ctrl &= ~EVMCS1_UNSUPPORTED_VMENTRY_CTRL;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001548
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001549}
Tianyu Lan877ad952018-07-19 08:40:23 +00001550
1551/* check_ept_pointer() should be under protection of ept_pointer_lock. */
1552static void check_ept_pointer_match(struct kvm *kvm)
1553{
1554 struct kvm_vcpu *vcpu;
1555 u64 tmp_eptp = INVALID_PAGE;
1556 int i;
1557
1558 kvm_for_each_vcpu(i, vcpu, kvm) {
1559 if (!VALID_PAGE(tmp_eptp)) {
1560 tmp_eptp = to_vmx(vcpu)->ept_pointer;
1561 } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
1562 to_kvm_vmx(kvm)->ept_pointers_match
1563 = EPT_POINTERS_MISMATCH;
1564 return;
1565 }
1566 }
1567
1568 to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
1569}
1570
1571static int vmx_hv_remote_flush_tlb(struct kvm *kvm)
1572{
Lan Tianyua5c214d2018-10-13 22:54:05 +08001573 struct kvm_vcpu *vcpu;
1574 int ret = -ENOTSUPP, i;
Tianyu Lan877ad952018-07-19 08:40:23 +00001575
1576 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1577
1578 if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
1579 check_ept_pointer_match(kvm);
1580
Vitaly Kuznetsov5f8bb002018-10-11 12:03:12 +02001581 /*
1582 * FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE hypercall needs the address of the
1583 * base of EPT PML4 table, strip off EPT configuration information.
1584 */
Tianyu Lan877ad952018-07-19 08:40:23 +00001585 if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
Lan Tianyua5c214d2018-10-13 22:54:05 +08001586 kvm_for_each_vcpu(i, vcpu, kvm)
1587 ret |= hyperv_flush_guest_mapping(
Linus Torvalds0d1e8b82018-10-25 17:57:35 -07001588 to_vmx(kvm_get_vcpu(kvm, i))->ept_pointer & PAGE_MASK);
Lan Tianyua5c214d2018-10-13 22:54:05 +08001589 } else {
1590 ret = hyperv_flush_guest_mapping(
Linus Torvalds0d1e8b82018-10-25 17:57:35 -07001591 to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer & PAGE_MASK);
Tianyu Lan877ad952018-07-19 08:40:23 +00001592 }
Tianyu Lan877ad952018-07-19 08:40:23 +00001593
Tianyu Lan877ad952018-07-19 08:40:23 +00001594 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1595 return ret;
1596}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001597#else /* !IS_ENABLED(CONFIG_HYPERV) */
1598static inline void evmcs_write64(unsigned long field, u64 value) {}
1599static inline void evmcs_write32(unsigned long field, u32 value) {}
1600static inline void evmcs_write16(unsigned long field, u16 value) {}
1601static inline u64 evmcs_read64(unsigned long field) { return 0; }
1602static inline u32 evmcs_read32(unsigned long field) { return 0; }
1603static inline u16 evmcs_read16(unsigned long field) { return 0; }
1604static inline void evmcs_load(u64 phys_addr) {}
1605static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001606static inline void evmcs_touch_msr_bitmap(void) {}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001607#endif /* IS_ENABLED(CONFIG_HYPERV) */
1608
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02001609static int nested_enable_evmcs(struct kvm_vcpu *vcpu,
1610 uint16_t *vmcs_version)
1611{
1612 struct vcpu_vmx *vmx = to_vmx(vcpu);
1613
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02001614 /*
1615 * vmcs_version represents the range of supported Enlightened VMCS
1616 * versions: lower 8 bits is the minimal version, higher 8 bits is the
1617 * maximum supported version. KVM supports versions from 1 to
1618 * KVM_EVMCS_VERSION.
1619 */
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02001620 if (vmcs_version)
1621 *vmcs_version = (KVM_EVMCS_VERSION << 8) | 1;
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02001622
Liran Alon7f9ad1d2018-11-17 14:05:06 +02001623 /* We don't support disabling the feature for simplicity. */
1624 if (vmx->nested.enlightened_vmcs_enabled)
1625 return 0;
1626
1627 vmx->nested.enlightened_vmcs_enabled = true;
1628
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02001629 vmx->nested.msrs.pinbased_ctls_high &= ~EVMCS1_UNSUPPORTED_PINCTRL;
1630 vmx->nested.msrs.entry_ctls_high &= ~EVMCS1_UNSUPPORTED_VMENTRY_CTRL;
1631 vmx->nested.msrs.exit_ctls_high &= ~EVMCS1_UNSUPPORTED_VMEXIT_CTRL;
1632 vmx->nested.msrs.secondary_ctls_high &= ~EVMCS1_UNSUPPORTED_2NDEXEC;
1633 vmx->nested.msrs.vmfunc_controls &= ~EVMCS1_UNSUPPORTED_VMFUNC;
1634
1635 return 0;
1636}
1637
Jan Kiszka5bb16012016-02-09 20:14:21 +01001638static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001639{
1640 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1641 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001642 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1643}
1644
Jan Kiszka6f054852016-02-09 20:15:18 +01001645static inline bool is_debug(u32 intr_info)
1646{
1647 return is_exception_n(intr_info, DB_VECTOR);
1648}
1649
1650static inline bool is_breakpoint(u32 intr_info)
1651{
1652 return is_exception_n(intr_info, BP_VECTOR);
1653}
1654
Jan Kiszka5bb16012016-02-09 20:14:21 +01001655static inline bool is_page_fault(u32 intr_info)
1656{
1657 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001658}
1659
Gui Jianfeng31299942010-03-15 17:29:09 +08001660static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001661{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001662 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001663}
1664
Liran Alon9e869482018-03-12 13:12:51 +02001665static inline bool is_gp_fault(u32 intr_info)
1666{
1667 return is_exception_n(intr_info, GP_VECTOR);
1668}
1669
Gui Jianfeng31299942010-03-15 17:29:09 +08001670static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001671{
1672 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1673 INTR_INFO_VALID_MASK)) ==
1674 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1675}
1676
Linus Torvalds32d43cd2018-03-20 12:16:59 -07001677/* Undocumented: icebp/int1 */
1678static inline bool is_icebp(u32 intr_info)
1679{
1680 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1681 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1682}
1683
Gui Jianfeng31299942010-03-15 17:29:09 +08001684static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001685{
Sheng Yang04547152009-04-01 15:52:31 +08001686 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001687}
1688
Gui Jianfeng31299942010-03-15 17:29:09 +08001689static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001690{
Sheng Yang04547152009-04-01 15:52:31 +08001691 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001692}
1693
Paolo Bonzini35754c92015-07-29 12:05:37 +02001694static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001695{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001696 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001697}
1698
Gui Jianfeng31299942010-03-15 17:29:09 +08001699static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001700{
Sheng Yang04547152009-04-01 15:52:31 +08001701 return vmcs_config.cpu_based_exec_ctrl &
1702 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001703}
1704
Avi Kivity774ead32007-12-26 13:57:04 +02001705static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001706{
Sheng Yang04547152009-04-01 15:52:31 +08001707 return vmcs_config.cpu_based_2nd_exec_ctrl &
1708 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1709}
1710
Yang Zhang8d146952013-01-25 10:18:50 +08001711static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1712{
1713 return vmcs_config.cpu_based_2nd_exec_ctrl &
1714 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1715}
1716
Yang Zhang83d4c282013-01-25 10:18:49 +08001717static inline bool cpu_has_vmx_apic_register_virt(void)
1718{
1719 return vmcs_config.cpu_based_2nd_exec_ctrl &
1720 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1721}
1722
Yang Zhangc7c9c562013-01-25 10:18:51 +08001723static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1724{
1725 return vmcs_config.cpu_based_2nd_exec_ctrl &
1726 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1727}
1728
Sean Christopherson0b665d32018-08-14 09:33:34 -07001729static inline bool cpu_has_vmx_encls_vmexit(void)
1730{
1731 return vmcs_config.cpu_based_2nd_exec_ctrl &
1732 SECONDARY_EXEC_ENCLS_EXITING;
1733}
1734
Yunhong Jiang64672c92016-06-13 14:19:59 -07001735/*
1736 * Comment's format: document - errata name - stepping - processor name.
1737 * Refer from
1738 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1739 */
1740static u32 vmx_preemption_cpu_tfms[] = {
1741/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
17420x000206E6,
1743/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1744/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1745/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
17460x00020652,
1747/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
17480x00020655,
1749/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1750/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1751/*
1752 * 320767.pdf - AAP86 - B1 -
1753 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1754 */
17550x000106E5,
1756/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
17570x000106A0,
1758/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
17590x000106A1,
1760/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
17610x000106A4,
1762 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1763 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1764 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
17650x000106A5,
1766};
1767
1768static inline bool cpu_has_broken_vmx_preemption_timer(void)
1769{
1770 u32 eax = cpuid_eax(0x00000001), i;
1771
1772 /* Clear the reserved bits */
1773 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001774 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001775 if (eax == vmx_preemption_cpu_tfms[i])
1776 return true;
1777
1778 return false;
1779}
1780
1781static inline bool cpu_has_vmx_preemption_timer(void)
1782{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001783 return vmcs_config.pin_based_exec_ctrl &
1784 PIN_BASED_VMX_PREEMPTION_TIMER;
1785}
1786
Yang Zhang01e439b2013-04-11 19:25:12 +08001787static inline bool cpu_has_vmx_posted_intr(void)
1788{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001789 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1790 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001791}
1792
1793static inline bool cpu_has_vmx_apicv(void)
1794{
1795 return cpu_has_vmx_apic_register_virt() &&
1796 cpu_has_vmx_virtual_intr_delivery() &&
1797 cpu_has_vmx_posted_intr();
1798}
1799
Sheng Yang04547152009-04-01 15:52:31 +08001800static inline bool cpu_has_vmx_flexpriority(void)
1801{
1802 return cpu_has_vmx_tpr_shadow() &&
1803 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001804}
1805
Marcelo Tosattie7997942009-06-11 12:07:40 -03001806static inline bool cpu_has_vmx_ept_execute_only(void)
1807{
Gui Jianfeng31299942010-03-15 17:29:09 +08001808 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001809}
1810
Marcelo Tosattie7997942009-06-11 12:07:40 -03001811static inline bool cpu_has_vmx_ept_2m_page(void)
1812{
Gui Jianfeng31299942010-03-15 17:29:09 +08001813 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001814}
1815
Sheng Yang878403b2010-01-05 19:02:29 +08001816static inline bool cpu_has_vmx_ept_1g_page(void)
1817{
Gui Jianfeng31299942010-03-15 17:29:09 +08001818 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001819}
1820
Sheng Yang4bc9b982010-06-02 14:05:24 +08001821static inline bool cpu_has_vmx_ept_4levels(void)
1822{
1823 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1824}
1825
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001826static inline bool cpu_has_vmx_ept_mt_wb(void)
1827{
1828 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1829}
1830
Yu Zhang855feb62017-08-24 20:27:55 +08001831static inline bool cpu_has_vmx_ept_5levels(void)
1832{
1833 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1834}
1835
Xudong Hao83c3a332012-05-28 19:33:35 +08001836static inline bool cpu_has_vmx_ept_ad_bits(void)
1837{
1838 return vmx_capability.ept & VMX_EPT_AD_BIT;
1839}
1840
Gui Jianfeng31299942010-03-15 17:29:09 +08001841static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001842{
Gui Jianfeng31299942010-03-15 17:29:09 +08001843 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001844}
1845
Gui Jianfeng31299942010-03-15 17:29:09 +08001846static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001847{
Gui Jianfeng31299942010-03-15 17:29:09 +08001848 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001849}
1850
Liran Aloncd9a4912018-05-22 17:16:15 +03001851static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1852{
1853 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1854}
1855
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001856static inline bool cpu_has_vmx_invvpid_single(void)
1857{
1858 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1859}
1860
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001861static inline bool cpu_has_vmx_invvpid_global(void)
1862{
1863 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1864}
1865
Wanpeng Li08d839c2017-03-23 05:30:08 -07001866static inline bool cpu_has_vmx_invvpid(void)
1867{
1868 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1869}
1870
Gui Jianfeng31299942010-03-15 17:29:09 +08001871static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001872{
Sheng Yang04547152009-04-01 15:52:31 +08001873 return vmcs_config.cpu_based_2nd_exec_ctrl &
1874 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001875}
1876
Gui Jianfeng31299942010-03-15 17:29:09 +08001877static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001878{
1879 return vmcs_config.cpu_based_2nd_exec_ctrl &
1880 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1881}
1882
Gui Jianfeng31299942010-03-15 17:29:09 +08001883static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001884{
1885 return vmcs_config.cpu_based_2nd_exec_ctrl &
1886 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1887}
1888
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001889static inline bool cpu_has_vmx_basic_inout(void)
1890{
1891 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1892}
1893
Paolo Bonzini35754c92015-07-29 12:05:37 +02001894static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001895{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001896 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001897}
1898
Gui Jianfeng31299942010-03-15 17:29:09 +08001899static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001900{
Sheng Yang04547152009-04-01 15:52:31 +08001901 return vmcs_config.cpu_based_2nd_exec_ctrl &
1902 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001903}
1904
Gui Jianfeng31299942010-03-15 17:29:09 +08001905static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001906{
1907 return vmcs_config.cpu_based_2nd_exec_ctrl &
1908 SECONDARY_EXEC_RDTSCP;
1909}
1910
Mao, Junjiead756a12012-07-02 01:18:48 +00001911static inline bool cpu_has_vmx_invpcid(void)
1912{
1913 return vmcs_config.cpu_based_2nd_exec_ctrl &
1914 SECONDARY_EXEC_ENABLE_INVPCID;
1915}
1916
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001917static inline bool cpu_has_virtual_nmis(void)
1918{
1919 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1920}
1921
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001922static inline bool cpu_has_vmx_wbinvd_exit(void)
1923{
1924 return vmcs_config.cpu_based_2nd_exec_ctrl &
1925 SECONDARY_EXEC_WBINVD_EXITING;
1926}
1927
Abel Gordonabc4fc52013-04-18 14:35:25 +03001928static inline bool cpu_has_vmx_shadow_vmcs(void)
1929{
1930 u64 vmx_msr;
1931 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1932 /* check if the cpu supports writing r/o exit information fields */
1933 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1934 return false;
1935
1936 return vmcs_config.cpu_based_2nd_exec_ctrl &
1937 SECONDARY_EXEC_SHADOW_VMCS;
1938}
1939
Kai Huang843e4332015-01-28 10:54:28 +08001940static inline bool cpu_has_vmx_pml(void)
1941{
1942 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1943}
1944
Haozhong Zhang64903d62015-10-20 15:39:09 +08001945static inline bool cpu_has_vmx_tsc_scaling(void)
1946{
1947 return vmcs_config.cpu_based_2nd_exec_ctrl &
1948 SECONDARY_EXEC_TSC_SCALING;
1949}
1950
Bandan Das2a499e42017-08-03 15:54:41 -04001951static inline bool cpu_has_vmx_vmfunc(void)
1952{
1953 return vmcs_config.cpu_based_2nd_exec_ctrl &
1954 SECONDARY_EXEC_ENABLE_VMFUNC;
1955}
1956
Sean Christopherson64f7a112018-04-30 10:01:06 -07001957static bool vmx_umip_emulated(void)
1958{
1959 return vmcs_config.cpu_based_2nd_exec_ctrl &
1960 SECONDARY_EXEC_DESC;
1961}
1962
Sheng Yang04547152009-04-01 15:52:31 +08001963static inline bool report_flexpriority(void)
1964{
1965 return flexpriority_enabled;
1966}
1967
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001968static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1969{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001970 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001971}
1972
Jim Mattsonf4160e42018-05-29 09:11:33 -07001973/*
1974 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1975 * to modify any valid field of the VMCS, or are the VM-exit
1976 * information fields read-only?
1977 */
1978static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1979{
1980 return to_vmx(vcpu)->nested.msrs.misc_low &
1981 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1982}
1983
Marc Orr04473782018-06-20 17:21:29 -07001984static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
1985{
1986 return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
1987}
1988
1989static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
1990{
1991 return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
1992 CPU_BASED_MONITOR_TRAP_FLAG;
1993}
1994
Liran Alonfa97d7d2018-07-18 14:07:59 +02001995static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu)
1996{
1997 return to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
1998 SECONDARY_EXEC_SHADOW_VMCS;
1999}
2000
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002001static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
2002{
2003 return vmcs12->cpu_based_vm_exec_control & bit;
2004}
2005
2006static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
2007{
2008 return (vmcs12->cpu_based_vm_exec_control &
2009 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
2010 (vmcs12->secondary_vm_exec_control & bit);
2011}
2012
Jan Kiszkaf41245002014-03-07 20:03:13 +01002013static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
2014{
2015 return vmcs12->pin_based_vm_exec_control &
2016 PIN_BASED_VMX_PREEMPTION_TIMER;
2017}
2018
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05002019static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
2020{
2021 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
2022}
2023
2024static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
2025{
2026 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
2027}
2028
Nadav Har'El155a97a2013-08-05 11:07:16 +03002029static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
2030{
2031 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
2032}
2033
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002034static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
2035{
Paolo Bonzini3db13482017-08-24 14:48:03 +02002036 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002037}
2038
Bandan Dasc5f983f2017-05-05 15:25:14 -04002039static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
2040{
2041 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
2042}
2043
Wincy Vanf2b93282015-02-03 23:56:03 +08002044static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
2045{
2046 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
2047}
2048
Wanpeng Li5c614b32015-10-13 09:18:36 -07002049static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
2050{
2051 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
2052}
2053
Wincy Van82f0dd42015-02-03 23:57:18 +08002054static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
2055{
2056 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
2057}
2058
Wincy Van608406e2015-02-03 23:57:51 +08002059static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
2060{
2061 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
2062}
2063
Wincy Van705699a2015-02-03 23:58:17 +08002064static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
2065{
2066 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
2067}
2068
Bandan Das27c42a12017-08-03 15:54:42 -04002069static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
2070{
2071 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
2072}
2073
Bandan Das41ab9372017-08-03 15:54:43 -04002074static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
2075{
2076 return nested_cpu_has_vmfunc(vmcs12) &&
2077 (vmcs12->vm_function_control &
2078 VMX_VMFUNC_EPTP_SWITCHING);
2079}
2080
Liran Alonf792d272018-06-23 02:35:05 +03002081static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12)
2082{
2083 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS);
2084}
2085
Krish Sadhukhan14aa61d2018-11-01 01:21:58 -04002086static inline bool nested_cpu_has_save_preemption_timer(struct vmcs12 *vmcs12)
2087{
2088 return vmcs12->vm_exit_controls &
2089 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER;
2090}
2091
Jim Mattsonef85b672016-12-12 11:01:37 -08002092static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03002093{
2094 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08002095 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03002096}
2097
Jan Kiszka533558b2014-01-04 18:47:20 +01002098static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
2099 u32 exit_intr_info,
2100 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03002101
Rusty Russell8b9cf982007-07-30 16:31:43 +10002102static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08002103{
2104 int i;
2105
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002106 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03002107 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03002108 return i;
2109 return -1;
2110}
2111
Uros Bizjak5ebb2722018-10-11 19:40:43 +02002112static inline void __invvpid(unsigned long ext, u16 vpid, gva_t gva)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002113{
2114 struct {
2115 u64 vpid : 16;
2116 u64 rsvd : 48;
2117 u64 gva;
2118 } operand = { vpid, 0, gva };
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002119 bool error;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002120
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002121 asm volatile (__ex("invvpid %2, %1") CC_SET(na)
2122 : CC_OUT(na) (error) : "r"(ext), "m"(operand));
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002123 BUG_ON(error);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002124}
2125
Uros Bizjak5ebb2722018-10-11 19:40:43 +02002126static inline void __invept(unsigned long ext, u64 eptp, gpa_t gpa)
Sheng Yang14394422008-04-28 12:24:45 +08002127{
2128 struct {
2129 u64 eptp, gpa;
2130 } operand = {eptp, gpa};
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002131 bool error;
Sheng Yang14394422008-04-28 12:24:45 +08002132
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002133 asm volatile (__ex("invept %2, %1") CC_SET(na)
2134 : CC_OUT(na) (error) : "r"(ext), "m"(operand));
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002135 BUG_ON(error);
Sheng Yang14394422008-04-28 12:24:45 +08002136}
2137
Avi Kivity26bb0982009-09-07 11:14:12 +03002138static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03002139{
2140 int i;
2141
Rusty Russell8b9cf982007-07-30 16:31:43 +10002142 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03002143 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002144 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00002145 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08002146}
2147
Avi Kivity6aa8b732006-12-10 02:21:36 -08002148static void vmcs_clear(struct vmcs *vmcs)
2149{
2150 u64 phys_addr = __pa(vmcs);
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002151 bool error;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002152
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002153 asm volatile (__ex("vmclear %1") CC_SET(na)
2154 : CC_OUT(na) (error) : "m"(phys_addr));
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002155 if (unlikely(error))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002156 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
2157 vmcs, phys_addr);
2158}
2159
Nadav Har'Eld462b812011-05-24 15:26:10 +03002160static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
2161{
2162 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07002163 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
2164 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002165 loaded_vmcs->cpu = -1;
2166 loaded_vmcs->launched = 0;
2167}
2168
Dongxiao Xu7725b892010-05-11 18:29:38 +08002169static void vmcs_load(struct vmcs *vmcs)
2170{
2171 u64 phys_addr = __pa(vmcs);
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002172 bool error;
Dongxiao Xu7725b892010-05-11 18:29:38 +08002173
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002174 if (static_branch_unlikely(&enable_evmcs))
2175 return evmcs_load(phys_addr);
2176
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002177 asm volatile (__ex("vmptrld %1") CC_SET(na)
2178 : CC_OUT(na) (error) : "m"(phys_addr));
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002179 if (unlikely(error))
Nadav Har'El2844d842011-05-25 23:16:40 +03002180 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08002181 vmcs, phys_addr);
2182}
2183
Dave Young2965faa2015-09-09 15:38:55 -07002184#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002185/*
2186 * This bitmap is used to indicate whether the vmclear
2187 * operation is enabled on all cpus. All disabled by
2188 * default.
2189 */
2190static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
2191
2192static inline void crash_enable_local_vmclear(int cpu)
2193{
2194 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
2195}
2196
2197static inline void crash_disable_local_vmclear(int cpu)
2198{
2199 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
2200}
2201
2202static inline int crash_local_vmclear_enabled(int cpu)
2203{
2204 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
2205}
2206
2207static void crash_vmclear_local_loaded_vmcss(void)
2208{
2209 int cpu = raw_smp_processor_id();
2210 struct loaded_vmcs *v;
2211
2212 if (!crash_local_vmclear_enabled(cpu))
2213 return;
2214
2215 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
2216 loaded_vmcss_on_cpu_link)
2217 vmcs_clear(v->vmcs);
2218}
2219#else
2220static inline void crash_enable_local_vmclear(int cpu) { }
2221static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07002222#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002223
Nadav Har'Eld462b812011-05-24 15:26:10 +03002224static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002225{
Nadav Har'Eld462b812011-05-24 15:26:10 +03002226 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08002227 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002228
Nadav Har'Eld462b812011-05-24 15:26:10 +03002229 if (loaded_vmcs->cpu != cpu)
2230 return; /* vcpu migration can race with cpu offline */
2231 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002232 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002233 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002234 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002235
2236 /*
2237 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
2238 * is before setting loaded_vmcs->vcpu to -1 which is done in
2239 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
2240 * then adds the vmcs into percpu list before it is deleted.
2241 */
2242 smp_wmb();
2243
Nadav Har'Eld462b812011-05-24 15:26:10 +03002244 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002245 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002246}
2247
Nadav Har'Eld462b812011-05-24 15:26:10 +03002248static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002249{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08002250 int cpu = loaded_vmcs->cpu;
2251
2252 if (cpu != -1)
2253 smp_call_function_single(cpu,
2254 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002255}
2256
Junaid Shahidfaff8752018-06-29 13:10:05 -07002257static inline bool vpid_sync_vcpu_addr(int vpid, gva_t addr)
2258{
2259 if (vpid == 0)
2260 return true;
2261
2262 if (cpu_has_vmx_invvpid_individual_addr()) {
2263 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, vpid, addr);
2264 return true;
2265 }
2266
2267 return false;
2268}
2269
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002270static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002271{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002272 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002273 return;
2274
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08002275 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002276 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002277}
2278
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002279static inline void vpid_sync_vcpu_global(void)
2280{
2281 if (cpu_has_vmx_invvpid_global())
2282 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
2283}
2284
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002285static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002286{
2287 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002288 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002289 else
2290 vpid_sync_vcpu_global();
2291}
2292
Sheng Yang14394422008-04-28 12:24:45 +08002293static inline void ept_sync_global(void)
2294{
David Hildenbrandf5f51582017-08-24 20:51:30 +02002295 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08002296}
2297
2298static inline void ept_sync_context(u64 eptp)
2299{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02002300 if (cpu_has_vmx_invept_context())
2301 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
2302 else
2303 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08002304}
2305
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002306static __always_inline void vmcs_check16(unsigned long field)
2307{
2308 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2309 "16-bit accessor invalid for 64-bit field");
2310 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2311 "16-bit accessor invalid for 64-bit high field");
2312 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2313 "16-bit accessor invalid for 32-bit high field");
2314 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2315 "16-bit accessor invalid for natural width field");
2316}
2317
2318static __always_inline void vmcs_check32(unsigned long field)
2319{
2320 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2321 "32-bit accessor invalid for 16-bit field");
2322 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2323 "32-bit accessor invalid for natural width field");
2324}
2325
2326static __always_inline void vmcs_check64(unsigned long field)
2327{
2328 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2329 "64-bit accessor invalid for 16-bit field");
2330 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2331 "64-bit accessor invalid for 64-bit high field");
2332 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2333 "64-bit accessor invalid for 32-bit field");
2334 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2335 "64-bit accessor invalid for natural width field");
2336}
2337
2338static __always_inline void vmcs_checkl(unsigned long field)
2339{
2340 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2341 "Natural width accessor invalid for 16-bit field");
2342 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2343 "Natural width accessor invalid for 64-bit field");
2344 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2345 "Natural width accessor invalid for 64-bit high field");
2346 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2347 "Natural width accessor invalid for 32-bit field");
2348}
2349
2350static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002351{
Avi Kivity5e520e62011-05-15 10:13:12 -04002352 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002353
Uros Bizjak44c2d662018-10-11 19:40:45 +02002354 asm volatile (__ex_clear("vmread %1, %0", "%k0")
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002355 : "=r"(value) : "r"(field));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002356 return value;
2357}
2358
Avi Kivity96304212011-05-15 10:13:13 -04002359static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002360{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002361 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002362 if (static_branch_unlikely(&enable_evmcs))
2363 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002364 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002365}
2366
Avi Kivity96304212011-05-15 10:13:13 -04002367static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002368{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002369 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002370 if (static_branch_unlikely(&enable_evmcs))
2371 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002372 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002373}
2374
Avi Kivity96304212011-05-15 10:13:13 -04002375static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002376{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002377 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002378 if (static_branch_unlikely(&enable_evmcs))
2379 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002380#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002381 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002382#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002383 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002384#endif
2385}
2386
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002387static __always_inline unsigned long vmcs_readl(unsigned long field)
2388{
2389 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002390 if (static_branch_unlikely(&enable_evmcs))
2391 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002392 return __vmcs_readl(field);
2393}
2394
Avi Kivitye52de1b2007-01-05 16:36:56 -08002395static noinline void vmwrite_error(unsigned long field, unsigned long value)
2396{
2397 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2398 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2399 dump_stack();
2400}
2401
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002402static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002403{
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002404 bool error;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002405
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002406 asm volatile (__ex("vmwrite %2, %1") CC_SET(na)
2407 : CC_OUT(na) (error) : "r"(field), "rm"(value));
Avi Kivitye52de1b2007-01-05 16:36:56 -08002408 if (unlikely(error))
2409 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002410}
2411
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002412static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002413{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002414 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002415 if (static_branch_unlikely(&enable_evmcs))
2416 return evmcs_write16(field, value);
2417
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002418 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002419}
2420
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002421static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002422{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002423 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002424 if (static_branch_unlikely(&enable_evmcs))
2425 return evmcs_write32(field, value);
2426
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002427 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002428}
2429
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002430static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002431{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002432 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002433 if (static_branch_unlikely(&enable_evmcs))
2434 return evmcs_write64(field, value);
2435
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002436 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03002437#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002438 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002439 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002440#endif
2441}
2442
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002443static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002444{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002445 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002446 if (static_branch_unlikely(&enable_evmcs))
2447 return evmcs_write64(field, value);
2448
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002449 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002450}
2451
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002452static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002453{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002454 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2455 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002456 if (static_branch_unlikely(&enable_evmcs))
2457 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2458
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002459 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2460}
2461
2462static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2463{
2464 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2465 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002466 if (static_branch_unlikely(&enable_evmcs))
2467 return evmcs_write32(field, evmcs_read32(field) | mask);
2468
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002469 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002470}
2471
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002472static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2473{
2474 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2475}
2476
Gleb Natapov2961e8762013-11-25 15:37:13 +02002477static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2478{
2479 vmcs_write32(VM_ENTRY_CONTROLS, val);
2480 vmx->vm_entry_controls_shadow = val;
2481}
2482
2483static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2484{
2485 if (vmx->vm_entry_controls_shadow != val)
2486 vm_entry_controls_init(vmx, val);
2487}
2488
2489static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2490{
2491 return vmx->vm_entry_controls_shadow;
2492}
2493
2494
2495static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2496{
2497 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2498}
2499
2500static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2501{
2502 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2503}
2504
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002505static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2506{
2507 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2508}
2509
Gleb Natapov2961e8762013-11-25 15:37:13 +02002510static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2511{
2512 vmcs_write32(VM_EXIT_CONTROLS, val);
2513 vmx->vm_exit_controls_shadow = val;
2514}
2515
2516static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2517{
2518 if (vmx->vm_exit_controls_shadow != val)
2519 vm_exit_controls_init(vmx, val);
2520}
2521
2522static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2523{
2524 return vmx->vm_exit_controls_shadow;
2525}
2526
2527
2528static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2529{
2530 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2531}
2532
2533static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2534{
2535 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2536}
2537
Avi Kivity2fb92db2011-04-27 19:42:18 +03002538static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2539{
2540 vmx->segment_cache.bitmask = 0;
2541}
2542
2543static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2544 unsigned field)
2545{
2546 bool ret;
2547 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2548
2549 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2550 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2551 vmx->segment_cache.bitmask = 0;
2552 }
2553 ret = vmx->segment_cache.bitmask & mask;
2554 vmx->segment_cache.bitmask |= mask;
2555 return ret;
2556}
2557
2558static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2559{
2560 u16 *p = &vmx->segment_cache.seg[seg].selector;
2561
2562 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2563 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2564 return *p;
2565}
2566
2567static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2568{
2569 ulong *p = &vmx->segment_cache.seg[seg].base;
2570
2571 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2572 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2573 return *p;
2574}
2575
2576static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2577{
2578 u32 *p = &vmx->segment_cache.seg[seg].limit;
2579
2580 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2581 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2582 return *p;
2583}
2584
2585static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2586{
2587 u32 *p = &vmx->segment_cache.seg[seg].ar;
2588
2589 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2590 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2591 return *p;
2592}
2593
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002594static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2595{
2596 u32 eb;
2597
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002598 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002599 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002600 /*
2601 * Guest access to VMware backdoor ports could legitimately
2602 * trigger #GP because of TSS I/O permission bitmap.
2603 * We intercept those #GP and allow access to them anyway
2604 * as VMware does.
2605 */
2606 if (enable_vmware_backdoor)
2607 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002608 if ((vcpu->guest_debug &
2609 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2610 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2611 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002612 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002613 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002614 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002615 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002616
2617 /* When we are running a nested L2 guest and L1 specified for it a
2618 * certain exception bitmap, we must trap the same exceptions and pass
2619 * them to L1. When running L2, we will only handle the exceptions
2620 * specified above if L1 did not want them.
2621 */
2622 if (is_guest_mode(vcpu))
2623 eb |= get_vmcs12(vcpu)->exception_bitmap;
2624
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002625 vmcs_write32(EXCEPTION_BITMAP, eb);
2626}
2627
Ashok Raj15d45072018-02-01 22:59:43 +01002628/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002629 * Check if MSR is intercepted for currently loaded MSR bitmap.
2630 */
2631static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2632{
2633 unsigned long *msr_bitmap;
2634 int f = sizeof(unsigned long);
2635
2636 if (!cpu_has_vmx_msr_bitmap())
2637 return true;
2638
2639 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2640
2641 if (msr <= 0x1fff) {
2642 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2643 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2644 msr &= 0x1fff;
2645 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2646 }
2647
2648 return true;
2649}
2650
2651/*
Ashok Raj15d45072018-02-01 22:59:43 +01002652 * Check if MSR is intercepted for L01 MSR bitmap.
2653 */
2654static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2655{
2656 unsigned long *msr_bitmap;
2657 int f = sizeof(unsigned long);
2658
2659 if (!cpu_has_vmx_msr_bitmap())
2660 return true;
2661
2662 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2663
2664 if (msr <= 0x1fff) {
2665 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2666 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2667 msr &= 0x1fff;
2668 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2669 }
2670
2671 return true;
2672}
2673
Gleb Natapov2961e8762013-11-25 15:37:13 +02002674static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2675 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002676{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002677 vm_entry_controls_clearbit(vmx, entry);
2678 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002679}
2680
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002681static int find_msr(struct vmx_msrs *m, unsigned int msr)
2682{
2683 unsigned int i;
2684
2685 for (i = 0; i < m->nr; ++i) {
2686 if (m->val[i].index == msr)
2687 return i;
2688 }
2689 return -ENOENT;
2690}
2691
Avi Kivity61d2ef22010-04-28 16:40:38 +03002692static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2693{
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002694 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002695 struct msr_autoload *m = &vmx->msr_autoload;
2696
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002697 switch (msr) {
2698 case MSR_EFER:
2699 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002700 clear_atomic_switch_msr_special(vmx,
2701 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002702 VM_EXIT_LOAD_IA32_EFER);
2703 return;
2704 }
2705 break;
2706 case MSR_CORE_PERF_GLOBAL_CTRL:
2707 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002708 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002709 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2710 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2711 return;
2712 }
2713 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002714 }
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002715 i = find_msr(&m->guest, msr);
2716 if (i < 0)
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002717 goto skip_guest;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002718 --m->guest.nr;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002719 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002720 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Avi Kivity110312c2010-12-21 12:54:20 +02002721
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002722skip_guest:
2723 i = find_msr(&m->host, msr);
2724 if (i < 0)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002725 return;
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002726
2727 --m->host.nr;
2728 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002729 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002730}
2731
Gleb Natapov2961e8762013-11-25 15:37:13 +02002732static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2733 unsigned long entry, unsigned long exit,
2734 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2735 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002736{
2737 vmcs_write64(guest_val_vmcs, guest_val);
Sean Christopherson5a5e8a12018-09-26 09:23:56 -07002738 if (host_val_vmcs != HOST_IA32_EFER)
2739 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002740 vm_entry_controls_setbit(vmx, entry);
2741 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002742}
2743
Avi Kivity61d2ef22010-04-28 16:40:38 +03002744static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002745 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002746{
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002747 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002748 struct msr_autoload *m = &vmx->msr_autoload;
2749
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002750 switch (msr) {
2751 case MSR_EFER:
2752 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002753 add_atomic_switch_msr_special(vmx,
2754 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002755 VM_EXIT_LOAD_IA32_EFER,
2756 GUEST_IA32_EFER,
2757 HOST_IA32_EFER,
2758 guest_val, host_val);
2759 return;
2760 }
2761 break;
2762 case MSR_CORE_PERF_GLOBAL_CTRL:
2763 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002764 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002765 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2766 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2767 GUEST_IA32_PERF_GLOBAL_CTRL,
2768 HOST_IA32_PERF_GLOBAL_CTRL,
2769 guest_val, host_val);
2770 return;
2771 }
2772 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002773 case MSR_IA32_PEBS_ENABLE:
2774 /* PEBS needs a quiescent period after being disabled (to write
2775 * a record). Disabling PEBS through VMX MSR swapping doesn't
2776 * provide that period, so a CPU could write host's record into
2777 * guest's memory.
2778 */
2779 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002780 }
2781
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002782 i = find_msr(&m->guest, msr);
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002783 if (!entry_only)
2784 j = find_msr(&m->host, msr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002785
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002786 if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002787 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002788 "Can't add msr %x\n", msr);
2789 return;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002790 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002791 if (i < 0) {
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002792 i = m->guest.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002793 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002794 }
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002795 m->guest.val[i].index = msr;
2796 m->guest.val[i].value = guest_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002797
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002798 if (entry_only)
2799 return;
2800
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002801 if (j < 0) {
2802 j = m->host.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002803 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002804 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002805 m->host.val[j].index = msr;
2806 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002807}
2808
Avi Kivity92c0d902009-10-29 11:00:16 +02002809static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002810{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002811 u64 guest_efer = vmx->vcpu.arch.efer;
2812 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002813
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002814 if (!enable_ept) {
2815 /*
2816 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2817 * host CPUID is more efficient than testing guest CPUID
2818 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2819 */
2820 if (boot_cpu_has(X86_FEATURE_SMEP))
2821 guest_efer |= EFER_NX;
2822 else if (!(guest_efer & EFER_NX))
2823 ignore_bits |= EFER_NX;
2824 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002825
Avi Kivity51c6cf62007-08-29 03:48:05 +03002826 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002827 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002828 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002829 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002830#ifdef CONFIG_X86_64
2831 ignore_bits |= EFER_LMA | EFER_LME;
2832 /* SCE is meaningful only in long mode on Intel */
2833 if (guest_efer & EFER_LMA)
2834 ignore_bits &= ~(u64)EFER_SCE;
2835#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002836
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002837 /*
2838 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2839 * On CPUs that support "load IA32_EFER", always switch EFER
2840 * atomically, since it's faster than switching it manually.
2841 */
2842 if (cpu_has_load_ia32_efer ||
2843 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002844 if (!(guest_efer & EFER_LMA))
2845 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002846 if (guest_efer != host_efer)
2847 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002848 guest_efer, host_efer, false);
Sean Christopherson02343cf2018-09-26 09:23:43 -07002849 else
2850 clear_atomic_switch_msr(vmx, MSR_EFER);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002851 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002852 } else {
Sean Christopherson02343cf2018-09-26 09:23:43 -07002853 clear_atomic_switch_msr(vmx, MSR_EFER);
2854
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002855 guest_efer &= ~ignore_bits;
2856 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002857
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002858 vmx->guest_msrs[efer_offset].data = guest_efer;
2859 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2860
2861 return true;
2862 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002863}
2864
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002865#ifdef CONFIG_X86_32
2866/*
2867 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2868 * VMCS rather than the segment table. KVM uses this helper to figure
2869 * out the current bases to poke them into the VMCS before entry.
2870 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002871static unsigned long segment_base(u16 selector)
2872{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002873 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002874 unsigned long v;
2875
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002876 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002877 return 0;
2878
Thomas Garnier45fc8752017-03-14 10:05:08 -07002879 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002880
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002881 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002882 u16 ldt_selector = kvm_read_ldt();
2883
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002884 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002885 return 0;
2886
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002887 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002888 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002889 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002890 return v;
2891}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002892#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002893
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002894static void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002895{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002896 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersond7ee0392018-07-23 12:32:47 -07002897 struct vmcs_host_state *host_state;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002898#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002899 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002900#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002901 unsigned long fs_base, gs_base;
2902 u16 fs_sel, gs_sel;
Avi Kivity26bb0982009-09-07 11:14:12 +03002903 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002904
Sean Christophersond264ee02018-08-27 15:21:12 -07002905 vmx->req_immediate_exit = false;
2906
Liran Alonf48b4712018-11-20 18:03:25 +02002907 /*
2908 * Note that guest MSRs to be saved/restored can also be changed
2909 * when guest state is loaded. This happens when guest transitions
2910 * to/from long-mode by setting MSR_EFER.LMA.
2911 */
2912 if (!vmx->loaded_cpu_state || vmx->guest_msrs_dirty) {
2913 vmx->guest_msrs_dirty = false;
2914 for (i = 0; i < vmx->save_nmsrs; ++i)
2915 kvm_set_shared_msr(vmx->guest_msrs[i].index,
2916 vmx->guest_msrs[i].data,
2917 vmx->guest_msrs[i].mask);
2918
2919 }
2920
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002921 if (vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002922 return;
2923
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002924 vmx->loaded_cpu_state = vmx->loaded_vmcs;
Sean Christophersond7ee0392018-07-23 12:32:47 -07002925 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002926
Avi Kivity33ed6322007-05-02 16:54:03 +03002927 /*
2928 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2929 * allow segment selectors with cpl > 0 or ti == 1.
2930 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07002931 host_state->ldt_sel = kvm_read_ldt();
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002932
2933#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002934 savesegment(ds, host_state->ds_sel);
2935 savesegment(es, host_state->es_sel);
Sean Christophersone368b872018-07-23 12:32:41 -07002936
2937 gs_base = cpu_kernelmode_gs_base(cpu);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002938 if (likely(is_64bit_mm(current->mm))) {
2939 save_fsgs_for_kvm();
Sean Christophersone368b872018-07-23 12:32:41 -07002940 fs_sel = current->thread.fsindex;
2941 gs_sel = current->thread.gsindex;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002942 fs_base = current->thread.fsbase;
Sean Christophersone368b872018-07-23 12:32:41 -07002943 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002944 } else {
Sean Christophersone368b872018-07-23 12:32:41 -07002945 savesegment(fs, fs_sel);
2946 savesegment(gs, gs_sel);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002947 fs_base = read_msr(MSR_FS_BASE);
Sean Christophersone368b872018-07-23 12:32:41 -07002948 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
Avi Kivity33ed6322007-05-02 16:54:03 +03002949 }
2950
Paolo Bonzini4679b612018-09-24 17:23:01 +02002951 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002952#else
Sean Christophersone368b872018-07-23 12:32:41 -07002953 savesegment(fs, fs_sel);
2954 savesegment(gs, gs_sel);
2955 fs_base = segment_base(fs_sel);
2956 gs_base = segment_base(gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002957#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002958
Sean Christopherson8f21a0b2018-07-23 12:32:49 -07002959 if (unlikely(fs_sel != host_state->fs_sel)) {
2960 if (!(fs_sel & 7))
2961 vmcs_write16(HOST_FS_SELECTOR, fs_sel);
2962 else
2963 vmcs_write16(HOST_FS_SELECTOR, 0);
2964 host_state->fs_sel = fs_sel;
2965 }
2966 if (unlikely(gs_sel != host_state->gs_sel)) {
2967 if (!(gs_sel & 7))
2968 vmcs_write16(HOST_GS_SELECTOR, gs_sel);
2969 else
2970 vmcs_write16(HOST_GS_SELECTOR, 0);
2971 host_state->gs_sel = gs_sel;
2972 }
Sean Christopherson5e079c72018-07-23 12:32:50 -07002973 if (unlikely(fs_base != host_state->fs_base)) {
2974 vmcs_writel(HOST_FS_BASE, fs_base);
2975 host_state->fs_base = fs_base;
2976 }
2977 if (unlikely(gs_base != host_state->gs_base)) {
2978 vmcs_writel(HOST_GS_BASE, gs_base);
2979 host_state->gs_base = gs_base;
2980 }
Avi Kivity33ed6322007-05-02 16:54:03 +03002981}
2982
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002983static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002984{
Sean Christophersond7ee0392018-07-23 12:32:47 -07002985 struct vmcs_host_state *host_state;
2986
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002987 if (!vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002988 return;
2989
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002990 WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs);
Sean Christophersond7ee0392018-07-23 12:32:47 -07002991 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002992
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002993 ++vmx->vcpu.stat.host_state_reload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002994 vmx->loaded_cpu_state = NULL;
2995
Avi Kivityc8770e72010-11-11 12:37:26 +02002996#ifdef CONFIG_X86_64
Paolo Bonzini4679b612018-09-24 17:23:01 +02002997 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivityc8770e72010-11-11 12:37:26 +02002998#endif
Sean Christophersond7ee0392018-07-23 12:32:47 -07002999 if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
3000 kvm_load_ldt(host_state->ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03003001#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07003002 load_gs_index(host_state->gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02003003#else
Sean Christophersond7ee0392018-07-23 12:32:47 -07003004 loadsegment(gs, host_state->gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03003005#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03003006 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07003007 if (host_state->fs_sel & 7)
3008 loadsegment(fs, host_state->fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03003009#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07003010 if (unlikely(host_state->ds_sel | host_state->es_sel)) {
3011 loadsegment(ds, host_state->ds_sel);
3012 loadsegment(es, host_state->es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03003013 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03003014#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08003015 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03003016#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02003017 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03003018#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07003019 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03003020}
3021
Sean Christopherson678e3152018-07-23 12:32:43 -07003022#ifdef CONFIG_X86_64
3023static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
Avi Kivitya9b21b62008-06-24 11:48:49 +03003024{
Paolo Bonzini4679b612018-09-24 17:23:01 +02003025 preempt_disable();
3026 if (vmx->loaded_cpu_state)
3027 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
3028 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07003029 return vmx->msr_guest_kernel_gs_base;
Avi Kivitya9b21b62008-06-24 11:48:49 +03003030}
3031
Sean Christopherson678e3152018-07-23 12:32:43 -07003032static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
3033{
Paolo Bonzini4679b612018-09-24 17:23:01 +02003034 preempt_disable();
3035 if (vmx->loaded_cpu_state)
3036 wrmsrl(MSR_KERNEL_GS_BASE, data);
3037 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07003038 vmx->msr_guest_kernel_gs_base = data;
3039}
3040#endif
3041
Feng Wu28b835d2015-09-18 22:29:54 +08003042static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
3043{
3044 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
3045 struct pi_desc old, new;
3046 unsigned int dest;
3047
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003048 /*
3049 * In case of hot-plug or hot-unplug, we may have to undo
3050 * vmx_vcpu_pi_put even if there is no assigned device. And we
3051 * always keep PI.NDST up to date for simplicity: it makes the
3052 * code easier, and CPU migration is not a fast path.
3053 */
3054 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08003055 return;
3056
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003057 /*
3058 * First handle the simple case where no cmpxchg is necessary; just
3059 * allow posting non-urgent interrupts.
3060 *
3061 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
3062 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
3063 * expects the VCPU to be on the blocked_vcpu_list that matches
3064 * PI.NDST.
3065 */
3066 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
3067 vcpu->cpu == cpu) {
3068 pi_clear_sn(pi_desc);
3069 return;
3070 }
3071
3072 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08003073 do {
3074 old.control = new.control = pi_desc->control;
3075
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003076 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08003077
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003078 if (x2apic_enabled())
3079 new.ndst = dest;
3080 else
3081 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08003082
Feng Wu28b835d2015-09-18 22:29:54 +08003083 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02003084 } while (cmpxchg64(&pi_desc->control, old.control,
3085 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08003086}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08003087
Peter Feinerc95ba922016-08-17 09:36:47 -07003088static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
3089{
3090 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
3091 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
3092}
3093
Avi Kivity6aa8b732006-12-10 02:21:36 -08003094/*
3095 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
3096 * vcpu mutex is already taken.
3097 */
Avi Kivity15ad7142007-07-11 18:17:21 +03003098static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003099{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003100 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003101 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003102
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003103 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003104 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003105 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003106 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08003107
3108 /*
3109 * Read loaded_vmcs->cpu should be before fetching
3110 * loaded_vmcs->loaded_vmcss_on_cpu_link.
3111 * See the comments in __loaded_vmcs_clear().
3112 */
3113 smp_rmb();
3114
Nadav Har'Eld462b812011-05-24 15:26:10 +03003115 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
3116 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003117 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003118 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003119 }
3120
3121 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
3122 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
3123 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01003124 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003125 }
3126
3127 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07003128 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003129 unsigned long sysenter_esp;
3130
3131 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003132
Avi Kivity6aa8b732006-12-10 02:21:36 -08003133 /*
3134 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08003135 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08003136 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08003137 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01003138 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07003139 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003140
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08003141 /*
3142 * VM exits change the host TR limit to 0x67 after a VM
3143 * exit. This is okay, since 0x67 covers everything except
3144 * the IO bitmap and have have code to handle the IO bitmap
3145 * being lost after a VM exit.
3146 */
3147 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
3148
Avi Kivity6aa8b732006-12-10 02:21:36 -08003149 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
3150 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08003151
Nadav Har'Eld462b812011-05-24 15:26:10 +03003152 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003153 }
Feng Wu28b835d2015-09-18 22:29:54 +08003154
Owen Hofmann2680d6d2016-03-01 13:36:13 -08003155 /* Setup TSC multiplier */
3156 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07003157 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
3158 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08003159
Feng Wu28b835d2015-09-18 22:29:54 +08003160 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08003161 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08003162 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08003163}
3164
3165static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
3166{
3167 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
3168
3169 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08003170 !irq_remapping_cap(IRQ_POSTING_CAP) ||
3171 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08003172 return;
3173
3174 /* Set SN when the vCPU is preempted */
3175 if (vcpu->preempted)
3176 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003177}
3178
3179static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
3180{
Feng Wu28b835d2015-09-18 22:29:54 +08003181 vmx_vcpu_pi_put(vcpu);
3182
Sean Christopherson6d6095b2018-07-23 12:32:44 -07003183 vmx_prepare_switch_to_host(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003184}
3185
Wanpeng Lif244dee2017-07-20 01:11:54 -07003186static bool emulation_required(struct kvm_vcpu *vcpu)
3187{
3188 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3189}
3190
Avi Kivityedcafe32009-12-30 18:07:40 +02003191static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
3192
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03003193/*
3194 * Return the cr0 value that a nested guest would read. This is a combination
3195 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
3196 * its hypervisor (cr0_read_shadow).
3197 */
3198static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
3199{
3200 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
3201 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
3202}
3203static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
3204{
3205 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
3206 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
3207}
3208
Avi Kivity6aa8b732006-12-10 02:21:36 -08003209static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
3210{
Avi Kivity78ac8b42010-04-08 18:19:35 +03003211 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03003212
Avi Kivity6de12732011-03-07 12:51:22 +02003213 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
3214 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3215 rflags = vmcs_readl(GUEST_RFLAGS);
3216 if (to_vmx(vcpu)->rmode.vm86_active) {
3217 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3218 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
3219 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
3220 }
3221 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003222 }
Avi Kivity6de12732011-03-07 12:51:22 +02003223 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003224}
3225
3226static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
3227{
Wanpeng Lif244dee2017-07-20 01:11:54 -07003228 unsigned long old_rflags = vmx_get_rflags(vcpu);
3229
Avi Kivity6de12732011-03-07 12:51:22 +02003230 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3231 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003232 if (to_vmx(vcpu)->rmode.vm86_active) {
3233 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003234 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003235 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003236 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07003237
3238 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
3239 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003240}
3241
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003242static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003243{
3244 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3245 int ret = 0;
3246
3247 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01003248 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003249 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01003250 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003251
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003252 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003253}
3254
3255static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
3256{
3257 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3258 u32 interruptibility = interruptibility_old;
3259
3260 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
3261
Jan Kiszka48005f62010-02-19 19:38:07 +01003262 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003263 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01003264 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003265 interruptibility |= GUEST_INTR_STATE_STI;
3266
3267 if ((interruptibility != interruptibility_old))
3268 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
3269}
3270
Avi Kivity6aa8b732006-12-10 02:21:36 -08003271static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
3272{
3273 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003274
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003275 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003276 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003277 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003278
Glauber Costa2809f5d2009-05-12 16:21:05 -04003279 /* skipping an emulated instruction also counts */
3280 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003281}
3282
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003283static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3284 unsigned long exit_qual)
3285{
3286 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3287 unsigned int nr = vcpu->arch.exception.nr;
3288 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3289
3290 if (vcpu->arch.exception.has_error_code) {
3291 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3292 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3293 }
3294
3295 if (kvm_exception_is_soft(nr))
3296 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3297 else
3298 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3299
3300 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3301 vmx_get_nmi_mask(vcpu))
3302 intr_info |= INTR_INFO_UNBLOCK_NMI;
3303
3304 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3305}
3306
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003307/*
3308 * KVM wants to inject page-faults which it got to the guest. This function
3309 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003310 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003311static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003312{
3313 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003314 unsigned int nr = vcpu->arch.exception.nr;
Jim Mattsonda998b42018-10-16 14:29:22 -07003315 bool has_payload = vcpu->arch.exception.has_payload;
3316 unsigned long payload = vcpu->arch.exception.payload;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003317
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003318 if (nr == PF_VECTOR) {
3319 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003320 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003321 return 1;
3322 }
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003323 if (nested_vmx_is_page_fault_vmexit(vmcs12,
3324 vcpu->arch.exception.error_code)) {
Jim Mattsonda998b42018-10-16 14:29:22 -07003325 *exit_qual = has_payload ? payload : vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003326 return 1;
3327 }
Jim Mattsonf10c7292018-10-16 14:29:23 -07003328 } else if (vmcs12->exception_bitmap & (1u << nr)) {
3329 if (nr == DB_VECTOR) {
3330 if (!has_payload) {
3331 payload = vcpu->arch.dr6;
3332 payload &= ~(DR6_FIXED_1 | DR6_BT);
3333 payload ^= DR6_RTM;
Jim Mattsoncfb634f2018-09-21 10:36:17 -07003334 }
Jim Mattsonf10c7292018-10-16 14:29:23 -07003335 *exit_qual = payload;
3336 } else
3337 *exit_qual = 0;
3338 return 1;
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003339 }
3340
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003341 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003342}
3343
Wanpeng Licaa057a2018-03-12 04:53:03 -07003344static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3345{
3346 /*
3347 * Ensure that we clear the HLT state in the VMCS. We don't need to
3348 * explicitly skip the instruction because if the HLT state is set,
3349 * then the instruction is already executing and RIP has already been
3350 * advanced.
3351 */
3352 if (kvm_hlt_in_guest(vcpu->kvm) &&
3353 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3354 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3355}
3356
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003357static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02003358{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003359 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003360 unsigned nr = vcpu->arch.exception.nr;
3361 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003362 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003363 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003364
Jim Mattsonda998b42018-10-16 14:29:22 -07003365 kvm_deliver_exception_payload(vcpu);
3366
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003367 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003368 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003369 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3370 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003371
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003372 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003373 int inc_eip = 0;
3374 if (kvm_exception_is_soft(nr))
3375 inc_eip = vcpu->arch.event_exit_inst_len;
3376 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003377 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003378 return;
3379 }
3380
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003381 WARN_ON_ONCE(vmx->emulation_required);
3382
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003383 if (kvm_exception_is_soft(nr)) {
3384 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3385 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003386 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3387 } else
3388 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3389
3390 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003391
3392 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003393}
3394
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003395static bool vmx_rdtscp_supported(void)
3396{
3397 return cpu_has_vmx_rdtscp();
3398}
3399
Mao, Junjiead756a12012-07-02 01:18:48 +00003400static bool vmx_invpcid_supported(void)
3401{
Junaid Shahideb4b2482018-06-27 14:59:14 -07003402 return cpu_has_vmx_invpcid();
Mao, Junjiead756a12012-07-02 01:18:48 +00003403}
3404
Avi Kivity6aa8b732006-12-10 02:21:36 -08003405/*
Eddie Donga75beee2007-05-17 18:55:15 +03003406 * Swap MSR entry in host/guest MSR entry array.
3407 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003408static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003409{
Avi Kivity26bb0982009-09-07 11:14:12 +03003410 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003411
3412 tmp = vmx->guest_msrs[to];
3413 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3414 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003415}
3416
3417/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003418 * Set up the vmcs to automatically save and restore system
3419 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3420 * mode, as fiddling with msrs is very expensive.
3421 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003422static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003423{
Avi Kivity26bb0982009-09-07 11:14:12 +03003424 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003425
Eddie Donga75beee2007-05-17 18:55:15 +03003426 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003427#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003428 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003429 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003430 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003431 move_msr_up(vmx, index, save_nmsrs++);
3432 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003433 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003434 move_msr_up(vmx, index, save_nmsrs++);
3435 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003436 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003437 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003438 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003439 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003440 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003441 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003442 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003443 * if efer.sce is enabled.
3444 */
Brian Gerst8c065852010-07-17 09:03:26 -04003445 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003446 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003447 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003448 }
Eddie Donga75beee2007-05-17 18:55:15 +03003449#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003450 index = __find_msr_index(vmx, MSR_EFER);
3451 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003452 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003453
Avi Kivity26bb0982009-09-07 11:14:12 +03003454 vmx->save_nmsrs = save_nmsrs;
Liran Alonf48b4712018-11-20 18:03:25 +02003455 vmx->guest_msrs_dirty = true;
Avi Kivity58972972009-02-24 22:26:47 +02003456
Yang Zhang8d146952013-01-25 10:18:50 +08003457 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003458 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003459}
3460
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003461static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003462{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003463 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003464
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003465 if (is_guest_mode(vcpu) &&
3466 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3467 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3468
3469 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003470}
3471
Leonid Shatz326e7422018-11-06 12:14:25 +02003472static u64 vmx_write_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003473{
Paolo Bonzini45c3af92018-11-25 18:45:35 +01003474 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3475 u64 g_tsc_offset = 0;
Leonid Shatz326e7422018-11-06 12:14:25 +02003476
Paolo Bonzini45c3af92018-11-25 18:45:35 +01003477 /*
3478 * We're here if L1 chose not to trap WRMSR to TSC. According
3479 * to the spec, this should set L1's TSC; The offset that L1
3480 * set for L2 remains unchanged, and still needs to be added
3481 * to the newly set TSC to get L2's TSC.
3482 */
3483 if (is_guest_mode(vcpu) &&
3484 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3485 g_tsc_offset = vmcs12->tsc_offset;
3486
3487 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3488 vcpu->arch.tsc_offset - g_tsc_offset,
3489 offset);
3490 vmcs_write64(TSC_OFFSET, offset + g_tsc_offset);
3491 return offset + g_tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003492}
3493
Nadav Har'El801d3422011-05-25 23:02:23 +03003494/*
3495 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3496 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3497 * all guests if the "nested" module option is off, and can also be disabled
3498 * for a single guest by disabling its VMX cpuid bit.
3499 */
3500static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3501{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003502 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003503}
3504
Avi Kivity6aa8b732006-12-10 02:21:36 -08003505/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003506 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3507 * returned for the various VMX controls MSRs when nested VMX is enabled.
3508 * The same values should also be used to verify that vmcs12 control fields are
3509 * valid during nested entry from L1 to L2.
3510 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3511 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3512 * bit in the high half is on if the corresponding bit in the control field
3513 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003514 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003515static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003516{
Paolo Bonzini13893092018-02-26 13:40:09 +01003517 if (!nested) {
3518 memset(msrs, 0, sizeof(*msrs));
3519 return;
3520 }
3521
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003522 /*
3523 * Note that as a general rule, the high half of the MSRs (bits in
3524 * the control fields which may be 1) should be initialized by the
3525 * intersection of the underlying hardware's MSR (i.e., features which
3526 * can be supported) and the list of features we want to expose -
3527 * because they are known to be properly supported in our code.
3528 * Also, usually, the low half of the MSRs (bits which must be 1) can
3529 * be set to 0, meaning that L1 may turn off any of these bits. The
3530 * reason is that if one of these bits is necessary, it will appear
3531 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3532 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003533 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003534 * These rules have exceptions below.
3535 */
3536
3537 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003538 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003539 msrs->pinbased_ctls_low,
3540 msrs->pinbased_ctls_high);
3541 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003542 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003543 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003544 PIN_BASED_EXT_INTR_MASK |
3545 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003546 PIN_BASED_VIRTUAL_NMIS |
3547 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003548 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003549 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003550 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003551
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003552 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003553 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003554 msrs->exit_ctls_low,
3555 msrs->exit_ctls_high);
3556 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003557 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003558
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003559 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003560#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003561 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003562#endif
Jan Kiszkaf41245002014-03-07 20:03:13 +01003563 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003564 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003565 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf41245002014-03-07 20:03:13 +01003566 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003567 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3568
Jan Kiszka2996fca2014-06-16 13:59:43 +02003569 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003570 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003571
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003572 /* entry controls */
3573 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003574 msrs->entry_ctls_low,
3575 msrs->entry_ctls_high);
3576 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003577 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003578 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003579#ifdef CONFIG_X86_64
3580 VM_ENTRY_IA32E_MODE |
3581#endif
3582 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003583 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003584 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Jan Kiszka57435342013-08-06 10:39:56 +02003585
Jan Kiszka2996fca2014-06-16 13:59:43 +02003586 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003587 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003588
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003589 /* cpu-based controls */
3590 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003591 msrs->procbased_ctls_low,
3592 msrs->procbased_ctls_high);
3593 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003594 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003595 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003596 CPU_BASED_VIRTUAL_INTR_PENDING |
3597 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003598 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3599 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3600 CPU_BASED_CR3_STORE_EXITING |
3601#ifdef CONFIG_X86_64
3602 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3603#endif
3604 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003605 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3606 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3607 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3608 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003609 /*
3610 * We can allow some features even when not supported by the
3611 * hardware. For example, L1 can specify an MSR bitmap - and we
3612 * can use it to avoid exits to L1 - even when L0 runs L2
3613 * without MSR bitmaps.
3614 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003615 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003616 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003617 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003618
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003619 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003620 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003621 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3622
Paolo Bonzini80154d72017-08-24 13:55:35 +02003623 /*
3624 * secondary cpu-based controls. Do not include those that
3625 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3626 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003627 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003628 msrs->secondary_ctls_low,
3629 msrs->secondary_ctls_high);
3630 msrs->secondary_ctls_low = 0;
3631 msrs->secondary_ctls_high &=
Paolo Bonzini1b073042016-10-25 16:06:30 +02003632 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003633 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003634 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003635 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003636 SECONDARY_EXEC_WBINVD_EXITING;
Paolo Bonzini2cf7ea92018-10-03 10:34:00 +02003637
Liran Alon32c7acf2018-06-23 02:35:11 +03003638 /*
3639 * We can emulate "VMCS shadowing," even if the hardware
3640 * doesn't support it.
3641 */
3642 msrs->secondary_ctls_high |=
3643 SECONDARY_EXEC_SHADOW_VMCS;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003644
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02003645 if (enable_ept) {
3646 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003647 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003648 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003649 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003650 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003651 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003652 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003653 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003654 msrs->ept_caps &= vmx_capability.ept;
3655 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003656 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3657 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003658 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003659 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003660 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003661 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003662 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003663 }
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02003664
Bandan Das27c42a12017-08-03 15:54:42 -04003665 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003666 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003667 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003668 /*
3669 * Advertise EPTP switching unconditionally
3670 * since we emulate it
3671 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003672 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003673 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003674 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003675 }
3676
Paolo Bonzinief697a72016-03-18 16:58:38 +01003677 /*
3678 * Old versions of KVM use the single-context version without
3679 * checking for support, so declare that it is supported even
3680 * though it is treated as global context. The alternative is
3681 * not failing the single-context invvpid, and it is worse.
3682 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003683 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003684 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003685 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003686 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003687 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003688 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003689
Radim Krčmář0790ec12015-03-17 14:02:32 +01003690 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003691 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003692 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3693
Paolo Bonzini2cf7ea92018-10-03 10:34:00 +02003694 if (flexpriority_enabled)
3695 msrs->secondary_ctls_high |=
3696 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
3697
Jan Kiszkac18911a2013-03-13 16:06:41 +01003698 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003699 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003700 msrs->misc_low,
3701 msrs->misc_high);
3702 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3703 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003704 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003705 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf41245002014-03-07 20:03:13 +01003706 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003707 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003708
3709 /*
3710 * This MSR reports some information about VMX support. We
3711 * should return information about the VMX we emulate for the
3712 * guest, and the VMCS structure we give it - not about the
3713 * VMX support of the underlying hardware.
3714 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003715 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003716 VMCS12_REVISION |
3717 VMX_BASIC_TRUE_CTLS |
3718 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3719 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3720
3721 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003722 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003723
3724 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003725 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003726 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3727 * We picked the standard core2 setting.
3728 */
3729#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3730#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003731 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3732 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003733
3734 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003735 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3736 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003737
3738 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003739 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003740}
3741
David Matlack38991522016-11-29 18:14:08 -08003742/*
3743 * if fixed0[i] == 1: val[i] must be 1
3744 * if fixed1[i] == 0: val[i] must be 0
3745 */
3746static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3747{
3748 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003749}
3750
3751static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3752{
David Matlack38991522016-11-29 18:14:08 -08003753 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003754}
3755
3756static inline u64 vmx_control_msr(u32 low, u32 high)
3757{
3758 return low | ((u64)high << 32);
3759}
3760
David Matlack62cc6b9d2016-11-29 18:14:07 -08003761static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3762{
3763 superset &= mask;
3764 subset &= mask;
3765
3766 return (superset | subset) == superset;
3767}
3768
3769static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3770{
3771 const u64 feature_and_reserved =
3772 /* feature (except bit 48; see below) */
3773 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3774 /* reserved */
3775 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003776 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003777
3778 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3779 return -EINVAL;
3780
3781 /*
3782 * KVM does not emulate a version of VMX that constrains physical
3783 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3784 */
3785 if (data & BIT_ULL(48))
3786 return -EINVAL;
3787
3788 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3789 vmx_basic_vmcs_revision_id(data))
3790 return -EINVAL;
3791
3792 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3793 return -EINVAL;
3794
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003795 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003796 return 0;
3797}
3798
3799static int
3800vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3801{
3802 u64 supported;
3803 u32 *lowp, *highp;
3804
3805 switch (msr_index) {
3806 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003807 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3808 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003809 break;
3810 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003811 lowp = &vmx->nested.msrs.procbased_ctls_low;
3812 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003813 break;
3814 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003815 lowp = &vmx->nested.msrs.exit_ctls_low;
3816 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003817 break;
3818 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003819 lowp = &vmx->nested.msrs.entry_ctls_low;
3820 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003821 break;
3822 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003823 lowp = &vmx->nested.msrs.secondary_ctls_low;
3824 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003825 break;
3826 default:
3827 BUG();
3828 }
3829
3830 supported = vmx_control_msr(*lowp, *highp);
3831
3832 /* Check must-be-1 bits are still 1. */
3833 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3834 return -EINVAL;
3835
3836 /* Check must-be-0 bits are still 0. */
3837 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3838 return -EINVAL;
3839
3840 *lowp = data;
3841 *highp = data >> 32;
3842 return 0;
3843}
3844
3845static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3846{
3847 const u64 feature_and_reserved_bits =
3848 /* feature */
3849 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3850 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3851 /* reserved */
3852 GENMASK_ULL(13, 9) | BIT_ULL(31);
3853 u64 vmx_misc;
3854
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003855 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3856 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003857
3858 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3859 return -EINVAL;
3860
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003861 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003862 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3863 vmx_misc_preemption_timer_rate(data) !=
3864 vmx_misc_preemption_timer_rate(vmx_misc))
3865 return -EINVAL;
3866
3867 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3868 return -EINVAL;
3869
3870 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3871 return -EINVAL;
3872
3873 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3874 return -EINVAL;
3875
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003876 vmx->nested.msrs.misc_low = data;
3877 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003878
3879 /*
3880 * If L1 has read-only VM-exit information fields, use the
3881 * less permissive vmx_vmwrite_bitmap to specify write
3882 * permissions for the shadow VMCS.
3883 */
3884 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3885 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3886
David Matlack62cc6b9d2016-11-29 18:14:07 -08003887 return 0;
3888}
3889
3890static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3891{
3892 u64 vmx_ept_vpid_cap;
3893
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003894 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3895 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003896
3897 /* Every bit is either reserved or a feature bit. */
3898 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3899 return -EINVAL;
3900
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003901 vmx->nested.msrs.ept_caps = data;
3902 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003903 return 0;
3904}
3905
3906static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3907{
3908 u64 *msr;
3909
3910 switch (msr_index) {
3911 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003912 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003913 break;
3914 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003915 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003916 break;
3917 default:
3918 BUG();
3919 }
3920
3921 /*
3922 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3923 * must be 1 in the restored value.
3924 */
3925 if (!is_bitwise_subset(data, *msr, -1ULL))
3926 return -EINVAL;
3927
3928 *msr = data;
3929 return 0;
3930}
3931
3932/*
3933 * Called when userspace is restoring VMX MSRs.
3934 *
3935 * Returns 0 on success, non-0 otherwise.
3936 */
3937static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3938{
3939 struct vcpu_vmx *vmx = to_vmx(vcpu);
3940
Jim Mattsona943ac52018-05-29 09:11:32 -07003941 /*
3942 * Don't allow changes to the VMX capability MSRs while the vCPU
3943 * is in VMX operation.
3944 */
3945 if (vmx->nested.vmxon)
3946 return -EBUSY;
3947
David Matlack62cc6b9d2016-11-29 18:14:07 -08003948 switch (msr_index) {
3949 case MSR_IA32_VMX_BASIC:
3950 return vmx_restore_vmx_basic(vmx, data);
3951 case MSR_IA32_VMX_PINBASED_CTLS:
3952 case MSR_IA32_VMX_PROCBASED_CTLS:
3953 case MSR_IA32_VMX_EXIT_CTLS:
3954 case MSR_IA32_VMX_ENTRY_CTLS:
3955 /*
3956 * The "non-true" VMX capability MSRs are generated from the
3957 * "true" MSRs, so we do not support restoring them directly.
3958 *
3959 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3960 * should restore the "true" MSRs with the must-be-1 bits
3961 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3962 * DEFAULT SETTINGS".
3963 */
3964 return -EINVAL;
3965 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3966 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3967 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3968 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3969 case MSR_IA32_VMX_PROCBASED_CTLS2:
3970 return vmx_restore_control_msr(vmx, msr_index, data);
3971 case MSR_IA32_VMX_MISC:
3972 return vmx_restore_vmx_misc(vmx, data);
3973 case MSR_IA32_VMX_CR0_FIXED0:
3974 case MSR_IA32_VMX_CR4_FIXED0:
3975 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3976 case MSR_IA32_VMX_CR0_FIXED1:
3977 case MSR_IA32_VMX_CR4_FIXED1:
3978 /*
3979 * These MSRs are generated based on the vCPU's CPUID, so we
3980 * do not support restoring them directly.
3981 */
3982 return -EINVAL;
3983 case MSR_IA32_VMX_EPT_VPID_CAP:
3984 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3985 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003986 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003987 return 0;
3988 default:
3989 /*
3990 * The rest of the VMX capability MSRs do not support restore.
3991 */
3992 return -EINVAL;
3993 }
3994}
3995
Jan Kiszkacae50132014-01-04 18:47:22 +01003996/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003997static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003998{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003999 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004000 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004001 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004002 break;
4003 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
4004 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08004005 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004006 msrs->pinbased_ctls_low,
4007 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08004008 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
4009 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004010 break;
4011 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
4012 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08004013 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004014 msrs->procbased_ctls_low,
4015 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08004016 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
4017 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004018 break;
4019 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
4020 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08004021 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004022 msrs->exit_ctls_low,
4023 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08004024 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
4025 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004026 break;
4027 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
4028 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08004029 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004030 msrs->entry_ctls_low,
4031 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08004032 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
4033 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004034 break;
4035 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08004036 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004037 msrs->misc_low,
4038 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004039 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004040 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004041 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004042 break;
4043 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004044 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004045 break;
4046 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004047 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004048 break;
4049 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004050 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004051 break;
4052 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004053 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004054 break;
4055 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08004056 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004057 msrs->secondary_ctls_low,
4058 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004059 break;
4060 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004061 *pdata = msrs->ept_caps |
4062 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004063 break;
Bandan Das27c42a12017-08-03 15:54:42 -04004064 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004065 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04004066 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004067 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004068 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08004069 }
4070
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004071 return 0;
4072}
4073
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004074static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
4075 uint64_t val)
4076{
4077 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
4078
4079 return !(val & ~valid_bits);
4080}
4081
Tom Lendacky801e4592018-02-21 13:39:51 -06004082static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
4083{
Paolo Bonzini13893092018-02-26 13:40:09 +01004084 switch (msr->index) {
4085 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4086 if (!nested)
4087 return 1;
4088 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
4089 default:
4090 return 1;
4091 }
4092
4093 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06004094}
4095
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004096/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08004097 * Reads an msr value (of 'msr_index') into 'pdata'.
4098 * Returns 0 on success, non-0 otherwise.
4099 * Assumes vcpu_load() was already called.
4100 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004101static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004102{
Borislav Petkova6cb0992017-12-20 12:50:28 +01004103 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004104 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004105
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004106 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004107#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004108 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004109 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004110 break;
4111 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004112 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004113 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03004114 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07004115 msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
Avi Kivity44ea2b12009-09-06 15:55:37 +03004116 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03004117#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08004118 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004119 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004120 case MSR_IA32_SPEC_CTRL:
4121 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004122 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4123 return 1;
4124
4125 msr_info->data = to_vmx(vcpu)->spec_ctrl;
4126 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004127 case MSR_IA32_ARCH_CAPABILITIES:
4128 if (!msr_info->host_initiated &&
4129 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
4130 return 1;
4131 msr_info->data = to_vmx(vcpu)->arch_capabilities;
4132 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004133 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004134 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004135 break;
4136 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004137 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004138 break;
4139 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004140 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004141 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004142 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004143 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004144 (!msr_info->host_initiated &&
4145 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004146 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004147 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004148 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004149 case MSR_IA32_MCG_EXT_CTL:
4150 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01004151 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08004152 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01004153 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004154 msr_info->data = vcpu->arch.mcg_ext_ctl;
4155 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004156 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01004157 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01004158 break;
4159 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4160 if (!nested_vmx_allowed(vcpu))
4161 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004162 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
4163 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08004164 case MSR_IA32_XSS:
4165 if (!vmx_xsaves_supported())
4166 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004167 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08004168 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004169 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004170 if (!msr_info->host_initiated &&
4171 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004172 return 1;
4173 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004174 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01004175 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004176 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004177 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004178 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004179 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004180 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004181 }
4182
Avi Kivity6aa8b732006-12-10 02:21:36 -08004183 return 0;
4184}
4185
Jan Kiszkacae50132014-01-04 18:47:22 +01004186static void vmx_leave_nested(struct kvm_vcpu *vcpu);
4187
Avi Kivity6aa8b732006-12-10 02:21:36 -08004188/*
4189 * Writes msr value into into the appropriate "register".
4190 * Returns 0 on success, non-0 otherwise.
4191 * Assumes vcpu_load() was already called.
4192 */
Will Auld8fe8ab42012-11-29 12:42:12 -08004193static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004194{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004195 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004196 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03004197 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08004198 u32 msr_index = msr_info->index;
4199 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03004200
Avi Kivity6aa8b732006-12-10 02:21:36 -08004201 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08004202 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08004203 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03004204 break;
Avi Kivity16175a72009-03-23 22:13:44 +02004205#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004206 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004207 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004208 vmcs_writel(GUEST_FS_BASE, data);
4209 break;
4210 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004211 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004212 vmcs_writel(GUEST_GS_BASE, data);
4213 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03004214 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07004215 vmx_write_guest_kernel_gs_base(vmx, data);
Avi Kivity44ea2b12009-09-06 15:55:37 +03004216 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004217#endif
4218 case MSR_IA32_SYSENTER_CS:
4219 vmcs_write32(GUEST_SYSENTER_CS, data);
4220 break;
4221 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004222 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004223 break;
4224 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004225 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004226 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004227 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004228 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004229 (!msr_info->host_initiated &&
4230 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004231 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08004232 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07004233 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004234 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004235 vmcs_write64(GUEST_BNDCFGS, data);
4236 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004237 case MSR_IA32_SPEC_CTRL:
4238 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004239 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4240 return 1;
4241
4242 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02004243 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004244 return 1;
4245
4246 vmx->spec_ctrl = data;
4247
4248 if (!data)
4249 break;
4250
4251 /*
4252 * For non-nested:
4253 * When it's written (to non-zero) for the first time, pass
4254 * it through.
4255 *
4256 * For nested:
4257 * The handling of the MSR bitmap for L2 guests is done in
4258 * nested_vmx_merge_msr_bitmap. We should not touch the
4259 * vmcs02.msr_bitmap here since it gets completely overwritten
4260 * in the merging. We update the vmcs01 here for L1 as well
4261 * since it will end up touching the MSR anyway now.
4262 */
4263 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
4264 MSR_IA32_SPEC_CTRL,
4265 MSR_TYPE_RW);
4266 break;
Ashok Raj15d45072018-02-01 22:59:43 +01004267 case MSR_IA32_PRED_CMD:
4268 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01004269 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4270 return 1;
4271
4272 if (data & ~PRED_CMD_IBPB)
4273 return 1;
4274
4275 if (!data)
4276 break;
4277
4278 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4279
4280 /*
4281 * For non-nested:
4282 * When it's written (to non-zero) for the first time, pass
4283 * it through.
4284 *
4285 * For nested:
4286 * The handling of the MSR bitmap for L2 guests is done in
4287 * nested_vmx_merge_msr_bitmap. We should not touch the
4288 * vmcs02.msr_bitmap here since it gets completely overwritten
4289 * in the merging.
4290 */
4291 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
4292 MSR_TYPE_W);
4293 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004294 case MSR_IA32_ARCH_CAPABILITIES:
4295 if (!msr_info->host_initiated)
4296 return 1;
4297 vmx->arch_capabilities = data;
4298 break;
Sheng Yang468d4722008-10-09 16:01:55 +08004299 case MSR_IA32_CR_PAT:
4300 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03004301 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4302 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004303 vmcs_write64(GUEST_IA32_PAT, data);
4304 vcpu->arch.pat = data;
4305 break;
4306 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004307 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004308 break;
Will Auldba904632012-11-29 12:42:50 -08004309 case MSR_IA32_TSC_ADJUST:
4310 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004311 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004312 case MSR_IA32_MCG_EXT_CTL:
4313 if ((!msr_info->host_initiated &&
4314 !(to_vmx(vcpu)->msr_ia32_feature_control &
4315 FEATURE_CONTROL_LMCE)) ||
4316 (data & ~MCG_EXT_CTL_LMCE_EN))
4317 return 1;
4318 vcpu->arch.mcg_ext_ctl = data;
4319 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004320 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004321 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08004322 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01004323 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
4324 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08004325 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01004326 if (msr_info->host_initiated && data == 0)
4327 vmx_leave_nested(vcpu);
4328 break;
4329 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08004330 if (!msr_info->host_initiated)
4331 return 1; /* they are read-only */
4332 if (!nested_vmx_allowed(vcpu))
4333 return 1;
4334 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08004335 case MSR_IA32_XSS:
4336 if (!vmx_xsaves_supported())
4337 return 1;
4338 /*
4339 * The only supported bit as of Skylake is bit 8, but
4340 * it is not supported on KVM.
4341 */
4342 if (data != 0)
4343 return 1;
4344 vcpu->arch.ia32_xss = data;
4345 if (vcpu->arch.ia32_xss != host_xss)
4346 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04004347 vcpu->arch.ia32_xss, host_xss, false);
Wanpeng Li20300092014-12-02 19:14:59 +08004348 else
4349 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4350 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004351 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004352 if (!msr_info->host_initiated &&
4353 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004354 return 1;
4355 /* Check reserved bit, higher 32 bits should be zero */
4356 if ((data >> 32) != 0)
4357 return 1;
4358 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004359 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004360 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004361 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004362 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004363 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004364 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4365 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004366 ret = kvm_set_shared_msr(msr->index, msr->data,
4367 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004368 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004369 if (ret)
4370 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004371 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004372 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004373 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004374 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004375 }
4376
Eddie Dong2cc51562007-05-21 07:28:09 +03004377 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004378}
4379
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004380static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004381{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004382 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4383 switch (reg) {
4384 case VCPU_REGS_RSP:
4385 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4386 break;
4387 case VCPU_REGS_RIP:
4388 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4389 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004390 case VCPU_EXREG_PDPTR:
4391 if (enable_ept)
4392 ept_save_pdptrs(vcpu);
4393 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004394 default:
4395 break;
4396 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004397}
4398
Avi Kivity6aa8b732006-12-10 02:21:36 -08004399static __init int cpu_has_kvm_support(void)
4400{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004401 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004402}
4403
4404static __init int vmx_disabled_by_bios(void)
4405{
4406 u64 msr;
4407
4408 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004409 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004410 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004411 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4412 && tboot_enabled())
4413 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004414 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004415 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004416 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004417 && !tboot_enabled()) {
4418 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004419 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004420 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004421 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004422 /* launched w/o TXT and VMX disabled */
4423 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4424 && !tboot_enabled())
4425 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004426 }
4427
4428 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004429}
4430
Dongxiao Xu7725b892010-05-11 18:29:38 +08004431static void kvm_cpu_vmxon(u64 addr)
4432{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004433 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004434 intel_pt_handle_vmx(1);
4435
Uros Bizjak4b1e5472018-10-11 19:40:44 +02004436 asm volatile ("vmxon %0" : : "m"(addr));
Dongxiao Xu7725b892010-05-11 18:29:38 +08004437}
4438
Radim Krčmář13a34e02014-08-28 15:13:03 +02004439static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004440{
4441 int cpu = raw_smp_processor_id();
4442 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004443 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004444
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004445 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004446 return -EBUSY;
4447
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004448 /*
4449 * This can happen if we hot-added a CPU but failed to allocate
4450 * VP assist page for it.
4451 */
4452 if (static_branch_unlikely(&enable_evmcs) &&
4453 !hv_get_vp_assist_page(cpu))
4454 return -EFAULT;
4455
Nadav Har'Eld462b812011-05-24 15:26:10 +03004456 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004457 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4458 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004459
4460 /*
4461 * Now we can enable the vmclear operation in kdump
4462 * since the loaded_vmcss_on_cpu list on this cpu
4463 * has been initialized.
4464 *
4465 * Though the cpu is not in VMX operation now, there
4466 * is no problem to enable the vmclear operation
4467 * for the loaded_vmcss_on_cpu list is empty!
4468 */
4469 crash_enable_local_vmclear(cpu);
4470
Avi Kivity6aa8b732006-12-10 02:21:36 -08004471 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004472
4473 test_bits = FEATURE_CONTROL_LOCKED;
4474 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4475 if (tboot_enabled())
4476 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4477
4478 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004479 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004480 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4481 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004482 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004483 if (enable_ept)
4484 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004485
4486 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004487}
4488
Nadav Har'Eld462b812011-05-24 15:26:10 +03004489static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004490{
4491 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004492 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004493
Nadav Har'Eld462b812011-05-24 15:26:10 +03004494 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4495 loaded_vmcss_on_cpu_link)
4496 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004497}
4498
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004499
4500/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4501 * tricks.
4502 */
4503static void kvm_cpu_vmxoff(void)
4504{
Uros Bizjak4b1e5472018-10-11 19:40:44 +02004505 asm volatile (__ex("vmxoff"));
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004506
4507 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004508 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004509}
4510
Radim Krčmář13a34e02014-08-28 15:13:03 +02004511static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004512{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004513 vmclear_local_loaded_vmcss();
4514 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004515}
4516
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004517static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004518 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004519{
4520 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004521 u32 ctl = ctl_min | ctl_opt;
4522
4523 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4524
4525 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4526 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4527
4528 /* Ensure minimum (required) set of control bits are supported. */
4529 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004530 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004531
4532 *result = ctl;
4533 return 0;
4534}
4535
Avi Kivity110312c2010-12-21 12:54:20 +02004536static __init bool allow_1_setting(u32 msr, u32 ctl)
4537{
4538 u32 vmx_msr_low, vmx_msr_high;
4539
4540 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4541 return vmx_msr_high & ctl;
4542}
4543
Yang, Sheng002c7f72007-07-31 14:23:01 +03004544static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004545{
4546 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004547 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004548 u32 _pin_based_exec_control = 0;
4549 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004550 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004551 u32 _vmexit_control = 0;
4552 u32 _vmentry_control = 0;
4553
Paolo Bonzini13893092018-02-26 13:40:09 +01004554 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304555 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004556#ifdef CONFIG_X86_64
4557 CPU_BASED_CR8_LOAD_EXITING |
4558 CPU_BASED_CR8_STORE_EXITING |
4559#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004560 CPU_BASED_CR3_LOAD_EXITING |
4561 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e2d2017-12-12 16:44:21 +08004562 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004563 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004564 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004565 CPU_BASED_MWAIT_EXITING |
4566 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004567 CPU_BASED_INVLPG_EXITING |
4568 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004569
Sheng Yangf78e0e22007-10-29 09:40:42 +08004570 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004571 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004572 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004573 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4574 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004575 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004576#ifdef CONFIG_X86_64
4577 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4578 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4579 ~CPU_BASED_CR8_STORE_EXITING;
4580#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004581 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004582 min2 = 0;
4583 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004584 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004585 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004586 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004587 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004588 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004589 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004590 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004591 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004592 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004593 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004594 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004595 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004596 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004597 SECONDARY_EXEC_RDSEED_EXITING |
4598 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004599 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004600 SECONDARY_EXEC_TSC_SCALING |
Sean Christopherson0b665d32018-08-14 09:33:34 -07004601 SECONDARY_EXEC_ENABLE_VMFUNC |
4602 SECONDARY_EXEC_ENCLS_EXITING;
Sheng Yangd56f5462008-04-25 10:13:16 +08004603 if (adjust_vmx_controls(min2, opt2,
4604 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004605 &_cpu_based_2nd_exec_control) < 0)
4606 return -EIO;
4607 }
4608#ifndef CONFIG_X86_64
4609 if (!(_cpu_based_2nd_exec_control &
4610 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4611 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4612#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004613
4614 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4615 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004616 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004617 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4618 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004619
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004620 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4621 &vmx_capability.ept, &vmx_capability.vpid);
4622
Sheng Yangd56f5462008-04-25 10:13:16 +08004623 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004624 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4625 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004626 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4627 CPU_BASED_CR3_STORE_EXITING |
4628 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004629 } else if (vmx_capability.ept) {
4630 vmx_capability.ept = 0;
4631 pr_warn_once("EPT CAP should not exist if not support "
4632 "1-setting enable EPT VM-execution control\n");
4633 }
4634 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4635 vmx_capability.vpid) {
4636 vmx_capability.vpid = 0;
4637 pr_warn_once("VPID CAP should not exist if not support "
4638 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004639 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004640
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004641 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004642#ifdef CONFIG_X86_64
4643 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4644#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004645 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004646 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004647 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4648 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004649 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004650
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004651 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4652 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4653 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004654 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4655 &_pin_based_exec_control) < 0)
4656 return -EIO;
4657
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004658 if (cpu_has_broken_vmx_preemption_timer())
4659 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004660 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004661 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004662 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4663
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004664 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004665 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004666 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4667 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004668 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004669
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004670 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004671
4672 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4673 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004674 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004675
4676#ifdef CONFIG_X86_64
4677 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4678 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004679 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004680#endif
4681
4682 /* Require Write-Back (WB) memory type for VMCS accesses. */
4683 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004684 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004685
Yang, Sheng002c7f72007-07-31 14:23:01 +03004686 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004687 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004688 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004689
Liran Alon2307af12018-06-29 22:59:04 +03004690 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004691
Yang, Sheng002c7f72007-07-31 14:23:01 +03004692 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4693 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004694 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004695 vmcs_conf->vmexit_ctrl = _vmexit_control;
4696 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004697
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004698 if (static_branch_unlikely(&enable_evmcs))
4699 evmcs_sanitize_exec_ctrls(vmcs_conf);
4700
Avi Kivity110312c2010-12-21 12:54:20 +02004701 cpu_has_load_ia32_efer =
4702 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4703 VM_ENTRY_LOAD_IA32_EFER)
4704 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4705 VM_EXIT_LOAD_IA32_EFER);
4706
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004707 cpu_has_load_perf_global_ctrl =
4708 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4709 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4710 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4711 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4712
4713 /*
4714 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004715 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004716 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4717 *
4718 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4719 *
4720 * AAK155 (model 26)
4721 * AAP115 (model 30)
4722 * AAT100 (model 37)
4723 * BC86,AAY89,BD102 (model 44)
4724 * BA97 (model 46)
4725 *
4726 */
4727 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4728 switch (boot_cpu_data.x86_model) {
4729 case 26:
4730 case 30:
4731 case 37:
4732 case 44:
4733 case 46:
4734 cpu_has_load_perf_global_ctrl = false;
4735 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4736 "does not work properly. Using workaround\n");
4737 break;
4738 default:
4739 break;
4740 }
4741 }
4742
Borislav Petkov782511b2016-04-04 22:25:03 +02004743 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004744 rdmsrl(MSR_IA32_XSS, host_xss);
4745
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004746 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004747}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004748
Liran Alon491a6032018-06-23 02:35:12 +03004749static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004750{
4751 int node = cpu_to_node(cpu);
4752 struct page *pages;
4753 struct vmcs *vmcs;
4754
Vlastimil Babka96db8002015-09-08 15:03:50 -07004755 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004756 if (!pages)
4757 return NULL;
4758 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004759 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03004760
4761 /* KVM supports Enlightened VMCS v1 only */
4762 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004763 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03004764 else
Liran Alon392b2f22018-06-23 02:35:01 +03004765 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004766
Liran Alon491a6032018-06-23 02:35:12 +03004767 if (shadow)
4768 vmcs->hdr.shadow_vmcs = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004769 return vmcs;
4770}
4771
Avi Kivity6aa8b732006-12-10 02:21:36 -08004772static void free_vmcs(struct vmcs *vmcs)
4773{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004774 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004775}
4776
Nadav Har'Eld462b812011-05-24 15:26:10 +03004777/*
4778 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4779 */
4780static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4781{
4782 if (!loaded_vmcs->vmcs)
4783 return;
4784 loaded_vmcs_clear(loaded_vmcs);
4785 free_vmcs(loaded_vmcs->vmcs);
4786 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004787 if (loaded_vmcs->msr_bitmap)
4788 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004789 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004790}
4791
Liran Alon491a6032018-06-23 02:35:12 +03004792static struct vmcs *alloc_vmcs(bool shadow)
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004793{
Liran Alon491a6032018-06-23 02:35:12 +03004794 return alloc_vmcs_cpu(shadow, raw_smp_processor_id());
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004795}
4796
4797static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4798{
Liran Alon491a6032018-06-23 02:35:12 +03004799 loaded_vmcs->vmcs = alloc_vmcs(false);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004800 if (!loaded_vmcs->vmcs)
4801 return -ENOMEM;
4802
4803 loaded_vmcs->shadow_vmcs = NULL;
4804 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004805
4806 if (cpu_has_vmx_msr_bitmap()) {
4807 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4808 if (!loaded_vmcs->msr_bitmap)
4809 goto out_vmcs;
4810 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004811
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004812 if (IS_ENABLED(CONFIG_HYPERV) &&
4813 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004814 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4815 struct hv_enlightened_vmcs *evmcs =
4816 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4817
4818 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4819 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004820 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07004821
4822 memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
4823
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004824 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004825
4826out_vmcs:
4827 free_loaded_vmcs(loaded_vmcs);
4828 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004829}
4830
Sam Ravnborg39959582007-06-01 00:47:13 -07004831static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004832{
4833 int cpu;
4834
Zachary Amsden3230bb42009-09-29 11:38:37 -10004835 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004836 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004837 per_cpu(vmxarea, cpu) = NULL;
4838 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004839}
4840
Jim Mattsond37f4262017-12-22 12:12:16 -08004841enum vmcs_field_width {
4842 VMCS_FIELD_WIDTH_U16 = 0,
4843 VMCS_FIELD_WIDTH_U64 = 1,
4844 VMCS_FIELD_WIDTH_U32 = 2,
4845 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004846};
4847
Jim Mattsond37f4262017-12-22 12:12:16 -08004848static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004849{
4850 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004851 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004852 return (field >> 13) & 0x3 ;
4853}
4854
4855static inline int vmcs_field_readonly(unsigned long field)
4856{
4857 return (((field >> 10) & 0x3) == 1);
4858}
4859
Bandan Dasfe2b2012014-04-21 15:20:14 -04004860static void init_vmcs_shadow_fields(void)
4861{
4862 int i, j;
4863
Sean Christophersondfae3c02018-12-03 13:52:52 -08004864 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
4865 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
4866
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004867 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4868 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004869 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004870 (i + 1 == max_shadow_read_only_fields ||
4871 shadow_read_only_fields[i + 1] != field + 1))
4872 pr_err("Missing field from shadow_read_only_field %x\n",
4873 field + 1);
4874
4875 clear_bit(field, vmx_vmread_bitmap);
4876#ifdef CONFIG_X86_64
4877 if (field & 1)
4878 continue;
4879#endif
4880 if (j < i)
4881 shadow_read_only_fields[j] = field;
4882 j++;
4883 }
4884 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004885
4886 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004887 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004888 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004889 (i + 1 == max_shadow_read_write_fields ||
4890 shadow_read_write_fields[i + 1] != field + 1))
4891 pr_err("Missing field from shadow_read_write_field %x\n",
4892 field + 1);
4893
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004894 /*
4895 * PML and the preemption timer can be emulated, but the
4896 * processor cannot vmwrite to fields that don't exist
4897 * on bare metal.
4898 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004899 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004900 case GUEST_PML_INDEX:
4901 if (!cpu_has_vmx_pml())
4902 continue;
4903 break;
4904 case VMX_PREEMPTION_TIMER_VALUE:
4905 if (!cpu_has_vmx_preemption_timer())
4906 continue;
4907 break;
4908 case GUEST_INTR_STATUS:
4909 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004910 continue;
4911 break;
4912 default:
4913 break;
4914 }
4915
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004916 clear_bit(field, vmx_vmwrite_bitmap);
4917 clear_bit(field, vmx_vmread_bitmap);
4918#ifdef CONFIG_X86_64
4919 if (field & 1)
4920 continue;
4921#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004922 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004923 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004924 j++;
4925 }
4926 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004927}
4928
Avi Kivity6aa8b732006-12-10 02:21:36 -08004929static __init int alloc_kvm_area(void)
4930{
4931 int cpu;
4932
Zachary Amsden3230bb42009-09-29 11:38:37 -10004933 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004934 struct vmcs *vmcs;
4935
Liran Alon491a6032018-06-23 02:35:12 +03004936 vmcs = alloc_vmcs_cpu(false, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004937 if (!vmcs) {
4938 free_kvm_area();
4939 return -ENOMEM;
4940 }
4941
Liran Alon2307af12018-06-29 22:59:04 +03004942 /*
4943 * When eVMCS is enabled, alloc_vmcs_cpu() sets
4944 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
4945 * revision_id reported by MSR_IA32_VMX_BASIC.
4946 *
4947 * However, even though not explictly documented by
4948 * TLFS, VMXArea passed as VMXON argument should
4949 * still be marked with revision_id reported by
4950 * physical CPU.
4951 */
4952 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004953 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004954
Avi Kivity6aa8b732006-12-10 02:21:36 -08004955 per_cpu(vmxarea, cpu) = vmcs;
4956 }
4957 return 0;
4958}
4959
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004960static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004961 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004962{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004963 if (!emulate_invalid_guest_state) {
4964 /*
4965 * CS and SS RPL should be equal during guest entry according
4966 * to VMX spec, but in reality it is not always so. Since vcpu
4967 * is in the middle of the transition from real mode to
4968 * protected mode it is safe to assume that RPL 0 is a good
4969 * default value.
4970 */
4971 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004972 save->selector &= ~SEGMENT_RPL_MASK;
4973 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004974 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004975 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004976 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004977}
4978
4979static void enter_pmode(struct kvm_vcpu *vcpu)
4980{
4981 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004982 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004983
Gleb Natapovd99e4152012-12-20 16:57:45 +02004984 /*
4985 * Update real mode segment cache. It may be not up-to-date if sement
4986 * register was written while vcpu was in a guest mode.
4987 */
4988 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4989 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4990 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4991 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4992 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4993 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4994
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004995 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004996
Avi Kivity2fb92db2011-04-27 19:42:18 +03004997 vmx_segment_cache_clear(vmx);
4998
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004999 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005000
5001 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03005002 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
5003 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005004 vmcs_writel(GUEST_RFLAGS, flags);
5005
Rusty Russell66aee912007-07-17 23:34:16 +10005006 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
5007 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005008
5009 update_exception_bitmap(vcpu);
5010
Gleb Natapov91b0aa22013-01-21 15:36:47 +02005011 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
5012 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
5013 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
5014 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
5015 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
5016 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005017}
5018
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005019static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005020{
Mathias Krause772e0312012-08-30 01:30:19 +02005021 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02005022 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005023
Gleb Natapovd99e4152012-12-20 16:57:45 +02005024 var.dpl = 0x3;
5025 if (seg == VCPU_SREG_CS)
5026 var.type = 0x3;
5027
5028 if (!emulate_invalid_guest_state) {
5029 var.selector = var.base >> 4;
5030 var.base = var.base & 0xffff0;
5031 var.limit = 0xffff;
5032 var.g = 0;
5033 var.db = 0;
5034 var.present = 1;
5035 var.s = 1;
5036 var.l = 0;
5037 var.unusable = 0;
5038 var.type = 0x3;
5039 var.avl = 0;
5040 if (save->base & 0xf)
5041 printk_once(KERN_WARNING "kvm: segment base is not "
5042 "paragraph aligned when entering "
5043 "protected mode (seg=%d)", seg);
5044 }
5045
5046 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05005047 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005048 vmcs_write32(sf->limit, var.limit);
5049 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005050}
5051
5052static void enter_rmode(struct kvm_vcpu *vcpu)
5053{
5054 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03005055 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005056 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005057
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005058 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
5059 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
5060 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
5061 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
5062 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005063 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
5064 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005065
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005066 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005067
Gleb Natapov776e58e2011-03-13 12:34:27 +02005068 /*
5069 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01005070 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02005071 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005072 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02005073 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
5074 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02005075
Avi Kivity2fb92db2011-04-27 19:42:18 +03005076 vmx_segment_cache_clear(vmx);
5077
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005078 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005079 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005080 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5081
5082 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03005083 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005084
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01005085 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005086
5087 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10005088 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005089 update_exception_bitmap(vcpu);
5090
Gleb Natapovd99e4152012-12-20 16:57:45 +02005091 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
5092 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
5093 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
5094 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
5095 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
5096 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03005097
Eddie Dong8668a3c2007-10-10 14:26:45 +08005098 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005099}
5100
Amit Shah401d10d2009-02-20 22:53:37 +05305101static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
5102{
5103 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03005104 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
5105
5106 if (!msr)
5107 return;
Amit Shah401d10d2009-02-20 22:53:37 +05305108
Avi Kivityf6801df2010-01-21 15:31:50 +02005109 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05305110 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02005111 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05305112 msr->data = efer;
5113 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02005114 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05305115
5116 msr->data = efer & ~EFER_LME;
5117 }
5118 setup_msrs(vmx);
5119}
5120
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005121#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005122
5123static void enter_lmode(struct kvm_vcpu *vcpu)
5124{
5125 u32 guest_tr_ar;
5126
Avi Kivity2fb92db2011-04-27 19:42:18 +03005127 vmx_segment_cache_clear(to_vmx(vcpu));
5128
Avi Kivity6aa8b732006-12-10 02:21:36 -08005129 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005130 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02005131 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
5132 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005133 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005134 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
5135 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005136 }
Avi Kivityda38f432010-07-06 11:30:49 +03005137 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005138}
5139
5140static void exit_lmode(struct kvm_vcpu *vcpu)
5141{
Gleb Natapov2961e8762013-11-25 15:37:13 +02005142 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03005143 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005144}
5145
5146#endif
5147
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005148static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
5149 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005150{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005151 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005152 if (!VALID_PAGE(vcpu->arch.mmu->root_hpa))
Xiao Guangrongdd180b32010-07-03 16:02:42 +08005153 return;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005154 ept_sync_context(construct_eptp(vcpu,
5155 vcpu->arch.mmu->root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07005156 } else {
5157 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08005158 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08005159}
5160
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005161static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005162{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005163 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005164}
5165
Junaid Shahidfaff8752018-06-29 13:10:05 -07005166static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
5167{
5168 int vpid = to_vmx(vcpu)->vpid;
5169
5170 if (!vpid_sync_vcpu_addr(vpid, addr))
5171 vpid_sync_context(vpid);
5172
5173 /*
5174 * If VPIDs are not supported or enabled, then the above is a no-op.
5175 * But we don't really need a TLB flush in that case anyway, because
5176 * each VM entry/exit includes an implicit flush when VPID is 0.
5177 */
5178}
5179
Avi Kivitye8467fd2009-12-29 18:43:06 +02005180static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
5181{
5182 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
5183
5184 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
5185 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
5186}
5187
Avi Kivityaff48ba2010-12-05 18:56:11 +02005188static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
5189{
Sean Christophersonb4d18512018-03-05 12:04:40 -08005190 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02005191 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
5192 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
5193}
5194
Anthony Liguori25c4c272007-04-27 09:29:21 +03005195static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08005196{
Avi Kivityfc78f512009-12-07 12:16:48 +02005197 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
5198
5199 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
5200 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08005201}
5202
Sheng Yang14394422008-04-28 12:24:45 +08005203static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
5204{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005205 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5206
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005207 if (!test_bit(VCPU_EXREG_PDPTR,
5208 (unsigned long *)&vcpu->arch.regs_dirty))
5209 return;
5210
Sheng Yang14394422008-04-28 12:24:45 +08005211 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005212 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
5213 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
5214 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
5215 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08005216 }
5217}
5218
Avi Kivity8f5d5492009-05-31 18:41:29 +03005219static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
5220{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005221 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5222
Avi Kivity8f5d5492009-05-31 18:41:29 +03005223 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005224 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
5225 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
5226 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
5227 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005228 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005229
5230 __set_bit(VCPU_EXREG_PDPTR,
5231 (unsigned long *)&vcpu->arch.regs_avail);
5232 __set_bit(VCPU_EXREG_PDPTR,
5233 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005234}
5235
David Matlack38991522016-11-29 18:14:08 -08005236static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5237{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005238 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5239 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005240 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5241
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005242 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08005243 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5244 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5245 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
5246
5247 return fixed_bits_valid(val, fixed0, fixed1);
5248}
5249
5250static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5251{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005252 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5253 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005254
5255 return fixed_bits_valid(val, fixed0, fixed1);
5256}
5257
5258static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
5259{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005260 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
5261 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005262
5263 return fixed_bits_valid(val, fixed0, fixed1);
5264}
5265
5266/* No difference in the restrictions on guest and host CR4 in VMX operation. */
5267#define nested_guest_cr4_valid nested_cr4_valid
5268#define nested_host_cr4_valid nested_cr4_valid
5269
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005270static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08005271
5272static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
5273 unsigned long cr0,
5274 struct kvm_vcpu *vcpu)
5275{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03005276 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
5277 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005278 if (!(cr0 & X86_CR0_PG)) {
5279 /* From paging/starting to nonpaging */
5280 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005281 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08005282 (CPU_BASED_CR3_LOAD_EXITING |
5283 CPU_BASED_CR3_STORE_EXITING));
5284 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005285 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005286 } else if (!is_paging(vcpu)) {
5287 /* From nonpaging to paging */
5288 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005289 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08005290 ~(CPU_BASED_CR3_LOAD_EXITING |
5291 CPU_BASED_CR3_STORE_EXITING));
5292 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005293 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005294 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08005295
5296 if (!(cr0 & X86_CR0_WP))
5297 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08005298}
5299
Avi Kivity6aa8b732006-12-10 02:21:36 -08005300static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
5301{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005302 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005303 unsigned long hw_cr0;
5304
Sean Christopherson3de63472018-07-13 08:42:30 -07005305 hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005306 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02005307 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02005308 else {
Gleb Natapov50378782013-02-04 16:00:28 +02005309 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005310
Gleb Natapov218e7632013-01-21 15:36:45 +02005311 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
5312 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005313
Gleb Natapov218e7632013-01-21 15:36:45 +02005314 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
5315 enter_rmode(vcpu);
5316 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005317
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005318#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02005319 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10005320 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005321 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10005322 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005323 exit_lmode(vcpu);
5324 }
5325#endif
5326
Sean Christophersonb4d18512018-03-05 12:04:40 -08005327 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08005328 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
5329
Avi Kivity6aa8b732006-12-10 02:21:36 -08005330 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08005331 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005332 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02005333
5334 /* depends on vcpu->arch.cr0 to be set to a new value */
5335 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005336}
5337
Yu Zhang855feb62017-08-24 20:27:55 +08005338static int get_ept_level(struct kvm_vcpu *vcpu)
5339{
5340 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
5341 return 5;
5342 return 4;
5343}
5344
Peter Feiner995f00a2017-06-30 17:26:32 -07005345static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08005346{
Yu Zhang855feb62017-08-24 20:27:55 +08005347 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08005348
Yu Zhang855feb62017-08-24 20:27:55 +08005349 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08005350
Peter Feiner995f00a2017-06-30 17:26:32 -07005351 if (enable_ept_ad_bits &&
5352 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02005353 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08005354 eptp |= (root_hpa & PAGE_MASK);
5355
5356 return eptp;
5357}
5358
Avi Kivity6aa8b732006-12-10 02:21:36 -08005359static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5360{
Tianyu Lan877ad952018-07-19 08:40:23 +00005361 struct kvm *kvm = vcpu->kvm;
Sheng Yang14394422008-04-28 12:24:45 +08005362 unsigned long guest_cr3;
5363 u64 eptp;
5364
5365 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02005366 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07005367 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08005368 vmcs_write64(EPT_POINTER, eptp);
Tianyu Lan877ad952018-07-19 08:40:23 +00005369
5370 if (kvm_x86_ops->tlb_remote_flush) {
5371 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5372 to_vmx(vcpu)->ept_pointer = eptp;
5373 to_kvm_vmx(kvm)->ept_pointers_match
5374 = EPT_POINTERS_CHECK;
5375 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5376 }
5377
Sean Christophersone90008d2018-03-05 12:04:37 -08005378 if (enable_unrestricted_guest || is_paging(vcpu) ||
5379 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02005380 guest_cr3 = kvm_read_cr3(vcpu);
5381 else
Tianyu Lan877ad952018-07-19 08:40:23 +00005382 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005383 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005384 }
5385
Sheng Yang14394422008-04-28 12:24:45 +08005386 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005387}
5388
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005389static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005390{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005391 /*
5392 * Pass through host's Machine Check Enable value to hw_cr4, which
5393 * is in force while we are in guest mode. Do not let guests control
5394 * this bit, even if host CR4.MCE == 0.
5395 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005396 unsigned long hw_cr4;
5397
5398 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5399 if (enable_unrestricted_guest)
5400 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5401 else if (to_vmx(vcpu)->rmode.vm86_active)
5402 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5403 else
5404 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005405
Sean Christopherson64f7a112018-04-30 10:01:06 -07005406 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5407 if (cr4 & X86_CR4_UMIP) {
5408 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005409 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005410 hw_cr4 &= ~X86_CR4_UMIP;
5411 } else if (!is_guest_mode(vcpu) ||
5412 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5413 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5414 SECONDARY_EXEC_DESC);
5415 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005416
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005417 if (cr4 & X86_CR4_VMXE) {
5418 /*
5419 * To use VMXON (and later other VMX instructions), a guest
5420 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5421 * So basically the check on whether to allow nested VMX
Paolo Bonzini5bea5122018-09-18 15:19:17 +02005422 * is here. We operate under the default treatment of SMM,
5423 * so VMX cannot be enabled under SMM.
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005424 */
Paolo Bonzini5bea5122018-09-18 15:19:17 +02005425 if (!nested_vmx_allowed(vcpu) || is_smm(vcpu))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005426 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005427 }
David Matlack38991522016-11-29 18:14:08 -08005428
5429 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005430 return 1;
5431
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005432 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005433
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005434 if (!enable_unrestricted_guest) {
5435 if (enable_ept) {
5436 if (!is_paging(vcpu)) {
5437 hw_cr4 &= ~X86_CR4_PAE;
5438 hw_cr4 |= X86_CR4_PSE;
5439 } else if (!(cr4 & X86_CR4_PAE)) {
5440 hw_cr4 &= ~X86_CR4_PAE;
5441 }
5442 }
5443
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005444 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005445 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5446 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5447 * to be manually disabled when guest switches to non-paging
5448 * mode.
5449 *
5450 * If !enable_unrestricted_guest, the CPU is always running
5451 * with CR0.PG=1 and CR4 needs to be modified.
5452 * If enable_unrestricted_guest, the CPU automatically
5453 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005454 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005455 if (!is_paging(vcpu))
5456 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5457 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005458
Sheng Yang14394422008-04-28 12:24:45 +08005459 vmcs_writel(CR4_READ_SHADOW, cr4);
5460 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005461 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005462}
5463
Avi Kivity6aa8b732006-12-10 02:21:36 -08005464static void vmx_get_segment(struct kvm_vcpu *vcpu,
5465 struct kvm_segment *var, int seg)
5466{
Avi Kivitya9179492011-01-03 14:28:52 +02005467 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005468 u32 ar;
5469
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005470 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005471 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005472 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005473 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005474 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005475 var->base = vmx_read_guest_seg_base(vmx, seg);
5476 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5477 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005478 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005479 var->base = vmx_read_guest_seg_base(vmx, seg);
5480 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5481 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5482 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005483 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005484 var->type = ar & 15;
5485 var->s = (ar >> 4) & 1;
5486 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005487 /*
5488 * Some userspaces do not preserve unusable property. Since usable
5489 * segment has to be present according to VMX spec we can use present
5490 * property to amend userspace bug by making unusable segment always
5491 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5492 * segment as unusable.
5493 */
5494 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005495 var->avl = (ar >> 12) & 1;
5496 var->l = (ar >> 13) & 1;
5497 var->db = (ar >> 14) & 1;
5498 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005499}
5500
Avi Kivitya9179492011-01-03 14:28:52 +02005501static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5502{
Avi Kivitya9179492011-01-03 14:28:52 +02005503 struct kvm_segment s;
5504
5505 if (to_vmx(vcpu)->rmode.vm86_active) {
5506 vmx_get_segment(vcpu, &s, seg);
5507 return s.base;
5508 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005509 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005510}
5511
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005512static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005513{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005514 struct vcpu_vmx *vmx = to_vmx(vcpu);
5515
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005516 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005517 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005518 else {
5519 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005520 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005521 }
Avi Kivity69c73022011-03-07 15:26:44 +02005522}
5523
Avi Kivity653e3102007-05-07 10:55:37 +03005524static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005525{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005526 u32 ar;
5527
Avi Kivityf0495f92012-06-07 17:06:10 +03005528 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005529 ar = 1 << 16;
5530 else {
5531 ar = var->type & 15;
5532 ar |= (var->s & 1) << 4;
5533 ar |= (var->dpl & 3) << 5;
5534 ar |= (var->present & 1) << 7;
5535 ar |= (var->avl & 1) << 12;
5536 ar |= (var->l & 1) << 13;
5537 ar |= (var->db & 1) << 14;
5538 ar |= (var->g & 1) << 15;
5539 }
Avi Kivity653e3102007-05-07 10:55:37 +03005540
5541 return ar;
5542}
5543
5544static void vmx_set_segment(struct kvm_vcpu *vcpu,
5545 struct kvm_segment *var, int seg)
5546{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005547 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005548 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005549
Avi Kivity2fb92db2011-04-27 19:42:18 +03005550 vmx_segment_cache_clear(vmx);
5551
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005552 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5553 vmx->rmode.segs[seg] = *var;
5554 if (seg == VCPU_SREG_TR)
5555 vmcs_write16(sf->selector, var->selector);
5556 else if (var->s)
5557 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005558 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005559 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005560
Avi Kivity653e3102007-05-07 10:55:37 +03005561 vmcs_writel(sf->base, var->base);
5562 vmcs_write32(sf->limit, var->limit);
5563 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005564
5565 /*
5566 * Fix the "Accessed" bit in AR field of segment registers for older
5567 * qemu binaries.
5568 * IA32 arch specifies that at the time of processor reset the
5569 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005570 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005571 * state vmexit when "unrestricted guest" mode is turned on.
5572 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5573 * tree. Newer qemu binaries with that qemu fix would not need this
5574 * kvm hack.
5575 */
5576 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005577 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005578
Gleb Natapovf924d662012-12-12 19:10:55 +02005579 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005580
5581out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005582 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005583}
5584
Avi Kivity6aa8b732006-12-10 02:21:36 -08005585static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5586{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005587 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005588
5589 *db = (ar >> 14) & 1;
5590 *l = (ar >> 13) & 1;
5591}
5592
Gleb Natapov89a27f42010-02-16 10:51:48 +02005593static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005594{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005595 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5596 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005597}
5598
Gleb Natapov89a27f42010-02-16 10:51:48 +02005599static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005600{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005601 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5602 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005603}
5604
Gleb Natapov89a27f42010-02-16 10:51:48 +02005605static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005606{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005607 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5608 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005609}
5610
Gleb Natapov89a27f42010-02-16 10:51:48 +02005611static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005612{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005613 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5614 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005615}
5616
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005617static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5618{
5619 struct kvm_segment var;
5620 u32 ar;
5621
5622 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005623 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005624 if (seg == VCPU_SREG_CS)
5625 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005626 ar = vmx_segment_access_rights(&var);
5627
5628 if (var.base != (var.selector << 4))
5629 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005630 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005631 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005632 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005633 return false;
5634
5635 return true;
5636}
5637
5638static bool code_segment_valid(struct kvm_vcpu *vcpu)
5639{
5640 struct kvm_segment cs;
5641 unsigned int cs_rpl;
5642
5643 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005644 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005645
Avi Kivity1872a3f2009-01-04 23:26:52 +02005646 if (cs.unusable)
5647 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005648 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005649 return false;
5650 if (!cs.s)
5651 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005652 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005653 if (cs.dpl > cs_rpl)
5654 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005655 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005656 if (cs.dpl != cs_rpl)
5657 return false;
5658 }
5659 if (!cs.present)
5660 return false;
5661
5662 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5663 return true;
5664}
5665
5666static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5667{
5668 struct kvm_segment ss;
5669 unsigned int ss_rpl;
5670
5671 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005672 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005673
Avi Kivity1872a3f2009-01-04 23:26:52 +02005674 if (ss.unusable)
5675 return true;
5676 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005677 return false;
5678 if (!ss.s)
5679 return false;
5680 if (ss.dpl != ss_rpl) /* DPL != RPL */
5681 return false;
5682 if (!ss.present)
5683 return false;
5684
5685 return true;
5686}
5687
5688static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5689{
5690 struct kvm_segment var;
5691 unsigned int rpl;
5692
5693 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005694 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005695
Avi Kivity1872a3f2009-01-04 23:26:52 +02005696 if (var.unusable)
5697 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005698 if (!var.s)
5699 return false;
5700 if (!var.present)
5701 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005702 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005703 if (var.dpl < rpl) /* DPL < RPL */
5704 return false;
5705 }
5706
5707 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5708 * rights flags
5709 */
5710 return true;
5711}
5712
5713static bool tr_valid(struct kvm_vcpu *vcpu)
5714{
5715 struct kvm_segment tr;
5716
5717 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5718
Avi Kivity1872a3f2009-01-04 23:26:52 +02005719 if (tr.unusable)
5720 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005721 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005722 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005723 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005724 return false;
5725 if (!tr.present)
5726 return false;
5727
5728 return true;
5729}
5730
5731static bool ldtr_valid(struct kvm_vcpu *vcpu)
5732{
5733 struct kvm_segment ldtr;
5734
5735 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5736
Avi Kivity1872a3f2009-01-04 23:26:52 +02005737 if (ldtr.unusable)
5738 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005739 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005740 return false;
5741 if (ldtr.type != 2)
5742 return false;
5743 if (!ldtr.present)
5744 return false;
5745
5746 return true;
5747}
5748
5749static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5750{
5751 struct kvm_segment cs, ss;
5752
5753 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5754 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5755
Nadav Amitb32a9912015-03-29 16:33:04 +03005756 return ((cs.selector & SEGMENT_RPL_MASK) ==
5757 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005758}
5759
5760/*
5761 * Check if guest state is valid. Returns true if valid, false if
5762 * not.
5763 * We assume that registers are always usable
5764 */
5765static bool guest_state_valid(struct kvm_vcpu *vcpu)
5766{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005767 if (enable_unrestricted_guest)
5768 return true;
5769
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005770 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005771 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005772 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5773 return false;
5774 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5775 return false;
5776 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5777 return false;
5778 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5779 return false;
5780 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5781 return false;
5782 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5783 return false;
5784 } else {
5785 /* protected mode guest state checks */
5786 if (!cs_ss_rpl_check(vcpu))
5787 return false;
5788 if (!code_segment_valid(vcpu))
5789 return false;
5790 if (!stack_segment_valid(vcpu))
5791 return false;
5792 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5793 return false;
5794 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5795 return false;
5796 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5797 return false;
5798 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5799 return false;
5800 if (!tr_valid(vcpu))
5801 return false;
5802 if (!ldtr_valid(vcpu))
5803 return false;
5804 }
5805 /* TODO:
5806 * - Add checks on RIP
5807 * - Add checks on RFLAGS
5808 */
5809
5810 return true;
5811}
5812
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005813static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5814{
5815 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5816}
5817
Mike Dayd77c26f2007-10-08 09:02:08 -04005818static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005819{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005820 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005821 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005822 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005823
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005824 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005825 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005826 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5827 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005828 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005829 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005830 r = kvm_write_guest_page(kvm, fn++, &data,
5831 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005832 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005833 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005834 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5835 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005836 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005837 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5838 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005839 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005840 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005841 r = kvm_write_guest_page(kvm, fn, &data,
5842 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5843 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005844out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005845 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005846 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005847}
5848
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005849static int init_rmode_identity_map(struct kvm *kvm)
5850{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005851 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005852 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005853 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005854 u32 tmp;
5855
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005856 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005857 mutex_lock(&kvm->slots_lock);
5858
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005859 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005860 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005861
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005862 if (!kvm_vmx->ept_identity_map_addr)
5863 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5864 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005865
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005866 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005867 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005868 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005869 goto out2;
5870
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005871 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005872 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5873 if (r < 0)
5874 goto out;
5875 /* Set up identity-mapping pagetable for EPT in real mode */
5876 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5877 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5878 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5879 r = kvm_write_guest_page(kvm, identity_map_pfn,
5880 &tmp, i * sizeof(tmp), sizeof(tmp));
5881 if (r < 0)
5882 goto out;
5883 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005884 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005885
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005886out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005887 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005888
5889out2:
5890 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005891 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005892}
5893
Avi Kivity6aa8b732006-12-10 02:21:36 -08005894static void seg_setup(int seg)
5895{
Mathias Krause772e0312012-08-30 01:30:19 +02005896 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005897 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005898
5899 vmcs_write16(sf->selector, 0);
5900 vmcs_writel(sf->base, 0);
5901 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005902 ar = 0x93;
5903 if (seg == VCPU_SREG_CS)
5904 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005905
5906 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005907}
5908
Sheng Yangf78e0e22007-10-29 09:40:42 +08005909static int alloc_apic_access_page(struct kvm *kvm)
5910{
Xiao Guangrong44841412012-09-07 14:14:20 +08005911 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005912 int r = 0;
5913
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005914 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005915 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005916 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005917 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5918 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005919 if (r)
5920 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005921
Tang Chen73a6d942014-09-11 13:38:00 +08005922 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005923 if (is_error_page(page)) {
5924 r = -EFAULT;
5925 goto out;
5926 }
5927
Tang Chenc24ae0d2014-09-24 15:57:58 +08005928 /*
5929 * Do not pin the page in memory, so that memory hot-unplug
5930 * is able to migrate it.
5931 */
5932 put_page(page);
5933 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005934out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005935 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005936 return r;
5937}
5938
Wanpeng Li991e7a02015-09-16 17:30:05 +08005939static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005940{
5941 int vpid;
5942
Avi Kivity919818a2009-03-23 18:01:29 +02005943 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005944 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005945 spin_lock(&vmx_vpid_lock);
5946 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005947 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005948 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005949 else
5950 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005951 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005952 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005953}
5954
Wanpeng Li991e7a02015-09-16 17:30:05 +08005955static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005956{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005957 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005958 return;
5959 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005960 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005961 spin_unlock(&vmx_vpid_lock);
5962}
5963
Yi Wang1e4329ee2018-11-08 11:22:21 +08005964static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005965 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005966{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005967 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005968
5969 if (!cpu_has_vmx_msr_bitmap())
5970 return;
5971
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005972 if (static_branch_unlikely(&enable_evmcs))
5973 evmcs_touch_msr_bitmap();
5974
Sheng Yang25c5f222008-03-28 13:18:56 +08005975 /*
5976 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5977 * have the write-low and read-high bitmap offsets the wrong way round.
5978 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5979 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005980 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005981 if (type & MSR_TYPE_R)
5982 /* read-low */
5983 __clear_bit(msr, msr_bitmap + 0x000 / f);
5984
5985 if (type & MSR_TYPE_W)
5986 /* write-low */
5987 __clear_bit(msr, msr_bitmap + 0x800 / f);
5988
Sheng Yang25c5f222008-03-28 13:18:56 +08005989 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5990 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005991 if (type & MSR_TYPE_R)
5992 /* read-high */
5993 __clear_bit(msr, msr_bitmap + 0x400 / f);
5994
5995 if (type & MSR_TYPE_W)
5996 /* write-high */
5997 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5998
5999 }
6000}
6001
Yi Wang1e4329ee2018-11-08 11:22:21 +08006002static __always_inline void vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006003 u32 msr, int type)
6004{
6005 int f = sizeof(unsigned long);
6006
6007 if (!cpu_has_vmx_msr_bitmap())
6008 return;
6009
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02006010 if (static_branch_unlikely(&enable_evmcs))
6011 evmcs_touch_msr_bitmap();
6012
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006013 /*
6014 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
6015 * have the write-low and read-high bitmap offsets the wrong way round.
6016 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
6017 */
6018 if (msr <= 0x1fff) {
6019 if (type & MSR_TYPE_R)
6020 /* read-low */
6021 __set_bit(msr, msr_bitmap + 0x000 / f);
6022
6023 if (type & MSR_TYPE_W)
6024 /* write-low */
6025 __set_bit(msr, msr_bitmap + 0x800 / f);
6026
6027 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
6028 msr &= 0x1fff;
6029 if (type & MSR_TYPE_R)
6030 /* read-high */
6031 __set_bit(msr, msr_bitmap + 0x400 / f);
6032
6033 if (type & MSR_TYPE_W)
6034 /* write-high */
6035 __set_bit(msr, msr_bitmap + 0xc00 / f);
6036
6037 }
6038}
6039
Yi Wang1e4329ee2018-11-08 11:22:21 +08006040static __always_inline void vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006041 u32 msr, int type, bool value)
6042{
6043 if (value)
6044 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
6045 else
6046 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
6047}
6048
Wincy Vanf2b93282015-02-03 23:56:03 +08006049/*
6050 * If a msr is allowed by L0, we should check whether it is allowed by L1.
6051 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
6052 */
6053static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
6054 unsigned long *msr_bitmap_nested,
6055 u32 msr, int type)
6056{
6057 int f = sizeof(unsigned long);
6058
Wincy Vanf2b93282015-02-03 23:56:03 +08006059 /*
6060 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
6061 * have the write-low and read-high bitmap offsets the wrong way round.
6062 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
6063 */
6064 if (msr <= 0x1fff) {
6065 if (type & MSR_TYPE_R &&
6066 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
6067 /* read-low */
6068 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
6069
6070 if (type & MSR_TYPE_W &&
6071 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
6072 /* write-low */
6073 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
6074
6075 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
6076 msr &= 0x1fff;
6077 if (type & MSR_TYPE_R &&
6078 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
6079 /* read-high */
6080 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
6081
6082 if (type & MSR_TYPE_W &&
6083 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
6084 /* write-high */
6085 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
6086
6087 }
6088}
6089
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006090static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02006091{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006092 u8 mode = 0;
6093
6094 if (cpu_has_secondary_exec_ctrls() &&
6095 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
6096 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
6097 mode |= MSR_BITMAP_MODE_X2APIC;
6098 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
6099 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
6100 }
6101
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006102 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08006103}
6104
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006105#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
6106
6107static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
6108 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08006109{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006110 int msr;
6111
6112 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
6113 unsigned word = msr / BITS_PER_LONG;
6114 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
6115 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006116 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006117
6118 if (mode & MSR_BITMAP_MODE_X2APIC) {
6119 /*
6120 * TPR reads and writes can be virtualized even if virtual interrupt
6121 * delivery is not in use.
6122 */
6123 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
6124 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
6125 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
6126 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
6127 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
6128 }
6129 }
6130}
6131
6132static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
6133{
6134 struct vcpu_vmx *vmx = to_vmx(vcpu);
6135 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
6136 u8 mode = vmx_msr_bitmap_mode(vcpu);
6137 u8 changed = mode ^ vmx->msr_bitmap_mode;
6138
6139 if (!changed)
6140 return;
6141
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006142 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
6143 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
6144
6145 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02006146}
6147
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05006148static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02006149{
Andrey Smetanind62caab2015-11-10 15:36:33 +03006150 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02006151}
6152
David Matlackc9f04402017-08-01 14:00:40 -07006153static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
6154{
6155 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6156 gfn_t gfn;
6157
6158 /*
6159 * Don't need to mark the APIC access page dirty; it is never
6160 * written to by the CPU during APIC virtualization.
6161 */
6162
6163 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
6164 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
6165 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6166 }
6167
6168 if (nested_cpu_has_posted_intr(vmcs12)) {
6169 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
6170 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6171 }
6172}
6173
6174
David Hildenbrand6342c502017-01-25 11:58:58 +01006175static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08006176{
6177 struct vcpu_vmx *vmx = to_vmx(vcpu);
6178 int max_irr;
6179 void *vapic_page;
6180 u16 status;
6181
David Matlackc9f04402017-08-01 14:00:40 -07006182 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
6183 return;
Wincy Van705699a2015-02-03 23:58:17 +08006184
David Matlackc9f04402017-08-01 14:00:40 -07006185 vmx->nested.pi_pending = false;
6186 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
6187 return;
Wincy Van705699a2015-02-03 23:58:17 +08006188
David Matlackc9f04402017-08-01 14:00:40 -07006189 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
6190 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08006191 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02006192 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
6193 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08006194 kunmap(vmx->nested.virtual_apic_page);
6195
6196 status = vmcs_read16(GUEST_INTR_STATUS);
6197 if ((u8)max_irr > ((u8)status & 0xff)) {
6198 status &= ~0xff;
6199 status |= (u8)max_irr;
6200 vmcs_write16(GUEST_INTR_STATUS, status);
6201 }
6202 }
David Matlackc9f04402017-08-01 14:00:40 -07006203
6204 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006205}
6206
Paolo Bonzini7e712682018-10-03 13:44:26 +02006207static u8 vmx_get_rvi(void)
6208{
6209 return vmcs_read16(GUEST_INTR_STATUS) & 0xff;
6210}
6211
Liran Alone6c67d82018-09-04 10:56:52 +03006212static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
6213{
6214 struct vcpu_vmx *vmx = to_vmx(vcpu);
6215 void *vapic_page;
6216 u32 vppr;
6217 int rvi;
6218
6219 if (WARN_ON_ONCE(!is_guest_mode(vcpu)) ||
6220 !nested_cpu_has_vid(get_vmcs12(vcpu)) ||
6221 WARN_ON_ONCE(!vmx->nested.virtual_apic_page))
6222 return false;
6223
Paolo Bonzini7e712682018-10-03 13:44:26 +02006224 rvi = vmx_get_rvi();
Liran Alone6c67d82018-09-04 10:56:52 +03006225
6226 vapic_page = kmap(vmx->nested.virtual_apic_page);
6227 vppr = *((u32 *)(vapic_page + APIC_PROCPRI));
6228 kunmap(vmx->nested.virtual_apic_page);
6229
6230 return ((rvi & 0xf0) > (vppr & 0xf0));
6231}
6232
Wincy Van06a55242017-04-28 13:13:59 +08006233static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
6234 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006235{
6236#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08006237 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
6238
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006239 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08006240 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006241 * The vector of interrupt to be delivered to vcpu had
6242 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08006243 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006244 * Following cases will be reached in this block, and
6245 * we always send a notification event in all cases as
6246 * explained below.
6247 *
6248 * Case 1: vcpu keeps in non-root mode. Sending a
6249 * notification event posts the interrupt to vcpu.
6250 *
6251 * Case 2: vcpu exits to root mode and is still
6252 * runnable. PIR will be synced to vIRR before the
6253 * next vcpu entry. Sending a notification event in
6254 * this case has no effect, as vcpu is not in root
6255 * mode.
6256 *
6257 * Case 3: vcpu exits to root mode and is blocked.
6258 * vcpu_block() has already synced PIR to vIRR and
6259 * never blocks vcpu if vIRR is not cleared. Therefore,
6260 * a blocked vcpu here does not wait for any requested
6261 * interrupts in PIR, and sending a notification event
6262 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08006263 */
Feng Wu28b835d2015-09-18 22:29:54 +08006264
Wincy Van06a55242017-04-28 13:13:59 +08006265 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006266 return true;
6267 }
6268#endif
6269 return false;
6270}
6271
Wincy Van705699a2015-02-03 23:58:17 +08006272static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
6273 int vector)
6274{
6275 struct vcpu_vmx *vmx = to_vmx(vcpu);
6276
6277 if (is_guest_mode(vcpu) &&
6278 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08006279 /*
6280 * If a posted intr is not recognized by hardware,
6281 * we will accomplish it in the next vmentry.
6282 */
6283 vmx->nested.pi_pending = true;
6284 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02006285 /* the PIR and ON have been set by L1. */
6286 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
6287 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006288 return 0;
6289 }
6290 return -1;
6291}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006292/*
Yang Zhanga20ed542013-04-11 19:25:15 +08006293 * Send interrupt to vcpu via posted interrupt way.
6294 * 1. If target vcpu is running(non-root mode), send posted interrupt
6295 * notification to vcpu and hardware will sync PIR to vIRR atomically.
6296 * 2. If target vcpu isn't running(root mode), kick it to pick up the
6297 * interrupt from PIR in next vmentry.
6298 */
6299static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
6300{
6301 struct vcpu_vmx *vmx = to_vmx(vcpu);
6302 int r;
6303
Wincy Van705699a2015-02-03 23:58:17 +08006304 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
6305 if (!r)
6306 return;
6307
Yang Zhanga20ed542013-04-11 19:25:15 +08006308 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
6309 return;
6310
Paolo Bonzinib95234c2016-12-19 13:57:33 +01006311 /* If a previous notification has sent the IPI, nothing to do. */
6312 if (pi_test_and_set_on(&vmx->pi_desc))
6313 return;
6314
Wincy Van06a55242017-04-28 13:13:59 +08006315 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08006316 kvm_vcpu_kick(vcpu);
6317}
6318
Avi Kivity6aa8b732006-12-10 02:21:36 -08006319/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006320 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
6321 * will not change in the lifetime of the guest.
6322 * Note that host-state that does change is set elsewhere. E.g., host-state
6323 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
6324 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006325static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006326{
6327 u32 low32, high32;
6328 unsigned long tmpl;
6329 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006330 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006331
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07006332 cr0 = read_cr0();
6333 WARN_ON(cr0 & X86_CR0_TS);
6334 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006335
6336 /*
6337 * Save the most likely value for this task's CR3 in the VMCS.
6338 * We can't use __get_current_cr3_fast() because we're not atomic.
6339 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07006340 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006341 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Sean Christophersond7ee0392018-07-23 12:32:47 -07006342 vmx->loaded_vmcs->host_state.cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006343
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006344 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07006345 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006346 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07006347 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006348
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006349 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006350#ifdef CONFIG_X86_64
6351 /*
6352 * Load null selectors, so we can avoid reloading them in
Sean Christopherson6d6095b2018-07-23 12:32:44 -07006353 * vmx_prepare_switch_to_host(), in case userspace uses
6354 * the null selectors too (the expected case).
Avi Kivityb2da15a2012-05-13 19:53:24 +03006355 */
6356 vmcs_write16(HOST_DS_SELECTOR, 0);
6357 vmcs_write16(HOST_ES_SELECTOR, 0);
6358#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006359 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6360 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006361#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006362 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6363 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
6364
Juergen Gross87930012017-09-04 12:25:27 +02006365 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006366 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006367 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006368
Avi Kivity83287ea422012-09-16 15:10:57 +03006369 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006370
6371 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
6372 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
6373 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
6374 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
6375
6376 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
6377 rdmsr(MSR_IA32_CR_PAT, low32, high32);
6378 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
6379 }
Sean Christopherson5a5e8a12018-09-26 09:23:56 -07006380
6381 if (cpu_has_load_ia32_efer)
6382 vmcs_write64(HOST_IA32_EFER, host_efer);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006383}
6384
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006385static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6386{
6387 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6388 if (enable_ept)
6389 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006390 if (is_guest_mode(&vmx->vcpu))
6391 vmx->vcpu.arch.cr4_guest_owned_bits &=
6392 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006393 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6394}
6395
Yang Zhang01e439b2013-04-11 19:25:12 +08006396static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6397{
6398 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6399
Andrey Smetanind62caab2015-11-10 15:36:33 +03006400 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006401 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006402
6403 if (!enable_vnmi)
6404 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6405
Yunhong Jiang64672c92016-06-13 14:19:59 -07006406 /* Enable the preemption timer dynamically */
6407 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006408 return pin_based_exec_ctrl;
6409}
6410
Andrey Smetanind62caab2015-11-10 15:36:33 +03006411static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6412{
6413 struct vcpu_vmx *vmx = to_vmx(vcpu);
6414
6415 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006416 if (cpu_has_secondary_exec_ctrls()) {
6417 if (kvm_vcpu_apicv_active(vcpu))
6418 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6419 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6420 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6421 else
6422 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6423 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6424 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6425 }
6426
6427 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006428 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006429}
6430
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006431static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6432{
6433 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006434
6435 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6436 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6437
Paolo Bonzini35754c92015-07-29 12:05:37 +02006438 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006439 exec_control &= ~CPU_BASED_TPR_SHADOW;
6440#ifdef CONFIG_X86_64
6441 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6442 CPU_BASED_CR8_LOAD_EXITING;
6443#endif
6444 }
6445 if (!enable_ept)
6446 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6447 CPU_BASED_CR3_LOAD_EXITING |
6448 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006449 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6450 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6451 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006452 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6453 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006454 return exec_control;
6455}
6456
Jim Mattson45ec3682017-08-23 16:32:04 -07006457static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006458{
Jim Mattson45ec3682017-08-23 16:32:04 -07006459 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006460 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006461}
6462
Jim Mattson75f4fc82017-08-23 16:32:03 -07006463static bool vmx_rdseed_supported(void)
6464{
6465 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006466 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006467}
6468
Paolo Bonzini80154d72017-08-24 13:55:35 +02006469static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006470{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006471 struct kvm_vcpu *vcpu = &vmx->vcpu;
6472
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006473 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006474
Paolo Bonzini80154d72017-08-24 13:55:35 +02006475 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006476 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6477 if (vmx->vpid == 0)
6478 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6479 if (!enable_ept) {
6480 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6481 enable_unrestricted_guest = 0;
6482 }
6483 if (!enable_unrestricted_guest)
6484 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006485 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006486 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006487 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006488 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6489 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006490 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006491
6492 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6493 * in vmx_set_cr4. */
6494 exec_control &= ~SECONDARY_EXEC_DESC;
6495
Abel Gordonabc4fc52013-04-18 14:35:25 +03006496 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6497 (handle_vmptrld).
6498 We can NOT enable shadow_vmcs here because we don't have yet
6499 a current VMCS12
6500 */
6501 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006502
6503 if (!enable_pml)
6504 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006505
Paolo Bonzini3db13482017-08-24 14:48:03 +02006506 if (vmx_xsaves_supported()) {
6507 /* Exposing XSAVES only when XSAVE is exposed */
6508 bool xsaves_enabled =
6509 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6510 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6511
6512 if (!xsaves_enabled)
6513 exec_control &= ~SECONDARY_EXEC_XSAVES;
6514
6515 if (nested) {
6516 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006517 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006518 SECONDARY_EXEC_XSAVES;
6519 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006520 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006521 ~SECONDARY_EXEC_XSAVES;
6522 }
6523 }
6524
Paolo Bonzini80154d72017-08-24 13:55:35 +02006525 if (vmx_rdtscp_supported()) {
6526 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6527 if (!rdtscp_enabled)
6528 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6529
6530 if (nested) {
6531 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006532 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006533 SECONDARY_EXEC_RDTSCP;
6534 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006535 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006536 ~SECONDARY_EXEC_RDTSCP;
6537 }
6538 }
6539
6540 if (vmx_invpcid_supported()) {
6541 /* Exposing INVPCID only when PCID is exposed */
6542 bool invpcid_enabled =
6543 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6544 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6545
6546 if (!invpcid_enabled) {
6547 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6548 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6549 }
6550
6551 if (nested) {
6552 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006553 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006554 SECONDARY_EXEC_ENABLE_INVPCID;
6555 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006556 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006557 ~SECONDARY_EXEC_ENABLE_INVPCID;
6558 }
6559 }
6560
Jim Mattson45ec3682017-08-23 16:32:04 -07006561 if (vmx_rdrand_supported()) {
6562 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6563 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006564 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006565
6566 if (nested) {
6567 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006568 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006569 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006570 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006571 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006572 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006573 }
6574 }
6575
Jim Mattson75f4fc82017-08-23 16:32:03 -07006576 if (vmx_rdseed_supported()) {
6577 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6578 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006579 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006580
6581 if (nested) {
6582 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006583 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006584 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006585 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006586 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006587 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006588 }
6589 }
6590
Paolo Bonzini80154d72017-08-24 13:55:35 +02006591 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006592}
6593
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006594static void ept_set_mmio_spte_mask(void)
6595{
6596 /*
6597 * EPT Misconfigurations can be generated if the value of bits 2:0
6598 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006599 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006600 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6601 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006602}
6603
Wanpeng Lif53cd632014-12-02 19:14:58 +08006604#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006605/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006606 * Sets up the vmcs for emulated real mode.
6607 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006608static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006609{
Avi Kivity6aa8b732006-12-10 02:21:36 -08006610 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006611
Abel Gordon4607c2d2013-04-18 14:35:55 +03006612 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006613 /*
6614 * At vCPU creation, "VMWRITE to any supported field
6615 * in the VMCS" is supported, so use the more
6616 * permissive vmx_vmread_bitmap to specify both read
6617 * and write permissions for the shadow VMCS.
6618 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006619 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006620 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006621 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006622 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006623 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006624
Avi Kivity6aa8b732006-12-10 02:21:36 -08006625 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6626
Avi Kivity6aa8b732006-12-10 02:21:36 -08006627 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006628 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006629 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006630
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006631 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006632
Dan Williamsdfa169b2016-06-02 11:17:24 -07006633 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006634 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006635 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006636 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006637 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006638
Andrey Smetanind62caab2015-11-10 15:36:33 +03006639 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006640 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6641 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6642 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6643 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6644
6645 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006646
Li RongQing0bcf2612015-12-03 13:29:34 +08006647 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006648 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006649 }
6650
Wanpeng Lib31c1142018-03-12 04:53:04 -07006651 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006652 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006653 vmx->ple_window = ple_window;
6654 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006655 }
6656
Xiao Guangrongc3707952011-07-12 03:28:04 +08006657 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6658 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006659 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6660
Avi Kivity9581d442010-10-19 16:46:55 +02006661 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6662 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006663 vmx_set_constant_host_state(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006664 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6665 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08006666
Bandan Das2a499e42017-08-03 15:54:41 -04006667 if (cpu_has_vmx_vmfunc())
6668 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6669
Eddie Dong2cc51562007-05-21 07:28:09 +03006670 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6671 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006672 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03006673 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006674 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006675
Radim Krčmář74545702015-04-27 15:11:25 +02006676 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6677 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006678
Paolo Bonzini03916db2014-07-24 14:21:57 +02006679 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006680 u32 index = vmx_msr_index[i];
6681 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006682 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006683
6684 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6685 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006686 if (wrmsr_safe(index, data_low, data_high) < 0)
6687 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006688 vmx->guest_msrs[j].index = i;
6689 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006690 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006691 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006692 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006693
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02006694 vmx->arch_capabilities = kvm_get_arch_capabilities();
Gleb Natapov2961e8762013-11-25 15:37:13 +02006695
6696 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006697
6698 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006699 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006700
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006701 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6702 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6703
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006704 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006705
Wanpeng Lif53cd632014-12-02 19:14:58 +08006706 if (vmx_xsaves_supported())
6707 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6708
Peter Feiner4e595162016-07-07 14:49:58 -07006709 if (enable_pml) {
Peter Feiner4e595162016-07-07 14:49:58 -07006710 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6711 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6712 }
Sean Christopherson0b665d32018-08-14 09:33:34 -07006713
6714 if (cpu_has_vmx_encls_vmexit())
6715 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006716}
6717
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006718static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006719{
6720 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006721 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006722 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006723
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006724 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006725 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006726
Wanpeng Li518e7b92018-02-28 14:03:31 +08006727 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006728 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006729 kvm_set_cr8(vcpu, 0);
6730
6731 if (!init_event) {
6732 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6733 MSR_IA32_APICBASE_ENABLE;
6734 if (kvm_vcpu_is_reset_bsp(vcpu))
6735 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6736 apic_base_msr.host_initiated = true;
6737 kvm_set_apic_base(vcpu, &apic_base_msr);
6738 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006739
Avi Kivity2fb92db2011-04-27 19:42:18 +03006740 vmx_segment_cache_clear(vmx);
6741
Avi Kivity5706be02008-08-20 15:07:31 +03006742 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006743 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006744 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006745
6746 seg_setup(VCPU_SREG_DS);
6747 seg_setup(VCPU_SREG_ES);
6748 seg_setup(VCPU_SREG_FS);
6749 seg_setup(VCPU_SREG_GS);
6750 seg_setup(VCPU_SREG_SS);
6751
6752 vmcs_write16(GUEST_TR_SELECTOR, 0);
6753 vmcs_writel(GUEST_TR_BASE, 0);
6754 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6755 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6756
6757 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6758 vmcs_writel(GUEST_LDTR_BASE, 0);
6759 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6760 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6761
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006762 if (!init_event) {
6763 vmcs_write32(GUEST_SYSENTER_CS, 0);
6764 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6765 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6766 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6767 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006768
Wanpeng Lic37c2872017-11-20 14:52:21 -08006769 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006770 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006771
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006772 vmcs_writel(GUEST_GDTR_BASE, 0);
6773 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6774
6775 vmcs_writel(GUEST_IDTR_BASE, 0);
6776 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6777
Anthony Liguori443381a2010-12-06 10:53:38 -06006778 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006779 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006780 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006781 if (kvm_mpx_supported())
6782 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006783
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006784 setup_msrs(vmx);
6785
Avi Kivity6aa8b732006-12-10 02:21:36 -08006786 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6787
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006788 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006789 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006790 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006791 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006792 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006793 vmcs_write32(TPR_THRESHOLD, 0);
6794 }
6795
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006796 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006797
Sheng Yang2384d2b2008-01-17 15:14:33 +08006798 if (vmx->vpid != 0)
6799 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6800
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006801 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006802 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006803 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006804 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006805 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006806
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006807 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006808
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006809 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006810 if (init_event)
6811 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006812}
6813
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006814/*
6815 * In nested virtualization, check if L1 asked to exit on external interrupts.
6816 * For most existing hypervisors, this will always return true.
6817 */
6818static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6819{
6820 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6821 PIN_BASED_EXT_INTR_MASK;
6822}
6823
Bandan Das77b0f5d2014-04-19 18:17:45 -04006824/*
6825 * In nested virtualization, check if L1 has set
6826 * VM_EXIT_ACK_INTR_ON_EXIT
6827 */
6828static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6829{
6830 return get_vmcs12(vcpu)->vm_exit_controls &
6831 VM_EXIT_ACK_INTR_ON_EXIT;
6832}
6833
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006834static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6835{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006836 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006837}
6838
Jan Kiszkac9a79532014-03-07 20:03:15 +01006839static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006840{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006841 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6842 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006843}
6844
Jan Kiszkac9a79532014-03-07 20:03:15 +01006845static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006846{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006847 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006848 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006849 enable_irq_window(vcpu);
6850 return;
6851 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006852
Paolo Bonzini47c01522016-12-19 11:44:07 +01006853 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6854 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006855}
6856
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006857static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006858{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006859 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006860 uint32_t intr;
6861 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006862
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006863 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006864
Avi Kivityfa89a812008-09-01 15:57:51 +03006865 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006866 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006867 int inc_eip = 0;
6868 if (vcpu->arch.interrupt.soft)
6869 inc_eip = vcpu->arch.event_exit_inst_len;
6870 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006871 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006872 return;
6873 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006874 intr = irq | INTR_INFO_VALID_MASK;
6875 if (vcpu->arch.interrupt.soft) {
6876 intr |= INTR_TYPE_SOFT_INTR;
6877 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6878 vmx->vcpu.arch.event_exit_inst_len);
6879 } else
6880 intr |= INTR_TYPE_EXT_INTR;
6881 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006882
6883 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006884}
6885
Sheng Yangf08864b2008-05-15 18:23:25 +08006886static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6887{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006888 struct vcpu_vmx *vmx = to_vmx(vcpu);
6889
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006890 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006891 /*
6892 * Tracking the NMI-blocked state in software is built upon
6893 * finding the next open IRQ window. This, in turn, depends on
6894 * well-behaving guests: They have to keep IRQs disabled at
6895 * least as long as the NMI handler runs. Otherwise we may
6896 * cause NMI nesting, maybe breaking the guest. But as this is
6897 * highly unlikely, we can live with the residual risk.
6898 */
6899 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6900 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6901 }
6902
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006903 ++vcpu->stat.nmi_injections;
6904 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006905
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006906 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006907 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006908 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006909 return;
6910 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006911
Sheng Yangf08864b2008-05-15 18:23:25 +08006912 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6913 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006914
6915 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006916}
6917
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006918static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6919{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006920 struct vcpu_vmx *vmx = to_vmx(vcpu);
6921 bool masked;
6922
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006923 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006924 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006925 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006926 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006927 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6928 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6929 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006930}
6931
6932static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6933{
6934 struct vcpu_vmx *vmx = to_vmx(vcpu);
6935
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006936 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006937 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6938 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6939 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6940 }
6941 } else {
6942 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6943 if (masked)
6944 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6945 GUEST_INTR_STATE_NMI);
6946 else
6947 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6948 GUEST_INTR_STATE_NMI);
6949 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006950}
6951
Jan Kiszka2505dc92013-04-14 12:12:47 +02006952static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6953{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006954 if (to_vmx(vcpu)->nested.nested_run_pending)
6955 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006956
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006957 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006958 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6959 return 0;
6960
Jan Kiszka2505dc92013-04-14 12:12:47 +02006961 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6962 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6963 | GUEST_INTR_STATE_NMI));
6964}
6965
Gleb Natapov78646122009-03-23 12:12:11 +02006966static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6967{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006968 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6969 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006970 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6971 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006972}
6973
Izik Eiduscbc94022007-10-25 00:29:55 +02006974static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6975{
6976 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006977
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006978 if (enable_unrestricted_guest)
6979 return 0;
6980
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006981 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6982 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006983 if (ret)
6984 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006985 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006986 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006987}
6988
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006989static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6990{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006991 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006992 return 0;
6993}
6994
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006995static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006996{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006997 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006998 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01006999 /*
7000 * Update instruction length as we may reinject the exception
7001 * from user space while in guest debugging mode.
7002 */
7003 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
7004 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007005 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02007006 return false;
7007 /* fall through */
7008 case DB_VECTOR:
7009 if (vcpu->guest_debug &
7010 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
7011 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007012 /* fall through */
7013 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02007014 case OF_VECTOR:
7015 case BR_VECTOR:
7016 case UD_VECTOR:
7017 case DF_VECTOR:
7018 case SS_VECTOR:
7019 case GP_VECTOR:
7020 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02007021 return true;
7022 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02007023 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02007024 return false;
7025}
7026
7027static int handle_rmode_exception(struct kvm_vcpu *vcpu,
7028 int vec, u32 err_code)
7029{
7030 /*
7031 * Instruction with address size override prefix opcode 0x67
7032 * Cause the #SS fault with 0 error code in VM86 mode.
7033 */
7034 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007035 if (kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE) {
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02007036 if (vcpu->arch.halt_request) {
7037 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007038 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02007039 }
7040 return 1;
7041 }
7042 return 0;
7043 }
7044
7045 /*
7046 * Forward all other exceptions that are valid in real mode.
7047 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
7048 * the required debugging infrastructure rework.
7049 */
7050 kvm_queue_exception(vcpu, vec);
7051 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007052}
7053
Andi Kleena0861c02009-06-08 17:37:09 +08007054/*
7055 * Trigger machine check on the host. We assume all the MSRs are already set up
7056 * by the CPU and that we still run on the same CPU as the MCE occurred on.
7057 * We pass a fake environment to the machine check handler because we want
7058 * the guest to be always treated like user space, no matter what context
7059 * it used internally.
7060 */
7061static void kvm_machine_check(void)
7062{
7063#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
7064 struct pt_regs regs = {
7065 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
7066 .flags = X86_EFLAGS_IF,
7067 };
7068
7069 do_machine_check(&regs, 0);
7070#endif
7071}
7072
Avi Kivity851ba692009-08-24 11:10:17 +03007073static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08007074{
7075 /* already handled by vcpu_run */
7076 return 1;
7077}
7078
Avi Kivity851ba692009-08-24 11:10:17 +03007079static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007080{
Avi Kivity1155f762007-11-22 11:30:47 +02007081 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007082 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007083 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007084 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007085 u32 vect_info;
7086 enum emulation_result er;
7087
Avi Kivity1155f762007-11-22 11:30:47 +02007088 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02007089 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007090
Andi Kleena0861c02009-06-08 17:37:09 +08007091 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03007092 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08007093
Jim Mattsonef85b672016-12-12 11:01:37 -08007094 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02007095 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03007096
Wanpeng Li082d06e2018-04-03 16:28:48 -07007097 if (is_invalid_opcode(intr_info))
7098 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05007099
Avi Kivity6aa8b732006-12-10 02:21:36 -08007100 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06007101 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007102 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007103
Liran Alon9e869482018-03-12 13:12:51 +02007104 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
7105 WARN_ON_ONCE(!enable_vmware_backdoor);
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007106 er = kvm_emulate_instruction(vcpu,
Liran Alon9e869482018-03-12 13:12:51 +02007107 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
7108 if (er == EMULATE_USER_EXIT)
7109 return 0;
7110 else if (er != EMULATE_DONE)
7111 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
7112 return 1;
7113 }
7114
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007115 /*
7116 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
7117 * MMIO, it is better to report an internal error.
7118 * See the comments in vmx_handle_exit.
7119 */
7120 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
7121 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
7122 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7123 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02007124 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007125 vcpu->run->internal.data[0] = vect_info;
7126 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02007127 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007128 return 0;
7129 }
7130
Avi Kivity6aa8b732006-12-10 02:21:36 -08007131 if (is_page_fault(intr_info)) {
7132 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07007133 /* EPT won't cause page fault directly */
7134 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02007135 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007136 }
7137
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007138 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02007139
7140 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
7141 return handle_rmode_exception(vcpu, ex_no, error_code);
7142
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007143 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01007144 case AC_VECTOR:
7145 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
7146 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007147 case DB_VECTOR:
7148 dr6 = vmcs_readl(EXIT_QUALIFICATION);
7149 if (!(vcpu->guest_debug &
7150 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01007151 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007152 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07007153 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01007154 skip_emulated_instruction(vcpu);
7155
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007156 kvm_queue_exception(vcpu, DB_VECTOR);
7157 return 1;
7158 }
7159 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
7160 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
7161 /* fall through */
7162 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01007163 /*
7164 * Update instruction length as we may reinject #BP from
7165 * user space while in guest debugging mode. Reading it for
7166 * #DB as well causes no harm, it is not used in that case.
7167 */
7168 vmx->vcpu.arch.event_exit_inst_len =
7169 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007170 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03007171 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007172 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
7173 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007174 break;
7175 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007176 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
7177 kvm_run->ex.exception = ex_no;
7178 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007179 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007180 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007181 return 0;
7182}
7183
Avi Kivity851ba692009-08-24 11:10:17 +03007184static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007185{
Avi Kivity1165f5f2007-04-19 17:27:43 +03007186 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007187 return 1;
7188}
7189
Avi Kivity851ba692009-08-24 11:10:17 +03007190static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08007191{
Avi Kivity851ba692009-08-24 11:10:17 +03007192 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07007193 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08007194 return 0;
7195}
Avi Kivity6aa8b732006-12-10 02:21:36 -08007196
Avi Kivity851ba692009-08-24 11:10:17 +03007197static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007198{
He, Qingbfdaab02007-09-12 14:18:28 +08007199 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08007200 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02007201 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007202
He, Qingbfdaab02007-09-12 14:18:28 +08007203 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02007204 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03007205
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007206 ++vcpu->stat.io_exits;
7207
Sean Christopherson432baf62018-03-08 08:57:26 -08007208 if (string)
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007209 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007210
7211 port = exit_qualification >> 16;
7212 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08007213 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007214
Sean Christophersondca7f122018-03-08 08:57:27 -08007215 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007216}
7217
Ingo Molnar102d8322007-02-19 14:37:47 +02007218static void
7219vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
7220{
7221 /*
7222 * Patch in the VMCALL instruction:
7223 */
7224 hypercall[0] = 0x0f;
7225 hypercall[1] = 0x01;
7226 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02007227}
7228
Guo Chao0fa06072012-06-28 15:16:19 +08007229/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007230static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
7231{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007232 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007233 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7234 unsigned long orig_val = val;
7235
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007236 /*
7237 * We get here when L2 changed cr0 in a way that did not change
7238 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007239 * but did change L0 shadowed bits. So we first calculate the
7240 * effective cr0 value that L1 would like to write into the
7241 * hardware. It consists of the L2-owned bits from the new
7242 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007243 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007244 val = (val & ~vmcs12->cr0_guest_host_mask) |
7245 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
7246
David Matlack38991522016-11-29 18:14:08 -08007247 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007248 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007249
7250 if (kvm_set_cr0(vcpu, val))
7251 return 1;
7252 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007253 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007254 } else {
7255 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08007256 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007257 return 1;
David Matlack38991522016-11-29 18:14:08 -08007258
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007259 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007260 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007261}
7262
7263static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
7264{
7265 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007266 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7267 unsigned long orig_val = val;
7268
7269 /* analogously to handle_set_cr0 */
7270 val = (val & ~vmcs12->cr4_guest_host_mask) |
7271 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
7272 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007273 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007274 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007275 return 0;
7276 } else
7277 return kvm_set_cr4(vcpu, val);
7278}
7279
Paolo Bonzini0367f202016-07-12 10:44:55 +02007280static int handle_desc(struct kvm_vcpu *vcpu)
7281{
7282 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007283 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02007284}
7285
Avi Kivity851ba692009-08-24 11:10:17 +03007286static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007287{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007288 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007289 int cr;
7290 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03007291 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007292 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007293
He, Qingbfdaab02007-09-12 14:18:28 +08007294 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007295 cr = exit_qualification & 15;
7296 reg = (exit_qualification >> 8) & 15;
7297 switch ((exit_qualification >> 4) & 3) {
7298 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03007299 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007300 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007301 switch (cr) {
7302 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007303 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007304 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007305 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007306 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03007307 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007308 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007309 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007310 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007311 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007312 case 8: {
7313 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03007314 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01007315 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007316 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02007317 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08007318 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007319 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007320 return ret;
7321 /*
7322 * TODO: we might be squashing a
7323 * KVM_GUESTDBG_SINGLESTEP-triggered
7324 * KVM_EXIT_DEBUG here.
7325 */
Avi Kivity851ba692009-08-24 11:10:17 +03007326 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007327 return 0;
7328 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02007329 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007330 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03007331 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08007332 WARN_ONCE(1, "Guest should always own CR0.TS");
7333 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02007334 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08007335 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007336 case 1: /*mov from cr*/
7337 switch (cr) {
7338 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007339 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02007340 val = kvm_read_cr3(vcpu);
7341 kvm_register_write(vcpu, reg, val);
7342 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007343 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007344 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007345 val = kvm_get_cr8(vcpu);
7346 kvm_register_write(vcpu, reg, val);
7347 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007348 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007349 }
7350 break;
7351 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02007352 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02007353 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02007354 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007355
Kyle Huey6affcbe2016-11-29 12:40:40 -08007356 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007357 default:
7358 break;
7359 }
Avi Kivity851ba692009-08-24 11:10:17 +03007360 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03007361 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08007362 (int)(exit_qualification >> 4) & 3, cr);
7363 return 0;
7364}
7365
Avi Kivity851ba692009-08-24 11:10:17 +03007366static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007367{
He, Qingbfdaab02007-09-12 14:18:28 +08007368 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007369 int dr, dr7, reg;
7370
7371 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7372 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
7373
7374 /* First, if DR does not exist, trigger UD */
7375 if (!kvm_require_dr(vcpu, dr))
7376 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007377
Jan Kiszkaf2483412010-01-20 18:20:20 +01007378 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03007379 if (!kvm_require_cpl(vcpu, 0))
7380 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007381 dr7 = vmcs_readl(GUEST_DR7);
7382 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007383 /*
7384 * As the vm-exit takes precedence over the debug trap, we
7385 * need to emulate the latter, either for the host or the
7386 * guest debugging itself.
7387 */
7388 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007389 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007390 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007391 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007392 vcpu->run->debug.arch.exception = DB_VECTOR;
7393 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007394 return 0;
7395 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007396 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007397 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007398 kvm_queue_exception(vcpu, DB_VECTOR);
7399 return 1;
7400 }
7401 }
7402
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007403 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007404 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7405 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007406
7407 /*
7408 * No more DR vmexits; force a reload of the debug registers
7409 * and reenter on this instruction. The next vmexit will
7410 * retrieve the full state of the debug registers.
7411 */
7412 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7413 return 1;
7414 }
7415
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007416 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7417 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007418 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007419
7420 if (kvm_get_dr(vcpu, dr, &val))
7421 return 1;
7422 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007423 } else
Nadav Amit57773922014-06-18 17:19:23 +03007424 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007425 return 1;
7426
Kyle Huey6affcbe2016-11-29 12:40:40 -08007427 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007428}
7429
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007430static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7431{
7432 return vcpu->arch.dr6;
7433}
7434
7435static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7436{
7437}
7438
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007439static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7440{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007441 get_debugreg(vcpu->arch.db[0], 0);
7442 get_debugreg(vcpu->arch.db[1], 1);
7443 get_debugreg(vcpu->arch.db[2], 2);
7444 get_debugreg(vcpu->arch.db[3], 3);
7445 get_debugreg(vcpu->arch.dr6, 6);
7446 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7447
7448 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007449 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007450}
7451
Gleb Natapov020df072010-04-13 10:05:23 +03007452static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7453{
7454 vmcs_writel(GUEST_DR7, val);
7455}
7456
Avi Kivity851ba692009-08-24 11:10:17 +03007457static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007458{
Kyle Huey6a908b62016-11-29 12:40:37 -08007459 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007460}
7461
Avi Kivity851ba692009-08-24 11:10:17 +03007462static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007463{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007464 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007465 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007466
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007467 msr_info.index = ecx;
7468 msr_info.host_initiated = false;
7469 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007470 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007471 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007472 return 1;
7473 }
7474
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007475 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007476
Avi Kivity6aa8b732006-12-10 02:21:36 -08007477 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007478 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7479 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007480 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007481}
7482
Avi Kivity851ba692009-08-24 11:10:17 +03007483static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007484{
Will Auld8fe8ab42012-11-29 12:42:12 -08007485 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007486 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7487 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7488 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007489
Will Auld8fe8ab42012-11-29 12:42:12 -08007490 msr.data = data;
7491 msr.index = ecx;
7492 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007493 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007494 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007495 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007496 return 1;
7497 }
7498
Avi Kivity59200272010-01-25 19:47:02 +02007499 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007500 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007501}
7502
Avi Kivity851ba692009-08-24 11:10:17 +03007503static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007504{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007505 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007506 return 1;
7507}
7508
Avi Kivity851ba692009-08-24 11:10:17 +03007509static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007510{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007511 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7512 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007513
Avi Kivity3842d132010-07-27 12:30:24 +03007514 kvm_make_request(KVM_REQ_EVENT, vcpu);
7515
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007516 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007517 return 1;
7518}
7519
Avi Kivity851ba692009-08-24 11:10:17 +03007520static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007521{
Avi Kivityd3bef152007-06-05 15:53:05 +03007522 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007523}
7524
Avi Kivity851ba692009-08-24 11:10:17 +03007525static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007526{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007527 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007528}
7529
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007530static int handle_invd(struct kvm_vcpu *vcpu)
7531{
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007532 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007533}
7534
Avi Kivity851ba692009-08-24 11:10:17 +03007535static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007536{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007537 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007538
7539 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007540 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007541}
7542
Avi Kivityfee84b02011-11-10 14:57:25 +02007543static int handle_rdpmc(struct kvm_vcpu *vcpu)
7544{
7545 int err;
7546
7547 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007548 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007549}
7550
Avi Kivity851ba692009-08-24 11:10:17 +03007551static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007552{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007553 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007554}
7555
Dexuan Cui2acf9232010-06-10 11:27:12 +08007556static int handle_xsetbv(struct kvm_vcpu *vcpu)
7557{
7558 u64 new_bv = kvm_read_edx_eax(vcpu);
7559 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7560
7561 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007562 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007563 return 1;
7564}
7565
Wanpeng Lif53cd632014-12-02 19:14:58 +08007566static int handle_xsaves(struct kvm_vcpu *vcpu)
7567{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007568 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007569 WARN(1, "this should never happen\n");
7570 return 1;
7571}
7572
7573static int handle_xrstors(struct kvm_vcpu *vcpu)
7574{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007575 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007576 WARN(1, "this should never happen\n");
7577 return 1;
7578}
7579
Avi Kivity851ba692009-08-24 11:10:17 +03007580static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007581{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007582 if (likely(fasteoi)) {
7583 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7584 int access_type, offset;
7585
7586 access_type = exit_qualification & APIC_ACCESS_TYPE;
7587 offset = exit_qualification & APIC_ACCESS_OFFSET;
7588 /*
7589 * Sane guest uses MOV to write EOI, with written value
7590 * not cared. So make a short-circuit here by avoiding
7591 * heavy instruction emulation.
7592 */
7593 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7594 (offset == APIC_EOI)) {
7595 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007596 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007597 }
7598 }
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007599 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007600}
7601
Yang Zhangc7c9c562013-01-25 10:18:51 +08007602static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7603{
7604 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7605 int vector = exit_qualification & 0xff;
7606
7607 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7608 kvm_apic_set_eoi_accelerated(vcpu, vector);
7609 return 1;
7610}
7611
Yang Zhang83d4c282013-01-25 10:18:49 +08007612static int handle_apic_write(struct kvm_vcpu *vcpu)
7613{
7614 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7615 u32 offset = exit_qualification & 0xfff;
7616
7617 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7618 kvm_apic_write_nodecode(vcpu, offset);
7619 return 1;
7620}
7621
Avi Kivity851ba692009-08-24 11:10:17 +03007622static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007623{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007624 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007625 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007626 bool has_error_code = false;
7627 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007628 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007629 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007630
7631 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007632 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007633 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007634
7635 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7636
7637 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007638 if (reason == TASK_SWITCH_GATE && idt_v) {
7639 switch (type) {
7640 case INTR_TYPE_NMI_INTR:
7641 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007642 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007643 break;
7644 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007645 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007646 kvm_clear_interrupt_queue(vcpu);
7647 break;
7648 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007649 if (vmx->idt_vectoring_info &
7650 VECTORING_INFO_DELIVER_CODE_MASK) {
7651 has_error_code = true;
7652 error_code =
7653 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7654 }
7655 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007656 case INTR_TYPE_SOFT_EXCEPTION:
7657 kvm_clear_exception_queue(vcpu);
7658 break;
7659 default:
7660 break;
7661 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007662 }
Izik Eidus37817f22008-03-24 23:14:53 +02007663 tss_selector = exit_qualification;
7664
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007665 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7666 type != INTR_TYPE_EXT_INTR &&
7667 type != INTR_TYPE_NMI_INTR))
7668 skip_emulated_instruction(vcpu);
7669
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007670 if (kvm_task_switch(vcpu, tss_selector,
7671 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7672 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007673 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7674 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7675 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007676 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007677 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007678
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007679 /*
7680 * TODO: What about debug traps on tss switch?
7681 * Are we supposed to inject them and update dr6?
7682 */
7683
7684 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007685}
7686
Avi Kivity851ba692009-08-24 11:10:17 +03007687static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007688{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007689 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007690 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007691 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007692
Sheng Yangf9c617f2009-03-25 10:08:52 +08007693 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007694
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007695 /*
7696 * EPT violation happened while executing iret from NMI,
7697 * "blocked by NMI" bit has to be set before next VM entry.
7698 * There are errata that may cause this bit to not be set:
7699 * AAK134, BY25.
7700 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007701 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007702 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007703 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007704 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7705
Sheng Yang14394422008-04-28 12:24:45 +08007706 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007707 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007708
Junaid Shahid27959a42016-12-06 16:46:10 -08007709 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007710 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007711 ? PFERR_USER_MASK : 0;
7712 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007713 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007714 ? PFERR_WRITE_MASK : 0;
7715 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007716 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007717 ? PFERR_FETCH_MASK : 0;
7718 /* ept page table entry is present? */
7719 error_code |= (exit_qualification &
7720 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7721 EPT_VIOLATION_EXECUTABLE))
7722 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007723
Paolo Bonzinieebed242016-11-28 14:39:58 +01007724 error_code |= (exit_qualification & 0x100) != 0 ?
7725 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007726
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007727 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007728 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007729}
7730
Avi Kivity851ba692009-08-24 11:10:17 +03007731static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007732{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007733 gpa_t gpa;
7734
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007735 /*
7736 * A nested guest cannot optimize MMIO vmexits, because we have an
7737 * nGPA here instead of the required GPA.
7738 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007739 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007740 if (!is_guest_mode(vcpu) &&
7741 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007742 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007743 /*
7744 * Doing kvm_skip_emulated_instruction() depends on undefined
7745 * behavior: Intel's manual doesn't mandate
7746 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7747 * occurs and while on real hardware it was observed to be set,
7748 * other hypervisors (namely Hyper-V) don't set it, we end up
7749 * advancing IP with some random value. Disable fast mmio when
7750 * running nested and keep it for real hardware in hope that
7751 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7752 */
7753 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7754 return kvm_skip_emulated_instruction(vcpu);
7755 else
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007756 return kvm_emulate_instruction(vcpu, EMULTYPE_SKIP) ==
Sean Christophersonc4409902018-08-23 13:56:46 -07007757 EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007758 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007759
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007760 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007761}
7762
Avi Kivity851ba692009-08-24 11:10:17 +03007763static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007764{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007765 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007766 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7767 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007768 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007769 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007770
7771 return 1;
7772}
7773
Mohammed Gamal80ced182009-09-01 12:48:18 +02007774static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007775{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007776 struct vcpu_vmx *vmx = to_vmx(vcpu);
7777 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007778 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007779 u32 cpu_exec_ctrl;
7780 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007781 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007782
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007783 /*
7784 * We should never reach the point where we are emulating L2
7785 * due to invalid guest state as that means we incorrectly
7786 * allowed a nested VMEntry with an invalid vmcs12.
7787 */
7788 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7789
Avi Kivity49e9d552010-09-19 14:34:08 +02007790 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7791 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007792
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007793 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007794 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007795 return handle_interrupt_window(&vmx->vcpu);
7796
Radim Krčmář72875d82017-04-26 22:32:19 +02007797 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007798 return 1;
7799
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007800 err = kvm_emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007801
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007802 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007803 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007804 ret = 0;
7805 goto out;
7806 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007807
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007808 if (err != EMULATE_DONE)
7809 goto emulation_error;
7810
7811 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7812 vcpu->arch.exception.pending)
7813 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007814
Gleb Natapov8d76c492013-05-08 18:38:44 +03007815 if (vcpu->arch.halt_request) {
7816 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007817 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007818 goto out;
7819 }
7820
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007821 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007822 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007823 if (need_resched())
7824 schedule();
7825 }
7826
Mohammed Gamal80ced182009-09-01 12:48:18 +02007827out:
7828 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007829
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007830emulation_error:
7831 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7832 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7833 vcpu->run->internal.ndata = 0;
7834 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007835}
7836
7837static void grow_ple_window(struct kvm_vcpu *vcpu)
7838{
7839 struct vcpu_vmx *vmx = to_vmx(vcpu);
7840 int old = vmx->ple_window;
7841
Babu Mogerc8e88712018-03-16 16:37:24 -04007842 vmx->ple_window = __grow_ple_window(old, ple_window,
7843 ple_window_grow,
7844 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007845
7846 if (vmx->ple_window != old)
7847 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007848
7849 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007850}
7851
7852static void shrink_ple_window(struct kvm_vcpu *vcpu)
7853{
7854 struct vcpu_vmx *vmx = to_vmx(vcpu);
7855 int old = vmx->ple_window;
7856
Babu Mogerc8e88712018-03-16 16:37:24 -04007857 vmx->ple_window = __shrink_ple_window(old, ple_window,
7858 ple_window_shrink,
7859 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007860
7861 if (vmx->ple_window != old)
7862 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007863
7864 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007865}
7866
7867/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007868 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7869 */
7870static void wakeup_handler(void)
7871{
7872 struct kvm_vcpu *vcpu;
7873 int cpu = smp_processor_id();
7874
7875 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7876 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7877 blocked_vcpu_list) {
7878 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7879
7880 if (pi_test_on(pi_desc) == 1)
7881 kvm_vcpu_kick(vcpu);
7882 }
7883 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7884}
7885
Peng Haoe01bca22018-04-07 05:47:32 +08007886static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007887{
7888 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7889 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7890 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7891 0ull, VMX_EPT_EXECUTABLE_MASK,
7892 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007893 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007894
7895 ept_set_mmio_spte_mask();
7896 kvm_enable_tdp();
7897}
7898
Tiejun Chenf2c76482014-10-28 10:14:47 +08007899static __init int hardware_setup(void)
7900{
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007901 unsigned long host_bndcfgs;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007902 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007903
7904 rdmsrl_safe(MSR_EFER, &host_efer);
7905
7906 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7907 kvm_define_shared_msr(i, vmx_msr_index[i]);
7908
Sean Christophersondfae3c02018-12-03 13:52:52 -08007909 if (setup_vmcs_config(&vmcs_config) < 0)
7910 return -EIO;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007911
7912 if (boot_cpu_has(X86_FEATURE_NX))
7913 kvm_enable_efer_bits(EFER_NX);
7914
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007915 if (boot_cpu_has(X86_FEATURE_MPX)) {
7916 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7917 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7918 }
7919
Wanpeng Li08d839c2017-03-23 05:30:08 -07007920 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7921 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007922 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007923
Tiejun Chenf2c76482014-10-28 10:14:47 +08007924 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007925 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007926 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007927 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007928 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007929
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007930 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007931 enable_ept_ad_bits = 0;
7932
Wanpeng Li8ad81822017-10-09 15:51:53 -07007933 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007934 enable_unrestricted_guest = 0;
7935
Paolo Bonziniad15a292015-01-30 16:18:49 +01007936 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007937 flexpriority_enabled = 0;
7938
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007939 if (!cpu_has_virtual_nmis())
7940 enable_vnmi = 0;
7941
Paolo Bonziniad15a292015-01-30 16:18:49 +01007942 /*
7943 * set_apic_access_page_addr() is used to reload apic access
7944 * page upon invalidation. No need to do anything if not
7945 * using the APIC_ACCESS_ADDR VMCS field.
7946 */
7947 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007948 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007949
7950 if (!cpu_has_vmx_tpr_shadow())
7951 kvm_x86_ops->update_cr8_intercept = NULL;
7952
7953 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7954 kvm_disable_largepages();
7955
Tianyu Lan877ad952018-07-19 08:40:23 +00007956#if IS_ENABLED(CONFIG_HYPERV)
7957 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
7958 && enable_ept)
7959 kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb;
7960#endif
7961
Wanpeng Li0f107682017-09-28 18:06:24 -07007962 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007963 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007964 ple_window = 0;
7965 ple_window_grow = 0;
7966 ple_window_max = 0;
7967 ple_window_shrink = 0;
7968 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007969
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007970 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007971 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007972 kvm_x86_ops->sync_pir_to_irr = NULL;
7973 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007974
Haozhong Zhang64903d62015-10-20 15:39:09 +08007975 if (cpu_has_vmx_tsc_scaling()) {
7976 kvm_has_tsc_control = true;
7977 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7978 kvm_tsc_scaling_ratio_frac_bits = 48;
7979 }
7980
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007981 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7982
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007983 if (enable_ept)
7984 vmx_enable_tdp();
7985 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007986 kvm_disable_tdp();
7987
Jim Mattson8fcc4b52018-07-10 11:27:20 +02007988 if (!nested) {
7989 kvm_x86_ops->get_nested_state = NULL;
7990 kvm_x86_ops->set_nested_state = NULL;
7991 }
7992
Kai Huang843e4332015-01-28 10:54:28 +08007993 /*
7994 * Only enable PML when hardware supports PML feature, and both EPT
7995 * and EPT A/D bit features are enabled -- PML depends on them to work.
7996 */
7997 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7998 enable_pml = 0;
7999
8000 if (!enable_pml) {
8001 kvm_x86_ops->slot_enable_log_dirty = NULL;
8002 kvm_x86_ops->slot_disable_log_dirty = NULL;
8003 kvm_x86_ops->flush_log_dirty = NULL;
8004 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
8005 }
8006
Sean Christophersond264ee02018-08-27 15:21:12 -07008007 if (!cpu_has_vmx_preemption_timer())
8008 kvm_x86_ops->request_immediate_exit = __kvm_request_immediate_exit;
8009
Yunhong Jiang64672c92016-06-13 14:19:59 -07008010 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
8011 u64 vmx_msr;
8012
8013 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
8014 cpu_preemption_timer_multi =
8015 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
8016 } else {
8017 kvm_x86_ops->set_hv_timer = NULL;
8018 kvm_x86_ops->cancel_hv_timer = NULL;
8019 }
8020
Sean Christophersondfae3c02018-12-03 13:52:52 -08008021 if (!cpu_has_vmx_shadow_vmcs() || !nested)
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01008022 enable_shadow_vmcs = 0;
Sean Christophersondfae3c02018-12-03 13:52:52 -08008023 if (enable_shadow_vmcs) {
8024 for (i = 0; i < VMX_BITMAP_NR; i++) {
8025 vmx_bitmap[i] = (unsigned long *)
8026 __get_free_page(GFP_KERNEL);
8027 if (!vmx_bitmap[i])
8028 goto out;
8029 }
8030
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01008031 init_vmcs_shadow_fields();
Sean Christophersondfae3c02018-12-03 13:52:52 -08008032 }
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01008033
Feng Wubf9f6ac2015-09-18 22:29:55 +08008034 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01008035 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008036
Ashok Rajc45dcc72016-06-22 14:59:56 +08008037 kvm_mce_cap_supported |= MCG_LMCE_P;
8038
Sean Christopherson1b3ab5a2018-12-03 13:52:51 -08008039 r = alloc_kvm_area();
8040 if (r)
8041 goto out;
8042 return 0;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008043
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008044out:
Sean Christophersondfae3c02018-12-03 13:52:52 -08008045 if (enable_shadow_vmcs) {
8046 for (i = 0; i < VMX_BITMAP_NR; i++)
8047 free_page((unsigned long)vmx_bitmap[i]);
8048 }
Sean Christopherson1b3ab5a2018-12-03 13:52:51 -08008049 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08008050}
8051
8052static __exit void hardware_unsetup(void)
8053{
Radim Krčmář23611332016-09-29 22:41:33 +02008054 int i;
8055
Sean Christophersondfae3c02018-12-03 13:52:52 -08008056 if (enable_shadow_vmcs) {
8057 for (i = 0; i < VMX_BITMAP_NR; i++)
8058 free_page((unsigned long)vmx_bitmap[i]);
8059 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008060
Tiejun Chenf2c76482014-10-28 10:14:47 +08008061 free_kvm_area();
8062}
8063
Avi Kivity6aa8b732006-12-10 02:21:36 -08008064/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008065 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
8066 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
8067 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03008068static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008069{
Wanpeng Lib31c1142018-03-12 04:53:04 -07008070 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02008071 grow_ple_window(vcpu);
8072
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08008073 /*
8074 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
8075 * VM-execution control is ignored if CPL > 0. OTOH, KVM
8076 * never set PAUSE_EXITING and just set PLE if supported,
8077 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
8078 */
8079 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008080 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008081}
8082
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008083static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08008084{
Kyle Huey6affcbe2016-11-29 12:40:40 -08008085 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08008086}
8087
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008088static int handle_mwait(struct kvm_vcpu *vcpu)
8089{
8090 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
8091 return handle_nop(vcpu);
8092}
8093
Jim Mattson45ec3682017-08-23 16:32:04 -07008094static int handle_invalid_op(struct kvm_vcpu *vcpu)
8095{
8096 kvm_queue_exception(vcpu, UD_VECTOR);
8097 return 1;
8098}
8099
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008100static int handle_monitor_trap(struct kvm_vcpu *vcpu)
8101{
8102 return 1;
8103}
8104
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008105static int handle_monitor(struct kvm_vcpu *vcpu)
8106{
8107 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
8108 return handle_nop(vcpu);
8109}
8110
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008111/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008112 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008113 * set the success or error code of an emulated VMX instruction (as specified
8114 * by Vol 2B, VMX Instruction Reference, "Conventions"), and skip the emulated
8115 * instruction.
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008116 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008117static int nested_vmx_succeed(struct kvm_vcpu *vcpu)
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008118{
8119 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
8120 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8121 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008122 return kvm_skip_emulated_instruction(vcpu);
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008123}
8124
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008125static int nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008126{
8127 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8128 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
8129 X86_EFLAGS_SF | X86_EFLAGS_OF))
8130 | X86_EFLAGS_CF);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008131 return kvm_skip_emulated_instruction(vcpu);
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008132}
8133
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008134static int nested_vmx_failValid(struct kvm_vcpu *vcpu,
8135 u32 vm_instruction_error)
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008136{
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02008137 struct vcpu_vmx *vmx = to_vmx(vcpu);
8138
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008139 /*
8140 * failValid writes the error number to the current VMCS, which
8141 * can't be done if there isn't a current VMCS.
8142 */
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02008143 if (vmx->nested.current_vmptr == -1ull && !vmx->nested.hv_evmcs)
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008144 return nested_vmx_failInvalid(vcpu);
8145
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008146 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8147 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8148 X86_EFLAGS_SF | X86_EFLAGS_OF))
8149 | X86_EFLAGS_ZF);
8150 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
8151 /*
8152 * We don't need to force a shadow sync because
8153 * VM_INSTRUCTION_ERROR is not shadowed
8154 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008155 return kvm_skip_emulated_instruction(vcpu);
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008156}
Abel Gordon145c28d2013-04-18 14:36:55 +03008157
Wincy Vanff651cb2014-12-11 08:52:58 +03008158static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
8159{
8160 /* TODO: not to reset guest simply here. */
8161 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02008162 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03008163}
8164
Jan Kiszkaf41245002014-03-07 20:03:13 +01008165static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
8166{
8167 struct vcpu_vmx *vmx =
8168 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
8169
8170 vmx->nested.preemption_timer_expired = true;
8171 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
8172 kvm_vcpu_kick(&vmx->vcpu);
8173
8174 return HRTIMER_NORESTART;
8175}
8176
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008177/*
Bandan Das19677e32014-05-06 02:19:15 -04008178 * Decode the memory-address operand of a vmx instruction, as recorded on an
8179 * exit caused by such an instruction (run by a guest hypervisor).
8180 * On success, returns 0. When the operand is invalid, returns 1 and throws
8181 * #UD or #GP.
8182 */
8183static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
8184 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008185 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04008186{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008187 gva_t off;
8188 bool exn;
8189 struct kvm_segment s;
8190
Bandan Das19677e32014-05-06 02:19:15 -04008191 /*
8192 * According to Vol. 3B, "Information for VM Exits Due to Instruction
8193 * Execution", on an exit, vmx_instruction_info holds most of the
8194 * addressing components of the operand. Only the displacement part
8195 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
8196 * For how an actual address is calculated from all these components,
8197 * refer to Vol. 1, "Operand Addressing".
8198 */
8199 int scaling = vmx_instruction_info & 3;
8200 int addr_size = (vmx_instruction_info >> 7) & 7;
8201 bool is_reg = vmx_instruction_info & (1u << 10);
8202 int seg_reg = (vmx_instruction_info >> 15) & 7;
8203 int index_reg = (vmx_instruction_info >> 18) & 0xf;
8204 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
8205 int base_reg = (vmx_instruction_info >> 23) & 0xf;
8206 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
8207
8208 if (is_reg) {
8209 kvm_queue_exception(vcpu, UD_VECTOR);
8210 return 1;
8211 }
8212
8213 /* Addr = segment_base + offset */
8214 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008215 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04008216 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008217 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04008218 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008219 off += kvm_register_read(vcpu, index_reg)<<scaling;
8220 vmx_get_segment(vcpu, &s, seg_reg);
8221 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04008222
8223 if (addr_size == 1) /* 32 bit */
8224 *ret &= 0xffffffff;
8225
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008226 /* Checks for #GP/#SS exceptions. */
8227 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008228 if (is_long_mode(vcpu)) {
8229 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
8230 * non-canonical form. This is the only check on the memory
8231 * destination for long mode!
8232 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08008233 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008234 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008235 /* Protected mode: apply checks for segment validity in the
8236 * following order:
8237 * - segment type check (#GP(0) may be thrown)
8238 * - usability check (#GP(0)/#SS(0))
8239 * - limit check (#GP(0)/#SS(0))
8240 */
8241 if (wr)
8242 /* #GP(0) if the destination operand is located in a
8243 * read-only data segment or any code segment.
8244 */
8245 exn = ((s.type & 0xa) == 0 || (s.type & 8));
8246 else
8247 /* #GP(0) if the source operand is located in an
8248 * execute-only code segment
8249 */
8250 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008251 if (exn) {
8252 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
8253 return 1;
8254 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008255 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
8256 */
8257 exn = (s.unusable != 0);
8258 /* Protected mode: #GP(0)/#SS(0) if the memory
8259 * operand is outside the segment limit.
8260 */
8261 exn = exn || (off + sizeof(u64) > s.limit);
8262 }
8263 if (exn) {
8264 kvm_queue_exception_e(vcpu,
8265 seg_reg == VCPU_SREG_SS ?
8266 SS_VECTOR : GP_VECTOR,
8267 0);
8268 return 1;
8269 }
8270
Bandan Das19677e32014-05-06 02:19:15 -04008271 return 0;
8272}
8273
Radim Krčmářcbf71272017-05-19 15:48:51 +02008274static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04008275{
8276 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04008277 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04008278
8279 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008280 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04008281 return 1;
8282
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008283 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04008284 kvm_inject_page_fault(vcpu, &e);
8285 return 1;
8286 }
8287
Bandan Das3573e222014-05-06 02:19:16 -04008288 return 0;
8289}
8290
Liran Alonabfc52c2018-06-23 02:35:13 +03008291/*
8292 * Allocate a shadow VMCS and associate it with the currently loaded
8293 * VMCS, unless such a shadow VMCS already exists. The newly allocated
8294 * VMCS is also VMCLEARed, so that it is ready for use.
8295 */
8296static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
8297{
8298 struct vcpu_vmx *vmx = to_vmx(vcpu);
8299 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
8300
8301 /*
8302 * We should allocate a shadow vmcs for vmcs01 only when L1
8303 * executes VMXON and free it when L1 executes VMXOFF.
8304 * As it is invalid to execute VMXON twice, we shouldn't reach
8305 * here when vmcs01 already have an allocated shadow vmcs.
8306 */
8307 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
8308
8309 if (!loaded_vmcs->shadow_vmcs) {
8310 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
8311 if (loaded_vmcs->shadow_vmcs)
8312 vmcs_clear(loaded_vmcs->shadow_vmcs);
8313 }
8314 return loaded_vmcs->shadow_vmcs;
8315}
8316
Jim Mattsone29acc52016-11-30 12:03:43 -08008317static int enter_vmx_operation(struct kvm_vcpu *vcpu)
8318{
8319 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008320 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08008321
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008322 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
8323 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06008324 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08008325
8326 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8327 if (!vmx->nested.cached_vmcs12)
8328 goto out_cached_vmcs12;
8329
Liran Alon61ada742018-06-23 02:35:08 +03008330 vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8331 if (!vmx->nested.cached_shadow_vmcs12)
8332 goto out_cached_shadow_vmcs12;
8333
Liran Alonabfc52c2018-06-23 02:35:13 +03008334 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
8335 goto out_shadow_vmcs;
Jim Mattsone29acc52016-11-30 12:03:43 -08008336
Jim Mattsone29acc52016-11-30 12:03:43 -08008337 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
8338 HRTIMER_MODE_REL_PINNED);
8339 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
8340
Roman Kagan63aff652018-07-19 21:59:07 +03008341 vmx->nested.vpid02 = allocate_vpid();
8342
Sean Christopherson9d6105b2018-09-26 09:23:51 -07008343 vmx->nested.vmcs02_initialized = false;
Jim Mattsone29acc52016-11-30 12:03:43 -08008344 vmx->nested.vmxon = true;
8345 return 0;
8346
8347out_shadow_vmcs:
Liran Alon61ada742018-06-23 02:35:08 +03008348 kfree(vmx->nested.cached_shadow_vmcs12);
8349
8350out_cached_shadow_vmcs12:
Jim Mattsone29acc52016-11-30 12:03:43 -08008351 kfree(vmx->nested.cached_vmcs12);
8352
8353out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06008354 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08008355
Jim Mattsonde3a0022017-11-27 17:22:25 -06008356out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08008357 return -ENOMEM;
8358}
8359
Bandan Das3573e222014-05-06 02:19:16 -04008360/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008361 * Emulate the VMXON instruction.
8362 * Currently, we just remember that VMX is active, and do not save or even
8363 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
8364 * do not currently need to store anything in that guest-allocated memory
8365 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
8366 * argument is different from the VMXON pointer (which the spec says they do).
8367 */
8368static int handle_vmon(struct kvm_vcpu *vcpu)
8369{
Jim Mattsone29acc52016-11-30 12:03:43 -08008370 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008371 gpa_t vmptr;
8372 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008373 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008374 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
8375 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008376
Jim Mattson70f3aac2017-04-26 08:53:46 -07008377 /*
8378 * The Intel VMX Instruction Reference lists a bunch of bits that are
8379 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
8380 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
8381 * Otherwise, we should fail with #UD. But most faulting conditions
8382 * have already been checked by hardware, prior to the VM-exit for
8383 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
8384 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008385 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07008386 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008387 kvm_queue_exception(vcpu, UD_VECTOR);
8388 return 1;
8389 }
8390
Felix Wilhelm727ba742018-06-11 09:43:44 +02008391 /* CPL=0 must be checked manually. */
8392 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008393 kvm_inject_gp(vcpu, 0);
Felix Wilhelm727ba742018-06-11 09:43:44 +02008394 return 1;
8395 }
8396
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008397 if (vmx->nested.vmxon)
8398 return nested_vmx_failValid(vcpu,
8399 VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008400
Haozhong Zhang3b840802016-06-22 14:59:54 +08008401 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008402 != VMXON_NEEDED_FEATURES) {
8403 kvm_inject_gp(vcpu, 0);
8404 return 1;
8405 }
8406
Radim Krčmářcbf71272017-05-19 15:48:51 +02008407 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08008408 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008409
8410 /*
8411 * SDM 3: 24.11.5
8412 * The first 4 bytes of VMXON region contain the supported
8413 * VMCS revision identifier
8414 *
8415 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8416 * which replaces physical address width with 32
8417 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008418 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu)))
8419 return nested_vmx_failInvalid(vcpu);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008420
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008421 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008422 if (is_error_page(page))
8423 return nested_vmx_failInvalid(vcpu);
8424
Radim Krčmářcbf71272017-05-19 15:48:51 +02008425 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8426 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008427 kvm_release_page_clean(page);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008428 return nested_vmx_failInvalid(vcpu);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008429 }
8430 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008431 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008432
8433 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08008434 ret = enter_vmx_operation(vcpu);
8435 if (ret)
8436 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008437
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008438 return nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008439}
8440
8441/*
8442 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8443 * for running VMX instructions (except VMXON, whose prerequisites are
8444 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008445 * Note that many of these exceptions have priority over VM exits, so they
8446 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008447 */
8448static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8449{
Jim Mattson70f3aac2017-04-26 08:53:46 -07008450 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008451 kvm_queue_exception(vcpu, UD_VECTOR);
8452 return 0;
8453 }
Jim Mattsone49fcb82018-07-27 13:44:45 -07008454
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008455 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008456 kvm_inject_gp(vcpu, 0);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008457 return 0;
8458 }
8459
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008460 return 1;
8461}
8462
David Matlack8ca44e82017-08-01 14:00:39 -07008463static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8464{
8465 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8466 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8467}
8468
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02008469static inline void nested_release_evmcs(struct kvm_vcpu *vcpu)
Abel Gordone7953d72013-04-18 14:37:55 +03008470{
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02008471 struct vcpu_vmx *vmx = to_vmx(vcpu);
8472
8473 if (!vmx->nested.hv_evmcs)
8474 return;
8475
8476 kunmap(vmx->nested.hv_evmcs_page);
8477 kvm_release_page_dirty(vmx->nested.hv_evmcs_page);
8478 vmx->nested.hv_evmcs_vmptr = -1ull;
8479 vmx->nested.hv_evmcs_page = NULL;
8480 vmx->nested.hv_evmcs = NULL;
8481}
8482
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008483static inline void nested_release_vmcs12(struct kvm_vcpu *vcpu)
Abel Gordone7953d72013-04-18 14:37:55 +03008484{
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008485 struct vcpu_vmx *vmx = to_vmx(vcpu);
8486
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008487 if (vmx->nested.current_vmptr == -1ull)
8488 return;
8489
Abel Gordon012f83c2013-04-18 14:39:25 +03008490 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008491 /* copy to memory all shadowed fields in case
8492 they were modified */
8493 copy_shadow_to_vmcs12(vmx);
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +02008494 vmx->nested.need_vmcs12_sync = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008495 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008496 }
Wincy Van705699a2015-02-03 23:58:17 +08008497 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008498
8499 /* Flush VMCS12 to guest memory */
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008500 kvm_vcpu_write_guest_page(vcpu,
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008501 vmx->nested.current_vmptr >> PAGE_SHIFT,
8502 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008503
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008504 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
8505
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008506 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008507}
8508
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008509/*
8510 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8511 * just stops using VMX.
8512 */
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008513static void free_nested(struct kvm_vcpu *vcpu)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008514{
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008515 struct vcpu_vmx *vmx = to_vmx(vcpu);
8516
Wanpeng Lib7455822017-11-22 14:04:00 -08008517 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008518 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008519
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008520 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008521 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008522 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008523 vmx->nested.posted_intr_nv = -1;
8524 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008525 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008526 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008527 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8528 free_vmcs(vmx->vmcs01.shadow_vmcs);
8529 vmx->vmcs01.shadow_vmcs = NULL;
8530 }
David Matlack4f2777b2016-07-13 17:16:37 -07008531 kfree(vmx->nested.cached_vmcs12);
Liran Alon61ada742018-06-23 02:35:08 +03008532 kfree(vmx->nested.cached_shadow_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008533 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008534 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008535 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008536 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008537 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008538 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008539 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008540 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008541 }
Wincy Van705699a2015-02-03 23:58:17 +08008542 if (vmx->nested.pi_desc_page) {
8543 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008544 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008545 vmx->nested.pi_desc_page = NULL;
8546 vmx->nested.pi_desc = NULL;
8547 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008548
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008549 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
8550
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02008551 nested_release_evmcs(vcpu);
8552
Jim Mattsonde3a0022017-11-27 17:22:25 -06008553 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008554}
8555
8556/* Emulate the VMXOFF instruction */
8557static int handle_vmoff(struct kvm_vcpu *vcpu)
8558{
8559 if (!nested_vmx_check_permission(vcpu))
8560 return 1;
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008561 free_nested(vcpu);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008562 return nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008563}
8564
Nadav Har'El27d6c862011-05-25 23:06:59 +03008565/* Emulate the VMCLEAR instruction */
8566static int handle_vmclear(struct kvm_vcpu *vcpu)
8567{
8568 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008569 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008570 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008571
8572 if (!nested_vmx_check_permission(vcpu))
8573 return 1;
8574
Radim Krčmářcbf71272017-05-19 15:48:51 +02008575 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008576 return 1;
8577
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008578 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu)))
8579 return nested_vmx_failValid(vcpu,
8580 VMXERR_VMCLEAR_INVALID_ADDRESS);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008581
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008582 if (vmptr == vmx->nested.vmxon_ptr)
8583 return nested_vmx_failValid(vcpu,
8584 VMXERR_VMCLEAR_VMXON_POINTER);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008585
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02008586 if (vmx->nested.hv_evmcs_page) {
8587 if (vmptr == vmx->nested.hv_evmcs_vmptr)
8588 nested_release_evmcs(vcpu);
8589 } else {
8590 if (vmptr == vmx->nested.current_vmptr)
8591 nested_release_vmcs12(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008592
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02008593 kvm_vcpu_write_guest(vcpu,
8594 vmptr + offsetof(struct vmcs12,
8595 launch_state),
8596 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008597 }
8598
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008599 return nested_vmx_succeed(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008600}
8601
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008602static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8603
8604/* Emulate the VMLAUNCH instruction */
8605static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8606{
8607 return nested_vmx_run(vcpu, true);
8608}
8609
8610/* Emulate the VMRESUME instruction */
8611static int handle_vmresume(struct kvm_vcpu *vcpu)
8612{
8613
8614 return nested_vmx_run(vcpu, false);
8615}
8616
Nadav Har'El49f705c2011-05-25 23:08:30 +03008617/*
8618 * Read a vmcs12 field. Since these can have varying lengths and we return
8619 * one type, we chose the biggest type (u64) and zero-extend the return value
8620 * to that size. Note that the caller, handle_vmread, might need to use only
8621 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8622 * 64-bit fields are to be returned).
8623 */
Liran Alone2536742018-06-23 02:35:02 +03008624static inline int vmcs12_read_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008625 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008626{
8627 short offset = vmcs_field_to_offset(field);
8628 char *p;
8629
8630 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008631 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008632
Liran Alone2536742018-06-23 02:35:02 +03008633 p = (char *)vmcs12 + offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008634
Jim Mattsond37f4262017-12-22 12:12:16 -08008635 switch (vmcs_field_width(field)) {
8636 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008637 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008638 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008639 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008640 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008641 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008642 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008643 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008644 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008645 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008646 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008647 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008648 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008649 WARN_ON(1);
8650 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008651 }
8652}
8653
Abel Gordon20b97fe2013-04-18 14:36:25 +03008654
Liran Alone2536742018-06-23 02:35:02 +03008655static inline int vmcs12_write_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008656 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008657 short offset = vmcs_field_to_offset(field);
Liran Alone2536742018-06-23 02:35:02 +03008658 char *p = (char *)vmcs12 + offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008659 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008660 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008661
Jim Mattsond37f4262017-12-22 12:12:16 -08008662 switch (vmcs_field_width(field)) {
8663 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008664 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008665 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008666 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008667 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008668 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008669 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008670 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008671 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008672 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008673 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008674 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008675 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008676 WARN_ON(1);
8677 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008678 }
8679
8680}
8681
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +02008682static int copy_enlightened_to_vmcs12(struct vcpu_vmx *vmx)
8683{
8684 struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
8685 struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
8686
8687 /* HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE */
8688 vmcs12->tpr_threshold = evmcs->tpr_threshold;
8689 vmcs12->guest_rip = evmcs->guest_rip;
8690
8691 if (unlikely(!(evmcs->hv_clean_fields &
8692 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC))) {
8693 vmcs12->guest_rsp = evmcs->guest_rsp;
8694 vmcs12->guest_rflags = evmcs->guest_rflags;
8695 vmcs12->guest_interruptibility_info =
8696 evmcs->guest_interruptibility_info;
8697 }
8698
8699 if (unlikely(!(evmcs->hv_clean_fields &
8700 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) {
8701 vmcs12->cpu_based_vm_exec_control =
8702 evmcs->cpu_based_vm_exec_control;
8703 }
8704
8705 if (unlikely(!(evmcs->hv_clean_fields &
8706 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) {
8707 vmcs12->exception_bitmap = evmcs->exception_bitmap;
8708 }
8709
8710 if (unlikely(!(evmcs->hv_clean_fields &
8711 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_ENTRY))) {
8712 vmcs12->vm_entry_controls = evmcs->vm_entry_controls;
8713 }
8714
8715 if (unlikely(!(evmcs->hv_clean_fields &
8716 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT))) {
8717 vmcs12->vm_entry_intr_info_field =
8718 evmcs->vm_entry_intr_info_field;
8719 vmcs12->vm_entry_exception_error_code =
8720 evmcs->vm_entry_exception_error_code;
8721 vmcs12->vm_entry_instruction_len =
8722 evmcs->vm_entry_instruction_len;
8723 }
8724
8725 if (unlikely(!(evmcs->hv_clean_fields &
8726 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) {
8727 vmcs12->host_ia32_pat = evmcs->host_ia32_pat;
8728 vmcs12->host_ia32_efer = evmcs->host_ia32_efer;
8729 vmcs12->host_cr0 = evmcs->host_cr0;
8730 vmcs12->host_cr3 = evmcs->host_cr3;
8731 vmcs12->host_cr4 = evmcs->host_cr4;
8732 vmcs12->host_ia32_sysenter_esp = evmcs->host_ia32_sysenter_esp;
8733 vmcs12->host_ia32_sysenter_eip = evmcs->host_ia32_sysenter_eip;
8734 vmcs12->host_rip = evmcs->host_rip;
8735 vmcs12->host_ia32_sysenter_cs = evmcs->host_ia32_sysenter_cs;
8736 vmcs12->host_es_selector = evmcs->host_es_selector;
8737 vmcs12->host_cs_selector = evmcs->host_cs_selector;
8738 vmcs12->host_ss_selector = evmcs->host_ss_selector;
8739 vmcs12->host_ds_selector = evmcs->host_ds_selector;
8740 vmcs12->host_fs_selector = evmcs->host_fs_selector;
8741 vmcs12->host_gs_selector = evmcs->host_gs_selector;
8742 vmcs12->host_tr_selector = evmcs->host_tr_selector;
8743 }
8744
8745 if (unlikely(!(evmcs->hv_clean_fields &
8746 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) {
8747 vmcs12->pin_based_vm_exec_control =
8748 evmcs->pin_based_vm_exec_control;
8749 vmcs12->vm_exit_controls = evmcs->vm_exit_controls;
8750 vmcs12->secondary_vm_exec_control =
8751 evmcs->secondary_vm_exec_control;
8752 }
8753
8754 if (unlikely(!(evmcs->hv_clean_fields &
8755 HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP))) {
8756 vmcs12->io_bitmap_a = evmcs->io_bitmap_a;
8757 vmcs12->io_bitmap_b = evmcs->io_bitmap_b;
8758 }
8759
8760 if (unlikely(!(evmcs->hv_clean_fields &
8761 HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP))) {
8762 vmcs12->msr_bitmap = evmcs->msr_bitmap;
8763 }
8764
8765 if (unlikely(!(evmcs->hv_clean_fields &
8766 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2))) {
8767 vmcs12->guest_es_base = evmcs->guest_es_base;
8768 vmcs12->guest_cs_base = evmcs->guest_cs_base;
8769 vmcs12->guest_ss_base = evmcs->guest_ss_base;
8770 vmcs12->guest_ds_base = evmcs->guest_ds_base;
8771 vmcs12->guest_fs_base = evmcs->guest_fs_base;
8772 vmcs12->guest_gs_base = evmcs->guest_gs_base;
8773 vmcs12->guest_ldtr_base = evmcs->guest_ldtr_base;
8774 vmcs12->guest_tr_base = evmcs->guest_tr_base;
8775 vmcs12->guest_gdtr_base = evmcs->guest_gdtr_base;
8776 vmcs12->guest_idtr_base = evmcs->guest_idtr_base;
8777 vmcs12->guest_es_limit = evmcs->guest_es_limit;
8778 vmcs12->guest_cs_limit = evmcs->guest_cs_limit;
8779 vmcs12->guest_ss_limit = evmcs->guest_ss_limit;
8780 vmcs12->guest_ds_limit = evmcs->guest_ds_limit;
8781 vmcs12->guest_fs_limit = evmcs->guest_fs_limit;
8782 vmcs12->guest_gs_limit = evmcs->guest_gs_limit;
8783 vmcs12->guest_ldtr_limit = evmcs->guest_ldtr_limit;
8784 vmcs12->guest_tr_limit = evmcs->guest_tr_limit;
8785 vmcs12->guest_gdtr_limit = evmcs->guest_gdtr_limit;
8786 vmcs12->guest_idtr_limit = evmcs->guest_idtr_limit;
8787 vmcs12->guest_es_ar_bytes = evmcs->guest_es_ar_bytes;
8788 vmcs12->guest_cs_ar_bytes = evmcs->guest_cs_ar_bytes;
8789 vmcs12->guest_ss_ar_bytes = evmcs->guest_ss_ar_bytes;
8790 vmcs12->guest_ds_ar_bytes = evmcs->guest_ds_ar_bytes;
8791 vmcs12->guest_fs_ar_bytes = evmcs->guest_fs_ar_bytes;
8792 vmcs12->guest_gs_ar_bytes = evmcs->guest_gs_ar_bytes;
8793 vmcs12->guest_ldtr_ar_bytes = evmcs->guest_ldtr_ar_bytes;
8794 vmcs12->guest_tr_ar_bytes = evmcs->guest_tr_ar_bytes;
8795 vmcs12->guest_es_selector = evmcs->guest_es_selector;
8796 vmcs12->guest_cs_selector = evmcs->guest_cs_selector;
8797 vmcs12->guest_ss_selector = evmcs->guest_ss_selector;
8798 vmcs12->guest_ds_selector = evmcs->guest_ds_selector;
8799 vmcs12->guest_fs_selector = evmcs->guest_fs_selector;
8800 vmcs12->guest_gs_selector = evmcs->guest_gs_selector;
8801 vmcs12->guest_ldtr_selector = evmcs->guest_ldtr_selector;
8802 vmcs12->guest_tr_selector = evmcs->guest_tr_selector;
8803 }
8804
8805 if (unlikely(!(evmcs->hv_clean_fields &
8806 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2))) {
8807 vmcs12->tsc_offset = evmcs->tsc_offset;
8808 vmcs12->virtual_apic_page_addr = evmcs->virtual_apic_page_addr;
8809 vmcs12->xss_exit_bitmap = evmcs->xss_exit_bitmap;
8810 }
8811
8812 if (unlikely(!(evmcs->hv_clean_fields &
8813 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR))) {
8814 vmcs12->cr0_guest_host_mask = evmcs->cr0_guest_host_mask;
8815 vmcs12->cr4_guest_host_mask = evmcs->cr4_guest_host_mask;
8816 vmcs12->cr0_read_shadow = evmcs->cr0_read_shadow;
8817 vmcs12->cr4_read_shadow = evmcs->cr4_read_shadow;
8818 vmcs12->guest_cr0 = evmcs->guest_cr0;
8819 vmcs12->guest_cr3 = evmcs->guest_cr3;
8820 vmcs12->guest_cr4 = evmcs->guest_cr4;
8821 vmcs12->guest_dr7 = evmcs->guest_dr7;
8822 }
8823
8824 if (unlikely(!(evmcs->hv_clean_fields &
8825 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER))) {
8826 vmcs12->host_fs_base = evmcs->host_fs_base;
8827 vmcs12->host_gs_base = evmcs->host_gs_base;
8828 vmcs12->host_tr_base = evmcs->host_tr_base;
8829 vmcs12->host_gdtr_base = evmcs->host_gdtr_base;
8830 vmcs12->host_idtr_base = evmcs->host_idtr_base;
8831 vmcs12->host_rsp = evmcs->host_rsp;
8832 }
8833
8834 if (unlikely(!(evmcs->hv_clean_fields &
8835 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT))) {
8836 vmcs12->ept_pointer = evmcs->ept_pointer;
8837 vmcs12->virtual_processor_id = evmcs->virtual_processor_id;
8838 }
8839
8840 if (unlikely(!(evmcs->hv_clean_fields &
8841 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1))) {
8842 vmcs12->vmcs_link_pointer = evmcs->vmcs_link_pointer;
8843 vmcs12->guest_ia32_debugctl = evmcs->guest_ia32_debugctl;
8844 vmcs12->guest_ia32_pat = evmcs->guest_ia32_pat;
8845 vmcs12->guest_ia32_efer = evmcs->guest_ia32_efer;
8846 vmcs12->guest_pdptr0 = evmcs->guest_pdptr0;
8847 vmcs12->guest_pdptr1 = evmcs->guest_pdptr1;
8848 vmcs12->guest_pdptr2 = evmcs->guest_pdptr2;
8849 vmcs12->guest_pdptr3 = evmcs->guest_pdptr3;
8850 vmcs12->guest_pending_dbg_exceptions =
8851 evmcs->guest_pending_dbg_exceptions;
8852 vmcs12->guest_sysenter_esp = evmcs->guest_sysenter_esp;
8853 vmcs12->guest_sysenter_eip = evmcs->guest_sysenter_eip;
8854 vmcs12->guest_bndcfgs = evmcs->guest_bndcfgs;
8855 vmcs12->guest_activity_state = evmcs->guest_activity_state;
8856 vmcs12->guest_sysenter_cs = evmcs->guest_sysenter_cs;
8857 }
8858
8859 /*
8860 * Not used?
8861 * vmcs12->vm_exit_msr_store_addr = evmcs->vm_exit_msr_store_addr;
8862 * vmcs12->vm_exit_msr_load_addr = evmcs->vm_exit_msr_load_addr;
8863 * vmcs12->vm_entry_msr_load_addr = evmcs->vm_entry_msr_load_addr;
8864 * vmcs12->cr3_target_value0 = evmcs->cr3_target_value0;
8865 * vmcs12->cr3_target_value1 = evmcs->cr3_target_value1;
8866 * vmcs12->cr3_target_value2 = evmcs->cr3_target_value2;
8867 * vmcs12->cr3_target_value3 = evmcs->cr3_target_value3;
8868 * vmcs12->page_fault_error_code_mask =
8869 * evmcs->page_fault_error_code_mask;
8870 * vmcs12->page_fault_error_code_match =
8871 * evmcs->page_fault_error_code_match;
8872 * vmcs12->cr3_target_count = evmcs->cr3_target_count;
8873 * vmcs12->vm_exit_msr_store_count = evmcs->vm_exit_msr_store_count;
8874 * vmcs12->vm_exit_msr_load_count = evmcs->vm_exit_msr_load_count;
8875 * vmcs12->vm_entry_msr_load_count = evmcs->vm_entry_msr_load_count;
8876 */
8877
8878 /*
8879 * Read only fields:
8880 * vmcs12->guest_physical_address = evmcs->guest_physical_address;
8881 * vmcs12->vm_instruction_error = evmcs->vm_instruction_error;
8882 * vmcs12->vm_exit_reason = evmcs->vm_exit_reason;
8883 * vmcs12->vm_exit_intr_info = evmcs->vm_exit_intr_info;
8884 * vmcs12->vm_exit_intr_error_code = evmcs->vm_exit_intr_error_code;
8885 * vmcs12->idt_vectoring_info_field = evmcs->idt_vectoring_info_field;
8886 * vmcs12->idt_vectoring_error_code = evmcs->idt_vectoring_error_code;
8887 * vmcs12->vm_exit_instruction_len = evmcs->vm_exit_instruction_len;
8888 * vmcs12->vmx_instruction_info = evmcs->vmx_instruction_info;
8889 * vmcs12->exit_qualification = evmcs->exit_qualification;
8890 * vmcs12->guest_linear_address = evmcs->guest_linear_address;
8891 *
8892 * Not present in struct vmcs12:
8893 * vmcs12->exit_io_instruction_ecx = evmcs->exit_io_instruction_ecx;
8894 * vmcs12->exit_io_instruction_esi = evmcs->exit_io_instruction_esi;
8895 * vmcs12->exit_io_instruction_edi = evmcs->exit_io_instruction_edi;
8896 * vmcs12->exit_io_instruction_eip = evmcs->exit_io_instruction_eip;
8897 */
8898
8899 return 0;
8900}
8901
8902static int copy_vmcs12_to_enlightened(struct vcpu_vmx *vmx)
8903{
8904 struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
8905 struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
8906
8907 /*
8908 * Should not be changed by KVM:
8909 *
8910 * evmcs->host_es_selector = vmcs12->host_es_selector;
8911 * evmcs->host_cs_selector = vmcs12->host_cs_selector;
8912 * evmcs->host_ss_selector = vmcs12->host_ss_selector;
8913 * evmcs->host_ds_selector = vmcs12->host_ds_selector;
8914 * evmcs->host_fs_selector = vmcs12->host_fs_selector;
8915 * evmcs->host_gs_selector = vmcs12->host_gs_selector;
8916 * evmcs->host_tr_selector = vmcs12->host_tr_selector;
8917 * evmcs->host_ia32_pat = vmcs12->host_ia32_pat;
8918 * evmcs->host_ia32_efer = vmcs12->host_ia32_efer;
8919 * evmcs->host_cr0 = vmcs12->host_cr0;
8920 * evmcs->host_cr3 = vmcs12->host_cr3;
8921 * evmcs->host_cr4 = vmcs12->host_cr4;
8922 * evmcs->host_ia32_sysenter_esp = vmcs12->host_ia32_sysenter_esp;
8923 * evmcs->host_ia32_sysenter_eip = vmcs12->host_ia32_sysenter_eip;
8924 * evmcs->host_rip = vmcs12->host_rip;
8925 * evmcs->host_ia32_sysenter_cs = vmcs12->host_ia32_sysenter_cs;
8926 * evmcs->host_fs_base = vmcs12->host_fs_base;
8927 * evmcs->host_gs_base = vmcs12->host_gs_base;
8928 * evmcs->host_tr_base = vmcs12->host_tr_base;
8929 * evmcs->host_gdtr_base = vmcs12->host_gdtr_base;
8930 * evmcs->host_idtr_base = vmcs12->host_idtr_base;
8931 * evmcs->host_rsp = vmcs12->host_rsp;
8932 * sync_vmcs12() doesn't read these:
8933 * evmcs->io_bitmap_a = vmcs12->io_bitmap_a;
8934 * evmcs->io_bitmap_b = vmcs12->io_bitmap_b;
8935 * evmcs->msr_bitmap = vmcs12->msr_bitmap;
8936 * evmcs->ept_pointer = vmcs12->ept_pointer;
8937 * evmcs->xss_exit_bitmap = vmcs12->xss_exit_bitmap;
8938 * evmcs->vm_exit_msr_store_addr = vmcs12->vm_exit_msr_store_addr;
8939 * evmcs->vm_exit_msr_load_addr = vmcs12->vm_exit_msr_load_addr;
8940 * evmcs->vm_entry_msr_load_addr = vmcs12->vm_entry_msr_load_addr;
8941 * evmcs->cr3_target_value0 = vmcs12->cr3_target_value0;
8942 * evmcs->cr3_target_value1 = vmcs12->cr3_target_value1;
8943 * evmcs->cr3_target_value2 = vmcs12->cr3_target_value2;
8944 * evmcs->cr3_target_value3 = vmcs12->cr3_target_value3;
8945 * evmcs->tpr_threshold = vmcs12->tpr_threshold;
8946 * evmcs->virtual_processor_id = vmcs12->virtual_processor_id;
8947 * evmcs->exception_bitmap = vmcs12->exception_bitmap;
8948 * evmcs->vmcs_link_pointer = vmcs12->vmcs_link_pointer;
8949 * evmcs->pin_based_vm_exec_control = vmcs12->pin_based_vm_exec_control;
8950 * evmcs->vm_exit_controls = vmcs12->vm_exit_controls;
8951 * evmcs->secondary_vm_exec_control = vmcs12->secondary_vm_exec_control;
8952 * evmcs->page_fault_error_code_mask =
8953 * vmcs12->page_fault_error_code_mask;
8954 * evmcs->page_fault_error_code_match =
8955 * vmcs12->page_fault_error_code_match;
8956 * evmcs->cr3_target_count = vmcs12->cr3_target_count;
8957 * evmcs->virtual_apic_page_addr = vmcs12->virtual_apic_page_addr;
8958 * evmcs->tsc_offset = vmcs12->tsc_offset;
8959 * evmcs->guest_ia32_debugctl = vmcs12->guest_ia32_debugctl;
8960 * evmcs->cr0_guest_host_mask = vmcs12->cr0_guest_host_mask;
8961 * evmcs->cr4_guest_host_mask = vmcs12->cr4_guest_host_mask;
8962 * evmcs->cr0_read_shadow = vmcs12->cr0_read_shadow;
8963 * evmcs->cr4_read_shadow = vmcs12->cr4_read_shadow;
8964 * evmcs->vm_exit_msr_store_count = vmcs12->vm_exit_msr_store_count;
8965 * evmcs->vm_exit_msr_load_count = vmcs12->vm_exit_msr_load_count;
8966 * evmcs->vm_entry_msr_load_count = vmcs12->vm_entry_msr_load_count;
8967 *
8968 * Not present in struct vmcs12:
8969 * evmcs->exit_io_instruction_ecx = vmcs12->exit_io_instruction_ecx;
8970 * evmcs->exit_io_instruction_esi = vmcs12->exit_io_instruction_esi;
8971 * evmcs->exit_io_instruction_edi = vmcs12->exit_io_instruction_edi;
8972 * evmcs->exit_io_instruction_eip = vmcs12->exit_io_instruction_eip;
8973 */
8974
8975 evmcs->guest_es_selector = vmcs12->guest_es_selector;
8976 evmcs->guest_cs_selector = vmcs12->guest_cs_selector;
8977 evmcs->guest_ss_selector = vmcs12->guest_ss_selector;
8978 evmcs->guest_ds_selector = vmcs12->guest_ds_selector;
8979 evmcs->guest_fs_selector = vmcs12->guest_fs_selector;
8980 evmcs->guest_gs_selector = vmcs12->guest_gs_selector;
8981 evmcs->guest_ldtr_selector = vmcs12->guest_ldtr_selector;
8982 evmcs->guest_tr_selector = vmcs12->guest_tr_selector;
8983
8984 evmcs->guest_es_limit = vmcs12->guest_es_limit;
8985 evmcs->guest_cs_limit = vmcs12->guest_cs_limit;
8986 evmcs->guest_ss_limit = vmcs12->guest_ss_limit;
8987 evmcs->guest_ds_limit = vmcs12->guest_ds_limit;
8988 evmcs->guest_fs_limit = vmcs12->guest_fs_limit;
8989 evmcs->guest_gs_limit = vmcs12->guest_gs_limit;
8990 evmcs->guest_ldtr_limit = vmcs12->guest_ldtr_limit;
8991 evmcs->guest_tr_limit = vmcs12->guest_tr_limit;
8992 evmcs->guest_gdtr_limit = vmcs12->guest_gdtr_limit;
8993 evmcs->guest_idtr_limit = vmcs12->guest_idtr_limit;
8994
8995 evmcs->guest_es_ar_bytes = vmcs12->guest_es_ar_bytes;
8996 evmcs->guest_cs_ar_bytes = vmcs12->guest_cs_ar_bytes;
8997 evmcs->guest_ss_ar_bytes = vmcs12->guest_ss_ar_bytes;
8998 evmcs->guest_ds_ar_bytes = vmcs12->guest_ds_ar_bytes;
8999 evmcs->guest_fs_ar_bytes = vmcs12->guest_fs_ar_bytes;
9000 evmcs->guest_gs_ar_bytes = vmcs12->guest_gs_ar_bytes;
9001 evmcs->guest_ldtr_ar_bytes = vmcs12->guest_ldtr_ar_bytes;
9002 evmcs->guest_tr_ar_bytes = vmcs12->guest_tr_ar_bytes;
9003
9004 evmcs->guest_es_base = vmcs12->guest_es_base;
9005 evmcs->guest_cs_base = vmcs12->guest_cs_base;
9006 evmcs->guest_ss_base = vmcs12->guest_ss_base;
9007 evmcs->guest_ds_base = vmcs12->guest_ds_base;
9008 evmcs->guest_fs_base = vmcs12->guest_fs_base;
9009 evmcs->guest_gs_base = vmcs12->guest_gs_base;
9010 evmcs->guest_ldtr_base = vmcs12->guest_ldtr_base;
9011 evmcs->guest_tr_base = vmcs12->guest_tr_base;
9012 evmcs->guest_gdtr_base = vmcs12->guest_gdtr_base;
9013 evmcs->guest_idtr_base = vmcs12->guest_idtr_base;
9014
9015 evmcs->guest_ia32_pat = vmcs12->guest_ia32_pat;
9016 evmcs->guest_ia32_efer = vmcs12->guest_ia32_efer;
9017
9018 evmcs->guest_pdptr0 = vmcs12->guest_pdptr0;
9019 evmcs->guest_pdptr1 = vmcs12->guest_pdptr1;
9020 evmcs->guest_pdptr2 = vmcs12->guest_pdptr2;
9021 evmcs->guest_pdptr3 = vmcs12->guest_pdptr3;
9022
9023 evmcs->guest_pending_dbg_exceptions =
9024 vmcs12->guest_pending_dbg_exceptions;
9025 evmcs->guest_sysenter_esp = vmcs12->guest_sysenter_esp;
9026 evmcs->guest_sysenter_eip = vmcs12->guest_sysenter_eip;
9027
9028 evmcs->guest_activity_state = vmcs12->guest_activity_state;
9029 evmcs->guest_sysenter_cs = vmcs12->guest_sysenter_cs;
9030
9031 evmcs->guest_cr0 = vmcs12->guest_cr0;
9032 evmcs->guest_cr3 = vmcs12->guest_cr3;
9033 evmcs->guest_cr4 = vmcs12->guest_cr4;
9034 evmcs->guest_dr7 = vmcs12->guest_dr7;
9035
9036 evmcs->guest_physical_address = vmcs12->guest_physical_address;
9037
9038 evmcs->vm_instruction_error = vmcs12->vm_instruction_error;
9039 evmcs->vm_exit_reason = vmcs12->vm_exit_reason;
9040 evmcs->vm_exit_intr_info = vmcs12->vm_exit_intr_info;
9041 evmcs->vm_exit_intr_error_code = vmcs12->vm_exit_intr_error_code;
9042 evmcs->idt_vectoring_info_field = vmcs12->idt_vectoring_info_field;
9043 evmcs->idt_vectoring_error_code = vmcs12->idt_vectoring_error_code;
9044 evmcs->vm_exit_instruction_len = vmcs12->vm_exit_instruction_len;
9045 evmcs->vmx_instruction_info = vmcs12->vmx_instruction_info;
9046
9047 evmcs->exit_qualification = vmcs12->exit_qualification;
9048
9049 evmcs->guest_linear_address = vmcs12->guest_linear_address;
9050 evmcs->guest_rsp = vmcs12->guest_rsp;
9051 evmcs->guest_rflags = vmcs12->guest_rflags;
9052
9053 evmcs->guest_interruptibility_info =
9054 vmcs12->guest_interruptibility_info;
9055 evmcs->cpu_based_vm_exec_control = vmcs12->cpu_based_vm_exec_control;
9056 evmcs->vm_entry_controls = vmcs12->vm_entry_controls;
9057 evmcs->vm_entry_intr_info_field = vmcs12->vm_entry_intr_info_field;
9058 evmcs->vm_entry_exception_error_code =
9059 vmcs12->vm_entry_exception_error_code;
9060 evmcs->vm_entry_instruction_len = vmcs12->vm_entry_instruction_len;
9061
9062 evmcs->guest_rip = vmcs12->guest_rip;
9063
9064 evmcs->guest_bndcfgs = vmcs12->guest_bndcfgs;
9065
9066 return 0;
9067}
9068
Jim Mattsonf4160e42018-05-29 09:11:33 -07009069/*
9070 * Copy the writable VMCS shadow fields back to the VMCS12, in case
9071 * they have been modified by the L1 guest. Note that the "read-only"
9072 * VM-exit information fields are actually writable if the vCPU is
9073 * configured to support "VMWRITE to any supported field in the VMCS."
9074 */
Abel Gordon16f5b902013-04-18 14:38:25 +03009075static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
9076{
Jim Mattsonf4160e42018-05-29 09:11:33 -07009077 const u16 *fields[] = {
9078 shadow_read_write_fields,
9079 shadow_read_only_fields
9080 };
9081 const int max_fields[] = {
9082 max_shadow_read_write_fields,
9083 max_shadow_read_only_fields
9084 };
9085 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03009086 unsigned long field;
9087 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07009088 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03009089
Jan Kiszka282da872014-10-08 18:05:39 +02009090 preempt_disable();
9091
Abel Gordon16f5b902013-04-18 14:38:25 +03009092 vmcs_load(shadow_vmcs);
9093
Jim Mattsonf4160e42018-05-29 09:11:33 -07009094 for (q = 0; q < ARRAY_SIZE(fields); q++) {
9095 for (i = 0; i < max_fields[q]; i++) {
9096 field = fields[q][i];
9097 field_value = __vmcs_readl(field);
Liran Alone2536742018-06-23 02:35:02 +03009098 vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
Jim Mattsonf4160e42018-05-29 09:11:33 -07009099 }
9100 /*
9101 * Skip the VM-exit information fields if they are read-only.
9102 */
9103 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
9104 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03009105 }
9106
9107 vmcs_clear(shadow_vmcs);
9108 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02009109
9110 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03009111}
9112
Abel Gordonc3114422013-04-18 14:38:55 +03009113static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
9114{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01009115 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02009116 shadow_read_write_fields,
9117 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03009118 };
Mathias Krausec2bae892013-06-26 20:36:21 +02009119 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03009120 max_shadow_read_write_fields,
9121 max_shadow_read_only_fields
9122 };
9123 int i, q;
9124 unsigned long field;
9125 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07009126 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03009127
9128 vmcs_load(shadow_vmcs);
9129
Mathias Krausec2bae892013-06-26 20:36:21 +02009130 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03009131 for (i = 0; i < max_fields[q]; i++) {
9132 field = fields[q][i];
Liran Alone2536742018-06-23 02:35:02 +03009133 vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01009134 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03009135 }
9136 }
9137
9138 vmcs_clear(shadow_vmcs);
9139 vmcs_load(vmx->loaded_vmcs->vmcs);
9140}
9141
Nadav Har'El49f705c2011-05-25 23:08:30 +03009142static int handle_vmread(struct kvm_vcpu *vcpu)
9143{
9144 unsigned long field;
9145 u64 field_value;
9146 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9147 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9148 gva_t gva = 0;
Liran Alon6d894f42018-06-23 02:35:09 +03009149 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03009150
Kyle Hueyeb277562016-11-29 12:40:39 -08009151 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03009152 return 1;
9153
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009154 if (to_vmx(vcpu)->nested.current_vmptr == -1ull)
9155 return nested_vmx_failInvalid(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08009156
Liran Alon6d894f42018-06-23 02:35:09 +03009157 if (!is_guest_mode(vcpu))
9158 vmcs12 = get_vmcs12(vcpu);
9159 else {
9160 /*
9161 * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
9162 * to shadowed-field sets the ALU flags for VMfailInvalid.
9163 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009164 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull)
9165 return nested_vmx_failInvalid(vcpu);
Liran Alon6d894f42018-06-23 02:35:09 +03009166 vmcs12 = get_shadow_vmcs12(vcpu);
9167 }
9168
Nadav Har'El49f705c2011-05-25 23:08:30 +03009169 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03009170 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03009171 /* Read the field, zero-extended to a u64 field_value */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009172 if (vmcs12_read_any(vmcs12, field, &field_value) < 0)
9173 return nested_vmx_failValid(vcpu,
9174 VMXERR_UNSUPPORTED_VMCS_COMPONENT);
9175
Nadav Har'El49f705c2011-05-25 23:08:30 +03009176 /*
9177 * Now copy part of this value to register or memory, as requested.
9178 * Note that the number of bits actually copied is 32 or 64 depending
9179 * on the guest's mode (32 or 64 bit), not on the given field's length.
9180 */
9181 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03009182 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03009183 field_value);
9184 } else {
9185 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00009186 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03009187 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02009188 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02009189 kvm_write_guest_virt_system(vcpu, gva, &field_value,
9190 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03009191 }
9192
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009193 return nested_vmx_succeed(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03009194}
9195
9196
9197static int handle_vmwrite(struct kvm_vcpu *vcpu)
9198{
9199 unsigned long field;
9200 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01009201 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03009202 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9203 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01009204
Nadav Har'El49f705c2011-05-25 23:08:30 +03009205 /* The value to write might be 32 or 64 bits, depending on L1's long
9206 * mode, and eventually we need to write that into a field of several
9207 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08009208 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03009209 * bits into the vmcs12 field.
9210 */
9211 u64 field_value = 0;
9212 struct x86_exception e;
Liran Alon6d894f42018-06-23 02:35:09 +03009213 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03009214
Kyle Hueyeb277562016-11-29 12:40:39 -08009215 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03009216 return 1;
9217
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009218 if (vmx->nested.current_vmptr == -1ull)
9219 return nested_vmx_failInvalid(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08009220
Nadav Har'El49f705c2011-05-25 23:08:30 +03009221 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03009222 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009223 (((vmx_instruction_info) >> 3) & 0xf));
9224 else {
9225 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00009226 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03009227 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02009228 if (kvm_read_guest_virt(vcpu, gva, &field_value,
9229 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03009230 kvm_inject_page_fault(vcpu, &e);
9231 return 1;
9232 }
9233 }
9234
9235
Nadav Amit27e6fb52014-06-18 17:19:26 +03009236 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07009237 /*
9238 * If the vCPU supports "VMWRITE to any supported field in the
9239 * VMCS," then the "read-only" fields are actually read/write.
9240 */
9241 if (vmcs_field_readonly(field) &&
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009242 !nested_cpu_has_vmwrite_any_field(vcpu))
9243 return nested_vmx_failValid(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009244 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Nadav Har'El49f705c2011-05-25 23:08:30 +03009245
Liran Alon6d894f42018-06-23 02:35:09 +03009246 if (!is_guest_mode(vcpu))
9247 vmcs12 = get_vmcs12(vcpu);
9248 else {
9249 /*
9250 * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
9251 * to shadowed-field sets the ALU flags for VMfailInvalid.
9252 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009253 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull)
9254 return nested_vmx_failInvalid(vcpu);
Liran Alon6d894f42018-06-23 02:35:09 +03009255 vmcs12 = get_shadow_vmcs12(vcpu);
Liran Alon6d894f42018-06-23 02:35:09 +03009256 }
9257
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009258 if (vmcs12_write_any(vmcs12, field, field_value) < 0)
9259 return nested_vmx_failValid(vcpu,
9260 VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Nadav Har'El49f705c2011-05-25 23:08:30 +03009261
Liran Alon6d894f42018-06-23 02:35:09 +03009262 /*
9263 * Do not track vmcs12 dirty-state if in guest-mode
9264 * as we actually dirty shadow vmcs12 instead of vmcs12.
9265 */
9266 if (!is_guest_mode(vcpu)) {
9267 switch (field) {
Paolo Bonzini74a497f2017-12-20 13:55:39 +01009268#define SHADOW_FIELD_RW(x) case x:
Sean Christophersone0123112018-12-03 13:52:57 -08009269#include "vmcs_shadow_fields.h"
Liran Alon6d894f42018-06-23 02:35:09 +03009270 /*
9271 * The fields that can be updated by L1 without a vmexit are
9272 * always updated in the vmcs02, the others go down the slow
9273 * path of prepare_vmcs02.
9274 */
9275 break;
9276 default:
9277 vmx->nested.dirty_vmcs12 = true;
9278 break;
9279 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01009280 }
9281
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009282 return nested_vmx_succeed(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03009283}
9284
Jim Mattsona8bc2842016-11-30 12:03:44 -08009285static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
9286{
9287 vmx->nested.current_vmptr = vmptr;
9288 if (enable_shadow_vmcs) {
9289 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
9290 SECONDARY_EXEC_SHADOW_VMCS);
9291 vmcs_write64(VMCS_LINK_POINTER,
9292 __pa(vmx->vmcs01.shadow_vmcs));
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +02009293 vmx->nested.need_vmcs12_sync = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08009294 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01009295 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08009296}
9297
Nadav Har'El63846662011-05-25 23:07:29 +03009298/* Emulate the VMPTRLD instruction */
9299static int handle_vmptrld(struct kvm_vcpu *vcpu)
9300{
9301 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03009302 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03009303
9304 if (!nested_vmx_check_permission(vcpu))
9305 return 1;
9306
Radim Krčmářcbf71272017-05-19 15:48:51 +02009307 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03009308 return 1;
9309
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009310 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu)))
9311 return nested_vmx_failValid(vcpu,
9312 VMXERR_VMPTRLD_INVALID_ADDRESS);
Radim Krčmářcbf71272017-05-19 15:48:51 +02009313
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009314 if (vmptr == vmx->nested.vmxon_ptr)
9315 return nested_vmx_failValid(vcpu,
9316 VMXERR_VMPTRLD_VMXON_POINTER);
Radim Krčmářcbf71272017-05-19 15:48:51 +02009317
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02009318 /* Forbid normal VMPTRLD if Enlightened version was used */
9319 if (vmx->nested.hv_evmcs)
9320 return 1;
Nadav Har'El0140cae2011-05-25 23:06:28 +03009321
Nadav Har'El63846662011-05-25 23:07:29 +03009322 if (vmx->nested.current_vmptr != vmptr) {
9323 struct vmcs12 *new_vmcs12;
9324 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009325 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
Jim Mattsonfca91f62017-03-15 07:40:55 -07009326 if (is_error_page(page)) {
9327 /*
9328 * Reads from an unbacked page return all 1s,
9329 * which means that the 32 bits located at the
9330 * given physical address won't match the required
9331 * VMCS12_REVISION identifier.
9332 */
9333 nested_vmx_failValid(vcpu,
9334 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
9335 return kvm_skip_emulated_instruction(vcpu);
9336 }
Nadav Har'El63846662011-05-25 23:07:29 +03009337 new_vmcs12 = kmap(page);
Liran Alon392b2f22018-06-23 02:35:01 +03009338 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
Liran Alonfa97d7d2018-07-18 14:07:59 +02009339 (new_vmcs12->hdr.shadow_vmcs &&
9340 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
Nadav Har'El63846662011-05-25 23:07:29 +03009341 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02009342 kvm_release_page_clean(page);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009343 return nested_vmx_failValid(vcpu,
Nadav Har'El63846662011-05-25 23:07:29 +03009344 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Nadav Har'El63846662011-05-25 23:07:29 +03009345 }
Nadav Har'El63846662011-05-25 23:07:29 +03009346
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02009347 nested_release_vmcs12(vcpu);
9348
David Matlack4f2777b2016-07-13 17:16:37 -07009349 /*
9350 * Load VMCS12 from guest memory since it is not already
9351 * cached.
9352 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02009353 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
9354 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02009355 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02009356
Jim Mattsona8bc2842016-11-30 12:03:44 -08009357 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03009358 }
9359
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009360 return nested_vmx_succeed(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03009361}
9362
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02009363/*
9364 * This is an equivalent of the nested hypervisor executing the vmptrld
9365 * instruction.
9366 */
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02009367static int nested_vmx_handle_enlightened_vmptrld(struct kvm_vcpu *vcpu,
9368 bool from_launch)
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02009369{
9370 struct vcpu_vmx *vmx = to_vmx(vcpu);
9371 struct hv_vp_assist_page assist_page;
9372
9373 if (likely(!vmx->nested.enlightened_vmcs_enabled))
9374 return 1;
9375
9376 if (unlikely(!kvm_hv_get_assist_page(vcpu, &assist_page)))
9377 return 1;
9378
9379 if (unlikely(!assist_page.enlighten_vmentry))
9380 return 1;
9381
9382 if (unlikely(assist_page.current_nested_vmcs !=
9383 vmx->nested.hv_evmcs_vmptr)) {
9384
9385 if (!vmx->nested.hv_evmcs)
9386 vmx->nested.current_vmptr = -1ull;
9387
9388 nested_release_evmcs(vcpu);
9389
9390 vmx->nested.hv_evmcs_page = kvm_vcpu_gpa_to_page(
9391 vcpu, assist_page.current_nested_vmcs);
9392
9393 if (unlikely(is_error_page(vmx->nested.hv_evmcs_page)))
9394 return 0;
9395
9396 vmx->nested.hv_evmcs = kmap(vmx->nested.hv_evmcs_page);
9397
Liran Alon72aeb602018-11-01 10:57:39 +02009398 /*
9399 * Currently, KVM only supports eVMCS version 1
9400 * (== KVM_EVMCS_VERSION) and thus we expect guest to set this
9401 * value to first u32 field of eVMCS which should specify eVMCS
9402 * VersionNumber.
9403 *
9404 * Guest should be aware of supported eVMCS versions by host by
9405 * examining CPUID.0x4000000A.EAX[0:15]. Host userspace VMM is
9406 * expected to set this CPUID leaf according to the value
9407 * returned in vmcs_version from nested_enable_evmcs().
9408 *
9409 * However, it turns out that Microsoft Hyper-V fails to comply
9410 * to their own invented interface: When Hyper-V use eVMCS, it
9411 * just sets first u32 field of eVMCS to revision_id specified
9412 * in MSR_IA32_VMX_BASIC. Instead of used eVMCS version number
9413 * which is one of the supported versions specified in
9414 * CPUID.0x4000000A.EAX[0:15].
9415 *
9416 * To overcome Hyper-V bug, we accept here either a supported
9417 * eVMCS version or VMCS12 revision_id as valid values for first
9418 * u32 field of eVMCS.
9419 */
9420 if ((vmx->nested.hv_evmcs->revision_id != KVM_EVMCS_VERSION) &&
9421 (vmx->nested.hv_evmcs->revision_id != VMCS12_REVISION)) {
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02009422 nested_release_evmcs(vcpu);
9423 return 0;
9424 }
9425
9426 vmx->nested.dirty_vmcs12 = true;
9427 /*
9428 * As we keep L2 state for one guest only 'hv_clean_fields' mask
9429 * can't be used when we switch between them. Reset it here for
9430 * simplicity.
9431 */
9432 vmx->nested.hv_evmcs->hv_clean_fields &=
9433 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
9434 vmx->nested.hv_evmcs_vmptr = assist_page.current_nested_vmcs;
9435
9436 /*
9437 * Unlike normal vmcs12, enlightened vmcs12 is not fully
9438 * reloaded from guest's memory (read only fields, fields not
9439 * present in struct hv_enlightened_vmcs, ...). Make sure there
9440 * are no leftovers.
9441 */
Liran Alon52ad7eb2018-11-13 17:44:46 +02009442 if (from_launch) {
9443 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9444 memset(vmcs12, 0, sizeof(*vmcs12));
9445 vmcs12->hdr.revision_id = VMCS12_REVISION;
9446 }
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02009447
9448 }
9449 return 1;
Nadav Har'El0140cae2011-05-25 23:06:28 +03009450}
9451
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009452/* Emulate the VMPTRST instruction */
9453static int handle_vmptrst(struct kvm_vcpu *vcpu)
9454{
Sean Christopherson0a06d422018-07-19 10:31:00 -07009455 unsigned long exit_qual = vmcs_readl(EXIT_QUALIFICATION);
9456 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9457 gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009458 struct x86_exception e;
Sean Christopherson0a06d422018-07-19 10:31:00 -07009459 gva_t gva;
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009460
9461 if (!nested_vmx_check_permission(vcpu))
9462 return 1;
9463
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02009464 if (unlikely(to_vmx(vcpu)->nested.hv_evmcs))
9465 return 1;
9466
Sean Christopherson0a06d422018-07-19 10:31:00 -07009467 if (get_vmx_mem_address(vcpu, exit_qual, instr_info, true, &gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009468 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02009469 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Sean Christopherson0a06d422018-07-19 10:31:00 -07009470 if (kvm_write_guest_virt_system(vcpu, gva, (void *)&current_vmptr,
9471 sizeof(gpa_t), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009472 kvm_inject_page_fault(vcpu, &e);
9473 return 1;
9474 }
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009475 return nested_vmx_succeed(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009476}
9477
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009478/* Emulate the INVEPT instruction */
9479static int handle_invept(struct kvm_vcpu *vcpu)
9480{
Wincy Vanb9c237b2015-02-03 23:56:30 +08009481 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009482 u32 vmx_instruction_info, types;
9483 unsigned long type;
9484 gva_t gva;
9485 struct x86_exception e;
9486 struct {
9487 u64 eptp, gpa;
9488 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009489
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009490 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08009491 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009492 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009493 kvm_queue_exception(vcpu, UD_VECTOR);
9494 return 1;
9495 }
9496
9497 if (!nested_vmx_check_permission(vcpu))
9498 return 1;
9499
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009500 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03009501 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009502
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009503 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009504
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009505 if (type >= 32 || !(types & (1 << type)))
9506 return nested_vmx_failValid(vcpu,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009507 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009508
9509 /* According to the Intel VMX instruction reference, the memory
9510 * operand is read even if it isn't needed (e.g., for type==global)
9511 */
9512 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00009513 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009514 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02009515 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009516 kvm_inject_page_fault(vcpu, &e);
9517 return 1;
9518 }
9519
9520 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009521 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04009522 /*
9523 * TODO: track mappings and invalidate
9524 * single context requests appropriately
9525 */
9526 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009527 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04009528 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009529 break;
9530 default:
9531 BUG_ON(1);
9532 break;
9533 }
9534
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009535 return nested_vmx_succeed(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009536}
9537
Liran Alon3d5bdae2018-10-08 23:42:18 +03009538static u16 nested_get_vpid02(struct kvm_vcpu *vcpu)
9539{
9540 struct vcpu_vmx *vmx = to_vmx(vcpu);
9541
9542 return vmx->nested.vpid02 ? vmx->nested.vpid02 : vmx->vpid;
Nadav Har'El0140cae2011-05-25 23:06:28 +03009543}
9544
Petr Matouseka642fc32014-09-23 20:22:30 +02009545static int handle_invvpid(struct kvm_vcpu *vcpu)
9546{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009547 struct vcpu_vmx *vmx = to_vmx(vcpu);
9548 u32 vmx_instruction_info;
9549 unsigned long type, types;
9550 gva_t gva;
9551 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07009552 struct {
9553 u64 vpid;
9554 u64 gla;
9555 } operand;
Liran Alon3d5bdae2018-10-08 23:42:18 +03009556 u16 vpid02;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009557
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009558 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009559 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009560 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009561 kvm_queue_exception(vcpu, UD_VECTOR);
9562 return 1;
9563 }
9564
9565 if (!nested_vmx_check_permission(vcpu))
9566 return 1;
9567
9568 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9569 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9570
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009571 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009572 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009573
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009574 if (type >= 32 || !(types & (1 << type)))
9575 return nested_vmx_failValid(vcpu,
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009576 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009577
9578 /* according to the intel vmx instruction reference, the memory
9579 * operand is read even if it isn't needed (e.g., for type==global)
9580 */
9581 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9582 vmx_instruction_info, false, &gva))
9583 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02009584 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009585 kvm_inject_page_fault(vcpu, &e);
9586 return 1;
9587 }
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009588 if (operand.vpid >> 16)
9589 return nested_vmx_failValid(vcpu,
Jim Mattson40352602017-06-28 09:37:37 -07009590 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009591
Liran Alon3d5bdae2018-10-08 23:42:18 +03009592 vpid02 = nested_get_vpid02(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009593 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009594 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03009595 if (!operand.vpid ||
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009596 is_noncanonical_address(operand.gla, vcpu))
9597 return nested_vmx_failValid(vcpu,
Jim Mattson40352602017-06-28 09:37:37 -07009598 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Liran Alon3d5bdae2018-10-08 23:42:18 +03009599 if (cpu_has_vmx_invvpid_individual_addr()) {
Liran Aloncd9a4912018-05-22 17:16:15 +03009600 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
Liran Alon3d5bdae2018-10-08 23:42:18 +03009601 vpid02, operand.gla);
Liran Aloncd9a4912018-05-22 17:16:15 +03009602 } else
Liran Alon327c0722018-10-08 23:42:19 +03009603 __vmx_flush_tlb(vcpu, vpid02, false);
Liran Aloncd9a4912018-05-22 17:16:15 +03009604 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01009605 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009606 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009607 if (!operand.vpid)
9608 return nested_vmx_failValid(vcpu,
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009609 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Liran Alon327c0722018-10-08 23:42:19 +03009610 __vmx_flush_tlb(vcpu, vpid02, false);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009611 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009612 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Alon327c0722018-10-08 23:42:19 +03009613 __vmx_flush_tlb(vcpu, vpid02, false);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009614 break;
9615 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009616 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009617 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009618 }
9619
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009620 return nested_vmx_succeed(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02009621}
9622
Junaid Shahideb4b2482018-06-27 14:59:14 -07009623static int handle_invpcid(struct kvm_vcpu *vcpu)
9624{
9625 u32 vmx_instruction_info;
9626 unsigned long type;
9627 bool pcid_enabled;
9628 gva_t gva;
9629 struct x86_exception e;
Junaid Shahidb94742c2018-06-27 14:59:20 -07009630 unsigned i;
9631 unsigned long roots_to_free = 0;
Junaid Shahideb4b2482018-06-27 14:59:14 -07009632 struct {
9633 u64 pcid;
9634 u64 gla;
9635 } operand;
9636
9637 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
9638 kvm_queue_exception(vcpu, UD_VECTOR);
9639 return 1;
9640 }
9641
9642 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9643 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9644
9645 if (type > 3) {
9646 kvm_inject_gp(vcpu, 0);
9647 return 1;
9648 }
9649
9650 /* According to the Intel instruction reference, the memory operand
9651 * is read even if it isn't needed (e.g., for type==all)
9652 */
9653 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9654 vmx_instruction_info, false, &gva))
9655 return 1;
9656
9657 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
9658 kvm_inject_page_fault(vcpu, &e);
9659 return 1;
9660 }
9661
9662 if (operand.pcid >> 12 != 0) {
9663 kvm_inject_gp(vcpu, 0);
9664 return 1;
9665 }
9666
9667 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
9668
9669 switch (type) {
9670 case INVPCID_TYPE_INDIV_ADDR:
9671 if ((!pcid_enabled && (operand.pcid != 0)) ||
9672 is_noncanonical_address(operand.gla, vcpu)) {
9673 kvm_inject_gp(vcpu, 0);
9674 return 1;
9675 }
9676 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
9677 return kvm_skip_emulated_instruction(vcpu);
9678
9679 case INVPCID_TYPE_SINGLE_CTXT:
9680 if (!pcid_enabled && (operand.pcid != 0)) {
9681 kvm_inject_gp(vcpu, 0);
9682 return 1;
9683 }
9684
9685 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
9686 kvm_mmu_sync_roots(vcpu);
9687 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
9688 }
9689
Junaid Shahidb94742c2018-06-27 14:59:20 -07009690 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02009691 if (kvm_get_pcid(vcpu, vcpu->arch.mmu->prev_roots[i].cr3)
Junaid Shahidb94742c2018-06-27 14:59:20 -07009692 == operand.pcid)
9693 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
Junaid Shahidade61e22018-06-27 14:59:15 -07009694
Vitaly Kuznetsov6a82cd12018-10-08 21:28:07 +02009695 kvm_mmu_free_roots(vcpu, vcpu->arch.mmu, roots_to_free);
Junaid Shahideb4b2482018-06-27 14:59:14 -07009696 /*
Junaid Shahidb94742c2018-06-27 14:59:20 -07009697 * If neither the current cr3 nor any of the prev_roots use the
Junaid Shahidade61e22018-06-27 14:59:15 -07009698 * given PCID, then nothing needs to be done here because a
9699 * resync will happen anyway before switching to any other CR3.
Junaid Shahideb4b2482018-06-27 14:59:14 -07009700 */
9701
9702 return kvm_skip_emulated_instruction(vcpu);
9703
9704 case INVPCID_TYPE_ALL_NON_GLOBAL:
9705 /*
9706 * Currently, KVM doesn't mark global entries in the shadow
9707 * page tables, so a non-global flush just degenerates to a
9708 * global flush. If needed, we could optimize this later by
9709 * keeping track of global entries in shadow page tables.
9710 */
9711
9712 /* fall-through */
9713 case INVPCID_TYPE_ALL_INCL_GLOBAL:
9714 kvm_mmu_unload(vcpu);
9715 return kvm_skip_emulated_instruction(vcpu);
9716
9717 default:
9718 BUG(); /* We have already checked above that type <= 3 */
9719 }
9720}
9721
Kai Huang843e4332015-01-28 10:54:28 +08009722static int handle_pml_full(struct kvm_vcpu *vcpu)
9723{
9724 unsigned long exit_qualification;
9725
9726 trace_kvm_pml_full(vcpu->vcpu_id);
9727
9728 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9729
9730 /*
9731 * PML buffer FULL happened while executing iret from NMI,
9732 * "blocked by NMI" bit has to be set before next VM entry.
9733 */
9734 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009735 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08009736 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
9737 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9738 GUEST_INTR_STATE_NMI);
9739
9740 /*
9741 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
9742 * here.., and there's no userspace involvement needed for PML.
9743 */
9744 return 1;
9745}
9746
Yunhong Jiang64672c92016-06-13 14:19:59 -07009747static int handle_preemption_timer(struct kvm_vcpu *vcpu)
9748{
Sean Christophersond264ee02018-08-27 15:21:12 -07009749 if (!to_vmx(vcpu)->req_immediate_exit)
9750 kvm_lapic_expired_hv_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -07009751 return 1;
9752}
9753
Bandan Das41ab9372017-08-03 15:54:43 -04009754static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
9755{
9756 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04009757 int maxphyaddr = cpuid_maxphyaddr(vcpu);
9758
9759 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009760 switch (address & VMX_EPTP_MT_MASK) {
9761 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009762 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009763 return false;
9764 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02009765 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009766 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009767 return false;
9768 break;
9769 default:
9770 return false;
9771 }
9772
David Hildenbrandbb97a012017-08-10 23:15:28 +02009773 /* only 4 levels page-walk length are valid */
9774 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04009775 return false;
9776
9777 /* Reserved bits should not be set */
9778 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
9779 return false;
9780
9781 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009782 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009783 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009784 return false;
9785 }
9786
9787 return true;
9788}
9789
9790static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
9791 struct vmcs12 *vmcs12)
9792{
9793 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
9794 u64 address;
9795 bool accessed_dirty;
9796 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
9797
9798 if (!nested_cpu_has_eptp_switching(vmcs12) ||
9799 !nested_cpu_has_ept(vmcs12))
9800 return 1;
9801
9802 if (index >= VMFUNC_EPTP_ENTRIES)
9803 return 1;
9804
9805
9806 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
9807 &address, index * 8, 8))
9808 return 1;
9809
David Hildenbrandbb97a012017-08-10 23:15:28 +02009810 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04009811
9812 /*
9813 * If the (L2) guest does a vmfunc to the currently
9814 * active ept pointer, we don't have to do anything else
9815 */
9816 if (vmcs12->ept_pointer != address) {
9817 if (!valid_ept_address(vcpu, address))
9818 return 1;
9819
9820 kvm_mmu_unload(vcpu);
9821 mmu->ept_ad = accessed_dirty;
Vitaly Kuznetsov36d9594d2018-10-08 21:28:10 +02009822 mmu->mmu_role.base.ad_disabled = !accessed_dirty;
Bandan Das41ab9372017-08-03 15:54:43 -04009823 vmcs12->ept_pointer = address;
9824 /*
9825 * TODO: Check what's the correct approach in case
9826 * mmu reload fails. Currently, we just let the next
9827 * reload potentially fail
9828 */
9829 kvm_mmu_reload(vcpu);
9830 }
9831
9832 return 0;
9833}
9834
Bandan Das2a499e42017-08-03 15:54:41 -04009835static int handle_vmfunc(struct kvm_vcpu *vcpu)
9836{
Bandan Das27c42a12017-08-03 15:54:42 -04009837 struct vcpu_vmx *vmx = to_vmx(vcpu);
9838 struct vmcs12 *vmcs12;
9839 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
9840
9841 /*
9842 * VMFUNC is only supported for nested guests, but we always enable the
9843 * secondary control for simplicity; for non-nested mode, fake that we
9844 * didn't by injecting #UD.
9845 */
9846 if (!is_guest_mode(vcpu)) {
9847 kvm_queue_exception(vcpu, UD_VECTOR);
9848 return 1;
9849 }
9850
9851 vmcs12 = get_vmcs12(vcpu);
9852 if ((vmcs12->vm_function_control & (1 << function)) == 0)
9853 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04009854
9855 switch (function) {
9856 case 0:
9857 if (nested_vmx_eptp_switching(vcpu, vmcs12))
9858 goto fail;
9859 break;
9860 default:
9861 goto fail;
9862 }
9863 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04009864
9865fail:
9866 nested_vmx_vmexit(vcpu, vmx->exit_reason,
9867 vmcs_read32(VM_EXIT_INTR_INFO),
9868 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04009869 return 1;
9870}
9871
Sean Christopherson0b665d32018-08-14 09:33:34 -07009872static int handle_encls(struct kvm_vcpu *vcpu)
9873{
9874 /*
9875 * SGX virtualization is not yet supported. There is no software
9876 * enable bit for SGX, so we have to trap ENCLS and inject a #UD
9877 * to prevent the guest from executing ENCLS.
9878 */
9879 kvm_queue_exception(vcpu, UD_VECTOR);
9880 return 1;
9881}
9882
Nadav Har'El0140cae2011-05-25 23:06:28 +03009883/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08009884 * The exit handlers return 1 if the exit was handled fully and guest execution
9885 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
9886 * to be done to userspace and return 0.
9887 */
Mathias Krause772e0312012-08-30 01:30:19 +02009888static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08009889 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
9890 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08009891 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08009892 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009893 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009894 [EXIT_REASON_CR_ACCESS] = handle_cr,
9895 [EXIT_REASON_DR_ACCESS] = handle_dr,
9896 [EXIT_REASON_CPUID] = handle_cpuid,
9897 [EXIT_REASON_MSR_READ] = handle_rdmsr,
9898 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
9899 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
9900 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02009901 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03009902 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02009903 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02009904 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03009905 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009906 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03009907 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009908 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009909 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009910 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009911 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03009912 [EXIT_REASON_VMOFF] = handle_vmoff,
9913 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08009914 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
9915 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08009916 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08009917 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02009918 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08009919 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02009920 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08009921 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02009922 [EXIT_REASON_GDTR_IDTR] = handle_desc,
9923 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03009924 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
9925 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08009926 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009927 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009928 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009929 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009930 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02009931 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07009932 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07009933 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08009934 [EXIT_REASON_XSAVES] = handle_xsaves,
9935 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08009936 [EXIT_REASON_PML_FULL] = handle_pml_full,
Junaid Shahideb4b2482018-06-27 14:59:14 -07009937 [EXIT_REASON_INVPCID] = handle_invpcid,
Bandan Das2a499e42017-08-03 15:54:41 -04009938 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07009939 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Sean Christopherson0b665d32018-08-14 09:33:34 -07009940 [EXIT_REASON_ENCLS] = handle_encls,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009941};
9942
9943static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04009944 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009945
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009946static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
9947 struct vmcs12 *vmcs12)
9948{
9949 unsigned long exit_qualification;
9950 gpa_t bitmap, last_bitmap;
9951 unsigned int port;
9952 int size;
9953 u8 b;
9954
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009955 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05009956 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009957
9958 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9959
9960 port = exit_qualification >> 16;
9961 size = (exit_qualification & 7) + 1;
9962
9963 last_bitmap = (gpa_t)-1;
9964 b = -1;
9965
9966 while (size > 0) {
9967 if (port < 0x8000)
9968 bitmap = vmcs12->io_bitmap_a;
9969 else if (port < 0x10000)
9970 bitmap = vmcs12->io_bitmap_b;
9971 else
Joe Perches1d804d02015-03-30 16:46:09 -07009972 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009973 bitmap += (port & 0x7fff) / 8;
9974
9975 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009976 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009977 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009978 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07009979 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009980
9981 port++;
9982 size--;
9983 last_bitmap = bitmap;
9984 }
9985
Joe Perches1d804d02015-03-30 16:46:09 -07009986 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009987}
9988
Nadav Har'El644d7112011-05-25 23:12:35 +03009989/*
9990 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
9991 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
9992 * disinterest in the current event (read or write a specific MSR) by using an
9993 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
9994 */
9995static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
9996 struct vmcs12 *vmcs12, u32 exit_reason)
9997{
9998 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
9999 gpa_t bitmap;
10000
Jan Kiszkacbd29cb2013-02-11 12:19:28 +010010001 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -070010002 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010003
10004 /*
10005 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
10006 * for the four combinations of read/write and low/high MSR numbers.
10007 * First we need to figure out which of the four to use:
10008 */
10009 bitmap = vmcs12->msr_bitmap;
10010 if (exit_reason == EXIT_REASON_MSR_WRITE)
10011 bitmap += 2048;
10012 if (msr_index >= 0xc0000000) {
10013 msr_index -= 0xc0000000;
10014 bitmap += 1024;
10015 }
10016
10017 /* Then read the msr_index'th bit from this bitmap: */
10018 if (msr_index < 1024*8) {
10019 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010020 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -070010021 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010022 return 1 & (b >> (msr_index & 7));
10023 } else
Joe Perches1d804d02015-03-30 16:46:09 -070010024 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +030010025}
10026
10027/*
10028 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
10029 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
10030 * intercept (via guest_host_mask etc.) the current event.
10031 */
10032static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
10033 struct vmcs12 *vmcs12)
10034{
10035 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
10036 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +020010037 int reg;
10038 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +030010039
10040 switch ((exit_qualification >> 4) & 3) {
10041 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +020010042 reg = (exit_qualification >> 8) & 15;
10043 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +030010044 switch (cr) {
10045 case 0:
10046 if (vmcs12->cr0_guest_host_mask &
10047 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -070010048 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010049 break;
10050 case 3:
10051 if ((vmcs12->cr3_target_count >= 1 &&
10052 vmcs12->cr3_target_value0 == val) ||
10053 (vmcs12->cr3_target_count >= 2 &&
10054 vmcs12->cr3_target_value1 == val) ||
10055 (vmcs12->cr3_target_count >= 3 &&
10056 vmcs12->cr3_target_value2 == val) ||
10057 (vmcs12->cr3_target_count >= 4 &&
10058 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -070010059 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +030010060 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -070010061 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010062 break;
10063 case 4:
10064 if (vmcs12->cr4_guest_host_mask &
10065 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -070010066 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010067 break;
10068 case 8:
10069 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -070010070 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010071 break;
10072 }
10073 break;
10074 case 2: /* clts */
10075 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
10076 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -070010077 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010078 break;
10079 case 1: /* mov from cr */
10080 switch (cr) {
10081 case 3:
10082 if (vmcs12->cpu_based_vm_exec_control &
10083 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -070010084 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010085 break;
10086 case 8:
10087 if (vmcs12->cpu_based_vm_exec_control &
10088 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -070010089 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010090 break;
10091 }
10092 break;
10093 case 3: /* lmsw */
10094 /*
10095 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
10096 * cr0. Other attempted changes are ignored, with no exit.
10097 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +020010098 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +030010099 if (vmcs12->cr0_guest_host_mask & 0xe &
10100 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -070010101 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010102 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
10103 !(vmcs12->cr0_read_shadow & 0x1) &&
10104 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -070010105 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010106 break;
10107 }
Joe Perches1d804d02015-03-30 16:46:09 -070010108 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +030010109}
10110
Liran Alona7cde482018-06-23 02:35:10 +030010111static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
10112 struct vmcs12 *vmcs12, gpa_t bitmap)
10113{
10114 u32 vmx_instruction_info;
10115 unsigned long field;
10116 u8 b;
10117
10118 if (!nested_cpu_has_shadow_vmcs(vmcs12))
10119 return true;
10120
10121 /* Decode instruction info and find the field to access */
10122 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
10123 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
10124
10125 /* Out-of-range fields always cause a VM exit from L2 to L1 */
10126 if (field >> 15)
10127 return true;
10128
10129 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
10130 return true;
10131
10132 return 1 & (b >> (field & 7));
10133}
10134
Nadav Har'El644d7112011-05-25 23:12:35 +030010135/*
10136 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
10137 * should handle it ourselves in L0 (and then continue L2). Only call this
10138 * when in is_guest_mode (L2).
10139 */
Paolo Bonzini7313c692017-07-27 10:31:25 +020010140static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +030010141{
Nadav Har'El644d7112011-05-25 23:12:35 +030010142 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10143 struct vcpu_vmx *vmx = to_vmx(vcpu);
10144 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10145
Jim Mattson4f350c62017-09-14 16:31:44 -070010146 if (vmx->nested.nested_run_pending)
10147 return false;
10148
10149 if (unlikely(vmx->fail)) {
10150 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
10151 vmcs_read32(VM_INSTRUCTION_ERROR));
10152 return true;
10153 }
Jan Kiszka542060e2014-01-04 18:47:21 +010010154
David Matlackc9f04402017-08-01 14:00:40 -070010155 /*
10156 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -060010157 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
10158 * Page). The CPU may write to these pages via their host
10159 * physical address while L2 is running, bypassing any
10160 * address-translation-based dirty tracking (e.g. EPT write
10161 * protection).
David Matlackc9f04402017-08-01 14:00:40 -070010162 *
10163 * Mark them dirty on every exit from L2 to prevent them from
10164 * getting out of sync with dirty tracking.
10165 */
10166 nested_mark_vmcs12_pages_dirty(vcpu);
10167
Jim Mattson4f350c62017-09-14 16:31:44 -070010168 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
10169 vmcs_readl(EXIT_QUALIFICATION),
10170 vmx->idt_vectoring_info,
10171 intr_info,
10172 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
10173 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +030010174
10175 switch (exit_reason) {
10176 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -080010177 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -070010178 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +030010179 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -070010180 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Jan Kiszka6f054852016-02-09 20:15:18 +010010181 else if (is_debug(intr_info) &&
10182 vcpu->guest_debug &
10183 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
10184 return false;
10185 else if (is_breakpoint(intr_info) &&
10186 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
10187 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +030010188 return vmcs12->exception_bitmap &
10189 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
10190 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -070010191 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +030010192 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -070010193 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010194 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +020010195 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +030010196 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +020010197 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +030010198 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -070010199 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010200 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -070010201 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010202 case EXIT_REASON_HLT:
10203 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
10204 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -070010205 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010206 case EXIT_REASON_INVLPG:
10207 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
10208 case EXIT_REASON_RDPMC:
10209 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +020010210 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +020010211 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +020010212 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +020010213 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +010010214 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +030010215 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
Liran Alona7cde482018-06-23 02:35:10 +030010216 case EXIT_REASON_VMREAD:
10217 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
10218 vmcs12->vmread_bitmap);
10219 case EXIT_REASON_VMWRITE:
10220 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
10221 vmcs12->vmwrite_bitmap);
Nadav Har'El644d7112011-05-25 23:12:35 +030010222 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
10223 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
Liran Alona7cde482018-06-23 02:35:10 +030010224 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
Nadav Har'El644d7112011-05-25 23:12:35 +030010225 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +020010226 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +030010227 /*
10228 * VMX instructions trap unconditionally. This allows L1 to
10229 * emulate them for its L2 guest, i.e., allows 3-level nesting!
10230 */
Joe Perches1d804d02015-03-30 16:46:09 -070010231 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010232 case EXIT_REASON_CR_ACCESS:
10233 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
10234 case EXIT_REASON_DR_ACCESS:
10235 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
10236 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +010010237 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +020010238 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
10239 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +030010240 case EXIT_REASON_MSR_READ:
10241 case EXIT_REASON_MSR_WRITE:
10242 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
10243 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -070010244 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010245 case EXIT_REASON_MWAIT_INSTRUCTION:
10246 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +030010247 case EXIT_REASON_MONITOR_TRAP_FLAG:
10248 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +030010249 case EXIT_REASON_MONITOR_INSTRUCTION:
10250 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
10251 case EXIT_REASON_PAUSE_INSTRUCTION:
10252 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
10253 nested_cpu_has2(vmcs12,
10254 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
10255 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -070010256 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +030010257 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010258 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +030010259 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +080010260 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +080010261 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -040010262 /*
10263 * The controls for "virtualize APIC accesses," "APIC-
10264 * register virtualization," and "virtual-interrupt
10265 * delivery" only come from vmcs12.
10266 */
Joe Perches1d804d02015-03-30 16:46:09 -070010267 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010268 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +030010269 /*
10270 * L0 always deals with the EPT violation. If nested EPT is
10271 * used, and the nested mmu code discovers that the address is
10272 * missing in the guest EPT table (EPT12), the EPT violation
10273 * will be injected with nested_ept_inject_page_fault()
10274 */
Joe Perches1d804d02015-03-30 16:46:09 -070010275 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +030010276 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +030010277 /*
10278 * L2 never uses directly L1's EPT, but rather L0's own EPT
10279 * table (shadow on EPT) or a merged EPT table that L0 built
10280 * (EPT on EPT). So any problems with the structure of the
10281 * table is L0's fault.
10282 */
Joe Perches1d804d02015-03-30 16:46:09 -070010283 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +020010284 case EXIT_REASON_INVPCID:
10285 return
10286 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
10287 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +030010288 case EXIT_REASON_WBINVD:
10289 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
10290 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -070010291 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +080010292 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
10293 /*
10294 * This should never happen, since it is not possible to
10295 * set XSS to a non-zero value---neither in L1 nor in L2.
10296 * If if it were, XSS would have to be checked against
10297 * the XSS exit bitmap in vmcs12.
10298 */
10299 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +080010300 case EXIT_REASON_PREEMPTION_TIMER:
10301 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +020010302 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -040010303 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +020010304 return false;
Bandan Das2a499e42017-08-03 15:54:41 -040010305 case EXIT_REASON_VMFUNC:
10306 /* VM functions are emulated through L2->L0 vmexits. */
10307 return false;
Sean Christopherson0b665d32018-08-14 09:33:34 -070010308 case EXIT_REASON_ENCLS:
10309 /* SGX is never exposed to L1 */
10310 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +030010311 default:
Joe Perches1d804d02015-03-30 16:46:09 -070010312 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010313 }
10314}
10315
Paolo Bonzini7313c692017-07-27 10:31:25 +020010316static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
10317{
10318 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10319
10320 /*
10321 * At this point, the exit interruption info in exit_intr_info
10322 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
10323 * we need to query the in-kernel LAPIC.
10324 */
10325 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
10326 if ((exit_intr_info &
10327 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
10328 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
10329 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10330 vmcs12->vm_exit_intr_error_code =
10331 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
10332 }
10333
10334 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
10335 vmcs_readl(EXIT_QUALIFICATION));
10336 return 1;
10337}
10338
Avi Kivity586f9602010-11-18 13:09:54 +020010339static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
10340{
10341 *info1 = vmcs_readl(EXIT_QUALIFICATION);
10342 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
10343}
10344
Kai Huanga3eaa862015-11-04 13:46:05 +080010345static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +080010346{
Kai Huanga3eaa862015-11-04 13:46:05 +080010347 if (vmx->pml_pg) {
10348 __free_page(vmx->pml_pg);
10349 vmx->pml_pg = NULL;
10350 }
Kai Huang843e4332015-01-28 10:54:28 +080010351}
10352
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010353static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +080010354{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010355 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +080010356 u64 *pml_buf;
10357 u16 pml_idx;
10358
10359 pml_idx = vmcs_read16(GUEST_PML_INDEX);
10360
10361 /* Do nothing if PML buffer is empty */
10362 if (pml_idx == (PML_ENTITY_NUM - 1))
10363 return;
10364
10365 /* PML index always points to next available PML buffer entity */
10366 if (pml_idx >= PML_ENTITY_NUM)
10367 pml_idx = 0;
10368 else
10369 pml_idx++;
10370
10371 pml_buf = page_address(vmx->pml_pg);
10372 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
10373 u64 gpa;
10374
10375 gpa = pml_buf[pml_idx];
10376 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010377 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +080010378 }
10379
10380 /* reset PML index */
10381 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
10382}
10383
10384/*
10385 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
10386 * Called before reporting dirty_bitmap to userspace.
10387 */
10388static void kvm_flush_pml_buffers(struct kvm *kvm)
10389{
10390 int i;
10391 struct kvm_vcpu *vcpu;
10392 /*
10393 * We only need to kick vcpu out of guest mode here, as PML buffer
10394 * is flushed at beginning of all VMEXITs, and it's obvious that only
10395 * vcpus running in guest are possible to have unflushed GPAs in PML
10396 * buffer.
10397 */
10398 kvm_for_each_vcpu(i, vcpu, kvm)
10399 kvm_vcpu_kick(vcpu);
10400}
10401
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010402static void vmx_dump_sel(char *name, uint32_t sel)
10403{
10404 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -050010405 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010406 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
10407 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
10408 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
10409}
10410
10411static void vmx_dump_dtsel(char *name, uint32_t limit)
10412{
10413 pr_err("%s limit=0x%08x, base=0x%016lx\n",
10414 name, vmcs_read32(limit),
10415 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
10416}
10417
10418static void dump_vmcs(void)
10419{
10420 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
10421 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
10422 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
10423 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
10424 u32 secondary_exec_control = 0;
10425 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +010010426 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010427 int i, n;
10428
10429 if (cpu_has_secondary_exec_ctrls())
10430 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10431
10432 pr_err("*** Guest State ***\n");
10433 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
10434 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
10435 vmcs_readl(CR0_GUEST_HOST_MASK));
10436 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
10437 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
10438 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
10439 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
10440 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
10441 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010442 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
10443 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
10444 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
10445 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010446 }
10447 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
10448 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
10449 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
10450 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
10451 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
10452 vmcs_readl(GUEST_SYSENTER_ESP),
10453 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
10454 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
10455 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
10456 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
10457 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
10458 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
10459 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
10460 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
10461 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
10462 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
10463 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
10464 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
10465 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010466 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
10467 efer, vmcs_read64(GUEST_IA32_PAT));
10468 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
10469 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010470 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010471 if (cpu_has_load_perf_global_ctrl &&
10472 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010473 pr_err("PerfGlobCtl = 0x%016llx\n",
10474 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010475 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010476 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010477 pr_err("Interruptibility = %08x ActivityState = %08x\n",
10478 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
10479 vmcs_read32(GUEST_ACTIVITY_STATE));
10480 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
10481 pr_err("InterruptStatus = %04x\n",
10482 vmcs_read16(GUEST_INTR_STATUS));
10483
10484 pr_err("*** Host State ***\n");
10485 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
10486 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
10487 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
10488 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
10489 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
10490 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
10491 vmcs_read16(HOST_TR_SELECTOR));
10492 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
10493 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
10494 vmcs_readl(HOST_TR_BASE));
10495 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
10496 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
10497 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
10498 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
10499 vmcs_readl(HOST_CR4));
10500 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
10501 vmcs_readl(HOST_IA32_SYSENTER_ESP),
10502 vmcs_read32(HOST_IA32_SYSENTER_CS),
10503 vmcs_readl(HOST_IA32_SYSENTER_EIP));
10504 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010505 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
10506 vmcs_read64(HOST_IA32_EFER),
10507 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010508 if (cpu_has_load_perf_global_ctrl &&
10509 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010510 pr_err("PerfGlobCtl = 0x%016llx\n",
10511 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010512
10513 pr_err("*** Control State ***\n");
10514 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
10515 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
10516 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
10517 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
10518 vmcs_read32(EXCEPTION_BITMAP),
10519 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
10520 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
10521 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
10522 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10523 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
10524 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
10525 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
10526 vmcs_read32(VM_EXIT_INTR_INFO),
10527 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
10528 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
10529 pr_err(" reason=%08x qualification=%016lx\n",
10530 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
10531 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
10532 vmcs_read32(IDT_VECTORING_INFO_FIELD),
10533 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010534 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +080010535 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010536 pr_err("TSC Multiplier = 0x%016llx\n",
10537 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010538 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
10539 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
10540 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
10541 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
10542 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010543 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010544 n = vmcs_read32(CR3_TARGET_COUNT);
10545 for (i = 0; i + 1 < n; i += 4)
10546 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
10547 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
10548 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
10549 if (i < n)
10550 pr_err("CR3 target%u=%016lx\n",
10551 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
10552 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
10553 pr_err("PLE Gap=%08x Window=%08x\n",
10554 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
10555 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
10556 pr_err("Virtual processor ID = 0x%04x\n",
10557 vmcs_read16(VIRTUAL_PROCESSOR_ID));
10558}
10559
Avi Kivity6aa8b732006-12-10 02:21:36 -080010560/*
10561 * The guest has exited. See if we can fix it or if we need userspace
10562 * assistance.
10563 */
Avi Kivity851ba692009-08-24 11:10:17 +030010564static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010565{
Avi Kivity29bd8a72007-09-10 17:27:03 +030010566 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +080010567 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +020010568 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +030010569
Paolo Bonzini8b89fe12015-12-10 18:37:32 +010010570 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
10571
Kai Huang843e4332015-01-28 10:54:28 +080010572 /*
10573 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
10574 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
10575 * querying dirty_bitmap, we only need to kick all vcpus out of guest
10576 * mode as if vcpus is in root mode, the PML buffer must has been
10577 * flushed already.
10578 */
10579 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010580 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +080010581
Mohammed Gamal80ced182009-09-01 12:48:18 +020010582 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +020010583 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +020010584 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +010010585
Paolo Bonzini7313c692017-07-27 10:31:25 +020010586 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
10587 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +030010588
Mohammed Gamal51207022010-05-31 22:40:54 +030010589 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010590 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +030010591 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10592 vcpu->run->fail_entry.hardware_entry_failure_reason
10593 = exit_reason;
10594 return 0;
10595 }
10596
Avi Kivity29bd8a72007-09-10 17:27:03 +030010597 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +030010598 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10599 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +030010600 = vmcs_read32(VM_INSTRUCTION_ERROR);
10601 return 0;
10602 }
Avi Kivity6aa8b732006-12-10 02:21:36 -080010603
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010604 /*
10605 * Note:
10606 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
10607 * delivery event since it indicates guest is accessing MMIO.
10608 * The vm-exit can be triggered again after return to guest that
10609 * will cause infinite loop.
10610 */
Mike Dayd77c26f2007-10-08 09:02:08 -040010611 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +080010612 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +020010613 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +000010614 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010615 exit_reason != EXIT_REASON_TASK_SWITCH)) {
10616 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
10617 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +020010618 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010619 vcpu->run->internal.data[0] = vectoring_info;
10620 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +020010621 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
10622 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
10623 vcpu->run->internal.ndata++;
10624 vcpu->run->internal.data[3] =
10625 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
10626 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010627 return 0;
10628 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +020010629
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010630 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010631 vmx->loaded_vmcs->soft_vnmi_blocked)) {
10632 if (vmx_interrupt_allowed(vcpu)) {
10633 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10634 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
10635 vcpu->arch.nmi_pending) {
10636 /*
10637 * This CPU don't support us in finding the end of an
10638 * NMI-blocked window if the guest runs with IRQs
10639 * disabled. So we pull the trigger after 1 s of
10640 * futile waiting, but inform the user about this.
10641 */
10642 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
10643 "state on VCPU %d after 1 s timeout\n",
10644 __func__, vcpu->vcpu_id);
10645 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10646 }
10647 }
10648
Avi Kivity6aa8b732006-12-10 02:21:36 -080010649 if (exit_reason < kvm_vmx_max_exit_handlers
10650 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +030010651 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010652 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +010010653 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
10654 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +030010655 kvm_queue_exception(vcpu, UD_VECTOR);
10656 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010657 }
Avi Kivity6aa8b732006-12-10 02:21:36 -080010658}
10659
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010660/*
10661 * Software based L1D cache flush which is used when microcode providing
10662 * the cache control MSR is not loaded.
10663 *
10664 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
10665 * flush it is required to read in 64 KiB because the replacement algorithm
10666 * is not exactly LRU. This could be sized at runtime via topology
10667 * information but as all relevant affected CPUs have 32KiB L1D cache size
10668 * there is no point in doing so.
10669 */
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010670static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010671{
10672 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010673
10674 /*
Thomas Gleixner2f055942018-07-13 16:23:17 +020010675 * This code is only executed when the the flush mode is 'cond' or
10676 * 'always'
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010677 */
Nicolai Stange427362a2018-07-21 22:25:00 +020010678 if (static_branch_likely(&vmx_l1d_flush_cond)) {
Nicolai Stange45b575c2018-07-27 13:22:16 +020010679 bool flush_l1d;
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010680
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010681 /*
Nicolai Stange45b575c2018-07-27 13:22:16 +020010682 * Clear the per-vcpu flush bit, it gets set again
10683 * either from vcpu_run() or from one of the unsafe
10684 * VMEXIT handlers.
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010685 */
Nicolai Stange45b575c2018-07-27 13:22:16 +020010686 flush_l1d = vcpu->arch.l1tf_flush_l1d;
Thomas Gleixner4c6523e2018-07-13 16:23:20 +020010687 vcpu->arch.l1tf_flush_l1d = false;
Nicolai Stange45b575c2018-07-27 13:22:16 +020010688
10689 /*
10690 * Clear the per-cpu flush bit, it gets set again from
10691 * the interrupt handlers.
10692 */
10693 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
10694 kvm_clear_cpu_l1tf_flush_l1d();
10695
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010696 if (!flush_l1d)
10697 return;
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010698 }
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010699
10700 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010701
Paolo Bonzini3fa045b2018-07-02 13:03:48 +020010702 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
10703 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
10704 return;
10705 }
10706
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010707 asm volatile(
10708 /* First ensure the pages are in the TLB */
10709 "xorl %%eax, %%eax\n"
10710 ".Lpopulate_tlb:\n\t"
Nicolai Stange288d1522018-07-18 19:07:38 +020010711 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010712 "addl $4096, %%eax\n\t"
10713 "cmpl %%eax, %[size]\n\t"
10714 "jne .Lpopulate_tlb\n\t"
10715 "xorl %%eax, %%eax\n\t"
10716 "cpuid\n\t"
10717 /* Now fill the cache */
10718 "xorl %%eax, %%eax\n"
10719 ".Lfill_cache:\n"
Nicolai Stange288d1522018-07-18 19:07:38 +020010720 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010721 "addl $64, %%eax\n\t"
10722 "cmpl %%eax, %[size]\n\t"
10723 "jne .Lfill_cache\n\t"
10724 "lfence\n"
Nicolai Stange288d1522018-07-18 19:07:38 +020010725 :: [flush_pages] "r" (vmx_l1d_flush_pages),
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010726 [size] "r" (size)
10727 : "eax", "ebx", "ecx", "edx");
10728}
10729
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010730static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010731{
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010732 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10733
10734 if (is_guest_mode(vcpu) &&
10735 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10736 return;
10737
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010738 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010739 vmcs_write32(TPR_THRESHOLD, 0);
10740 return;
10741 }
10742
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010743 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010744}
10745
Jim Mattson8d860bb2018-05-09 16:56:05 -040010746static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +080010747{
10748 u32 sec_exec_control;
10749
Jim Mattson8d860bb2018-05-09 16:56:05 -040010750 if (!lapic_in_kernel(vcpu))
10751 return;
10752
Sean Christophersonfd6b6d92018-10-01 14:25:34 -070010753 if (!flexpriority_enabled &&
10754 !cpu_has_vmx_virtualize_x2apic_mode())
10755 return;
10756
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010757 /* Postpone execution until vmcs01 is the current VMCS. */
10758 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -040010759 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010760 return;
10761 }
10762
Yang Zhang8d146952013-01-25 10:18:50 +080010763 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -040010764 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10765 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +080010766
Jim Mattson8d860bb2018-05-09 16:56:05 -040010767 switch (kvm_get_apic_mode(vcpu)) {
10768 case LAPIC_MODE_INVALID:
10769 WARN_ONCE(true, "Invalid local APIC state");
10770 case LAPIC_MODE_DISABLED:
10771 break;
10772 case LAPIC_MODE_XAPIC:
10773 if (flexpriority_enabled) {
10774 sec_exec_control |=
10775 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
10776 vmx_flush_tlb(vcpu, true);
10777 }
10778 break;
10779 case LAPIC_MODE_X2APIC:
10780 if (cpu_has_vmx_virtualize_x2apic_mode())
10781 sec_exec_control |=
10782 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
10783 break;
Yang Zhang8d146952013-01-25 10:18:50 +080010784 }
10785 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
10786
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010787 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +080010788}
10789
Tang Chen38b99172014-09-24 15:57:54 +080010790static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
10791{
Jim Mattsonab5df312018-05-09 17:02:03 -040010792 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +080010793 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -070010794 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070010795 }
Tang Chen38b99172014-09-24 15:57:54 +080010796}
10797
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010798static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010799{
10800 u16 status;
10801 u8 old;
10802
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010803 if (max_isr == -1)
10804 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +080010805
10806 status = vmcs_read16(GUEST_INTR_STATUS);
10807 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010808 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +080010809 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010810 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +080010811 vmcs_write16(GUEST_INTR_STATUS, status);
10812 }
10813}
10814
10815static void vmx_set_rvi(int vector)
10816{
10817 u16 status;
10818 u8 old;
10819
Wei Wang4114c272014-11-05 10:53:43 +080010820 if (vector == -1)
10821 vector = 0;
10822
Yang Zhangc7c9c562013-01-25 10:18:51 +080010823 status = vmcs_read16(GUEST_INTR_STATUS);
10824 old = (u8)status & 0xff;
10825 if ((u8)vector != old) {
10826 status &= ~0xff;
10827 status |= (u8)vector;
10828 vmcs_write16(GUEST_INTR_STATUS, status);
10829 }
10830}
10831
10832static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
10833{
Liran Alon851c1a182017-12-24 18:12:56 +020010834 /*
10835 * When running L2, updating RVI is only relevant when
10836 * vmcs12 virtual-interrupt-delivery enabled.
10837 * However, it can be enabled only when L1 also
10838 * intercepts external-interrupts and in that case
10839 * we should not update vmcs02 RVI but instead intercept
10840 * interrupt. Therefore, do nothing when running L2.
10841 */
10842 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +080010843 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +080010844}
10845
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010846static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010847{
10848 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010849 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +020010850 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010851
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010852 WARN_ON(!vcpu->arch.apicv_active);
10853 if (pi_test_on(&vmx->pi_desc)) {
10854 pi_clear_on(&vmx->pi_desc);
10855 /*
10856 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
10857 * But on x86 this is just a compiler barrier anyway.
10858 */
10859 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +020010860 max_irr_updated =
10861 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
10862
10863 /*
10864 * If we are running L2 and L1 has a new pending interrupt
10865 * which can be injected, we should re-evaluate
10866 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +020010867 * If L1 intercepts external-interrupts, we should
10868 * exit from L2 to L1. Otherwise, interrupt should be
10869 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +020010870 */
Liran Alon851c1a182017-12-24 18:12:56 +020010871 if (is_guest_mode(vcpu) && max_irr_updated) {
10872 if (nested_exit_on_intr(vcpu))
10873 kvm_vcpu_exiting_guest_mode(vcpu);
10874 else
10875 kvm_make_request(KVM_REQ_EVENT, vcpu);
10876 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010877 } else {
10878 max_irr = kvm_lapic_find_highest_irr(vcpu);
10879 }
10880 vmx_hwapic_irr_update(vcpu, max_irr);
10881 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010882}
10883
Paolo Bonzini7e712682018-10-03 13:44:26 +020010884static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu)
10885{
10886 u8 rvi = vmx_get_rvi();
10887 u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI);
10888
10889 return ((rvi & 0xf0) > (vppr & 0xf0));
10890}
10891
Andrey Smetanin63086302015-11-10 15:36:32 +030010892static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010893{
Andrey Smetanind62caab2015-11-10 15:36:33 +030010894 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +080010895 return;
10896
Yang Zhangc7c9c562013-01-25 10:18:51 +080010897 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
10898 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
10899 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
10900 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
10901}
10902
Paolo Bonzini967235d2016-12-19 14:03:45 +010010903static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
10904{
10905 struct vcpu_vmx *vmx = to_vmx(vcpu);
10906
10907 pi_clear_on(&vmx->pi_desc);
10908 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
10909}
10910
Avi Kivity51aa01d2010-07-20 14:31:20 +030010911static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +030010912{
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010913 u32 exit_intr_info = 0;
10914 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +020010915
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010916 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
10917 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +020010918 return;
10919
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010920 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10921 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10922 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +080010923
Wanpeng Li1261bfa2017-07-13 18:30:40 -070010924 /* if exit due to PF check for async PF */
10925 if (is_page_fault(exit_intr_info))
10926 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
10927
Andi Kleena0861c02009-06-08 17:37:09 +080010928 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010929 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
10930 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +080010931 kvm_machine_check();
10932
Gleb Natapov20f65982009-05-11 13:35:55 +030010933 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -080010934 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -070010935 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +030010936 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -070010937 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080010938 }
Avi Kivity51aa01d2010-07-20 14:31:20 +030010939}
Gleb Natapov20f65982009-05-11 13:35:55 +030010940
Yang Zhanga547c6d2013-04-11 19:25:10 +080010941static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
10942{
10943 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10944
Yang Zhanga547c6d2013-04-11 19:25:10 +080010945 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
10946 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
10947 unsigned int vector;
10948 unsigned long entry;
10949 gate_desc *desc;
10950 struct vcpu_vmx *vmx = to_vmx(vcpu);
10951#ifdef CONFIG_X86_64
10952 unsigned long tmp;
10953#endif
10954
10955 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10956 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +020010957 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010958 asm volatile(
10959#ifdef CONFIG_X86_64
10960 "mov %%" _ASM_SP ", %[sp]\n\t"
10961 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
10962 "push $%c[ss]\n\t"
10963 "push %[sp]\n\t"
10964#endif
10965 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +080010966 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010967 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +080010968 :
10969#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -060010970 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010971#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -050010972 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +080010973 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010974 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010975 [ss]"i"(__KERNEL_DS),
10976 [cs]"i"(__KERNEL_CS)
10977 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +020010978 }
Yang Zhanga547c6d2013-04-11 19:25:10 +080010979}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010980STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010981
Tom Lendackybc226f02018-05-10 22:06:39 +020010982static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010983{
Tom Lendackybc226f02018-05-10 22:06:39 +020010984 switch (index) {
10985 case MSR_IA32_SMBASE:
10986 /*
10987 * We cannot do SMM unless we can run the guest in big
10988 * real mode.
10989 */
10990 return enable_unrestricted_guest || emulate_invalid_guest_state;
10991 case MSR_AMD64_VIRT_SPEC_CTRL:
10992 /* This is AMD only. */
10993 return false;
10994 default:
10995 return true;
10996 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010997}
10998
Liu, Jinsongda8999d2014-02-24 10:55:46 +000010999static bool vmx_mpx_supported(void)
11000{
11001 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
11002 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
11003}
11004
Wanpeng Li55412b22014-12-02 19:21:30 +080011005static bool vmx_xsaves_supported(void)
11006{
11007 return vmcs_config.cpu_based_2nd_exec_ctrl &
11008 SECONDARY_EXEC_XSAVES;
11009}
11010
Avi Kivity51aa01d2010-07-20 14:31:20 +030011011static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
11012{
Avi Kivityc5ca8e52011-03-07 17:37:37 +020011013 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +030011014 bool unblock_nmi;
11015 u8 vector;
11016 bool idtv_info_valid;
11017
11018 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +030011019
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010011020 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010011021 if (vmx->loaded_vmcs->nmi_known_unmasked)
11022 return;
11023 /*
11024 * Can't use vmx->exit_intr_info since we're not sure what
11025 * the exit reason is.
11026 */
11027 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
11028 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
11029 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
11030 /*
11031 * SDM 3: 27.7.1.2 (September 2008)
11032 * Re-set bit "block by NMI" before VM entry if vmexit caused by
11033 * a guest IRET fault.
11034 * SDM 3: 23.2.2 (September 2008)
11035 * Bit 12 is undefined in any of the following cases:
11036 * If the VM exit sets the valid bit in the IDT-vectoring
11037 * information field.
11038 * If the VM exit is due to a double fault.
11039 */
11040 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
11041 vector != DF_VECTOR && !idtv_info_valid)
11042 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
11043 GUEST_INTR_STATE_NMI);
11044 else
11045 vmx->loaded_vmcs->nmi_known_unmasked =
11046 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
11047 & GUEST_INTR_STATE_NMI);
11048 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
11049 vmx->loaded_vmcs->vnmi_blocked_time +=
11050 ktime_to_ns(ktime_sub(ktime_get(),
11051 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +030011052}
11053
Jan Kiszka3ab66e82013-02-20 14:03:24 +010011054static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +030011055 u32 idt_vectoring_info,
11056 int instr_len_field,
11057 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +030011058{
Avi Kivity51aa01d2010-07-20 14:31:20 +030011059 u8 vector;
11060 int type;
11061 bool idtv_info_valid;
11062
11063 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +030011064
Jan Kiszka3ab66e82013-02-20 14:03:24 +010011065 vcpu->arch.nmi_injected = false;
11066 kvm_clear_exception_queue(vcpu);
11067 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +030011068
11069 if (!idtv_info_valid)
11070 return;
11071
Jan Kiszka3ab66e82013-02-20 14:03:24 +010011072 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +030011073
Avi Kivity668f6122008-07-02 09:28:55 +030011074 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
11075 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +030011076
Gleb Natapov64a7ec02009-03-30 16:03:29 +030011077 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +030011078 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010011079 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +030011080 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +030011081 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +030011082 * Clear bit "block by NMI" before VM entry if a NMI
11083 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +030011084 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +010011085 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +030011086 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +030011087 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010011088 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030011089 /* fall through */
11090 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +030011091 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +030011092 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +030011093 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +030011094 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +030011095 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +030011096 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +030011097 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010011098 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030011099 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +030011100 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010011101 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +030011102 break;
11103 default:
11104 break;
Avi Kivityf7d92382008-07-03 16:14:28 +030011105 }
Avi Kivitycf393f72008-07-01 16:20:21 +030011106}
11107
Avi Kivity83422e12010-07-20 14:43:23 +030011108static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
11109{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010011110 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +030011111 VM_EXIT_INSTRUCTION_LEN,
11112 IDT_VECTORING_ERROR_CODE);
11113}
11114
Avi Kivityb463a6f2010-07-20 15:06:17 +030011115static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
11116{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010011117 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +030011118 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
11119 VM_ENTRY_INSTRUCTION_LEN,
11120 VM_ENTRY_EXCEPTION_ERROR_CODE);
11121
11122 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
11123}
11124
Gleb Natapovd7cd9792011-10-05 14:01:23 +020011125static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
11126{
11127 int i, nr_msrs;
11128 struct perf_guest_switch_msr *msrs;
11129
11130 msrs = perf_guest_get_msrs(&nr_msrs);
11131
11132 if (!msrs)
11133 return;
11134
11135 for (i = 0; i < nr_msrs; i++)
11136 if (msrs[i].host == msrs[i].guest)
11137 clear_atomic_switch_msr(vmx, msrs[i].msr);
11138 else
11139 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -040011140 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +020011141}
11142
Sean Christophersonf459a702018-08-27 15:21:11 -070011143static void vmx_arm_hv_timer(struct vcpu_vmx *vmx, u32 val)
11144{
11145 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, val);
11146 if (!vmx->loaded_vmcs->hv_timer_armed)
11147 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11148 PIN_BASED_VMX_PREEMPTION_TIMER);
11149 vmx->loaded_vmcs->hv_timer_armed = true;
11150}
11151
11152static void vmx_update_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -070011153{
11154 struct vcpu_vmx *vmx = to_vmx(vcpu);
11155 u64 tscl;
11156 u32 delta_tsc;
11157
Sean Christophersond264ee02018-08-27 15:21:12 -070011158 if (vmx->req_immediate_exit) {
11159 vmx_arm_hv_timer(vmx, 0);
11160 return;
11161 }
11162
Sean Christophersonf459a702018-08-27 15:21:11 -070011163 if (vmx->hv_deadline_tsc != -1) {
11164 tscl = rdtsc();
11165 if (vmx->hv_deadline_tsc > tscl)
11166 /* set_hv_timer ensures the delta fits in 32-bits */
11167 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
11168 cpu_preemption_timer_multi);
11169 else
11170 delta_tsc = 0;
11171
11172 vmx_arm_hv_timer(vmx, delta_tsc);
Yunhong Jiang64672c92016-06-13 14:19:59 -070011173 return;
Sean Christophersonf459a702018-08-27 15:21:11 -070011174 }
Yunhong Jiang64672c92016-06-13 14:19:59 -070011175
Sean Christophersonf459a702018-08-27 15:21:11 -070011176 if (vmx->loaded_vmcs->hv_timer_armed)
11177 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11178 PIN_BASED_VMX_PREEMPTION_TIMER);
11179 vmx->loaded_vmcs->hv_timer_armed = false;
Yunhong Jiang64672c92016-06-13 14:19:59 -070011180}
11181
Lai Jiangshana3b5ba42011-02-11 14:29:40 +080011182static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080011183{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040011184 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010011185 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020011186
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010011187 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010011188 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010011189 vmx->loaded_vmcs->soft_vnmi_blocked))
11190 vmx->loaded_vmcs->entry_time = ktime_get();
11191
Avi Kivity104f2262010-11-18 13:12:52 +020011192 /* Don't enter VMX if guest state is invalid, let the exit handler
11193 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020011194 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020011195 return;
11196
Radim Krčmářa7653ec2014-08-21 18:08:07 +020011197 if (vmx->ple_window_dirty) {
11198 vmx->ple_window_dirty = false;
11199 vmcs_write32(PLE_WINDOW, vmx->ple_window);
11200 }
11201
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020011202 if (vmx->nested.need_vmcs12_sync) {
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020011203 /*
11204 * hv_evmcs may end up being not mapped after migration (when
11205 * L2 was running), map it here to make sure vmcs12 changes are
11206 * properly reflected.
11207 */
11208 if (vmx->nested.enlightened_vmcs_enabled &&
11209 !vmx->nested.hv_evmcs)
11210 nested_vmx_handle_enlightened_vmptrld(vcpu, false);
11211
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020011212 if (vmx->nested.hv_evmcs) {
11213 copy_vmcs12_to_enlightened(vmx);
11214 /* All fields are clean */
11215 vmx->nested.hv_evmcs->hv_clean_fields |=
11216 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
11217 } else {
11218 copy_vmcs12_to_shadow(vmx);
11219 }
11220 vmx->nested.need_vmcs12_sync = false;
Abel Gordon012f83c2013-04-18 14:39:25 +030011221 }
11222
Avi Kivity104f2262010-11-18 13:12:52 +020011223 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
11224 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
11225 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
11226 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
11227
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070011228 cr3 = __get_current_cr3_fast();
Sean Christophersond7ee0392018-07-23 12:32:47 -070011229 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070011230 vmcs_writel(HOST_CR3, cr3);
Sean Christophersond7ee0392018-07-23 12:32:47 -070011231 vmx->loaded_vmcs->host_state.cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070011232 }
11233
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070011234 cr4 = cr4_read_shadow();
Sean Christophersond7ee0392018-07-23 12:32:47 -070011235 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070011236 vmcs_writel(HOST_CR4, cr4);
Sean Christophersond7ee0392018-07-23 12:32:47 -070011237 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070011238 }
11239
Avi Kivity104f2262010-11-18 13:12:52 +020011240 /* When single-stepping over STI and MOV SS, we must clear the
11241 * corresponding interruptibility bits in the guest state. Otherwise
11242 * vmentry fails as it then expects bit 14 (BS) in pending debug
11243 * exceptions being set, but that's not correct for the guest debugging
11244 * case. */
11245 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
11246 vmx_set_interrupt_shadow(vcpu, 0);
11247
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020011248 if (static_cpu_has(X86_FEATURE_PKU) &&
11249 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
11250 vcpu->arch.pkru != vmx->host_pkru)
11251 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080011252
Gleb Natapovd7cd9792011-10-05 14:01:23 +020011253 atomic_switch_perf_msrs(vmx);
11254
Sean Christophersonf459a702018-08-27 15:21:11 -070011255 vmx_update_hv_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -070011256
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011257 /*
11258 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
11259 * it's non-zero. Since vmentry is serialising on affected CPUs, there
11260 * is no need to worry about the conditional branch over the wrmsr
11261 * being speculatively taken.
11262 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020011263 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011264
Nadav Har'Eld462b812011-05-24 15:26:10 +030011265 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010011266
11267 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
11268 (unsigned long)&current_evmcs->host_rsp : 0;
11269
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020011270 if (static_branch_unlikely(&vmx_l1d_should_flush))
11271 vmx_l1d_flush(vcpu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +020011272
Avi Kivity104f2262010-11-18 13:12:52 +020011273 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080011274 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030011275 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
11276 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
11277 "push %%" _ASM_CX " \n\t"
11278 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +030011279 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030011280 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010011281 /* Avoid VMWRITE when Enlightened VMCS is in use */
11282 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
11283 "jz 2f \n\t"
11284 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
11285 "jmp 1f \n\t"
11286 "2: \n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +020011287 __ex("vmwrite %%" _ASM_SP ", %%" _ASM_DX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +030011288 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030011289 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030011290 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
11291 "mov %%cr2, %%" _ASM_DX " \n\t"
11292 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010011293 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030011294 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010011295 "3: \n\t"
Uros Bizjak00df9182018-10-23 00:09:11 +020011296 /* Check if vmlaunch or vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020011297 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080011298 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030011299 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
11300 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
11301 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
11302 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
11303 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
11304 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080011305#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020011306 "mov %c[r8](%0), %%r8 \n\t"
11307 "mov %c[r9](%0), %%r9 \n\t"
11308 "mov %c[r10](%0), %%r10 \n\t"
11309 "mov %c[r11](%0), %%r11 \n\t"
11310 "mov %c[r12](%0), %%r12 \n\t"
11311 "mov %c[r13](%0), %%r13 \n\t"
11312 "mov %c[r14](%0), %%r14 \n\t"
11313 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080011314#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030011315 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030011316
Avi Kivity6aa8b732006-12-10 02:21:36 -080011317 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030011318 "jne 1f \n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +020011319 __ex("vmlaunch") "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030011320 "jmp 2f \n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +020011321 "1: " __ex("vmresume") "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030011322 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080011323 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030011324 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020011325 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080011326 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030011327 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
11328 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
11329 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
11330 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
11331 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
11332 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
11333 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080011334#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020011335 "mov %%r8, %c[r8](%0) \n\t"
11336 "mov %%r9, %c[r9](%0) \n\t"
11337 "mov %%r10, %c[r10](%0) \n\t"
11338 "mov %%r11, %c[r11](%0) \n\t"
11339 "mov %%r12, %c[r12](%0) \n\t"
11340 "mov %%r13, %c[r13](%0) \n\t"
11341 "mov %%r14, %c[r14](%0) \n\t"
11342 "mov %%r15, %c[r15](%0) \n\t"
Uros Bizjak43ce76c2018-10-17 16:46:57 +020011343 /*
11344 * Clear host registers marked as clobbered to prevent
11345 * speculative use.
11346 */
Jim Mattson0cb5b302018-01-03 14:31:38 -080011347 "xor %%r8d, %%r8d \n\t"
11348 "xor %%r9d, %%r9d \n\t"
11349 "xor %%r10d, %%r10d \n\t"
11350 "xor %%r11d, %%r11d \n\t"
11351 "xor %%r12d, %%r12d \n\t"
11352 "xor %%r13d, %%r13d \n\t"
11353 "xor %%r14d, %%r14d \n\t"
11354 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080011355#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030011356 "mov %%cr2, %%" _ASM_AX " \n\t"
11357 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030011358
Jim Mattson0cb5b302018-01-03 14:31:38 -080011359 "xor %%eax, %%eax \n\t"
11360 "xor %%ebx, %%ebx \n\t"
11361 "xor %%esi, %%esi \n\t"
11362 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030011363 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030011364 ".pushsection .rodata \n\t"
11365 ".global vmx_return \n\t"
11366 "vmx_return: " _ASM_PTR " 2b \n\t"
11367 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010011368 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030011369 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020011370 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +030011371 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080011372 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
11373 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
11374 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
11375 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
11376 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
11377 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
11378 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080011379#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080011380 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
11381 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
11382 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
11383 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
11384 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
11385 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
11386 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
11387 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080011388#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020011389 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
11390 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020011391 : "cc", "memory"
11392#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010011393 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020011394 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030011395#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010011396 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020011397#endif
11398 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080011399
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011400 /*
11401 * We do not use IBRS in the kernel. If this vCPU has used the
11402 * SPEC_CTRL MSR it may have left it on; save the value and
11403 * turn it off. This is much more efficient than blindly adding
11404 * it to the atomic save/restore list. Especially as the former
11405 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
11406 *
11407 * For non-nested case:
11408 * If the L01 MSR bitmap does not intercept the MSR, then we need to
11409 * save it.
11410 *
11411 * For nested case:
11412 * If the L02 MSR bitmap does not intercept the MSR, then we need to
11413 * save it.
11414 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010011415 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010011416 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011417
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020011418 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011419
David Woodhouse117cc7a2018-01-12 11:11:27 +000011420 /* Eliminate branch target predictions from guest mode */
11421 vmexit_fill_RSB();
11422
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010011423 /* All fields are clean at this point */
11424 if (static_branch_unlikely(&enable_evmcs))
11425 current_evmcs->hv_clean_fields |=
11426 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
11427
Gleb Natapov2a7921b2012-08-12 16:12:29 +030011428 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080011429 if (vmx->host_debugctlmsr)
11430 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030011431
Avi Kivityaa67f602012-08-01 16:48:03 +030011432#ifndef CONFIG_X86_64
11433 /*
11434 * The sysexit path does not restore ds/es, so we must set them to
11435 * a reasonable value ourselves.
11436 *
Sean Christopherson6d6095b2018-07-23 12:32:44 -070011437 * We can't defer this to vmx_prepare_switch_to_host() since that
11438 * function may be executed in interrupt context, which saves and
11439 * restore segments around it, nullifying its effect.
Avi Kivityaa67f602012-08-01 16:48:03 +030011440 */
11441 loadsegment(ds, __USER_DS);
11442 loadsegment(es, __USER_DS);
11443#endif
11444
Avi Kivity6de4f3a2009-05-31 22:58:47 +030011445 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020011446 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020011447 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030011448 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020011449 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030011450 vcpu->arch.regs_dirty = 0;
11451
Gleb Natapove0b890d2013-09-25 12:51:33 +030011452 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080011453 * eager fpu is enabled if PKEY is supported and CR4 is switched
11454 * back on host, so it is safe to read guest PKRU from current
11455 * XSAVE.
11456 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020011457 if (static_cpu_has(X86_FEATURE_PKU) &&
11458 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
11459 vcpu->arch.pkru = __read_pkru();
11460 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080011461 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080011462 }
11463
Gleb Natapove0b890d2013-09-25 12:51:33 +030011464 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070011465 vmx->idt_vectoring_info = 0;
11466
11467 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
11468 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
11469 return;
11470
11471 vmx->loaded_vmcs->launched = 1;
11472 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030011473
Avi Kivity51aa01d2010-07-20 14:31:20 +030011474 vmx_complete_atomic_exit(vmx);
11475 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030011476 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080011477}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050011478STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080011479
Sean Christopherson434a1e92018-03-20 12:17:18 -070011480static struct kvm *vmx_vm_alloc(void)
11481{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070011482 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070011483 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070011484}
11485
11486static void vmx_vm_free(struct kvm *kvm)
11487{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070011488 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070011489}
11490
David Hildenbrand1279a6b12017-03-20 10:00:08 +010011491static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020011492{
11493 struct vcpu_vmx *vmx = to_vmx(vcpu);
11494 int cpu;
11495
David Hildenbrand1279a6b12017-03-20 10:00:08 +010011496 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020011497 return;
11498
11499 cpu = get_cpu();
Paolo Bonzini4fa77342014-07-17 12:25:16 +020011500 vmx_vcpu_put(vcpu);
Sean Christophersonbd9966d2018-07-23 12:32:42 -070011501 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020011502 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020011503 put_cpu();
Sean Christophersonb7031fd2018-09-26 09:23:42 -070011504
11505 vm_entry_controls_reset_shadow(vmx);
11506 vm_exit_controls_reset_shadow(vmx);
11507 vmx_segment_cache_clear(vmx);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020011508}
11509
Jim Mattson2f1fe812016-07-08 15:36:06 -070011510/*
11511 * Ensure that the current vmcs of the logical processor is the
11512 * vmcs01 of the vcpu before calling free_nested().
11513 */
11514static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
11515{
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +020011516 vcpu_load(vcpu);
11517 vmx_switch_vmcs(vcpu, &to_vmx(vcpu)->vmcs01);
11518 free_nested(vcpu);
11519 vcpu_put(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070011520}
11521
Avi Kivity6aa8b732006-12-10 02:21:36 -080011522static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
11523{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011524 struct vcpu_vmx *vmx = to_vmx(vcpu);
11525
Kai Huang843e4332015-01-28 10:54:28 +080011526 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080011527 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080011528 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020011529 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070011530 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020011531 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011532 kfree(vmx->guest_msrs);
11533 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100011534 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080011535}
11536
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011537static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080011538{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011539 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100011540 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011541 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030011542 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080011543
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040011544 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011545 return ERR_PTR(-ENOMEM);
11546
Wanpeng Li991e7a02015-09-16 17:30:05 +080011547 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080011548
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011549 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
11550 if (err)
11551 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011552
Peter Feiner4e595162016-07-07 14:49:58 -070011553 err = -ENOMEM;
11554
11555 /*
11556 * If PML is turned on, failure on enabling PML just results in failure
11557 * of creating the vcpu, therefore we can simplify PML logic (by
11558 * avoiding dealing with cases, such as enabling PML partially on vcpus
11559 * for the guest, etc.
11560 */
11561 if (enable_pml) {
11562 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
11563 if (!vmx->pml_pg)
11564 goto uninit_vcpu;
11565 }
11566
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040011567 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020011568 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
11569 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030011570
Peter Feiner4e595162016-07-07 14:49:58 -070011571 if (!vmx->guest_msrs)
11572 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011573
Paolo Bonzinif21f1652018-01-11 12:16:15 +010011574 err = alloc_loaded_vmcs(&vmx->vmcs01);
11575 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011576 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040011577
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011578 msr_bitmap = vmx->vmcs01.msr_bitmap;
11579 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
11580 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
11581 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
11582 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
11583 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
11584 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
11585 vmx->msr_bitmap_mode = 0;
11586
Paolo Bonzinif21f1652018-01-11 12:16:15 +010011587 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030011588 cpu = get_cpu();
11589 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100011590 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020011591 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011592 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030011593 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020011594 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020011595 err = alloc_apic_access_page(kvm);
11596 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020011597 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020011598 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080011599
Sean Christophersone90008d2018-03-05 12:04:37 -080011600 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080011601 err = init_rmode_identity_map(kvm);
11602 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020011603 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080011604 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080011605
Roman Kagan63aff652018-07-19 21:59:07 +030011606 if (nested)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011607 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
11608 kvm_vcpu_apicv_active(&vmx->vcpu));
Wincy Vanb9c237b2015-02-03 23:56:30 +080011609
Wincy Van705699a2015-02-03 23:58:17 +080011610 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030011611 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030011612
Haozhong Zhang37e4c992016-06-22 14:59:55 +080011613 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
11614
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020011615 /*
11616 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
11617 * or POSTED_INTR_WAKEUP_VECTOR.
11618 */
11619 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
11620 vmx->pi_desc.sn = 1;
11621
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011622 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011623
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011624free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080011625 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011626free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011627 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070011628free_pml:
11629 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011630uninit_vcpu:
11631 kvm_vcpu_uninit(&vmx->vcpu);
11632free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080011633 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100011634 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011635 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080011636}
11637
Jiri Kosinad90a7a02018-07-13 16:23:25 +020011638#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"
11639#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 -040011640
Wanpeng Lib31c1142018-03-12 04:53:04 -070011641static int vmx_vm_init(struct kvm *kvm)
11642{
Tianyu Lan877ad952018-07-19 08:40:23 +000011643 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
11644
Wanpeng Lib31c1142018-03-12 04:53:04 -070011645 if (!ple_gap)
11646 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011647
Jiri Kosinad90a7a02018-07-13 16:23:25 +020011648 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
11649 switch (l1tf_mitigation) {
11650 case L1TF_MITIGATION_OFF:
11651 case L1TF_MITIGATION_FLUSH_NOWARN:
11652 /* 'I explicitly don't care' is set */
11653 break;
11654 case L1TF_MITIGATION_FLUSH:
11655 case L1TF_MITIGATION_FLUSH_NOSMT:
11656 case L1TF_MITIGATION_FULL:
11657 /*
11658 * Warn upon starting the first VM in a potentially
11659 * insecure environment.
11660 */
11661 if (cpu_smt_control == CPU_SMT_ENABLED)
11662 pr_warn_once(L1TF_MSG_SMT);
11663 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
11664 pr_warn_once(L1TF_MSG_L1D);
11665 break;
11666 case L1TF_MITIGATION_FULL_FORCE:
11667 /* Flush is enforced */
11668 break;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011669 }
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011670 }
Wanpeng Lib31c1142018-03-12 04:53:04 -070011671 return 0;
11672}
11673
Yang, Sheng002c7f72007-07-31 14:23:01 +030011674static void __init vmx_check_processor_compat(void *rtn)
11675{
11676 struct vmcs_config vmcs_conf;
11677
11678 *(int *)rtn = 0;
11679 if (setup_vmcs_config(&vmcs_conf) < 0)
11680 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010011681 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030011682 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
11683 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
11684 smp_processor_id());
11685 *(int *)rtn = -EIO;
11686 }
11687}
11688
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011689static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080011690{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011691 u8 cache;
11692 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011693
Sheng Yang522c68c2009-04-27 20:35:43 +080011694 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020011695 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080011696 * 2. EPT with VT-d:
11697 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020011698 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080011699 * b. VT-d with snooping control feature: snooping control feature of
11700 * VT-d engine can guarantee the cache correctness. Just set it
11701 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080011702 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080011703 * consistent with host MTRR
11704 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020011705 if (is_mmio) {
11706 cache = MTRR_TYPE_UNCACHABLE;
11707 goto exit;
11708 }
11709
11710 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011711 ipat = VMX_EPT_IPAT_BIT;
11712 cache = MTRR_TYPE_WRBACK;
11713 goto exit;
11714 }
11715
11716 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
11717 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020011718 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080011719 cache = MTRR_TYPE_WRBACK;
11720 else
11721 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011722 goto exit;
11723 }
11724
Xiao Guangrongff536042015-06-15 16:55:22 +080011725 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011726
11727exit:
11728 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080011729}
11730
Sheng Yang17cc3932010-01-05 19:02:27 +080011731static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020011732{
Sheng Yang878403b2010-01-05 19:02:29 +080011733 if (enable_ept && !cpu_has_vmx_ept_1g_page())
11734 return PT_DIRECTORY_LEVEL;
11735 else
11736 /* For shadow and EPT supported 1GB page */
11737 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020011738}
11739
Xiao Guangrongfeda8052015-09-09 14:05:55 +080011740static void vmcs_set_secondary_exec_control(u32 new_ctl)
11741{
11742 /*
11743 * These bits in the secondary execution controls field
11744 * are dynamic, the others are mostly based on the hypervisor
11745 * architecture and the guest's CPUID. Do not touch the
11746 * dynamic bits.
11747 */
11748 u32 mask =
11749 SECONDARY_EXEC_SHADOW_VMCS |
11750 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020011751 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
11752 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080011753
11754 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
11755
11756 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
11757 (new_ctl & ~mask) | (cur_ctl & mask));
11758}
11759
David Matlack8322ebb2016-11-29 18:14:09 -080011760/*
11761 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
11762 * (indicating "allowed-1") if they are supported in the guest's CPUID.
11763 */
11764static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
11765{
11766 struct vcpu_vmx *vmx = to_vmx(vcpu);
11767 struct kvm_cpuid_entry2 *entry;
11768
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011769 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
11770 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080011771
11772#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
11773 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011774 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080011775} while (0)
11776
11777 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
11778 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
11779 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
11780 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
11781 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
11782 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
11783 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
11784 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
11785 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
11786 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
11787 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
11788 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
11789 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
11790 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
11791 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
11792
11793 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
11794 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
11795 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
11796 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
11797 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010011798 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080011799
11800#undef cr4_fixed1_update
11801}
11802
Liran Alon5f76f6f2018-09-14 03:25:52 +030011803static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
11804{
11805 struct vcpu_vmx *vmx = to_vmx(vcpu);
11806
11807 if (kvm_mpx_supported()) {
11808 bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX);
11809
11810 if (mpx_enabled) {
11811 vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
11812 vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
11813 } else {
11814 vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS;
11815 vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS;
11816 }
11817 }
11818}
11819
Sheng Yang0e851882009-12-18 16:48:46 +080011820static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
11821{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011822 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011823
Paolo Bonzini80154d72017-08-24 13:55:35 +020011824 if (cpu_has_secondary_exec_ctrls()) {
11825 vmx_compute_secondary_exec_control(vmx);
11826 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011827 }
Mao, Junjiead756a12012-07-02 01:18:48 +000011828
Haozhong Zhang37e4c992016-06-22 14:59:55 +080011829 if (nested_vmx_allowed(vcpu))
11830 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11831 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
11832 else
11833 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11834 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080011835
Liran Alon5f76f6f2018-09-14 03:25:52 +030011836 if (nested_vmx_allowed(vcpu)) {
David Matlack8322ebb2016-11-29 18:14:09 -080011837 nested_vmx_cr_fixed1_bits_update(vcpu);
Liran Alon5f76f6f2018-09-14 03:25:52 +030011838 nested_vmx_entry_exit_ctls_update(vcpu);
11839 }
Sheng Yang0e851882009-12-18 16:48:46 +080011840}
11841
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011842static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
11843{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030011844 if (func == 1 && nested)
11845 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011846}
11847
Yang Zhang25d92082013-08-06 12:00:32 +030011848static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
11849 struct x86_exception *fault)
11850{
Jan Kiszka533558b2014-01-04 18:47:20 +010011851 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040011852 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010011853 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011854 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030011855
Bandan Dasc5f983f2017-05-05 15:25:14 -040011856 if (vmx->nested.pml_full) {
11857 exit_reason = EXIT_REASON_PML_FULL;
11858 vmx->nested.pml_full = false;
11859 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
11860 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010011861 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030011862 else
Jan Kiszka533558b2014-01-04 18:47:20 +010011863 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011864
11865 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030011866 vmcs12->guest_physical_address = fault->address;
11867}
11868
Peter Feiner995f00a2017-06-30 17:26:32 -070011869static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
11870{
David Hildenbrandbb97a012017-08-10 23:15:28 +020011871 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070011872}
11873
Nadav Har'El155a97a2013-08-05 11:07:16 +030011874/* Callbacks for nested_ept_init_mmu_context: */
11875
11876static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
11877{
11878 /* return the page table to be shadowed - in our case, EPT12 */
11879 return get_vmcs12(vcpu)->ept_pointer;
11880}
11881
Sean Christopherson5b8ba412018-09-26 09:23:40 -070011882static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030011883{
Paolo Bonziniad896af2013-10-02 16:56:14 +020011884 WARN_ON(mmu_is_nested(vcpu));
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011885
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +020011886 vcpu->arch.mmu = &vcpu->arch.guest_mmu;
Paolo Bonziniad896af2013-10-02 16:56:14 +020011887 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011888 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011889 VMX_EPT_EXECUTE_ONLY_BIT,
Junaid Shahid50c28f22018-06-27 14:59:11 -070011890 nested_ept_ad_enabled(vcpu),
11891 nested_ept_get_cr3(vcpu));
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020011892 vcpu->arch.mmu->set_cr3 = vmx_set_cr3;
11893 vcpu->arch.mmu->get_cr3 = nested_ept_get_cr3;
11894 vcpu->arch.mmu->inject_page_fault = nested_ept_inject_page_fault;
Vitaly Kuznetsov3dc773e2018-10-08 21:28:06 +020011895 vcpu->arch.mmu->get_pdptr = kvm_pdptr_read;
Nadav Har'El155a97a2013-08-05 11:07:16 +030011896
11897 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +030011898}
11899
11900static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
11901{
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +020011902 vcpu->arch.mmu = &vcpu->arch.root_mmu;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020011903 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +030011904}
11905
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030011906static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
11907 u16 error_code)
11908{
11909 bool inequality, bit;
11910
11911 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
11912 inequality =
11913 (error_code & vmcs12->page_fault_error_code_mask) !=
11914 vmcs12->page_fault_error_code_match;
11915 return inequality ^ bit;
11916}
11917
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011918static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
11919 struct x86_exception *fault)
11920{
11921 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11922
11923 WARN_ON(!is_guest_mode(vcpu));
11924
Wanpeng Li305d0ab2017-09-28 18:16:44 -070011925 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
11926 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020011927 vmcs12->vm_exit_intr_error_code = fault->error_code;
11928 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11929 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
11930 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
11931 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020011932 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011933 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020011934 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011935}
11936
Paolo Bonzinic9923842017-12-13 14:16:30 +010011937static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11938 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011939
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011940static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011941{
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011942 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011943 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011944 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011945 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011946
11947 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011948 /*
11949 * Translate L1 physical address to host physical
11950 * address for vmcs02. Keep the page pinned, so this
11951 * physical address remains valid. We keep a reference
11952 * to it so we can release it later.
11953 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011954 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011955 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011956 vmx->nested.apic_access_page = NULL;
11957 }
11958 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011959 /*
11960 * If translation failed, no matter: This feature asks
11961 * to exit when accessing the given address, and if it
11962 * can never be accessed, this feature won't do
11963 * anything anyway.
11964 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011965 if (!is_error_page(page)) {
11966 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011967 hpa = page_to_phys(vmx->nested.apic_access_page);
11968 vmcs_write64(APIC_ACCESS_ADDR, hpa);
11969 } else {
11970 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
11971 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
11972 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011973 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011974
11975 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011976 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011977 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011978 vmx->nested.virtual_apic_page = NULL;
11979 }
11980 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011981
11982 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011983 * If translation failed, VM entry will fail because
11984 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
11985 * Failing the vm entry is _not_ what the processor
11986 * does but it's basically the only possibility we
11987 * have. We could still enter the guest if CR8 load
11988 * exits are enabled, CR8 store exits are enabled, and
11989 * virtualize APIC access is disabled; in this case
11990 * the processor would never use the TPR shadow and we
11991 * could simply clear the bit from the execution
11992 * control. But such a configuration is useless, so
11993 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011994 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011995 if (!is_error_page(page)) {
11996 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011997 hpa = page_to_phys(vmx->nested.virtual_apic_page);
11998 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
11999 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012000 }
12001
Wincy Van705699a2015-02-03 23:58:17 +080012002 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080012003 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
12004 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012005 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020012006 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080012007 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020012008 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
12009 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012010 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020012011 vmx->nested.pi_desc_page = page;
12012 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080012013 vmx->nested.pi_desc =
12014 (struct pi_desc *)((void *)vmx->nested.pi_desc +
12015 (unsigned long)(vmcs12->posted_intr_desc_addr &
12016 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012017 vmcs_write64(POSTED_INTR_DESC_ADDR,
12018 page_to_phys(vmx->nested.pi_desc_page) +
12019 (unsigned long)(vmcs12->posted_intr_desc_addr &
12020 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080012021 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080012022 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000012023 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
12024 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012025 else
12026 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
12027 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080012028}
12029
Jan Kiszkaf41245002014-03-07 20:03:13 +010012030static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
12031{
12032 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
12033 struct vcpu_vmx *vmx = to_vmx(vcpu);
12034
Sean Christopherson4c008122018-08-27 15:21:10 -070012035 /*
12036 * A timer value of zero is architecturally guaranteed to cause
12037 * a VMExit prior to executing any instructions in the guest.
12038 */
12039 if (preemption_timeout == 0) {
Jan Kiszkaf41245002014-03-07 20:03:13 +010012040 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
12041 return;
12042 }
12043
Sean Christopherson4c008122018-08-27 15:21:10 -070012044 if (vcpu->arch.virtual_tsc_khz == 0)
12045 return;
12046
Jan Kiszkaf41245002014-03-07 20:03:13 +010012047 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
12048 preemption_timeout *= 1000000;
12049 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
12050 hrtimer_start(&vmx->nested.preemption_timer,
12051 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
12052}
12053
Jim Mattson56a20512017-07-06 16:33:06 -070012054static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
12055 struct vmcs12 *vmcs12)
12056{
12057 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
12058 return 0;
12059
12060 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
12061 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
12062 return -EINVAL;
12063
12064 return 0;
12065}
12066
Wincy Van3af18d92015-02-03 23:49:31 +080012067static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
12068 struct vmcs12 *vmcs12)
12069{
Wincy Van3af18d92015-02-03 23:49:31 +080012070 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
12071 return 0;
12072
Jim Mattson5fa99cb2017-07-06 16:33:07 -070012073 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080012074 return -EINVAL;
12075
12076 return 0;
12077}
12078
Jim Mattson712b12d2017-08-24 13:24:47 -070012079static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
12080 struct vmcs12 *vmcs12)
12081{
12082 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
12083 return 0;
12084
12085 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
12086 return -EINVAL;
12087
12088 return 0;
12089}
12090
Wincy Van3af18d92015-02-03 23:49:31 +080012091/*
12092 * Merge L0's and L1's MSR bitmap, return false to indicate that
12093 * we do not use the hardware.
12094 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010012095static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
12096 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080012097{
Wincy Van82f0dd42015-02-03 23:57:18 +080012098 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080012099 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020012100 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010012101 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010012102 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010012103 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010012104 *
12105 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
12106 * ensures that we do not accidentally generate an L02 MSR bitmap
12107 * from the L12 MSR bitmap that is too permissive.
12108 * 2. That L1 or L2s have actually used the MSR. This avoids
12109 * unnecessarily merging of the bitmap if the MSR is unused. This
12110 * works properly because we only update the L01 MSR bitmap lazily.
12111 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
12112 * updated to reflect this when L1 (or its L2s) actually write to
12113 * the MSR.
12114 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000012115 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
12116 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080012117
Paolo Bonzinic9923842017-12-13 14:16:30 +010012118 /* Nothing to do if the MSR bitmap is not in use. */
12119 if (!cpu_has_vmx_msr_bitmap() ||
12120 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
12121 return false;
12122
Ashok Raj15d45072018-02-01 22:59:43 +010012123 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010012124 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080012125 return false;
12126
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020012127 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
12128 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080012129 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010012130
Radim Krčmářd048c092016-08-08 20:16:22 +020012131 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010012132 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
12133 /*
12134 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
12135 * just lets the processor take the value from the virtual-APIC page;
12136 * take those 256 bits directly from the L1 bitmap.
12137 */
12138 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
12139 unsigned word = msr / BITS_PER_LONG;
12140 msr_bitmap_l0[word] = msr_bitmap_l1[word];
12141 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080012142 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010012143 } else {
12144 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
12145 unsigned word = msr / BITS_PER_LONG;
12146 msr_bitmap_l0[word] = ~0;
12147 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
12148 }
12149 }
12150
12151 nested_vmx_disable_intercept_for_msr(
12152 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010012153 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010012154 MSR_TYPE_W);
12155
12156 if (nested_cpu_has_vid(vmcs12)) {
12157 nested_vmx_disable_intercept_for_msr(
12158 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010012159 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010012160 MSR_TYPE_W);
12161 nested_vmx_disable_intercept_for_msr(
12162 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010012163 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010012164 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080012165 }
Ashok Raj15d45072018-02-01 22:59:43 +010012166
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010012167 if (spec_ctrl)
12168 nested_vmx_disable_intercept_for_msr(
12169 msr_bitmap_l1, msr_bitmap_l0,
12170 MSR_IA32_SPEC_CTRL,
12171 MSR_TYPE_R | MSR_TYPE_W);
12172
Ashok Raj15d45072018-02-01 22:59:43 +010012173 if (pred_cmd)
12174 nested_vmx_disable_intercept_for_msr(
12175 msr_bitmap_l1, msr_bitmap_l0,
12176 MSR_IA32_PRED_CMD,
12177 MSR_TYPE_W);
12178
Wincy Vanf2b93282015-02-03 23:56:03 +080012179 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012180 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080012181
12182 return true;
12183}
12184
Liran Alon61ada742018-06-23 02:35:08 +030012185static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
12186 struct vmcs12 *vmcs12)
12187{
12188 struct vmcs12 *shadow;
12189 struct page *page;
12190
12191 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
12192 vmcs12->vmcs_link_pointer == -1ull)
12193 return;
12194
12195 shadow = get_shadow_vmcs12(vcpu);
12196 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
12197
12198 memcpy(shadow, kmap(page), VMCS12_SIZE);
12199
12200 kunmap(page);
12201 kvm_release_page_clean(page);
12202}
12203
12204static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
12205 struct vmcs12 *vmcs12)
12206{
12207 struct vcpu_vmx *vmx = to_vmx(vcpu);
12208
12209 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
12210 vmcs12->vmcs_link_pointer == -1ull)
12211 return;
12212
12213 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
12214 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
12215}
12216
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040012217static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
12218 struct vmcs12 *vmcs12)
12219{
12220 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
12221 !page_address_valid(vcpu, vmcs12->apic_access_addr))
12222 return -EINVAL;
12223 else
12224 return 0;
12225}
12226
Wincy Vanf2b93282015-02-03 23:56:03 +080012227static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
12228 struct vmcs12 *vmcs12)
12229{
Wincy Van82f0dd42015-02-03 23:57:18 +080012230 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080012231 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080012232 !nested_cpu_has_vid(vmcs12) &&
12233 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080012234 return 0;
12235
12236 /*
12237 * If virtualize x2apic mode is enabled,
12238 * virtualize apic access must be disabled.
12239 */
Wincy Van82f0dd42015-02-03 23:57:18 +080012240 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
12241 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080012242 return -EINVAL;
12243
Wincy Van608406e2015-02-03 23:57:51 +080012244 /*
12245 * If virtual interrupt delivery is enabled,
12246 * we must exit on external interrupts.
12247 */
12248 if (nested_cpu_has_vid(vmcs12) &&
12249 !nested_exit_on_intr(vcpu))
12250 return -EINVAL;
12251
Wincy Van705699a2015-02-03 23:58:17 +080012252 /*
12253 * bits 15:8 should be zero in posted_intr_nv,
12254 * the descriptor address has been already checked
12255 * in nested_get_vmcs12_pages.
Krish Sadhukhan6de84e52018-08-23 20:03:03 -040012256 *
12257 * bits 5:0 of posted_intr_desc_addr should be zero.
Wincy Van705699a2015-02-03 23:58:17 +080012258 */
12259 if (nested_cpu_has_posted_intr(vmcs12) &&
12260 (!nested_cpu_has_vid(vmcs12) ||
12261 !nested_exit_intr_ack_set(vcpu) ||
Krish Sadhukhan6de84e52018-08-23 20:03:03 -040012262 (vmcs12->posted_intr_nv & 0xff00) ||
12263 (vmcs12->posted_intr_desc_addr & 0x3f) ||
KarimAllah Ahmed22a7cdc2018-10-20 23:42:59 +020012264 (vmcs12->posted_intr_desc_addr >> cpuid_maxphyaddr(vcpu))))
Wincy Van705699a2015-02-03 23:58:17 +080012265 return -EINVAL;
12266
Wincy Vanf2b93282015-02-03 23:56:03 +080012267 /* tpr shadow is needed by all apicv features. */
12268 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
12269 return -EINVAL;
12270
12271 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080012272}
12273
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012274static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
12275 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030012276 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030012277{
Liran Alone2536742018-06-23 02:35:02 +030012278 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030012279 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012280 u64 count, addr;
12281
Liran Alone2536742018-06-23 02:35:02 +030012282 if (vmcs12_read_any(vmcs12, count_field, &count) ||
12283 vmcs12_read_any(vmcs12, addr_field, &addr)) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012284 WARN_ON(1);
12285 return -EINVAL;
12286 }
12287 if (count == 0)
12288 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030012289 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012290 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
12291 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020012292 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012293 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
12294 addr_field, maxphyaddr, count, addr);
12295 return -EINVAL;
12296 }
12297 return 0;
12298}
12299
12300static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
12301 struct vmcs12 *vmcs12)
12302{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012303 if (vmcs12->vm_exit_msr_load_count == 0 &&
12304 vmcs12->vm_exit_msr_store_count == 0 &&
12305 vmcs12->vm_entry_msr_load_count == 0)
12306 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012307 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030012308 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012309 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030012310 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012311 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030012312 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030012313 return -EINVAL;
12314 return 0;
12315}
12316
Bandan Dasc5f983f2017-05-05 15:25:14 -040012317static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
12318 struct vmcs12 *vmcs12)
12319{
Krish Sadhukhan55c1dcd2018-09-27 14:33:27 -040012320 if (!nested_cpu_has_pml(vmcs12))
12321 return 0;
Bandan Dasc5f983f2017-05-05 15:25:14 -040012322
Krish Sadhukhan55c1dcd2018-09-27 14:33:27 -040012323 if (!nested_cpu_has_ept(vmcs12) ||
12324 !page_address_valid(vcpu, vmcs12->pml_address))
12325 return -EINVAL;
Bandan Dasc5f983f2017-05-05 15:25:14 -040012326
12327 return 0;
12328}
12329
Jim Mattson886560402018-09-24 11:05:43 -070012330static int nested_vmx_check_unrestricted_guest_controls(struct kvm_vcpu *vcpu,
12331 struct vmcs12 *vmcs12)
12332{
12333 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST) &&
12334 !nested_cpu_has_ept(vmcs12))
12335 return -EINVAL;
12336 return 0;
12337}
12338
12339static int nested_vmx_check_mode_based_ept_exec_controls(struct kvm_vcpu *vcpu,
12340 struct vmcs12 *vmcs12)
12341{
12342 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_MODE_BASED_EPT_EXEC) &&
12343 !nested_cpu_has_ept(vmcs12))
12344 return -EINVAL;
12345 return 0;
12346}
12347
Liran Alona8a7c022018-06-23 02:35:06 +030012348static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
12349 struct vmcs12 *vmcs12)
12350{
12351 if (!nested_cpu_has_shadow_vmcs(vmcs12))
12352 return 0;
12353
12354 if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
12355 !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
12356 return -EINVAL;
12357
12358 return 0;
12359}
12360
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012361static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
12362 struct vmx_msr_entry *e)
12363{
12364 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020012365 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012366 return -EINVAL;
12367 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
12368 e->index == MSR_IA32_UCODE_REV)
12369 return -EINVAL;
12370 if (e->reserved != 0)
12371 return -EINVAL;
12372 return 0;
12373}
12374
12375static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
12376 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030012377{
12378 if (e->index == MSR_FS_BASE ||
12379 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012380 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
12381 nested_vmx_msr_check_common(vcpu, e))
12382 return -EINVAL;
12383 return 0;
12384}
12385
12386static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
12387 struct vmx_msr_entry *e)
12388{
12389 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
12390 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030012391 return -EINVAL;
12392 return 0;
12393}
12394
12395/*
12396 * Load guest's/host's msr at nested entry/exit.
12397 * return 0 for success, entry index for failure.
12398 */
12399static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
12400{
12401 u32 i;
12402 struct vmx_msr_entry e;
12403 struct msr_data msr;
12404
12405 msr.host_initiated = false;
12406 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020012407 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
12408 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020012409 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012410 "%s cannot read MSR entry (%u, 0x%08llx)\n",
12411 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030012412 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012413 }
12414 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020012415 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012416 "%s check failed (%u, 0x%x, 0x%x)\n",
12417 __func__, i, e.index, e.reserved);
12418 goto fail;
12419 }
Wincy Vanff651cb2014-12-11 08:52:58 +030012420 msr.index = e.index;
12421 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012422 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020012423 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012424 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
12425 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030012426 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012427 }
Wincy Vanff651cb2014-12-11 08:52:58 +030012428 }
12429 return 0;
12430fail:
12431 return i + 1;
12432}
12433
12434static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
12435{
12436 u32 i;
12437 struct vmx_msr_entry e;
12438
12439 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020012440 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020012441 if (kvm_vcpu_read_guest(vcpu,
12442 gpa + i * sizeof(e),
12443 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020012444 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012445 "%s cannot read MSR entry (%u, 0x%08llx)\n",
12446 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030012447 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012448 }
12449 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020012450 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012451 "%s check failed (%u, 0x%x, 0x%x)\n",
12452 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030012453 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012454 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020012455 msr_info.host_initiated = false;
12456 msr_info.index = e.index;
12457 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020012458 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012459 "%s cannot read MSR (%u, 0x%x)\n",
12460 __func__, i, e.index);
12461 return -EINVAL;
12462 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020012463 if (kvm_vcpu_write_guest(vcpu,
12464 gpa + i * sizeof(e) +
12465 offsetof(struct vmx_msr_entry, value),
12466 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020012467 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012468 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020012469 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012470 return -EINVAL;
12471 }
Wincy Vanff651cb2014-12-11 08:52:58 +030012472 }
12473 return 0;
12474}
12475
Ladi Prosek1dc35da2016-11-30 16:03:11 +010012476static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
12477{
12478 unsigned long invalid_mask;
12479
12480 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
12481 return (val & invalid_mask) == 0;
12482}
12483
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012484/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012485 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
12486 * emulating VM entry into a guest with EPT enabled.
12487 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
12488 * is assigned to entry_failure_code on failure.
12489 */
12490static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080012491 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012492{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012493 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010012494 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012495 *entry_failure_code = ENTRY_FAIL_DEFAULT;
12496 return 1;
12497 }
12498
12499 /*
12500 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
12501 * must not be dereferenced.
12502 */
12503 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
12504 !nested_ept) {
12505 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
12506 *entry_failure_code = ENTRY_FAIL_PDPTE;
12507 return 1;
12508 }
12509 }
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012510 }
12511
Junaid Shahid50c28f22018-06-27 14:59:11 -070012512 if (!nested_ept)
Junaid Shahidade61e22018-06-27 14:59:15 -070012513 kvm_mmu_new_cr3(vcpu, cr3, false);
Junaid Shahid50c28f22018-06-27 14:59:11 -070012514
12515 vcpu->arch.cr3 = cr3;
12516 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
12517
12518 kvm_init_mmu(vcpu, false);
12519
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012520 return 0;
12521}
12522
Liran Alonefebf0a2018-10-08 23:42:20 +030012523/*
12524 * Returns if KVM is able to config CPU to tag TLB entries
12525 * populated by L2 differently than TLB entries populated
12526 * by L1.
12527 *
12528 * If L1 uses EPT, then TLB entries are tagged with different EPTP.
12529 *
12530 * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged
12531 * with different VPID (L1 entries are tagged with vmx->vpid
12532 * while L2 entries are tagged with vmx->nested.vpid02).
12533 */
12534static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010012535{
Liran Alonefebf0a2018-10-08 23:42:20 +030012536 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012537
Liran Alonefebf0a2018-10-08 23:42:20 +030012538 return nested_cpu_has_ept(vmcs12) ||
12539 (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02);
12540}
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012541
Sean Christopherson3df5c372018-09-26 09:23:44 -070012542static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
12543{
12544 if (vmx->nested.nested_run_pending &&
12545 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
12546 return vmcs12->guest_ia32_efer;
12547 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
12548 return vmx->vcpu.arch.efer | (EFER_LMA | EFER_LME);
12549 else
12550 return vmx->vcpu.arch.efer & ~(EFER_LMA | EFER_LME);
12551}
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012552
Sean Christopherson09abe322018-09-26 09:23:50 -070012553static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010012554{
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012555 /*
Sean Christopherson9d6105b2018-09-26 09:23:51 -070012556 * If vmcs02 hasn't been initialized, set the constant vmcs02 state
Sean Christopherson09abe322018-09-26 09:23:50 -070012557 * according to L0's settings (vmcs12 is irrelevant here). Host
12558 * fields that come from L0 and are not constant, e.g. HOST_CR3,
12559 * will be set as needed prior to VMLAUNCH/VMRESUME.
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012560 */
Sean Christopherson9d6105b2018-09-26 09:23:51 -070012561 if (vmx->nested.vmcs02_initialized)
Sean Christopherson09abe322018-09-26 09:23:50 -070012562 return;
Sean Christopherson9d6105b2018-09-26 09:23:51 -070012563 vmx->nested.vmcs02_initialized = true;
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012564
12565 /*
Sean Christopherson52017602018-09-26 09:23:57 -070012566 * We don't care what the EPTP value is we just need to guarantee
12567 * it's valid so we don't get a false positive when doing early
12568 * consistency checks.
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012569 */
Sean Christopherson52017602018-09-26 09:23:57 -070012570 if (enable_ept && nested_early_check)
12571 vmcs_write64(EPT_POINTER, construct_eptp(&vmx->vcpu, 0));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012572
12573 /* All VMFUNCs are currently emulated through L0 vmexits. */
12574 if (cpu_has_vmx_vmfunc())
12575 vmcs_write64(VM_FUNCTION_CONTROL, 0);
12576
Sean Christopherson09abe322018-09-26 09:23:50 -070012577 if (cpu_has_vmx_posted_intr())
12578 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
12579
12580 if (cpu_has_vmx_msr_bitmap())
12581 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
12582
12583 if (enable_pml)
12584 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012585
12586 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070012587 * Set the MSR load/store lists to match L0's settings. Only the
12588 * addresses are constant (for vmcs02), the counts can change based
12589 * on L2's behavior, e.g. switching to/from long mode.
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012590 */
12591 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040012592 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040012593 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012594
Sean Christopherson09abe322018-09-26 09:23:50 -070012595 vmx_set_constant_host_state(vmx);
12596}
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012597
Sean Christopherson09abe322018-09-26 09:23:50 -070012598static void prepare_vmcs02_early_full(struct vcpu_vmx *vmx,
12599 struct vmcs12 *vmcs12)
12600{
12601 prepare_vmcs02_constant_state(vmx);
12602
12603 vmcs_write64(VMCS_LINK_POINTER, -1ull);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012604
12605 if (enable_vpid) {
12606 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
12607 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
12608 else
12609 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
12610 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +010012611}
12612
Sean Christopherson09abe322018-09-26 09:23:50 -070012613static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012614{
Bandan Das03efce62017-05-05 15:25:15 -040012615 u32 exec_control, vmcs12_exec_ctrl;
Sean Christopherson09abe322018-09-26 09:23:50 -070012616 u64 guest_efer = nested_vmx_calc_efer(vmx, vmcs12);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012617
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020012618 if (vmx->nested.dirty_vmcs12 || vmx->nested.hv_evmcs)
Sean Christopherson09abe322018-09-26 09:23:50 -070012619 prepare_vmcs02_early_full(vmx, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080012620
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012621 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070012622 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
12623 * entry, but only if the current (host) sp changed from the value
12624 * we wrote last (vmx->host_rsp). This cache is no longer relevant
12625 * if we switch vmcs, and rather than hold a separate cache per vmcs,
Sean Christopherson52017602018-09-26 09:23:57 -070012626 * here we just force the write to happen on entry. host_rsp will
12627 * also be written unconditionally by nested_vmx_check_vmentry_hw()
12628 * if we are doing early consistency checks via hardware.
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012629 */
Sean Christopherson09abe322018-09-26 09:23:50 -070012630 vmx->host_rsp = 0;
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012631
Sean Christopherson09abe322018-09-26 09:23:50 -070012632 /*
12633 * PIN CONTROLS
12634 */
Jan Kiszkaf41245002014-03-07 20:03:13 +010012635 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080012636
Sean Christophersonf459a702018-08-27 15:21:11 -070012637 /* Preemption timer setting is computed directly in vmx_vcpu_run. */
Paolo Bonzini9314006db2016-07-06 13:23:51 +020012638 exec_control |= vmcs_config.pin_based_exec_ctrl;
Sean Christophersonf459a702018-08-27 15:21:11 -070012639 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
12640 vmx->loaded_vmcs->hv_timer_armed = false;
Paolo Bonzini9314006db2016-07-06 13:23:51 +020012641
12642 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080012643 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080012644 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
12645 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012646 } else {
Wincy Van705699a2015-02-03 23:58:17 +080012647 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012648 }
Jan Kiszkaf41245002014-03-07 20:03:13 +010012649 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012650
Sean Christopherson09abe322018-09-26 09:23:50 -070012651 /*
12652 * EXEC CONTROLS
12653 */
12654 exec_control = vmx_exec_control(vmx); /* L0's desires */
12655 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
12656 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
12657 exec_control &= ~CPU_BASED_TPR_SHADOW;
12658 exec_control |= vmcs12->cpu_based_vm_exec_control;
Jan Kiszka0238ea92013-03-13 11:31:24 +010012659
Sean Christopherson09abe322018-09-26 09:23:50 -070012660 /*
12661 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
12662 * nested_get_vmcs12_pages can't fix it up, the illegal value
12663 * will result in a VM entry failure.
12664 */
12665 if (exec_control & CPU_BASED_TPR_SHADOW) {
12666 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
12667 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
12668 } else {
12669#ifdef CONFIG_X86_64
12670 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
12671 CPU_BASED_CR8_STORE_EXITING;
12672#endif
12673 }
12674
12675 /*
12676 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
12677 * for I/O port accesses.
12678 */
12679 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
12680 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
12681 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
12682
12683 /*
12684 * SECONDARY EXEC CONTROLS
12685 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012686 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020012687 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080012688
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012689 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020012690 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020012691 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010012692 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020012693 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020012694 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040012695 SECONDARY_EXEC_APIC_REGISTER_VIRT |
12696 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012697 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040012698 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
12699 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
12700 ~SECONDARY_EXEC_ENABLE_PML;
12701 exec_control |= vmcs12_exec_ctrl;
12702 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012703
Liran Alon32c7acf2018-06-23 02:35:11 +030012704 /* VMCS shadowing for L2 is emulated for now */
12705 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
12706
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012707 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080012708 vmcs_write16(GUEST_INTR_STATUS,
12709 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080012710
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012711 /*
12712 * Write an illegal value to APIC_ACCESS_ADDR. Later,
12713 * nested_get_vmcs12_pages will either fix it up or
12714 * remove the VM execution control.
12715 */
12716 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
12717 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
12718
Sean Christopherson0b665d32018-08-14 09:33:34 -070012719 if (exec_control & SECONDARY_EXEC_ENCLS_EXITING)
12720 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
12721
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012722 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
12723 }
12724
Jim Mattson83bafef2016-10-04 10:48:38 -070012725 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070012726 * ENTRY CONTROLS
12727 *
Sean Christopherson3df5c372018-09-26 09:23:44 -070012728 * vmcs12's VM_{ENTRY,EXIT}_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE
Sean Christopherson09abe322018-09-26 09:23:50 -070012729 * are emulated by vmx_set_efer() in prepare_vmcs02(), but speculate
12730 * on the related bits (if supported by the CPU) in the hope that
12731 * we can avoid VMWrites during vmx_set_efer().
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012732 */
Sean Christopherson3df5c372018-09-26 09:23:44 -070012733 exec_control = (vmcs12->vm_entry_controls | vmcs_config.vmentry_ctrl) &
12734 ~VM_ENTRY_IA32E_MODE & ~VM_ENTRY_LOAD_IA32_EFER;
12735 if (cpu_has_load_ia32_efer) {
12736 if (guest_efer & EFER_LMA)
12737 exec_control |= VM_ENTRY_IA32E_MODE;
12738 if (guest_efer != host_efer)
12739 exec_control |= VM_ENTRY_LOAD_IA32_EFER;
12740 }
12741 vm_entry_controls_init(vmx, exec_control);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012742
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012743 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070012744 * EXIT CONTROLS
12745 *
12746 * L2->L1 exit controls are emulated - the hardware exit is to L0 so
12747 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
12748 * bits may be modified by vmx_set_efer() in prepare_vmcs02().
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012749 */
Sean Christopherson09abe322018-09-26 09:23:50 -070012750 exec_control = vmcs_config.vmexit_ctrl;
12751 if (cpu_has_load_ia32_efer && guest_efer != host_efer)
12752 exec_control |= VM_EXIT_LOAD_IA32_EFER;
12753 vm_exit_controls_init(vmx, exec_control);
12754
12755 /*
12756 * Conceptually we want to copy the PML address and index from
12757 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
12758 * since we always flush the log on each vmexit and never change
12759 * the PML address (once set), this happens to be equivalent to
12760 * simply resetting the index in vmcs02.
12761 */
12762 if (enable_pml)
12763 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
12764
12765 /*
12766 * Interrupt/Exception Fields
12767 */
12768 if (vmx->nested.nested_run_pending) {
12769 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
12770 vmcs12->vm_entry_intr_info_field);
12771 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
12772 vmcs12->vm_entry_exception_error_code);
12773 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
12774 vmcs12->vm_entry_instruction_len);
12775 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
12776 vmcs12->guest_interruptibility_info);
12777 vmx->loaded_vmcs->nmi_known_unmasked =
12778 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattson51aa68e2017-09-12 13:02:54 -070012779 } else {
Sean Christopherson09abe322018-09-26 09:23:50 -070012780 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
12781 }
12782}
12783
12784static void prepare_vmcs02_full(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
12785{
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020012786 struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
Sean Christopherson09abe322018-09-26 09:23:50 -070012787
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020012788 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
12789 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) {
12790 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
Vitaly Kuznetsovcbe3f892018-10-19 16:16:03 +020012791 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020012792 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
12793 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
12794 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
12795 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
12796 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
12797 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
12798 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
Vitaly Kuznetsovcbe3f892018-10-19 16:16:03 +020012799 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020012800 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
12801 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
12802 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
12803 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
12804 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
12805 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
12806 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
12807 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
12808 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020012809 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
12810 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
12811 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
12812 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
12813 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
Vitaly Kuznetsovcbe3f892018-10-19 16:16:03 +020012814 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
12815 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020012816 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
12817 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
12818 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
12819 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
12820 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
12821 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
12822 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
12823 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
12824 }
12825
12826 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
12827 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1)) {
12828 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
12829 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
12830 vmcs12->guest_pending_dbg_exceptions);
12831 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
12832 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
12833
12834 /*
12835 * L1 may access the L2's PDPTR, so save them to construct
12836 * vmcs12
12837 */
12838 if (enable_ept) {
12839 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
12840 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
12841 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
12842 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
12843 }
12844 }
Sean Christopherson09abe322018-09-26 09:23:50 -070012845
12846 if (nested_cpu_has_xsaves(vmcs12))
12847 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
12848
12849 /*
12850 * Whether page-faults are trapped is determined by a combination of
12851 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
12852 * If enable_ept, L0 doesn't care about page faults and we should
12853 * set all of these to L1's desires. However, if !enable_ept, L0 does
12854 * care about (at least some) page faults, and because it is not easy
12855 * (if at all possible?) to merge L0 and L1's desires, we simply ask
12856 * to exit on each and every L2 page fault. This is done by setting
12857 * MASK=MATCH=0 and (see below) EB.PF=1.
12858 * Note that below we don't need special code to set EB.PF beyond the
12859 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
12860 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
12861 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
12862 */
12863 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
12864 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
12865 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
12866 enable_ept ? vmcs12->page_fault_error_code_match : 0);
12867
12868 if (cpu_has_vmx_apicv()) {
12869 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
12870 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
12871 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
12872 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
12873 }
12874
12875 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
12876 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
12877
12878 set_cr4_guest_host_mask(vmx);
12879
12880 if (kvm_mpx_supported()) {
12881 if (vmx->nested.nested_run_pending &&
12882 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
12883 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
12884 else
12885 vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs);
12886 }
Sean Christopherson09abe322018-09-26 09:23:50 -070012887}
12888
12889/*
12890 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
12891 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
12892 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
12893 * guest in a way that will both be appropriate to L1's requests, and our
12894 * needs. In addition to modifying the active vmcs (which is vmcs02), this
12895 * function also has additional necessary side-effects, like setting various
12896 * vcpu->arch fields.
12897 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
12898 * is assigned to entry_failure_code on failure.
12899 */
12900static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12901 u32 *entry_failure_code)
12902{
12903 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020012904 struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
Sean Christopherson09abe322018-09-26 09:23:50 -070012905
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020012906 if (vmx->nested.dirty_vmcs12 || vmx->nested.hv_evmcs) {
Sean Christopherson09abe322018-09-26 09:23:50 -070012907 prepare_vmcs02_full(vmx, vmcs12);
12908 vmx->nested.dirty_vmcs12 = false;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012909 }
12910
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012911 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070012912 * First, the fields that are shadowed. This must be kept in sync
Sean Christophersone0123112018-12-03 13:52:57 -080012913 * with vmcs_shadow_fields.h.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012914 */
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020012915 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
12916 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) {
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020012917 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Vitaly Kuznetsovcbe3f892018-10-19 16:16:03 +020012918 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020012919 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012920
Sean Christopherson09abe322018-09-26 09:23:50 -070012921 if (vmx->nested.nested_run_pending &&
12922 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
12923 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
12924 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
12925 } else {
12926 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
12927 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
12928 }
12929 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
12930
12931 vmx->nested.preemption_timer_expired = false;
12932 if (nested_cpu_has_preemption_timer(vmcs12))
12933 vmx_start_preemption_timer(vcpu);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012934
12935 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
12936 * bitwise-or of what L1 wants to trap for L2, and what we want to
12937 * trap. Note that CR0.TS also needs updating - we do this later.
12938 */
12939 update_exception_bitmap(vcpu);
12940 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
12941 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
12942
Jim Mattson6514dc32018-04-26 16:09:12 -070012943 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012944 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012945 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012946 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012947 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012948 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012949 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012950
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012951 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
12952
Peter Feinerc95ba922016-08-17 09:36:47 -070012953 if (kvm_has_tsc_control)
12954 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012955
12956 if (enable_vpid) {
12957 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070012958 * There is no direct mapping between vpid02 and vpid12, the
12959 * vpid02 is per-vCPU for L0 and reused while the value of
12960 * vpid12 is changed w/ one invvpid during nested vmentry.
12961 * The vpid12 is allocated by L1 for L2, so it will not
12962 * influence global bitmap(for vpid01 and vpid02 allocation)
12963 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012964 */
Liran Alonefebf0a2018-10-08 23:42:20 +030012965 if (nested_cpu_has_vpid(vmcs12) && nested_has_guest_tlb_tag(vcpu)) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070012966 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
12967 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alonefebf0a2018-10-08 23:42:20 +030012968 __vmx_flush_tlb(vcpu, nested_get_vpid02(vcpu), false);
Wanpeng Li5c614b32015-10-13 09:18:36 -070012969 }
12970 } else {
Liran Alon14389212018-10-08 23:42:17 +030012971 /*
12972 * If L1 use EPT, then L0 needs to execute INVEPT on
12973 * EPTP02 instead of EPTP01. Therefore, delay TLB
12974 * flush until vmcs02->eptp is fully updated by
12975 * KVM_REQ_LOAD_CR3. Note that this assumes
12976 * KVM_REQ_TLB_FLUSH is evaluated after
12977 * KVM_REQ_LOAD_CR3 in vcpu_enter_guest().
12978 */
12979 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Wanpeng Li5c614b32015-10-13 09:18:36 -070012980 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012981 }
12982
Sean Christopherson5b8ba412018-09-26 09:23:40 -070012983 if (nested_cpu_has_ept(vmcs12))
12984 nested_ept_init_mmu_context(vcpu);
12985 else if (nested_cpu_has2(vmcs12,
12986 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Junaid Shahida468f2d2018-04-26 13:09:50 -070012987 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030012988
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012989 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012990 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
12991 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012992 * The CR0_READ_SHADOW is what L2 should have expected to read given
12993 * the specifications by L1; It's not enough to take
12994 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
12995 * have more bits than L1 expected.
12996 */
12997 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
12998 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
12999
13000 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
13001 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
13002
Sean Christopherson09abe322018-09-26 09:23:50 -070013003 vcpu->arch.efer = nested_vmx_calc_efer(vmx, vmcs12);
Sean Christopherson3df5c372018-09-26 09:23:44 -070013004 /* Note: may modify VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
David Matlack5a6a9742016-11-29 18:14:10 -080013005 vmx_set_efer(vcpu, vcpu->arch.efer);
13006
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070013007 /*
13008 * Guest state is invalid and unrestricted guest is disabled,
13009 * which means L1 attempted VMEntry to L2 with invalid state.
13010 * Fail the VMEntry.
13011 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010013012 if (vmx->emulation_required) {
13013 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070013014 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010013015 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070013016
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010013017 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010013018 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010013019 entry_failure_code))
13020 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010013021
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030013022 if (!enable_ept)
13023 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
13024
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030013025 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
13026 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010013027 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030013028}
13029
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050013030static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
13031{
13032 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
13033 nested_cpu_has_virtual_nmis(vmcs12))
13034 return -EINVAL;
13035
13036 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
13037 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
13038 return -EINVAL;
13039
13040 return 0;
13041}
13042
Jim Mattsonca0bde22016-11-30 12:03:46 -080013043static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
13044{
13045 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson64a919f2018-09-26 09:23:39 -070013046 bool ia32e;
Jim Mattsonca0bde22016-11-30 12:03:46 -080013047
13048 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
13049 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
13050 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13051
Krish Sadhukhanba8e23d2018-09-04 14:42:58 -040013052 if (nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id)
13053 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13054
Jim Mattson56a20512017-07-06 16:33:06 -070013055 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
13056 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13057
Jim Mattsonca0bde22016-11-30 12:03:46 -080013058 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
13059 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13060
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040013061 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
13062 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13063
Jim Mattson712b12d2017-08-24 13:24:47 -070013064 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
13065 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13066
Jim Mattsonca0bde22016-11-30 12:03:46 -080013067 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
13068 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13069
13070 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
13071 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13072
Krish Sadhukhan14aa61d2018-11-01 01:21:58 -040013073 if (!nested_cpu_has_preemption_timer(vmcs12) &&
13074 nested_cpu_has_save_preemption_timer(vmcs12))
13075 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13076
Bandan Dasc5f983f2017-05-05 15:25:14 -040013077 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
13078 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13079
Jim Mattson886560402018-09-24 11:05:43 -070013080 if (nested_vmx_check_unrestricted_guest_controls(vcpu, vmcs12))
13081 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13082
13083 if (nested_vmx_check_mode_based_ept_exec_controls(vcpu, vmcs12))
13084 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13085
Liran Alona8a7c022018-06-23 02:35:06 +030013086 if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
13087 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13088
Jim Mattsonca0bde22016-11-30 12:03:46 -080013089 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010013090 vmx->nested.msrs.procbased_ctls_low,
13091 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070013092 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
13093 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010013094 vmx->nested.msrs.secondary_ctls_low,
13095 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080013096 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010013097 vmx->nested.msrs.pinbased_ctls_low,
13098 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080013099 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010013100 vmx->nested.msrs.exit_ctls_low,
13101 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080013102 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010013103 vmx->nested.msrs.entry_ctls_low,
13104 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080013105 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13106
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050013107 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080013108 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13109
Bandan Das41ab9372017-08-03 15:54:43 -040013110 if (nested_cpu_has_vmfunc(vmcs12)) {
13111 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010013112 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040013113 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13114
13115 if (nested_cpu_has_eptp_switching(vmcs12)) {
13116 if (!nested_cpu_has_ept(vmcs12) ||
13117 !page_address_valid(vcpu, vmcs12->eptp_list_address))
13118 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13119 }
13120 }
Bandan Das27c42a12017-08-03 15:54:42 -040013121
Jim Mattsonc7c2c7092017-05-05 11:28:09 -070013122 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
13123 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13124
Jim Mattsonca0bde22016-11-30 12:03:46 -080013125 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
13126 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
13127 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
13128 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
13129
Marc Orr04473782018-06-20 17:21:29 -070013130 /*
Sean Christopherson64a919f2018-09-26 09:23:39 -070013131 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
13132 * IA32_EFER MSR must be 0 in the field for that register. In addition,
13133 * the values of the LMA and LME bits in the field must each be that of
13134 * the host address-space size VM-exit control.
13135 */
13136 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
13137 ia32e = (vmcs12->vm_exit_controls &
13138 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
13139 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
13140 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
13141 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
13142 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
13143 }
13144
13145 /*
Marc Orr04473782018-06-20 17:21:29 -070013146 * From the Intel SDM, volume 3:
13147 * Fields relevant to VM-entry event injection must be set properly.
13148 * These fields are the VM-entry interruption-information field, the
13149 * VM-entry exception error code, and the VM-entry instruction length.
13150 */
13151 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
13152 u32 intr_info = vmcs12->vm_entry_intr_info_field;
13153 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
13154 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
13155 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
13156 bool should_have_error_code;
13157 bool urg = nested_cpu_has2(vmcs12,
13158 SECONDARY_EXEC_UNRESTRICTED_GUEST);
13159 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
13160
13161 /* VM-entry interruption-info field: interruption type */
13162 if (intr_type == INTR_TYPE_RESERVED ||
13163 (intr_type == INTR_TYPE_OTHER_EVENT &&
13164 !nested_cpu_supports_monitor_trap_flag(vcpu)))
13165 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13166
13167 /* VM-entry interruption-info field: vector */
13168 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
13169 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
13170 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
13171 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13172
13173 /* VM-entry interruption-info field: deliver error code */
13174 should_have_error_code =
13175 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
13176 x86_exception_has_error_code(vector);
13177 if (has_error_code != should_have_error_code)
13178 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13179
13180 /* VM-entry exception error code */
13181 if (has_error_code &&
13182 vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
13183 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13184
13185 /* VM-entry interruption-info field: reserved bits */
13186 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
13187 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13188
13189 /* VM-entry instruction length */
13190 switch (intr_type) {
13191 case INTR_TYPE_SOFT_EXCEPTION:
13192 case INTR_TYPE_SOFT_INTR:
13193 case INTR_TYPE_PRIV_SW_EXCEPTION:
13194 if ((vmcs12->vm_entry_instruction_len > 15) ||
13195 (vmcs12->vm_entry_instruction_len == 0 &&
13196 !nested_cpu_has_zero_length_injection(vcpu)))
13197 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13198 }
13199 }
13200
Sean Christopherson5b8ba412018-09-26 09:23:40 -070013201 if (nested_cpu_has_ept(vmcs12) &&
13202 !valid_ept_address(vcpu, vmcs12->ept_pointer))
13203 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13204
Jim Mattsonca0bde22016-11-30 12:03:46 -080013205 return 0;
13206}
13207
Liran Alonf145d902018-06-23 02:35:07 +030013208static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
13209 struct vmcs12 *vmcs12)
13210{
13211 int r;
13212 struct page *page;
13213 struct vmcs12 *shadow;
13214
13215 if (vmcs12->vmcs_link_pointer == -1ull)
13216 return 0;
13217
13218 if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
13219 return -EINVAL;
13220
13221 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
13222 if (is_error_page(page))
13223 return -EINVAL;
13224
13225 r = 0;
13226 shadow = kmap(page);
13227 if (shadow->hdr.revision_id != VMCS12_REVISION ||
13228 shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
13229 r = -EINVAL;
13230 kunmap(page);
13231 kvm_release_page_clean(page);
13232 return r;
13233}
13234
Jim Mattsonca0bde22016-11-30 12:03:46 -080013235static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
13236 u32 *exit_qual)
13237{
13238 bool ia32e;
13239
13240 *exit_qual = ENTRY_FAIL_DEFAULT;
13241
13242 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
13243 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
13244 return 1;
13245
Liran Alonf145d902018-06-23 02:35:07 +030013246 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Jim Mattsonca0bde22016-11-30 12:03:46 -080013247 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
13248 return 1;
13249 }
13250
13251 /*
13252 * If the load IA32_EFER VM-entry control is 1, the following checks
13253 * are performed on the field for the IA32_EFER MSR:
13254 * - Bits reserved in the IA32_EFER MSR must be 0.
13255 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
13256 * the IA-32e mode guest VM-exit control. It must also be identical
13257 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
13258 * CR0.PG) is 1.
13259 */
13260 if (to_vmx(vcpu)->nested.nested_run_pending &&
13261 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
13262 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
13263 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
13264 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
13265 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
13266 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
13267 return 1;
13268 }
13269
Wanpeng Lif1b026a2017-11-05 16:54:48 -080013270 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
13271 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
13272 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
13273 return 1;
13274
Jim Mattsonca0bde22016-11-30 12:03:46 -080013275 return 0;
13276}
13277
Sean Christopherson52017602018-09-26 09:23:57 -070013278static int __noclone nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu)
13279{
13280 struct vcpu_vmx *vmx = to_vmx(vcpu);
13281 unsigned long cr3, cr4;
13282
13283 if (!nested_early_check)
13284 return 0;
13285
13286 if (vmx->msr_autoload.host.nr)
13287 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
13288 if (vmx->msr_autoload.guest.nr)
13289 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
13290
13291 preempt_disable();
13292
13293 vmx_prepare_switch_to_guest(vcpu);
13294
13295 /*
13296 * Induce a consistency check VMExit by clearing bit 1 in GUEST_RFLAGS,
13297 * which is reserved to '1' by hardware. GUEST_RFLAGS is guaranteed to
13298 * be written (by preparve_vmcs02()) before the "real" VMEnter, i.e.
13299 * there is no need to preserve other bits or save/restore the field.
13300 */
13301 vmcs_writel(GUEST_RFLAGS, 0);
13302
13303 vmcs_writel(HOST_RIP, vmx_early_consistency_check_return);
13304
13305 cr3 = __get_current_cr3_fast();
13306 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
13307 vmcs_writel(HOST_CR3, cr3);
13308 vmx->loaded_vmcs->host_state.cr3 = cr3;
13309 }
13310
13311 cr4 = cr4_read_shadow();
13312 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
13313 vmcs_writel(HOST_CR4, cr4);
13314 vmx->loaded_vmcs->host_state.cr4 = cr4;
13315 }
13316
13317 vmx->__launched = vmx->loaded_vmcs->launched;
13318
13319 asm(
13320 /* Set HOST_RSP */
Uros Bizjak4b1e5472018-10-11 19:40:44 +020013321 __ex("vmwrite %%" _ASM_SP ", %%" _ASM_DX) "\n\t"
Sean Christopherson52017602018-09-26 09:23:57 -070013322 "mov %%" _ASM_SP ", %c[host_rsp](%0)\n\t"
13323
Uros Bizjak00df9182018-10-23 00:09:11 +020013324 /* Check if vmlaunch or vmresume is needed */
Sean Christopherson52017602018-09-26 09:23:57 -070013325 "cmpl $0, %c[launched](%0)\n\t"
Uros Bizjak00df9182018-10-23 00:09:11 +020013326 "jne 1f\n\t"
13327 __ex("vmlaunch") "\n\t"
Sean Christopherson52017602018-09-26 09:23:57 -070013328 "jmp 2f\n\t"
Uros Bizjak00df9182018-10-23 00:09:11 +020013329 "1: " __ex("vmresume") "\n\t"
Sean Christopherson52017602018-09-26 09:23:57 -070013330 "2: "
Sean Christopherson52017602018-09-26 09:23:57 -070013331 /* Set vmx->fail accordingly */
13332 "setbe %c[fail](%0)\n\t"
13333
13334 ".pushsection .rodata\n\t"
13335 ".global vmx_early_consistency_check_return\n\t"
13336 "vmx_early_consistency_check_return: " _ASM_PTR " 2b\n\t"
13337 ".popsection"
13338 :
13339 : "c"(vmx), "d"((unsigned long)HOST_RSP),
13340 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
13341 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
13342 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp))
13343 : "rax", "cc", "memory"
13344 );
13345
13346 vmcs_writel(HOST_RIP, vmx_return);
13347
13348 preempt_enable();
13349
13350 if (vmx->msr_autoload.host.nr)
13351 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
13352 if (vmx->msr_autoload.guest.nr)
13353 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
13354
13355 if (vmx->fail) {
13356 WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
13357 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
13358 vmx->fail = 0;
13359 return 1;
13360 }
13361
13362 /*
13363 * VMExit clears RFLAGS.IF and DR7, even on a consistency check.
13364 */
13365 local_irq_enable();
13366 if (hw_breakpoint_active())
13367 set_debugreg(__this_cpu_read(cpu_dr7), 7);
13368
13369 /*
13370 * A non-failing VMEntry means we somehow entered guest mode with
13371 * an illegal RIP, and that's just the tip of the iceberg. There
13372 * is no telling what memory has been modified or what state has
13373 * been exposed to unknown code. Hitting this all but guarantees
13374 * a (very critical) hardware issue.
13375 */
13376 WARN_ON(!(vmcs_read32(VM_EXIT_REASON) &
13377 VMX_EXIT_REASONS_FAILED_VMENTRY));
13378
13379 return 0;
13380}
13381STACK_FRAME_NON_STANDARD(nested_vmx_check_vmentry_hw);
13382
Sean Christophersona633e412018-09-26 09:23:47 -070013383static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
13384 struct vmcs12 *vmcs12);
13385
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013386/*
Sean Christophersona633e412018-09-26 09:23:47 -070013387 * If from_vmentry is false, this is being called from state restore (either RSM
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013388 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Sean Christopherson52017602018-09-26 09:23:57 -070013389+ *
13390+ * Returns:
13391+ * 0 - success, i.e. proceed with actual VMEnter
13392+ * 1 - consistency check VMExit
13393+ * -1 - consistency check VMFail
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013394 */
Sean Christophersona633e412018-09-26 09:23:47 -070013395static int nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu,
13396 bool from_vmentry)
Jim Mattson858e25c2016-11-30 12:03:47 -080013397{
13398 struct vcpu_vmx *vmx = to_vmx(vcpu);
13399 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzini7e712682018-10-03 13:44:26 +020013400 bool evaluate_pending_interrupts;
Sean Christophersona633e412018-09-26 09:23:47 -070013401 u32 exit_reason = EXIT_REASON_INVALID_STATE;
13402 u32 exit_qual;
Jim Mattson858e25c2016-11-30 12:03:47 -080013403
Paolo Bonzini7e712682018-10-03 13:44:26 +020013404 evaluate_pending_interrupts = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
13405 (CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_VIRTUAL_NMI_PENDING);
13406 if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu))
13407 evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu);
Liran Alonb5861e52018-09-03 15:20:22 +030013408
Jim Mattson858e25c2016-11-30 12:03:47 -080013409 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
13410 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
Liran Alon62cf9bd812018-09-14 03:25:54 +030013411 if (kvm_mpx_supported() &&
13412 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
13413 vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattson858e25c2016-11-30 12:03:47 -080013414
Jim Mattsonde3a0022017-11-27 17:22:25 -060013415 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080013416
Sean Christopherson16fb9a42018-09-26 09:23:52 -070013417 prepare_vmcs02_early(vmx, vmcs12);
Jim Mattson858e25c2016-11-30 12:03:47 -080013418
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013419 if (from_vmentry) {
13420 nested_get_vmcs12_pages(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080013421
Sean Christopherson52017602018-09-26 09:23:57 -070013422 if (nested_vmx_check_vmentry_hw(vcpu)) {
13423 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
13424 return -1;
13425 }
13426
Sean Christopherson16fb9a42018-09-26 09:23:52 -070013427 if (check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
13428 goto vmentry_fail_vmexit;
13429 }
13430
13431 enter_guest_mode(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080013432 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
13433 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
13434
Sean Christophersona633e412018-09-26 09:23:47 -070013435 if (prepare_vmcs02(vcpu, vmcs12, &exit_qual))
Sean Christopherson39f9c382018-09-26 09:23:48 -070013436 goto vmentry_fail_vmexit_guest_mode;
Jim Mattson858e25c2016-11-30 12:03:47 -080013437
13438 if (from_vmentry) {
Sean Christophersona633e412018-09-26 09:23:47 -070013439 exit_reason = EXIT_REASON_MSR_LOAD_FAIL;
13440 exit_qual = nested_vmx_load_msr(vcpu,
13441 vmcs12->vm_entry_msr_load_addr,
13442 vmcs12->vm_entry_msr_load_count);
13443 if (exit_qual)
Sean Christopherson39f9c382018-09-26 09:23:48 -070013444 goto vmentry_fail_vmexit_guest_mode;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013445 } else {
13446 /*
13447 * The MMU is not initialized to point at the right entities yet and
13448 * "get pages" would need to read data from the guest (i.e. we will
13449 * need to perform gpa to hpa translation). Request a call
13450 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
13451 * have already been set at vmentry time and should not be reset.
13452 */
13453 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
13454 }
Jim Mattson858e25c2016-11-30 12:03:47 -080013455
Jim Mattson858e25c2016-11-30 12:03:47 -080013456 /*
Liran Alonb5861e52018-09-03 15:20:22 +030013457 * If L1 had a pending IRQ/NMI until it executed
13458 * VMLAUNCH/VMRESUME which wasn't delivered because it was
13459 * disallowed (e.g. interrupts disabled), L0 needs to
13460 * evaluate if this pending event should cause an exit from L2
13461 * to L1 or delivered directly to L2 (e.g. In case L1 don't
13462 * intercept EXTERNAL_INTERRUPT).
13463 *
Paolo Bonzini7e712682018-10-03 13:44:26 +020013464 * Usually this would be handled by the processor noticing an
13465 * IRQ/NMI window request, or checking RVI during evaluation of
13466 * pending virtual interrupts. However, this setting was done
13467 * on VMCS01 and now VMCS02 is active instead. Thus, we force L0
13468 * to perform pending event evaluation by requesting a KVM_REQ_EVENT.
Liran Alonb5861e52018-09-03 15:20:22 +030013469 */
Paolo Bonzini7e712682018-10-03 13:44:26 +020013470 if (unlikely(evaluate_pending_interrupts))
Liran Alonb5861e52018-09-03 15:20:22 +030013471 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alonb5861e52018-09-03 15:20:22 +030013472
13473 /*
Jim Mattson858e25c2016-11-30 12:03:47 -080013474 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
13475 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
13476 * returned as far as L1 is concerned. It will only return (and set
13477 * the success flag) when L2 exits (see nested_vmx_vmexit()).
13478 */
13479 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013480
Sean Christophersona633e412018-09-26 09:23:47 -070013481 /*
13482 * A failed consistency check that leads to a VMExit during L1's
13483 * VMEnter to L2 is a variation of a normal VMexit, as explained in
13484 * 26.7 "VM-entry failures during or after loading guest state".
13485 */
Sean Christopherson39f9c382018-09-26 09:23:48 -070013486vmentry_fail_vmexit_guest_mode:
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013487 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
13488 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
13489 leave_guest_mode(vcpu);
Sean Christopherson16fb9a42018-09-26 09:23:52 -070013490
13491vmentry_fail_vmexit:
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013492 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Sean Christophersona633e412018-09-26 09:23:47 -070013493
13494 if (!from_vmentry)
13495 return 1;
13496
Sean Christophersona633e412018-09-26 09:23:47 -070013497 load_vmcs12_host_state(vcpu, vmcs12);
13498 vmcs12->vm_exit_reason = exit_reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
13499 vmcs12->exit_qualification = exit_qual;
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020013500 if (enable_shadow_vmcs || vmx->nested.hv_evmcs)
13501 vmx->nested.need_vmcs12_sync = true;
Sean Christophersona633e412018-09-26 09:23:47 -070013502 return 1;
Jim Mattson858e25c2016-11-30 12:03:47 -080013503}
13504
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030013505/*
13506 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
13507 * for running an L2 nested guest.
13508 */
13509static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
13510{
13511 struct vmcs12 *vmcs12;
13512 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070013513 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080013514 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030013515
Kyle Hueyeb277562016-11-29 12:40:39 -080013516 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030013517 return 1;
13518
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020013519 if (!nested_vmx_handle_enlightened_vmptrld(vcpu, true))
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +020013520 return 1;
13521
13522 if (!vmx->nested.hv_evmcs && vmx->nested.current_vmptr == -1ull)
Sean Christopherson09abb5e2018-09-26 09:23:55 -070013523 return nested_vmx_failInvalid(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -080013524
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030013525 vmcs12 = get_vmcs12(vcpu);
13526
Liran Alona6192d42018-06-23 02:35:04 +030013527 /*
13528 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
13529 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
13530 * rather than RFLAGS.ZF, and no error number is stored to the
13531 * VM-instruction error field.
13532 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -070013533 if (vmcs12->hdr.shadow_vmcs)
13534 return nested_vmx_failInvalid(vcpu);
Liran Alona6192d42018-06-23 02:35:04 +030013535
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020013536 if (vmx->nested.hv_evmcs) {
13537 copy_enlightened_to_vmcs12(vmx);
13538 /* Enlightened VMCS doesn't have launch state */
13539 vmcs12->launch_state = !launch;
13540 } else if (enable_shadow_vmcs) {
Abel Gordon012f83c2013-04-18 14:39:25 +030013541 copy_shadow_to_vmcs12(vmx);
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020013542 }
Abel Gordon012f83c2013-04-18 14:39:25 +030013543
Nadav Har'El7c177932011-05-25 23:12:04 +030013544 /*
13545 * The nested entry process starts with enforcing various prerequisites
13546 * on vmcs12 as required by the Intel SDM, and act appropriately when
13547 * they fail: As the SDM explains, some conditions should cause the
13548 * instruction to fail, while others will cause the instruction to seem
13549 * to succeed, but return an EXIT_REASON_INVALID_STATE.
13550 * To speed up the normal (success) code path, we should avoid checking
13551 * for misconfigurations which will anyway be caught by the processor
13552 * when using the merged vmcs02.
13553 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -070013554 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS)
13555 return nested_vmx_failValid(vcpu,
13556 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070013557
Sean Christopherson09abb5e2018-09-26 09:23:55 -070013558 if (vmcs12->launch_state == launch)
13559 return nested_vmx_failValid(vcpu,
Nadav Har'El7c177932011-05-25 23:12:04 +030013560 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
13561 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Nadav Har'El7c177932011-05-25 23:12:04 +030013562
Jim Mattsonca0bde22016-11-30 12:03:46 -080013563 ret = check_vmentry_prereqs(vcpu, vmcs12);
Sean Christopherson09abb5e2018-09-26 09:23:55 -070013564 if (ret)
13565 return nested_vmx_failValid(vcpu, ret);
Jan Kiszka384bb782013-04-20 10:52:36 +020013566
13567 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030013568 * We're finally done with prerequisite checking, and can start with
13569 * the nested entry.
13570 */
Jim Mattson6514dc32018-04-26 16:09:12 -070013571 vmx->nested.nested_run_pending = 1;
Sean Christophersona633e412018-09-26 09:23:47 -070013572 ret = nested_vmx_enter_non_root_mode(vcpu, true);
Sean Christopherson52017602018-09-26 09:23:57 -070013573 vmx->nested.nested_run_pending = !ret;
13574 if (ret > 0)
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013575 return 1;
Sean Christopherson52017602018-09-26 09:23:57 -070013576 else if (ret)
13577 return nested_vmx_failValid(vcpu,
13578 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wincy Vanff651cb2014-12-11 08:52:58 +030013579
Paolo Bonzinic595cee2018-07-02 13:07:14 +020013580 /* Hide L1D cache contents from the nested guest. */
13581 vmx->vcpu.arch.l1tf_flush_l1d = true;
13582
Chao Gao135a06c2018-02-11 10:06:30 +080013583 /*
Sean Christophersond63907d2018-09-26 09:23:45 -070013584 * Must happen outside of nested_vmx_enter_non_root_mode() as it will
Liran Alon61ada742018-06-23 02:35:08 +030013585 * also be used as part of restoring nVMX state for
13586 * snapshot restore (migration).
13587 *
13588 * In this flow, it is assumed that vmcs12 cache was
13589 * trasferred as part of captured nVMX state and should
13590 * therefore not be read from guest memory (which may not
13591 * exist on destination host yet).
13592 */
13593 nested_cache_shadow_vmcs12(vcpu, vmcs12);
13594
13595 /*
Chao Gao135a06c2018-02-11 10:06:30 +080013596 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
13597 * by event injection, halt vcpu.
13598 */
13599 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070013600 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
13601 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060013602 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070013603 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030013604 return 1;
13605}
13606
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013607/*
13608 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
13609 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
13610 * This function returns the new value we should put in vmcs12.guest_cr0.
13611 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
13612 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
13613 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
13614 * didn't trap the bit, because if L1 did, so would L0).
13615 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
13616 * been modified by L2, and L1 knows it. So just leave the old value of
13617 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
13618 * isn't relevant, because if L0 traps this bit it can set it to anything.
13619 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
13620 * changed these bits, and therefore they need to be updated, but L0
13621 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
13622 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
13623 */
13624static inline unsigned long
13625vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
13626{
13627 return
13628 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
13629 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
13630 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
13631 vcpu->arch.cr0_guest_owned_bits));
13632}
13633
13634static inline unsigned long
13635vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
13636{
13637 return
13638 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
13639 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
13640 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
13641 vcpu->arch.cr4_guest_owned_bits));
13642}
13643
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013644static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
13645 struct vmcs12 *vmcs12)
13646{
13647 u32 idt_vectoring;
13648 unsigned int nr;
13649
Wanpeng Li664f8e22017-08-24 03:35:09 -070013650 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013651 nr = vcpu->arch.exception.nr;
13652 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
13653
13654 if (kvm_exception_is_soft(nr)) {
13655 vmcs12->vm_exit_instruction_len =
13656 vcpu->arch.event_exit_inst_len;
13657 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
13658 } else
13659 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
13660
13661 if (vcpu->arch.exception.has_error_code) {
13662 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
13663 vmcs12->idt_vectoring_error_code =
13664 vcpu->arch.exception.error_code;
13665 }
13666
13667 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010013668 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013669 vmcs12->idt_vectoring_info_field =
13670 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030013671 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013672 nr = vcpu->arch.interrupt.nr;
13673 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
13674
13675 if (vcpu->arch.interrupt.soft) {
13676 idt_vectoring |= INTR_TYPE_SOFT_INTR;
13677 vmcs12->vm_entry_instruction_len =
13678 vcpu->arch.event_exit_inst_len;
13679 } else
13680 idt_vectoring |= INTR_TYPE_EXT_INTR;
13681
13682 vmcs12->idt_vectoring_info_field = idt_vectoring;
13683 }
13684}
13685
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013686static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
13687{
13688 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070013689 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020013690 bool block_nested_events =
13691 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080013692
Wanpeng Libfcf83b2017-08-24 03:35:11 -070013693 if (vcpu->arch.exception.pending &&
13694 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020013695 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070013696 return -EBUSY;
13697 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070013698 return 0;
13699 }
13700
Jan Kiszkaf41245002014-03-07 20:03:13 +010013701 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
13702 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020013703 if (block_nested_events)
Jan Kiszkaf41245002014-03-07 20:03:13 +010013704 return -EBUSY;
13705 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
13706 return 0;
13707 }
13708
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013709 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020013710 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013711 return -EBUSY;
13712 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
13713 NMI_VECTOR | INTR_TYPE_NMI_INTR |
13714 INTR_INFO_VALID_MASK, 0);
13715 /*
13716 * The NMI-triggered VM exit counts as injection:
13717 * clear this one and block further NMIs.
13718 */
13719 vcpu->arch.nmi_pending = 0;
13720 vmx_set_nmi_mask(vcpu, true);
13721 return 0;
13722 }
13723
13724 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
13725 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020013726 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013727 return -EBUSY;
13728 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080013729 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013730 }
13731
David Hildenbrand6342c502017-01-25 11:58:58 +010013732 vmx_complete_nested_posted_interrupt(vcpu);
13733 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013734}
13735
Sean Christophersond264ee02018-08-27 15:21:12 -070013736static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)
13737{
13738 to_vmx(vcpu)->req_immediate_exit = true;
13739}
13740
Jan Kiszkaf41245002014-03-07 20:03:13 +010013741static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
13742{
13743 ktime_t remaining =
13744 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
13745 u64 value;
13746
13747 if (ktime_to_ns(remaining) <= 0)
13748 return 0;
13749
13750 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
13751 do_div(value, 1000000);
13752 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
13753}
13754
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013755/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080013756 * Update the guest state fields of vmcs12 to reflect changes that
13757 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
13758 * VM-entry controls is also updated, since this is really a guest
13759 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013760 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080013761static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013762{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013763 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
13764 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
13765
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013766 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
13767 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
13768 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
13769
13770 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
13771 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
13772 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
13773 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
13774 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
13775 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
13776 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
13777 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
13778 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
13779 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
13780 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
13781 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
13782 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
13783 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
13784 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
13785 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
13786 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
13787 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
13788 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
13789 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
13790 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
13791 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
13792 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
13793 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
13794 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
13795 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
13796 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
13797 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
13798 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
13799 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
13800 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
13801 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
13802 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
13803 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
13804 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
13805 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
13806
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013807 vmcs12->guest_interruptibility_info =
13808 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
13809 vmcs12->guest_pending_dbg_exceptions =
13810 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010013811 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
13812 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
13813 else
13814 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013815
Jan Kiszkaf41245002014-03-07 20:03:13 +010013816 if (nested_cpu_has_preemption_timer(vmcs12)) {
13817 if (vmcs12->vm_exit_controls &
13818 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
13819 vmcs12->vmx_preemption_timer_value =
13820 vmx_get_preemption_timer_value(vcpu);
13821 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
13822 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080013823
Nadav Har'El3633cfc2013-08-05 11:07:07 +030013824 /*
13825 * In some cases (usually, nested EPT), L2 is allowed to change its
13826 * own CR3 without exiting. If it has changed it, we must keep it.
13827 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
13828 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
13829 *
13830 * Additionally, restore L2's PDPTR to vmcs12.
13831 */
13832 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010013833 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030013834 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
13835 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
13836 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
13837 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
13838 }
13839
Jim Mattsond281e132017-06-01 12:44:46 -070013840 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030013841
Wincy Van608406e2015-02-03 23:57:51 +080013842 if (nested_cpu_has_vid(vmcs12))
13843 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
13844
Jan Kiszkac18911a2013-03-13 16:06:41 +010013845 vmcs12->vm_entry_controls =
13846 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020013847 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010013848
Jan Kiszka2996fca2014-06-16 13:59:43 +020013849 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
13850 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
13851 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
13852 }
13853
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013854 /* TODO: These cannot have changed unless we have MSR bitmaps and
13855 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020013856 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013857 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020013858 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
13859 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013860 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
13861 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
13862 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010013863 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010013864 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080013865}
13866
13867/*
13868 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
13869 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
13870 * and this function updates it to reflect the changes to the guest state while
13871 * L2 was running (and perhaps made some exits which were handled directly by L0
13872 * without going back to L1), and to reflect the exit reason.
13873 * Note that we do not have to copy here all VMCS fields, just those that
13874 * could have changed by the L2 guest or the exit - i.e., the guest-state and
13875 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
13876 * which already writes to vmcs12 directly.
13877 */
13878static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
13879 u32 exit_reason, u32 exit_intr_info,
13880 unsigned long exit_qualification)
13881{
13882 /* update guest state fields: */
13883 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013884
13885 /* update exit information fields: */
13886
Jan Kiszka533558b2014-01-04 18:47:20 +010013887 vmcs12->vm_exit_reason = exit_reason;
13888 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010013889 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020013890
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013891 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013892 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
13893 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
13894
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013895 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070013896 vmcs12->launch_state = 1;
13897
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013898 /* vm_entry_intr_info_field is cleared on exit. Emulate this
13899 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013900 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013901
13902 /*
13903 * Transfer the event that L0 or L1 may wanted to inject into
13904 * L2 to IDT_VECTORING_INFO_FIELD.
13905 */
13906 vmcs12_save_pending_event(vcpu, vmcs12);
13907 }
13908
13909 /*
13910 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
13911 * preserved above and would only end up incorrectly in L1.
13912 */
13913 vcpu->arch.nmi_injected = false;
13914 kvm_clear_exception_queue(vcpu);
13915 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013916}
13917
13918/*
13919 * A part of what we need to when the nested L2 guest exits and we want to
13920 * run its L1 parent, is to reset L1's guest state to the host state specified
13921 * in vmcs12.
13922 * This function is to be called not only on normal nested exit, but also on
13923 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
13924 * Failures During or After Loading Guest State").
13925 * This function should be called when the active VMCS is L1's (vmcs01).
13926 */
Jan Kiszka733568f2013-02-23 15:07:47 +010013927static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
13928 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013929{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013930 struct kvm_segment seg;
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013931 u32 entry_failure_code;
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013932
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013933 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
13934 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020013935 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013936 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
13937 else
13938 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
13939 vmx_set_efer(vcpu, vcpu->arch.efer);
13940
13941 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
13942 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070013943 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Sean Christophersoncb61de22018-09-26 09:23:53 -070013944 vmx_set_interrupt_shadow(vcpu, 0);
13945
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013946 /*
13947 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013948 * actually changed, because vmx_set_cr0 refers to efer set above.
13949 *
13950 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
13951 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013952 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013953 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4dbf2013-09-03 21:11:45 +020013954 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013955
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013956 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013957 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080013958 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013959
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013960 nested_ept_uninit_mmu_context(vcpu);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030013961
Liran Alon6f1e03b2018-05-22 17:16:14 +030013962 /*
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013963 * Only PDPTE load can fail as the value of cr3 was checked on entry and
13964 * couldn't have changed.
13965 */
13966 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
13967 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
13968
13969 if (!enable_ept)
13970 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
Jan Kiszka44811c02013-08-04 17:17:27 +020013971
Liran Alon6f1e03b2018-05-22 17:16:14 +030013972 /*
Liran Alonefebf0a2018-10-08 23:42:20 +030013973 * If vmcs01 doesn't use VPID, CPU flushes TLB on every
Liran Alon6f1e03b2018-05-22 17:16:14 +030013974 * VMEntry/VMExit. Thus, no need to flush TLB.
13975 *
Liran Alonefebf0a2018-10-08 23:42:20 +030013976 * If vmcs12 doesn't use VPID, L1 expects TLB to be
13977 * flushed on every VMEntry/VMExit.
Liran Alon6f1e03b2018-05-22 17:16:14 +030013978 *
Liran Alonefebf0a2018-10-08 23:42:20 +030013979 * Otherwise, we can preserve TLB entries as long as we are
13980 * able to tag L1 TLB entries differently than L2 TLB entries.
Liran Alon14389212018-10-08 23:42:17 +030013981 *
13982 * If vmcs12 uses EPT, we need to execute this flush on EPTP01
13983 * and therefore we request the TLB flush to happen only after VMCS EPTP
13984 * has been set by KVM_REQ_LOAD_CR3.
Liran Alon6f1e03b2018-05-22 17:16:14 +030013985 */
13986 if (enable_vpid &&
Liran Alonefebf0a2018-10-08 23:42:20 +030013987 (!nested_cpu_has_vpid(vmcs12) || !nested_has_guest_tlb_tag(vcpu))) {
Liran Alon14389212018-10-08 23:42:17 +030013988 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013989 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013990
13991 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
13992 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
13993 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
13994 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013995 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020013996 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
13997 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013998
Paolo Bonzini36be0b92014-02-24 12:30:04 +010013999 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
14000 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
14001 vmcs_write64(GUEST_BNDCFGS, 0);
14002
Nadav Har'El4704d0b2011-05-25 23:11:34 +030014003 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
14004 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
14005 vcpu->arch.pat = vmcs12->host_ia32_pat;
14006 }
Jan Kiszka503cd0c2013-03-03 13:05:44 +010014007 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080014008 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
14009 vmcs12->host_ia32_perf_global_ctrl);
14010
14011 /* Set L1 segment info according to Intel SDM
14012 27.5.2 Loading Host Segment and Descriptor-Table Registers */
14013 seg = (struct kvm_segment) {
14014 .base = 0,
14015 .limit = 0xFFFFFFFF,
14016 .selector = vmcs12->host_cs_selector,
14017 .type = 11,
14018 .present = 1,
14019 .s = 1,
14020 .g = 1
14021 };
14022 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
14023 seg.l = 1;
14024 else
14025 seg.db = 1;
14026 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
14027 seg = (struct kvm_segment) {
14028 .base = 0,
14029 .limit = 0xFFFFFFFF,
14030 .type = 3,
14031 .present = 1,
14032 .s = 1,
14033 .db = 1,
14034 .g = 1
14035 };
14036 seg.selector = vmcs12->host_ds_selector;
14037 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
14038 seg.selector = vmcs12->host_es_selector;
14039 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
14040 seg.selector = vmcs12->host_ss_selector;
14041 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
14042 seg.selector = vmcs12->host_fs_selector;
14043 seg.base = vmcs12->host_fs_base;
14044 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
14045 seg.selector = vmcs12->host_gs_selector;
Gleb Natapov205befd2013-08-04 15:08:06 +030014046 seg.base = vmcs12->host_gs_base;
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080014047 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
14048 seg = (struct kvm_segment) {
14049 .base = vmcs12->host_tr_base,
14050 .limit = 0x67,
14051 .selector = vmcs12->host_tr_selector,
14052 .type = 11,
14053 .present = 1
Jan Kiszka503cd0c2013-03-03 13:05:44 +010014054 };
14055 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030014056
14057 kvm_set_dr(vcpu, 7, 0x400);
14058 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030014059
Wincy Van3af18d92015-02-03 23:49:31 +080014060 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010014061 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080014062
Wincy Vanff651cb2014-12-11 08:52:58 +030014063 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
14064 vmcs12->vm_exit_msr_load_count))
14065 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030014066}
14067
Sean Christophersonbd18bff2018-08-22 14:57:07 -070014068static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx)
14069{
14070 struct shared_msr_entry *efer_msr;
14071 unsigned int i;
14072
14073 if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER)
14074 return vmcs_read64(GUEST_IA32_EFER);
14075
14076 if (cpu_has_load_ia32_efer)
14077 return host_efer;
14078
14079 for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) {
14080 if (vmx->msr_autoload.guest.val[i].index == MSR_EFER)
14081 return vmx->msr_autoload.guest.val[i].value;
14082 }
14083
14084 efer_msr = find_msr_entry(vmx, MSR_EFER);
14085 if (efer_msr)
14086 return efer_msr->data;
14087
14088 return host_efer;
14089}
14090
14091static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu)
14092{
14093 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
14094 struct vcpu_vmx *vmx = to_vmx(vcpu);
14095 struct vmx_msr_entry g, h;
14096 struct msr_data msr;
14097 gpa_t gpa;
14098 u32 i, j;
14099
14100 vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT);
14101
14102 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
14103 /*
14104 * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set
14105 * as vmcs01.GUEST_DR7 contains a userspace defined value
14106 * and vcpu->arch.dr7 is not squirreled away before the
14107 * nested VMENTER (not worth adding a variable in nested_vmx).
14108 */
14109 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
14110 kvm_set_dr(vcpu, 7, DR7_FIXED_1);
14111 else
14112 WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7)));
14113 }
14114
14115 /*
14116 * Note that calling vmx_set_{efer,cr0,cr4} is important as they
14117 * handle a variety of side effects to KVM's software model.
14118 */
14119 vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx));
14120
14121 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
14122 vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW));
14123
14124 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
14125 vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW));
14126
14127 nested_ept_uninit_mmu_context(vcpu);
14128 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
14129 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
14130
14131 /*
14132 * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs
14133 * from vmcs01 (if necessary). The PDPTRs are not loaded on
14134 * VMFail, like everything else we just need to ensure our
14135 * software model is up-to-date.
14136 */
14137 ept_save_pdptrs(vcpu);
14138
14139 kvm_mmu_reset_context(vcpu);
14140
14141 if (cpu_has_vmx_msr_bitmap())
14142 vmx_update_msr_bitmap(vcpu);
14143
14144 /*
14145 * This nasty bit of open coding is a compromise between blindly
14146 * loading L1's MSRs using the exit load lists (incorrect emulation
14147 * of VMFail), leaving the nested VM's MSRs in the software model
14148 * (incorrect behavior) and snapshotting the modified MSRs (too
14149 * expensive since the lists are unbound by hardware). For each
14150 * MSR that was (prematurely) loaded from the nested VMEntry load
14151 * list, reload it from the exit load list if it exists and differs
14152 * from the guest value. The intent is to stuff host state as
14153 * silently as possible, not to fully process the exit load list.
14154 */
14155 msr.host_initiated = false;
14156 for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) {
14157 gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g));
14158 if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) {
14159 pr_debug_ratelimited(
14160 "%s read MSR index failed (%u, 0x%08llx)\n",
14161 __func__, i, gpa);
14162 goto vmabort;
14163 }
14164
14165 for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) {
14166 gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h));
14167 if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) {
14168 pr_debug_ratelimited(
14169 "%s read MSR failed (%u, 0x%08llx)\n",
14170 __func__, j, gpa);
14171 goto vmabort;
14172 }
14173 if (h.index != g.index)
14174 continue;
14175 if (h.value == g.value)
14176 break;
14177
14178 if (nested_vmx_load_msr_check(vcpu, &h)) {
14179 pr_debug_ratelimited(
14180 "%s check failed (%u, 0x%x, 0x%x)\n",
14181 __func__, j, h.index, h.reserved);
14182 goto vmabort;
14183 }
14184
14185 msr.index = h.index;
14186 msr.data = h.value;
14187 if (kvm_set_msr(vcpu, &msr)) {
14188 pr_debug_ratelimited(
14189 "%s WRMSR failed (%u, 0x%x, 0x%llx)\n",
14190 __func__, j, h.index, h.value);
14191 goto vmabort;
14192 }
14193 }
14194 }
14195
14196 return;
14197
14198vmabort:
14199 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
14200}
14201
Nadav Har'El4704d0b2011-05-25 23:11:34 +030014202/*
14203 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
14204 * and modify vmcs12 to make it see what it would expect to see there if
14205 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
14206 */
Jan Kiszka533558b2014-01-04 18:47:20 +010014207static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
14208 u32 exit_intr_info,
14209 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030014210{
14211 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030014212 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
14213
Jan Kiszka5f3d5792013-04-14 12:12:46 +020014214 /* trying to cancel vmlaunch/vmresume is a bug */
14215 WARN_ON_ONCE(vmx->nested.nested_run_pending);
14216
Jim Mattson4f350c62017-09-14 16:31:44 -070014217 leave_guest_mode(vcpu);
14218
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020014219 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
14220 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
14221
Jim Mattson4f350c62017-09-14 16:31:44 -070014222 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014223 if (exit_reason == -1)
14224 sync_vmcs12(vcpu, vmcs12);
14225 else
14226 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
14227 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070014228
Liran Alon61ada742018-06-23 02:35:08 +030014229 /*
14230 * Must happen outside of sync_vmcs12() as it will
14231 * also be used to capture vmcs12 cache as part of
14232 * capturing nVMX state for snapshot (migration).
14233 *
14234 * Otherwise, this flush will dirty guest memory at a
14235 * point it is already assumed by user-space to be
14236 * immutable.
14237 */
14238 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
14239
Jim Mattson4f350c62017-09-14 16:31:44 -070014240 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
14241 vmcs12->vm_exit_msr_store_count))
14242 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Sean Christopherson2768c0c2018-09-26 09:23:58 -070014243 } else {
14244 /*
14245 * The only expected VM-instruction error is "VM entry with
14246 * invalid control field(s)." Anything else indicates a
14247 * problem with L0. And we should never get here with a
14248 * VMFail of any type if early consistency checks are enabled.
14249 */
14250 WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
14251 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
14252 WARN_ON_ONCE(nested_early_check);
Bandan Das77b0f5d2014-04-19 18:17:45 -040014253 }
14254
Jim Mattson4f350c62017-09-14 16:31:44 -070014255 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010014256
Paolo Bonzini9314006db2016-07-06 13:23:51 +020014257 /* Update any VMCS fields that might have changed while L2 ran */
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040014258 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
14259 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010014260 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Sean Christophersonf459a702018-08-27 15:21:11 -070014261
Peter Feinerc95ba922016-08-17 09:36:47 -070014262 if (kvm_has_tsc_control)
14263 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030014264
Jim Mattson8d860bb2018-05-09 16:56:05 -040014265 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
14266 vmx->nested.change_vmcs01_virtual_apic_mode = false;
14267 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070014268 } else if (!nested_cpu_has_ept(vmcs12) &&
14269 nested_cpu_has2(vmcs12,
14270 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070014271 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020014272 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030014273
14274 /* This is needed for same reason as it was needed in prepare_vmcs02 */
14275 vmx->host_rsp = 0;
14276
14277 /* Unpin physical memory we referred to in vmcs02 */
14278 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020014279 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020014280 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030014281 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080014282 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020014283 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020014284 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080014285 }
Wincy Van705699a2015-02-03 23:58:17 +080014286 if (vmx->nested.pi_desc_page) {
14287 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020014288 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080014289 vmx->nested.pi_desc_page = NULL;
14290 vmx->nested.pi_desc = NULL;
14291 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030014292
14293 /*
Tang Chen38b99172014-09-24 15:57:54 +080014294 * We are now running in L2, mmu_notifier will force to reload the
14295 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
14296 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080014297 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080014298
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020014299 if ((exit_reason != -1) && (enable_shadow_vmcs || vmx->nested.hv_evmcs))
14300 vmx->nested.need_vmcs12_sync = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010014301
14302 /* in case we halted in L2 */
14303 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070014304
14305 if (likely(!vmx->fail)) {
14306 /*
14307 * TODO: SDM says that with acknowledge interrupt on
14308 * exit, bit 31 of the VM-exit interrupt information
14309 * (valid interrupt) is always set to 1 on
14310 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
14311 * need kvm_cpu_has_interrupt(). See the commit
14312 * message for details.
14313 */
14314 if (nested_exit_intr_ack_set(vcpu) &&
14315 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
14316 kvm_cpu_has_interrupt(vcpu)) {
14317 int irq = kvm_cpu_get_interrupt(vcpu);
14318 WARN_ON(irq < 0);
14319 vmcs12->vm_exit_intr_info = irq |
14320 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
14321 }
14322
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014323 if (exit_reason != -1)
14324 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
14325 vmcs12->exit_qualification,
14326 vmcs12->idt_vectoring_info_field,
14327 vmcs12->vm_exit_intr_info,
14328 vmcs12->vm_exit_intr_error_code,
14329 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070014330
14331 load_vmcs12_host_state(vcpu, vmcs12);
14332
14333 return;
14334 }
Sean Christopherson09abb5e2018-09-26 09:23:55 -070014335
Jim Mattson4f350c62017-09-14 16:31:44 -070014336 /*
14337 * After an early L2 VM-entry failure, we're now back
14338 * in L1 which thinks it just finished a VMLAUNCH or
14339 * VMRESUME instruction, so we need to set the failure
14340 * flag and the VM-instruction error field of the VMCS
Sean Christophersoncb61de22018-09-26 09:23:53 -070014341 * accordingly, and skip the emulated instruction.
Jim Mattson4f350c62017-09-14 16:31:44 -070014342 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -070014343 (void)nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080014344
Jim Mattson4f350c62017-09-14 16:31:44 -070014345 /*
Sean Christophersonbd18bff2018-08-22 14:57:07 -070014346 * Restore L1's host state to KVM's software model. We're here
14347 * because a consistency check was caught by hardware, which
14348 * means some amount of guest state has been propagated to KVM's
14349 * model and needs to be unwound to the host's state.
Jim Mattson4f350c62017-09-14 16:31:44 -070014350 */
Sean Christophersonbd18bff2018-08-22 14:57:07 -070014351 nested_vmx_restore_host_state(vcpu);
Jim Mattson4f350c62017-09-14 16:31:44 -070014352
Jim Mattson4f350c62017-09-14 16:31:44 -070014353 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030014354}
14355
Nadav Har'El7c177932011-05-25 23:12:04 +030014356/*
Jan Kiszka42124922014-01-04 18:47:19 +010014357 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
14358 */
14359static void vmx_leave_nested(struct kvm_vcpu *vcpu)
14360{
Wanpeng Li2f707d92017-03-06 04:03:28 -080014361 if (is_guest_mode(vcpu)) {
14362 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010014363 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080014364 }
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +020014365 free_nested(vcpu);
Nadav Har'El7c177932011-05-25 23:12:04 +030014366}
14367
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020014368static int vmx_check_intercept(struct kvm_vcpu *vcpu,
14369 struct x86_instruction_info *info,
14370 enum x86_intercept_stage stage)
14371{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020014372 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
14373 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
14374
14375 /*
14376 * RDPID causes #UD if disabled through secondary execution controls.
14377 * Because it is marked as EmulateOnUD, we need to intercept it here.
14378 */
14379 if (info->intercept == x86_intercept_rdtscp &&
14380 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
14381 ctxt->exception.vector = UD_VECTOR;
14382 ctxt->exception.error_code_valid = false;
14383 return X86EMUL_PROPAGATE_FAULT;
14384 }
14385
14386 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020014387 return X86EMUL_CONTINUE;
14388}
14389
Yunhong Jiang64672c92016-06-13 14:19:59 -070014390#ifdef CONFIG_X86_64
14391/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
14392static inline int u64_shl_div_u64(u64 a, unsigned int shift,
14393 u64 divisor, u64 *result)
14394{
14395 u64 low = a << shift, high = a >> (64 - shift);
14396
14397 /* To avoid the overflow on divq */
14398 if (high >= divisor)
14399 return 1;
14400
14401 /* Low hold the result, high hold rem which is discarded */
14402 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
14403 "rm" (divisor), "0" (low), "1" (high));
14404 *result = low;
14405
14406 return 0;
14407}
14408
14409static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
14410{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020014411 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080014412 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020014413
14414 if (kvm_mwait_in_guest(vcpu->kvm))
14415 return -EOPNOTSUPP;
14416
14417 vmx = to_vmx(vcpu);
14418 tscl = rdtsc();
14419 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
14420 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080014421 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
14422
14423 if (delta_tsc > lapic_timer_advance_cycles)
14424 delta_tsc -= lapic_timer_advance_cycles;
14425 else
14426 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070014427
14428 /* Convert to host delta tsc if tsc scaling is enabled */
14429 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
14430 u64_shl_div_u64(delta_tsc,
14431 kvm_tsc_scaling_ratio_frac_bits,
14432 vcpu->arch.tsc_scaling_ratio,
14433 &delta_tsc))
14434 return -ERANGE;
14435
14436 /*
14437 * If the delta tsc can't fit in the 32 bit after the multi shift,
14438 * we can't use the preemption timer.
14439 * It's possible that it fits on later vmentries, but checking
14440 * on every vmentry is costly so we just use an hrtimer.
14441 */
14442 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
14443 return -ERANGE;
14444
14445 vmx->hv_deadline_tsc = tscl + delta_tsc;
Wanpeng Lic8533542017-06-29 06:28:09 -070014446 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070014447}
14448
14449static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
14450{
Sean Christophersonf459a702018-08-27 15:21:11 -070014451 to_vmx(vcpu)->hv_deadline_tsc = -1;
Yunhong Jiang64672c92016-06-13 14:19:59 -070014452}
14453#endif
14454
Paolo Bonzini48d89b92014-08-26 13:27:46 +020014455static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020014456{
Wanpeng Lib31c1142018-03-12 04:53:04 -070014457 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020014458 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020014459}
14460
Kai Huang843e4332015-01-28 10:54:28 +080014461static void vmx_slot_enable_log_dirty(struct kvm *kvm,
14462 struct kvm_memory_slot *slot)
14463{
14464 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
14465 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
14466}
14467
14468static void vmx_slot_disable_log_dirty(struct kvm *kvm,
14469 struct kvm_memory_slot *slot)
14470{
14471 kvm_mmu_slot_set_dirty(kvm, slot);
14472}
14473
14474static void vmx_flush_log_dirty(struct kvm *kvm)
14475{
14476 kvm_flush_pml_buffers(kvm);
14477}
14478
Bandan Dasc5f983f2017-05-05 15:25:14 -040014479static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
14480{
14481 struct vmcs12 *vmcs12;
14482 struct vcpu_vmx *vmx = to_vmx(vcpu);
14483 gpa_t gpa;
14484 struct page *page = NULL;
14485 u64 *pml_address;
14486
14487 if (is_guest_mode(vcpu)) {
14488 WARN_ON_ONCE(vmx->nested.pml_full);
14489
14490 /*
14491 * Check if PML is enabled for the nested guest.
14492 * Whether eptp bit 6 is set is already checked
14493 * as part of A/D emulation.
14494 */
14495 vmcs12 = get_vmcs12(vcpu);
14496 if (!nested_cpu_has_pml(vmcs12))
14497 return 0;
14498
Dan Carpenter47698862017-05-10 22:43:17 +030014499 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040014500 vmx->nested.pml_full = true;
14501 return 1;
14502 }
14503
14504 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
14505
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020014506 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
14507 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040014508 return 0;
14509
14510 pml_address = kmap(page);
14511 pml_address[vmcs12->guest_pml_index--] = gpa;
14512 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020014513 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040014514 }
14515
14516 return 0;
14517}
14518
Kai Huang843e4332015-01-28 10:54:28 +080014519static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
14520 struct kvm_memory_slot *memslot,
14521 gfn_t offset, unsigned long mask)
14522{
14523 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
14524}
14525
Paolo Bonzinicd39e112017-06-06 12:57:04 +020014526static void __pi_post_block(struct kvm_vcpu *vcpu)
14527{
14528 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
14529 struct pi_desc old, new;
14530 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020014531
14532 do {
14533 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020014534 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
14535 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020014536
14537 dest = cpu_physical_id(vcpu->cpu);
14538
14539 if (x2apic_enabled())
14540 new.ndst = dest;
14541 else
14542 new.ndst = (dest << 8) & 0xFF00;
14543
Paolo Bonzinicd39e112017-06-06 12:57:04 +020014544 /* set 'NV' to 'notification vector' */
14545 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020014546 } while (cmpxchg64(&pi_desc->control, old.control,
14547 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020014548
Paolo Bonzini8b306e22017-06-06 12:57:05 +020014549 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
14550 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020014551 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020014552 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020014553 vcpu->pre_pcpu = -1;
14554 }
14555}
14556
Feng Wuefc64402015-09-18 22:29:51 +080014557/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080014558 * This routine does the following things for vCPU which is going
14559 * to be blocked if VT-d PI is enabled.
14560 * - Store the vCPU to the wakeup list, so when interrupts happen
14561 * we can find the right vCPU to wake up.
14562 * - Change the Posted-interrupt descriptor as below:
14563 * 'NDST' <-- vcpu->pre_pcpu
14564 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
14565 * - If 'ON' is set during this process, which means at least one
14566 * interrupt is posted for this vCPU, we cannot block it, in
14567 * this case, return 1, otherwise, return 0.
14568 *
14569 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070014570static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080014571{
Feng Wubf9f6ac2015-09-18 22:29:55 +080014572 unsigned int dest;
14573 struct pi_desc old, new;
14574 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
14575
14576 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080014577 !irq_remapping_cap(IRQ_POSTING_CAP) ||
14578 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080014579 return 0;
14580
Paolo Bonzini8b306e22017-06-06 12:57:05 +020014581 WARN_ON(irqs_disabled());
14582 local_irq_disable();
14583 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
14584 vcpu->pre_pcpu = vcpu->cpu;
14585 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
14586 list_add_tail(&vcpu->blocked_vcpu_list,
14587 &per_cpu(blocked_vcpu_on_cpu,
14588 vcpu->pre_pcpu));
14589 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
14590 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080014591
14592 do {
14593 old.control = new.control = pi_desc->control;
14594
Feng Wubf9f6ac2015-09-18 22:29:55 +080014595 WARN((pi_desc->sn == 1),
14596 "Warning: SN field of posted-interrupts "
14597 "is set before blocking\n");
14598
14599 /*
14600 * Since vCPU can be preempted during this process,
14601 * vcpu->cpu could be different with pre_pcpu, we
14602 * need to set pre_pcpu as the destination of wakeup
14603 * notification event, then we can find the right vCPU
14604 * to wakeup in wakeup handler if interrupts happen
14605 * when the vCPU is in blocked state.
14606 */
14607 dest = cpu_physical_id(vcpu->pre_pcpu);
14608
14609 if (x2apic_enabled())
14610 new.ndst = dest;
14611 else
14612 new.ndst = (dest << 8) & 0xFF00;
14613
14614 /* set 'NV' to 'wakeup vector' */
14615 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020014616 } while (cmpxchg64(&pi_desc->control, old.control,
14617 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080014618
Paolo Bonzini8b306e22017-06-06 12:57:05 +020014619 /* We should not block the vCPU if an interrupt is posted for it. */
14620 if (pi_test_on(pi_desc) == 1)
14621 __pi_post_block(vcpu);
14622
14623 local_irq_enable();
14624 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080014625}
14626
Yunhong Jiangbc225122016-06-13 14:19:58 -070014627static int vmx_pre_block(struct kvm_vcpu *vcpu)
14628{
14629 if (pi_pre_block(vcpu))
14630 return 1;
14631
Yunhong Jiang64672c92016-06-13 14:19:59 -070014632 if (kvm_lapic_hv_timer_in_use(vcpu))
14633 kvm_lapic_switch_to_sw_timer(vcpu);
14634
Yunhong Jiangbc225122016-06-13 14:19:58 -070014635 return 0;
14636}
14637
14638static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080014639{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020014640 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080014641 return;
14642
Paolo Bonzini8b306e22017-06-06 12:57:05 +020014643 WARN_ON(irqs_disabled());
14644 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020014645 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020014646 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080014647}
14648
Yunhong Jiangbc225122016-06-13 14:19:58 -070014649static void vmx_post_block(struct kvm_vcpu *vcpu)
14650{
Yunhong Jiang64672c92016-06-13 14:19:59 -070014651 if (kvm_x86_ops->set_hv_timer)
14652 kvm_lapic_switch_to_hv_timer(vcpu);
14653
Yunhong Jiangbc225122016-06-13 14:19:58 -070014654 pi_post_block(vcpu);
14655}
14656
Feng Wubf9f6ac2015-09-18 22:29:55 +080014657/*
Feng Wuefc64402015-09-18 22:29:51 +080014658 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
14659 *
14660 * @kvm: kvm
14661 * @host_irq: host irq of the interrupt
14662 * @guest_irq: gsi of the interrupt
14663 * @set: set or unset PI
14664 * returns 0 on success, < 0 on failure
14665 */
14666static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
14667 uint32_t guest_irq, bool set)
14668{
14669 struct kvm_kernel_irq_routing_entry *e;
14670 struct kvm_irq_routing_table *irq_rt;
14671 struct kvm_lapic_irq irq;
14672 struct kvm_vcpu *vcpu;
14673 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010014674 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080014675
14676 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080014677 !irq_remapping_cap(IRQ_POSTING_CAP) ||
14678 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080014679 return 0;
14680
14681 idx = srcu_read_lock(&kvm->irq_srcu);
14682 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010014683 if (guest_irq >= irq_rt->nr_rt_entries ||
14684 hlist_empty(&irq_rt->map[guest_irq])) {
14685 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
14686 guest_irq, irq_rt->nr_rt_entries);
14687 goto out;
14688 }
Feng Wuefc64402015-09-18 22:29:51 +080014689
14690 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
14691 if (e->type != KVM_IRQ_ROUTING_MSI)
14692 continue;
14693 /*
14694 * VT-d PI cannot support posting multicast/broadcast
14695 * interrupts to a vCPU, we still use interrupt remapping
14696 * for these kind of interrupts.
14697 *
14698 * For lowest-priority interrupts, we only support
14699 * those with single CPU as the destination, e.g. user
14700 * configures the interrupts via /proc/irq or uses
14701 * irqbalance to make the interrupts single-CPU.
14702 *
14703 * We will support full lowest-priority interrupt later.
14704 */
14705
Radim Krčmář371313132016-07-12 22:09:27 +020014706 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080014707 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
14708 /*
14709 * Make sure the IRTE is in remapped mode if
14710 * we don't handle it in posted mode.
14711 */
14712 ret = irq_set_vcpu_affinity(host_irq, NULL);
14713 if (ret < 0) {
14714 printk(KERN_INFO
14715 "failed to back to remapped mode, irq: %u\n",
14716 host_irq);
14717 goto out;
14718 }
14719
Feng Wuefc64402015-09-18 22:29:51 +080014720 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080014721 }
Feng Wuefc64402015-09-18 22:29:51 +080014722
14723 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
14724 vcpu_info.vector = irq.vector;
14725
hu huajun2698d822018-04-11 15:16:40 +080014726 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080014727 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
14728
14729 if (set)
14730 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080014731 else
Feng Wuefc64402015-09-18 22:29:51 +080014732 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080014733
14734 if (ret < 0) {
14735 printk(KERN_INFO "%s: failed to update PI IRTE\n",
14736 __func__);
14737 goto out;
14738 }
14739 }
14740
14741 ret = 0;
14742out:
14743 srcu_read_unlock(&kvm->irq_srcu, idx);
14744 return ret;
14745}
14746
Ashok Rajc45dcc72016-06-22 14:59:56 +080014747static void vmx_setup_mce(struct kvm_vcpu *vcpu)
14748{
14749 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
14750 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
14751 FEATURE_CONTROL_LMCE;
14752 else
14753 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
14754 ~FEATURE_CONTROL_LMCE;
14755}
14756
Ladi Prosek72d7b372017-10-11 16:54:41 +020014757static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
14758{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014759 /* we need a nested vmexit to enter SMM, postpone if run is pending */
14760 if (to_vmx(vcpu)->nested.nested_run_pending)
14761 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020014762 return 1;
14763}
14764
Ladi Prosek0234bf82017-10-11 16:54:40 +020014765static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
14766{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014767 struct vcpu_vmx *vmx = to_vmx(vcpu);
14768
14769 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
14770 if (vmx->nested.smm.guest_mode)
14771 nested_vmx_vmexit(vcpu, -1, 0, 0);
14772
14773 vmx->nested.smm.vmxon = vmx->nested.vmxon;
14774 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070014775 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020014776 return 0;
14777}
14778
14779static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
14780{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014781 struct vcpu_vmx *vmx = to_vmx(vcpu);
14782 int ret;
14783
14784 if (vmx->nested.smm.vmxon) {
14785 vmx->nested.vmxon = true;
14786 vmx->nested.smm.vmxon = false;
14787 }
14788
14789 if (vmx->nested.smm.guest_mode) {
14790 vcpu->arch.hflags &= ~HF_SMM_MASK;
Sean Christophersona633e412018-09-26 09:23:47 -070014791 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014792 vcpu->arch.hflags |= HF_SMM_MASK;
14793 if (ret)
14794 return ret;
14795
14796 vmx->nested.smm.guest_mode = false;
14797 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020014798 return 0;
14799}
14800
Ladi Prosekcc3d9672017-10-17 16:02:39 +020014801static int enable_smi_window(struct kvm_vcpu *vcpu)
14802{
14803 return 0;
14804}
14805
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020014806static inline int vmx_has_valid_vmcs12(struct kvm_vcpu *vcpu)
14807{
14808 struct vcpu_vmx *vmx = to_vmx(vcpu);
14809
14810 /*
14811 * In case we do two consecutive get/set_nested_state()s while L2 was
14812 * running hv_evmcs may end up not being mapped (we map it from
14813 * nested_vmx_run()/vmx_vcpu_run()). Check is_guest_mode() as we always
14814 * have vmcs12 if it is true.
14815 */
14816 return is_guest_mode(vcpu) || vmx->nested.current_vmptr != -1ull ||
14817 vmx->nested.hv_evmcs;
14818}
14819
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014820static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
14821 struct kvm_nested_state __user *user_kvm_nested_state,
14822 u32 user_data_size)
14823{
14824 struct vcpu_vmx *vmx;
14825 struct vmcs12 *vmcs12;
14826 struct kvm_nested_state kvm_state = {
14827 .flags = 0,
14828 .format = 0,
14829 .size = sizeof(kvm_state),
14830 .vmx.vmxon_pa = -1ull,
14831 .vmx.vmcs_pa = -1ull,
14832 };
14833
14834 if (!vcpu)
14835 return kvm_state.size + 2 * VMCS12_SIZE;
14836
14837 vmx = to_vmx(vcpu);
14838 vmcs12 = get_vmcs12(vcpu);
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020014839
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020014840 if (nested_vmx_allowed(vcpu) && vmx->nested.enlightened_vmcs_enabled)
14841 kvm_state.flags |= KVM_STATE_NESTED_EVMCS;
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020014842
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014843 if (nested_vmx_allowed(vcpu) &&
14844 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
14845 kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
14846 kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
14847
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020014848 if (vmx_has_valid_vmcs12(vcpu)) {
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014849 kvm_state.size += VMCS12_SIZE;
14850
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014851 if (is_guest_mode(vcpu) &&
14852 nested_cpu_has_shadow_vmcs(vmcs12) &&
14853 vmcs12->vmcs_link_pointer != -1ull)
14854 kvm_state.size += VMCS12_SIZE;
14855 }
14856
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014857 if (vmx->nested.smm.vmxon)
14858 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
14859
14860 if (vmx->nested.smm.guest_mode)
14861 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
14862
14863 if (is_guest_mode(vcpu)) {
14864 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
14865
14866 if (vmx->nested.nested_run_pending)
14867 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
14868 }
14869 }
14870
14871 if (user_data_size < kvm_state.size)
14872 goto out;
14873
14874 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
14875 return -EFAULT;
14876
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020014877 if (!vmx_has_valid_vmcs12(vcpu))
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014878 goto out;
14879
14880 /*
14881 * When running L2, the authoritative vmcs12 state is in the
14882 * vmcs02. When running L1, the authoritative vmcs12 state is
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020014883 * in the shadow or enlightened vmcs linked to vmcs01, unless
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020014884 * need_vmcs12_sync is set, in which case, the authoritative
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014885 * vmcs12 state is in the vmcs12 already.
14886 */
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020014887 if (is_guest_mode(vcpu)) {
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014888 sync_vmcs12(vcpu, vmcs12);
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020014889 } else if (!vmx->nested.need_vmcs12_sync) {
14890 if (vmx->nested.hv_evmcs)
14891 copy_enlightened_to_vmcs12(vmx);
14892 else if (enable_shadow_vmcs)
14893 copy_shadow_to_vmcs12(vmx);
14894 }
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014895
14896 if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
14897 return -EFAULT;
14898
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014899 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
14900 vmcs12->vmcs_link_pointer != -1ull) {
14901 if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
14902 get_shadow_vmcs12(vcpu), sizeof(*vmcs12)))
14903 return -EFAULT;
14904 }
14905
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014906out:
14907 return kvm_state.size;
14908}
14909
14910static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
14911 struct kvm_nested_state __user *user_kvm_nested_state,
14912 struct kvm_nested_state *kvm_state)
14913{
14914 struct vcpu_vmx *vmx = to_vmx(vcpu);
14915 struct vmcs12 *vmcs12;
14916 u32 exit_qual;
14917 int ret;
14918
14919 if (kvm_state->format != 0)
14920 return -EINVAL;
14921
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020014922 if (kvm_state->flags & KVM_STATE_NESTED_EVMCS)
14923 nested_enable_evmcs(vcpu, NULL);
14924
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014925 if (!nested_vmx_allowed(vcpu))
14926 return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
14927
14928 if (kvm_state->vmx.vmxon_pa == -1ull) {
14929 if (kvm_state->vmx.smm.flags)
14930 return -EINVAL;
14931
14932 if (kvm_state->vmx.vmcs_pa != -1ull)
14933 return -EINVAL;
14934
14935 vmx_leave_nested(vcpu);
14936 return 0;
14937 }
14938
14939 if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
14940 return -EINVAL;
14941
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014942 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
14943 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
14944 return -EINVAL;
14945
14946 if (kvm_state->vmx.smm.flags &
14947 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
14948 return -EINVAL;
14949
Paolo Bonzini5bea5122018-09-18 15:19:17 +020014950 /*
14951 * SMM temporarily disables VMX, so we cannot be in guest mode,
14952 * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
14953 * must be zero.
14954 */
14955 if (is_smm(vcpu) ? kvm_state->flags : kvm_state->vmx.smm.flags)
14956 return -EINVAL;
14957
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014958 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
14959 !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
14960 return -EINVAL;
14961
14962 vmx_leave_nested(vcpu);
14963 if (kvm_state->vmx.vmxon_pa == -1ull)
14964 return 0;
14965
14966 vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
14967 ret = enter_vmx_operation(vcpu);
14968 if (ret)
14969 return ret;
14970
Vitaly Kuznetsova1b0c1c2018-10-16 18:50:07 +020014971 /* Empty 'VMXON' state is permitted */
14972 if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
14973 return 0;
14974
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020014975 if (kvm_state->vmx.vmcs_pa != -1ull) {
14976 if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
14977 !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
14978 return -EINVAL;
Vitaly Kuznetsova1b0c1c2018-10-16 18:50:07 +020014979
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020014980 set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
14981 } else if (kvm_state->flags & KVM_STATE_NESTED_EVMCS) {
14982 /*
14983 * Sync eVMCS upon entry as we may not have
14984 * HV_X64_MSR_VP_ASSIST_PAGE set up yet.
14985 */
14986 vmx->nested.need_vmcs12_sync = true;
14987 } else {
14988 return -EINVAL;
14989 }
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014990
14991 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
14992 vmx->nested.smm.vmxon = true;
14993 vmx->nested.vmxon = false;
14994
14995 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
14996 vmx->nested.smm.guest_mode = true;
14997 }
14998
14999 vmcs12 = get_vmcs12(vcpu);
15000 if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
15001 return -EFAULT;
15002
Liran Alon392b2f22018-06-23 02:35:01 +030015003 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
Jim Mattson8fcc4b52018-07-10 11:27:20 +020015004 return -EINVAL;
15005
15006 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
15007 return 0;
15008
15009 vmx->nested.nested_run_pending =
15010 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
15011
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020015012 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
15013 vmcs12->vmcs_link_pointer != -1ull) {
15014 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
15015 if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12))
15016 return -EINVAL;
15017
15018 if (copy_from_user(shadow_vmcs12,
15019 user_kvm_nested_state->data + VMCS12_SIZE,
15020 sizeof(*vmcs12)))
15021 return -EFAULT;
15022
15023 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
15024 !shadow_vmcs12->hdr.shadow_vmcs)
15025 return -EINVAL;
15026 }
15027
Jim Mattson8fcc4b52018-07-10 11:27:20 +020015028 if (check_vmentry_prereqs(vcpu, vmcs12) ||
15029 check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
15030 return -EINVAL;
15031
Jim Mattson8fcc4b52018-07-10 11:27:20 +020015032 vmx->nested.dirty_vmcs12 = true;
Sean Christophersona633e412018-09-26 09:23:47 -070015033 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Jim Mattson8fcc4b52018-07-10 11:27:20 +020015034 if (ret)
15035 return -EINVAL;
15036
15037 return 0;
15038}
15039
Kees Cook404f6aa2016-08-08 16:29:06 -070015040static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080015041 .cpu_has_kvm_support = cpu_has_kvm_support,
15042 .disabled_by_bios = vmx_disabled_by_bios,
15043 .hardware_setup = hardware_setup,
15044 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030015045 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080015046 .hardware_enable = hardware_enable,
15047 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080015048 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020015049 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080015050
Wanpeng Lib31c1142018-03-12 04:53:04 -070015051 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070015052 .vm_alloc = vmx_vm_alloc,
15053 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070015054
Avi Kivity6aa8b732006-12-10 02:21:36 -080015055 .vcpu_create = vmx_create_vcpu,
15056 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030015057 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080015058
Sean Christopherson6d6095b2018-07-23 12:32:44 -070015059 .prepare_guest_switch = vmx_prepare_switch_to_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -080015060 .vcpu_load = vmx_vcpu_load,
15061 .vcpu_put = vmx_vcpu_put,
15062
Paolo Bonzinia96036b2015-11-10 11:55:36 +010015063 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060015064 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080015065 .get_msr = vmx_get_msr,
15066 .set_msr = vmx_set_msr,
15067 .get_segment_base = vmx_get_segment_base,
15068 .get_segment = vmx_get_segment,
15069 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020015070 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080015071 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020015072 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020015073 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030015074 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080015075 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080015076 .set_cr3 = vmx_set_cr3,
15077 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080015078 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080015079 .get_idt = vmx_get_idt,
15080 .set_idt = vmx_set_idt,
15081 .get_gdt = vmx_get_gdt,
15082 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010015083 .get_dr6 = vmx_get_dr6,
15084 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030015085 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010015086 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030015087 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080015088 .get_rflags = vmx_get_rflags,
15089 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080015090
Avi Kivity6aa8b732006-12-10 02:21:36 -080015091 .tlb_flush = vmx_flush_tlb,
Junaid Shahidfaff8752018-06-29 13:10:05 -070015092 .tlb_flush_gva = vmx_flush_tlb_gva,
Avi Kivity6aa8b732006-12-10 02:21:36 -080015093
Avi Kivity6aa8b732006-12-10 02:21:36 -080015094 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020015095 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080015096 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040015097 .set_interrupt_shadow = vmx_set_interrupt_shadow,
15098 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020015099 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030015100 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030015101 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020015102 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030015103 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020015104 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030015105 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010015106 .get_nmi_mask = vmx_get_nmi_mask,
15107 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030015108 .enable_nmi_window = enable_nmi_window,
15109 .enable_irq_window = enable_irq_window,
15110 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040015111 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080015112 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030015113 .get_enable_apicv = vmx_get_enable_apicv,
15114 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080015115 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010015116 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080015117 .hwapic_irr_update = vmx_hwapic_irr_update,
15118 .hwapic_isr_update = vmx_hwapic_isr_update,
Liran Alone6c67d82018-09-04 10:56:52 +030015119 .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt,
Yang Zhanga20ed542013-04-11 19:25:15 +080015120 .sync_pir_to_irr = vmx_sync_pir_to_irr,
15121 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030015122
Izik Eiduscbc94022007-10-25 00:29:55 +020015123 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070015124 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080015125 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080015126 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030015127
Avi Kivity586f9602010-11-18 13:09:54 +020015128 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020015129
Sheng Yang17cc3932010-01-05 19:02:27 +080015130 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080015131
15132 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080015133
15134 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000015135 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020015136
15137 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080015138
15139 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100015140
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020015141 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Leonid Shatz326e7422018-11-06 12:14:25 +020015142 .write_l1_tsc_offset = vmx_write_l1_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020015143
15144 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020015145
15146 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080015147 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000015148 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080015149 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020015150 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010015151
15152 .check_nested_events = vmx_check_nested_events,
Sean Christophersond264ee02018-08-27 15:21:12 -070015153 .request_immediate_exit = vmx_request_immediate_exit,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020015154
15155 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080015156
15157 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
15158 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
15159 .flush_log_dirty = vmx_flush_log_dirty,
15160 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040015161 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020015162
Feng Wubf9f6ac2015-09-18 22:29:55 +080015163 .pre_block = vmx_pre_block,
15164 .post_block = vmx_post_block,
15165
Wei Huang25462f72015-06-19 15:45:05 +020015166 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080015167
15168 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070015169
15170#ifdef CONFIG_X86_64
15171 .set_hv_timer = vmx_set_hv_timer,
15172 .cancel_hv_timer = vmx_cancel_hv_timer,
15173#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080015174
15175 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020015176
Jim Mattson8fcc4b52018-07-10 11:27:20 +020015177 .get_nested_state = vmx_get_nested_state,
15178 .set_nested_state = vmx_set_nested_state,
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020015179 .get_vmcs12_pages = nested_get_vmcs12_pages,
15180
Ladi Prosek72d7b372017-10-11 16:54:41 +020015181 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020015182 .pre_enter_smm = vmx_pre_enter_smm,
15183 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020015184 .enable_smi_window = enable_smi_window,
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +020015185
15186 .nested_enable_evmcs = nested_enable_evmcs,
Avi Kivity6aa8b732006-12-10 02:21:36 -080015187};
15188
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020015189static void vmx_cleanup_l1d_flush(void)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020015190{
15191 if (vmx_l1d_flush_pages) {
15192 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
15193 vmx_l1d_flush_pages = NULL;
15194 }
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020015195 /* Restore state so sysfs ignores VMX */
15196 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020015197}
15198
Thomas Gleixnera7b90202018-07-13 16:23:18 +020015199static void vmx_exit(void)
15200{
15201#ifdef CONFIG_KEXEC_CORE
15202 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
15203 synchronize_rcu();
15204#endif
15205
15206 kvm_exit();
15207
15208#if IS_ENABLED(CONFIG_HYPERV)
15209 if (static_branch_unlikely(&enable_evmcs)) {
15210 int cpu;
15211 struct hv_vp_assist_page *vp_ap;
15212 /*
15213 * Reset everything to support using non-enlightened VMCS
15214 * access later (e.g. when we reload the module with
15215 * enlightened_vmcs=0)
15216 */
15217 for_each_online_cpu(cpu) {
15218 vp_ap = hv_get_vp_assist_page(cpu);
15219
15220 if (!vp_ap)
15221 continue;
15222
15223 vp_ap->current_nested_vmcs = 0;
15224 vp_ap->enlighten_vmentry = 0;
15225 }
15226
15227 static_branch_disable(&enable_evmcs);
15228 }
15229#endif
15230 vmx_cleanup_l1d_flush();
15231}
15232module_exit(vmx_exit);
15233
Avi Kivity6aa8b732006-12-10 02:21:36 -080015234static int __init vmx_init(void)
15235{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010015236 int r;
15237
15238#if IS_ENABLED(CONFIG_HYPERV)
15239 /*
15240 * Enlightened VMCS usage should be recommended and the host needs
15241 * to support eVMCS v1 or above. We can also disable eVMCS support
15242 * with module parameter.
15243 */
15244 if (enlightened_vmcs &&
15245 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
15246 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
15247 KVM_EVMCS_VERSION) {
15248 int cpu;
15249
15250 /* Check that we have assist pages on all online CPUs */
15251 for_each_online_cpu(cpu) {
15252 if (!hv_get_vp_assist_page(cpu)) {
15253 enlightened_vmcs = false;
15254 break;
15255 }
15256 }
15257
15258 if (enlightened_vmcs) {
15259 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
15260 static_branch_enable(&enable_evmcs);
15261 }
15262 } else {
15263 enlightened_vmcs = false;
15264 }
15265#endif
15266
15267 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Thomas Gleixnera7b90202018-07-13 16:23:18 +020015268 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030015269 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080015270 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080015271
Thomas Gleixnera7b90202018-07-13 16:23:18 +020015272 /*
Thomas Gleixner7db92e12018-07-13 16:23:19 +020015273 * Must be called after kvm_init() so enable_ept is properly set
15274 * up. Hand the parameter mitigation value in which was stored in
15275 * the pre module init parser. If no parameter was given, it will
15276 * contain 'auto' which will be turned into the default 'cond'
15277 * mitigation mode.
Thomas Gleixnera7b90202018-07-13 16:23:18 +020015278 */
Thomas Gleixner7db92e12018-07-13 16:23:19 +020015279 if (boot_cpu_has(X86_BUG_L1TF)) {
15280 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
15281 if (r) {
15282 vmx_exit();
15283 return r;
15284 }
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020015285 }
15286
Dave Young2965faa2015-09-09 15:38:55 -070015287#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080015288 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
15289 crash_vmclear_local_loaded_vmcss);
15290#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070015291 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080015292
He, Qingfdef3ad2007-04-30 09:45:24 +030015293 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080015294}
Thomas Gleixnera7b90202018-07-13 16:23:18 +020015295module_init(vmx_init);