blob: 02edd9960e9d94cf8cbac80ea1bfccc5673f3089 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020021#include "cpuid.h"
Andrey Smetanind62caab2015-11-10 15:36:33 +030022#include "lapic.h"
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +020023#include "hyperv.h"
Avi Kivitye4956062007-06-28 14:15:57 -040024
Avi Kivityedf88412007-12-16 11:02:48 +020025#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080026#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020027#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080028#include <linux/mm.h>
29#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040030#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020031#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070032#include <linux/mod_devicetable.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040033#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040035#include <linux/tboot.h>
Jan Kiszkaf4124502014-03-07 20:03:13 +010036#include <linux/hrtimer.h>
Josh Poimboeufc207aee2017-06-28 10:11:06 -050037#include <linux/frame.h>
Dan Williams085331d2018-01-31 17:47:03 -080038#include <linux/nospec.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030039#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030040#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040041
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +020042#include <asm/asm.h>
Feng Wu28b835d2015-09-18 22:29:54 +080043#include <asm/cpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080044#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080045#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020046#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020047#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080048#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020049#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020050#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010051#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080052#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010053#include <asm/apic.h>
Feng Wuefc64402015-09-18 22:29:51 +080054#include <asm/irq_remapping.h>
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070055#include <asm/mmu_context.h>
Thomas Gleixner28a27752018-04-29 15:01:37 +020056#include <asm/spec-ctrl.h>
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010057#include <asm/mshyperv.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080058
Marcelo Tosatti229456f2009-06-17 09:22:14 -030059#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020060#include "pmu.h"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010061#include "vmx_evmcs.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030062
Avi Kivity4ecac3f2008-05-13 13:23:38 +030063#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040064#define __ex_clear(x, reg) \
Uros Bizjak43ce76c2018-10-17 16:46:57 +020065 ____kvm_handle_fault_on_reboot(x, "xor " reg ", " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030066
Avi Kivity6aa8b732006-12-10 02:21:36 -080067MODULE_AUTHOR("Qumranet");
68MODULE_LICENSE("GPL");
69
Josh Triplette9bda3b2012-03-20 23:33:51 -070070static const struct x86_cpu_id vmx_cpu_id[] = {
71 X86_FEATURE_MATCH(X86_FEATURE_VMX),
72 {}
73};
74MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
75
Rusty Russell476bc002012-01-13 09:32:18 +103076static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020077module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080078
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010079static bool __read_mostly enable_vnmi = 1;
80module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
81
Rusty Russell476bc002012-01-13 09:32:18 +103082static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020083module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020084
Rusty Russell476bc002012-01-13 09:32:18 +103085static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020086module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080087
Rusty Russell476bc002012-01-13 09:32:18 +103088static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070089module_param_named(unrestricted_guest,
90 enable_unrestricted_guest, bool, S_IRUGO);
91
Xudong Hao83c3a332012-05-28 19:33:35 +080092static bool __read_mostly enable_ept_ad_bits = 1;
93module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
94
Avi Kivitya27685c2012-06-12 20:30:18 +030095static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020096module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030097
Rusty Russell476bc002012-01-13 09:32:18 +103098static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030099module_param(fasteoi, bool, S_IRUGO);
100
Yang Zhang5a717852013-04-11 19:25:16 +0800101static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +0800102module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800103
Abel Gordonabc4fc52013-04-18 14:35:25 +0300104static bool __read_mostly enable_shadow_vmcs = 1;
105module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300106/*
107 * If nested=1, nested virtualization is supported, i.e., guests may use
108 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
109 * use VMX instructions.
110 */
Paolo Bonzini1e58e5e2018-10-17 00:55:22 +0200111static bool __read_mostly nested = 1;
Nadav Har'El801d3422011-05-25 23:02:23 +0300112module_param(nested, bool, S_IRUGO);
113
Sean Christopherson52017602018-09-26 09:23:57 -0700114static bool __read_mostly nested_early_check = 0;
115module_param(nested_early_check, bool, S_IRUGO);
116
Wanpeng Li20300092014-12-02 19:14:59 +0800117static u64 __read_mostly host_xss;
118
Kai Huang843e4332015-01-28 10:54:28 +0800119static bool __read_mostly enable_pml = 1;
120module_param_named(pml, enable_pml, bool, S_IRUGO);
121
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100122#define MSR_TYPE_R 1
123#define MSR_TYPE_W 2
124#define MSR_TYPE_RW 3
125
126#define MSR_BITMAP_MODE_X2APIC 1
127#define MSR_BITMAP_MODE_X2APIC_APICV 2
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100128
Haozhong Zhang64903d62015-10-20 15:39:09 +0800129#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
130
Yunhong Jiang64672c92016-06-13 14:19:59 -0700131/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
132static int __read_mostly cpu_preemption_timer_multi;
133static bool __read_mostly enable_preemption_timer = 1;
134#ifdef CONFIG_X86_64
135module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
136#endif
137
Sean Christopherson3de63472018-07-13 08:42:30 -0700138#define KVM_VM_CR0_ALWAYS_OFF (X86_CR0_NW | X86_CR0_CD)
Sean Christopherson1706bd02018-03-05 12:04:38 -0800139#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
140#define KVM_VM_CR0_ALWAYS_ON \
141 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
142 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200143#define KVM_CR4_GUEST_OWNED_BITS \
144 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Yu Zhangfd8cb432017-08-24 20:27:56 +0800145 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200146
Sean Christopherson5dc1f042018-03-05 12:04:39 -0800147#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
Avi Kivitycdc0e242009-12-06 17:21:14 +0200148#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
149#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
150
Avi Kivity78ac8b42010-04-08 18:19:35 +0300151#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
152
Jan Kiszkaf4124502014-03-07 20:03:13 +0100153#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
154
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800155/*
Jan Dakinevich16c2aec2016-10-28 07:00:30 +0300156 * Hyper-V requires all of these, so mark them as supported even though
157 * they are just treated the same as all-context.
158 */
159#define VMX_VPID_EXTENT_SUPPORTED_MASK \
160 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
161 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
162 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
163 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
164
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800165/*
166 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
167 * ple_gap: upper bound on the amount of time between two successive
168 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500169 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800170 * ple_window: upper bound on the amount of time a guest is allowed to execute
171 * in a PAUSE loop. Tests indicate that most spinlocks are held for
172 * less than 2^12 cycles
173 * Time is measured based on a counter that runs at the same rate as the TSC,
174 * refer SDM volume 3b section 21.6.13 & 22.1.3.
175 */
Babu Mogerc8e88712018-03-16 16:37:24 -0400176static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
Luiz Capitulinoa87c99e2018-11-23 12:02:14 -0500177module_param(ple_gap, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200178
Babu Moger7fbc85a2018-03-16 16:37:22 -0400179static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
180module_param(ple_window, uint, 0444);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800181
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200182/* Default doubles per-vcpu window every exit. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400183static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400184module_param(ple_window_grow, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200185
186/* Default resets per-vcpu window every exit to ple_window. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400187static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400188module_param(ple_window_shrink, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200189
190/* Default is to compute the maximum so we can never overflow. */
Babu Moger7fbc85a2018-03-16 16:37:22 -0400191static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
192module_param(ple_window_max, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200193
Avi Kivity83287ea422012-09-16 15:10:57 +0300194extern const ulong vmx_return;
Sean Christopherson52017602018-09-26 09:23:57 -0700195extern const ulong vmx_early_consistency_check_return;
Avi Kivity83287ea422012-09-16 15:10:57 +0300196
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200197static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
Nicolai Stange427362a2018-07-21 22:25:00 +0200198static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200199static DEFINE_MUTEX(vmx_l1d_flush_mutex);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200200
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200201/* Storage for pre module init parameter parsing */
202static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200203
204static const struct {
205 const char *option;
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200206 bool for_parse;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200207} vmentry_l1d_param[] = {
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200208 [VMENTER_L1D_FLUSH_AUTO] = {"auto", true},
209 [VMENTER_L1D_FLUSH_NEVER] = {"never", true},
210 [VMENTER_L1D_FLUSH_COND] = {"cond", true},
211 [VMENTER_L1D_FLUSH_ALWAYS] = {"always", true},
212 [VMENTER_L1D_FLUSH_EPT_DISABLED] = {"EPT disabled", false},
213 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = {"not required", false},
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200214};
215
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200216#define L1D_CACHE_ORDER 4
217static void *vmx_l1d_flush_pages;
218
219static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
220{
221 struct page *page;
Nicolai Stange288d1522018-07-18 19:07:38 +0200222 unsigned int i;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200223
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200224 if (!enable_ept) {
225 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
226 return 0;
227 }
228
Yi Wangd806afa2018-08-16 13:42:39 +0800229 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) {
230 u64 msr;
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200231
Yi Wangd806afa2018-08-16 13:42:39 +0800232 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr);
233 if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) {
234 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
235 return 0;
236 }
237 }
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200238
Jiri Kosinad90a7a02018-07-13 16:23:25 +0200239 /* If set to auto use the default l1tf mitigation method */
240 if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
241 switch (l1tf_mitigation) {
242 case L1TF_MITIGATION_OFF:
243 l1tf = VMENTER_L1D_FLUSH_NEVER;
244 break;
245 case L1TF_MITIGATION_FLUSH_NOWARN:
246 case L1TF_MITIGATION_FLUSH:
247 case L1TF_MITIGATION_FLUSH_NOSMT:
248 l1tf = VMENTER_L1D_FLUSH_COND;
249 break;
250 case L1TF_MITIGATION_FULL:
251 case L1TF_MITIGATION_FULL_FORCE:
252 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
253 break;
254 }
255 } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) {
256 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
257 }
258
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200259 if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
260 !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
261 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
262 if (!page)
263 return -ENOMEM;
264 vmx_l1d_flush_pages = page_address(page);
Nicolai Stange288d1522018-07-18 19:07:38 +0200265
266 /*
267 * Initialize each page with a different pattern in
268 * order to protect against KSM in the nested
269 * virtualization case.
270 */
271 for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) {
272 memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1,
273 PAGE_SIZE);
274 }
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200275 }
276
277 l1tf_vmx_mitigation = l1tf;
278
Thomas Gleixner895ae472018-07-13 16:23:22 +0200279 if (l1tf != VMENTER_L1D_FLUSH_NEVER)
280 static_branch_enable(&vmx_l1d_should_flush);
281 else
282 static_branch_disable(&vmx_l1d_should_flush);
Thomas Gleixner4c6523e2018-07-13 16:23:20 +0200283
Nicolai Stange427362a2018-07-21 22:25:00 +0200284 if (l1tf == VMENTER_L1D_FLUSH_COND)
285 static_branch_enable(&vmx_l1d_flush_cond);
Thomas Gleixner895ae472018-07-13 16:23:22 +0200286 else
Nicolai Stange427362a2018-07-21 22:25:00 +0200287 static_branch_disable(&vmx_l1d_flush_cond);
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200288 return 0;
289}
290
291static int vmentry_l1d_flush_parse(const char *s)
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200292{
293 unsigned int i;
294
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200295 if (s) {
296 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200297 if (vmentry_l1d_param[i].for_parse &&
298 sysfs_streq(s, vmentry_l1d_param[i].option))
299 return i;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200300 }
301 }
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200302 return -EINVAL;
303}
304
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200305static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
306{
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200307 int l1tf, ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200308
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200309 l1tf = vmentry_l1d_flush_parse(s);
310 if (l1tf < 0)
311 return l1tf;
312
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200313 if (!boot_cpu_has(X86_BUG_L1TF))
314 return 0;
315
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200316 /*
317 * Has vmx_init() run already? If not then this is the pre init
318 * parameter parsing. In that case just store the value and let
319 * vmx_init() do the proper setup after enable_ept has been
320 * established.
321 */
322 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
323 vmentry_l1d_flush_param = l1tf;
324 return 0;
325 }
326
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200327 mutex_lock(&vmx_l1d_flush_mutex);
328 ret = vmx_setup_l1d_flush(l1tf);
329 mutex_unlock(&vmx_l1d_flush_mutex);
330 return ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200331}
332
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200333static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
334{
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200335 if (WARN_ON_ONCE(l1tf_vmx_mitigation >= ARRAY_SIZE(vmentry_l1d_param)))
336 return sprintf(s, "???\n");
337
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200338 return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200339}
340
341static const struct kernel_param_ops vmentry_l1d_flush_ops = {
342 .set = vmentry_l1d_flush_set,
343 .get = vmentry_l1d_flush_get,
344};
Thomas Gleixner895ae472018-07-13 16:23:22 +0200345module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200346
Tianyu Lan877ad952018-07-19 08:40:23 +0000347enum ept_pointers_status {
348 EPT_POINTERS_CHECK = 0,
349 EPT_POINTERS_MATCH = 1,
350 EPT_POINTERS_MISMATCH = 2
351};
352
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700353struct kvm_vmx {
354 struct kvm kvm;
355
356 unsigned int tss_addr;
357 bool ept_identity_pagetable_done;
358 gpa_t ept_identity_map_addr;
Tianyu Lan877ad952018-07-19 08:40:23 +0000359
360 enum ept_pointers_status ept_pointers_match;
361 spinlock_t ept_pointer_lock;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700362};
363
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200364#define NR_AUTOLOAD_MSRS 8
Avi Kivity61d2ef22010-04-28 16:40:38 +0300365
Liran Alon392b2f22018-06-23 02:35:01 +0300366struct vmcs_hdr {
367 u32 revision_id:31;
368 u32 shadow_vmcs:1;
369};
370
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400371struct vmcs {
Liran Alon392b2f22018-06-23 02:35:01 +0300372 struct vmcs_hdr hdr;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400373 u32 abort;
374 char data[0];
375};
376
Nadav Har'Eld462b812011-05-24 15:26:10 +0300377/*
Sean Christophersond7ee0392018-07-23 12:32:47 -0700378 * vmcs_host_state tracks registers that are loaded from the VMCS on VMEXIT
379 * and whose values change infrequently, but are not constant. I.e. this is
380 * used as a write-through cache of the corresponding VMCS fields.
381 */
382struct vmcs_host_state {
383 unsigned long cr3; /* May not match real cr3 */
384 unsigned long cr4; /* May not match real cr4 */
Sean Christopherson5e079c72018-07-23 12:32:50 -0700385 unsigned long gs_base;
386 unsigned long fs_base;
Sean Christophersond7ee0392018-07-23 12:32:47 -0700387
388 u16 fs_sel, gs_sel, ldt_sel;
389#ifdef CONFIG_X86_64
390 u16 ds_sel, es_sel;
391#endif
392};
393
394/*
Nadav Har'Eld462b812011-05-24 15:26:10 +0300395 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
396 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
397 * loaded on this CPU (so we can clear them if the CPU goes down).
398 */
399struct loaded_vmcs {
400 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700401 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300402 int cpu;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +0200403 bool launched;
404 bool nmi_known_unmasked;
Sean Christophersonf459a702018-08-27 15:21:11 -0700405 bool hv_timer_armed;
Paolo Bonzini8a1b4392017-11-06 13:31:12 +0100406 /* Support for vnmi-less CPUs */
407 int soft_vnmi_blocked;
408 ktime_t entry_time;
409 s64 vnmi_blocked_time;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100410 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300411 struct list_head loaded_vmcss_on_cpu_link;
Sean Christophersond7ee0392018-07-23 12:32:47 -0700412 struct vmcs_host_state host_state;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300413};
414
Avi Kivity26bb0982009-09-07 11:14:12 +0300415struct shared_msr_entry {
416 unsigned index;
417 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200418 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300419};
420
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300421/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300422 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
423 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
424 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
425 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
426 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
427 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattsonde3a0022017-11-27 17:22:25 -0600428 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300429 * underlying hardware which will be used to run L2.
430 * This structure is packed to ensure that its layout is identical across
431 * machines (necessary for live migration).
Jim Mattsonb348e792018-05-01 15:40:27 -0700432 *
433 * IMPORTANT: Changing the layout of existing fields in this structure
434 * will break save/restore compatibility with older kvm releases. When
435 * adding new fields, either use space in the reserved padding* arrays
436 * or add the new fields to the end of the structure.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300437 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300438typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300439struct __packed vmcs12 {
440 /* According to the Intel spec, a VMCS region must start with the
441 * following two fields. Then follow implementation-specific data.
442 */
Liran Alon392b2f22018-06-23 02:35:01 +0300443 struct vmcs_hdr hdr;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300444 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300445
Nadav Har'El27d6c862011-05-25 23:06:59 +0300446 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
447 u32 padding[7]; /* room for future expansion */
448
Nadav Har'El22bd0352011-05-25 23:05:57 +0300449 u64 io_bitmap_a;
450 u64 io_bitmap_b;
451 u64 msr_bitmap;
452 u64 vm_exit_msr_store_addr;
453 u64 vm_exit_msr_load_addr;
454 u64 vm_entry_msr_load_addr;
455 u64 tsc_offset;
456 u64 virtual_apic_page_addr;
457 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800458 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300459 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800460 u64 eoi_exit_bitmap0;
461 u64 eoi_exit_bitmap1;
462 u64 eoi_exit_bitmap2;
463 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800464 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300465 u64 guest_physical_address;
466 u64 vmcs_link_pointer;
467 u64 guest_ia32_debugctl;
468 u64 guest_ia32_pat;
469 u64 guest_ia32_efer;
470 u64 guest_ia32_perf_global_ctrl;
471 u64 guest_pdptr0;
472 u64 guest_pdptr1;
473 u64 guest_pdptr2;
474 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100475 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300476 u64 host_ia32_pat;
477 u64 host_ia32_efer;
478 u64 host_ia32_perf_global_ctrl;
Jim Mattsonb348e792018-05-01 15:40:27 -0700479 u64 vmread_bitmap;
480 u64 vmwrite_bitmap;
481 u64 vm_function_control;
482 u64 eptp_list_address;
483 u64 pml_address;
484 u64 padding64[3]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300485 /*
486 * To allow migration of L1 (complete with its L2 guests) between
487 * machines of different natural widths (32 or 64 bit), we cannot have
488 * unsigned long fields with no explict size. We use u64 (aliased
489 * natural_width) instead. Luckily, x86 is little-endian.
490 */
491 natural_width cr0_guest_host_mask;
492 natural_width cr4_guest_host_mask;
493 natural_width cr0_read_shadow;
494 natural_width cr4_read_shadow;
495 natural_width cr3_target_value0;
496 natural_width cr3_target_value1;
497 natural_width cr3_target_value2;
498 natural_width cr3_target_value3;
499 natural_width exit_qualification;
500 natural_width guest_linear_address;
501 natural_width guest_cr0;
502 natural_width guest_cr3;
503 natural_width guest_cr4;
504 natural_width guest_es_base;
505 natural_width guest_cs_base;
506 natural_width guest_ss_base;
507 natural_width guest_ds_base;
508 natural_width guest_fs_base;
509 natural_width guest_gs_base;
510 natural_width guest_ldtr_base;
511 natural_width guest_tr_base;
512 natural_width guest_gdtr_base;
513 natural_width guest_idtr_base;
514 natural_width guest_dr7;
515 natural_width guest_rsp;
516 natural_width guest_rip;
517 natural_width guest_rflags;
518 natural_width guest_pending_dbg_exceptions;
519 natural_width guest_sysenter_esp;
520 natural_width guest_sysenter_eip;
521 natural_width host_cr0;
522 natural_width host_cr3;
523 natural_width host_cr4;
524 natural_width host_fs_base;
525 natural_width host_gs_base;
526 natural_width host_tr_base;
527 natural_width host_gdtr_base;
528 natural_width host_idtr_base;
529 natural_width host_ia32_sysenter_esp;
530 natural_width host_ia32_sysenter_eip;
531 natural_width host_rsp;
532 natural_width host_rip;
533 natural_width paddingl[8]; /* room for future expansion */
534 u32 pin_based_vm_exec_control;
535 u32 cpu_based_vm_exec_control;
536 u32 exception_bitmap;
537 u32 page_fault_error_code_mask;
538 u32 page_fault_error_code_match;
539 u32 cr3_target_count;
540 u32 vm_exit_controls;
541 u32 vm_exit_msr_store_count;
542 u32 vm_exit_msr_load_count;
543 u32 vm_entry_controls;
544 u32 vm_entry_msr_load_count;
545 u32 vm_entry_intr_info_field;
546 u32 vm_entry_exception_error_code;
547 u32 vm_entry_instruction_len;
548 u32 tpr_threshold;
549 u32 secondary_vm_exec_control;
550 u32 vm_instruction_error;
551 u32 vm_exit_reason;
552 u32 vm_exit_intr_info;
553 u32 vm_exit_intr_error_code;
554 u32 idt_vectoring_info_field;
555 u32 idt_vectoring_error_code;
556 u32 vm_exit_instruction_len;
557 u32 vmx_instruction_info;
558 u32 guest_es_limit;
559 u32 guest_cs_limit;
560 u32 guest_ss_limit;
561 u32 guest_ds_limit;
562 u32 guest_fs_limit;
563 u32 guest_gs_limit;
564 u32 guest_ldtr_limit;
565 u32 guest_tr_limit;
566 u32 guest_gdtr_limit;
567 u32 guest_idtr_limit;
568 u32 guest_es_ar_bytes;
569 u32 guest_cs_ar_bytes;
570 u32 guest_ss_ar_bytes;
571 u32 guest_ds_ar_bytes;
572 u32 guest_fs_ar_bytes;
573 u32 guest_gs_ar_bytes;
574 u32 guest_ldtr_ar_bytes;
575 u32 guest_tr_ar_bytes;
576 u32 guest_interruptibility_info;
577 u32 guest_activity_state;
578 u32 guest_sysenter_cs;
579 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100580 u32 vmx_preemption_timer_value;
581 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300582 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800583 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300584 u16 guest_es_selector;
585 u16 guest_cs_selector;
586 u16 guest_ss_selector;
587 u16 guest_ds_selector;
588 u16 guest_fs_selector;
589 u16 guest_gs_selector;
590 u16 guest_ldtr_selector;
591 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800592 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300593 u16 host_es_selector;
594 u16 host_cs_selector;
595 u16 host_ss_selector;
596 u16 host_ds_selector;
597 u16 host_fs_selector;
598 u16 host_gs_selector;
599 u16 host_tr_selector;
Jim Mattsonb348e792018-05-01 15:40:27 -0700600 u16 guest_pml_index;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300601};
602
603/*
Jim Mattson21ebf532018-05-01 15:40:28 -0700604 * For save/restore compatibility, the vmcs12 field offsets must not change.
605 */
606#define CHECK_OFFSET(field, loc) \
607 BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
608 "Offset of " #field " in struct vmcs12 has changed.")
609
610static inline void vmx_check_vmcs12_offsets(void) {
Liran Alon392b2f22018-06-23 02:35:01 +0300611 CHECK_OFFSET(hdr, 0);
Jim Mattson21ebf532018-05-01 15:40:28 -0700612 CHECK_OFFSET(abort, 4);
613 CHECK_OFFSET(launch_state, 8);
614 CHECK_OFFSET(io_bitmap_a, 40);
615 CHECK_OFFSET(io_bitmap_b, 48);
616 CHECK_OFFSET(msr_bitmap, 56);
617 CHECK_OFFSET(vm_exit_msr_store_addr, 64);
618 CHECK_OFFSET(vm_exit_msr_load_addr, 72);
619 CHECK_OFFSET(vm_entry_msr_load_addr, 80);
620 CHECK_OFFSET(tsc_offset, 88);
621 CHECK_OFFSET(virtual_apic_page_addr, 96);
622 CHECK_OFFSET(apic_access_addr, 104);
623 CHECK_OFFSET(posted_intr_desc_addr, 112);
624 CHECK_OFFSET(ept_pointer, 120);
625 CHECK_OFFSET(eoi_exit_bitmap0, 128);
626 CHECK_OFFSET(eoi_exit_bitmap1, 136);
627 CHECK_OFFSET(eoi_exit_bitmap2, 144);
628 CHECK_OFFSET(eoi_exit_bitmap3, 152);
629 CHECK_OFFSET(xss_exit_bitmap, 160);
630 CHECK_OFFSET(guest_physical_address, 168);
631 CHECK_OFFSET(vmcs_link_pointer, 176);
632 CHECK_OFFSET(guest_ia32_debugctl, 184);
633 CHECK_OFFSET(guest_ia32_pat, 192);
634 CHECK_OFFSET(guest_ia32_efer, 200);
635 CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
636 CHECK_OFFSET(guest_pdptr0, 216);
637 CHECK_OFFSET(guest_pdptr1, 224);
638 CHECK_OFFSET(guest_pdptr2, 232);
639 CHECK_OFFSET(guest_pdptr3, 240);
640 CHECK_OFFSET(guest_bndcfgs, 248);
641 CHECK_OFFSET(host_ia32_pat, 256);
642 CHECK_OFFSET(host_ia32_efer, 264);
643 CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
644 CHECK_OFFSET(vmread_bitmap, 280);
645 CHECK_OFFSET(vmwrite_bitmap, 288);
646 CHECK_OFFSET(vm_function_control, 296);
647 CHECK_OFFSET(eptp_list_address, 304);
648 CHECK_OFFSET(pml_address, 312);
649 CHECK_OFFSET(cr0_guest_host_mask, 344);
650 CHECK_OFFSET(cr4_guest_host_mask, 352);
651 CHECK_OFFSET(cr0_read_shadow, 360);
652 CHECK_OFFSET(cr4_read_shadow, 368);
653 CHECK_OFFSET(cr3_target_value0, 376);
654 CHECK_OFFSET(cr3_target_value1, 384);
655 CHECK_OFFSET(cr3_target_value2, 392);
656 CHECK_OFFSET(cr3_target_value3, 400);
657 CHECK_OFFSET(exit_qualification, 408);
658 CHECK_OFFSET(guest_linear_address, 416);
659 CHECK_OFFSET(guest_cr0, 424);
660 CHECK_OFFSET(guest_cr3, 432);
661 CHECK_OFFSET(guest_cr4, 440);
662 CHECK_OFFSET(guest_es_base, 448);
663 CHECK_OFFSET(guest_cs_base, 456);
664 CHECK_OFFSET(guest_ss_base, 464);
665 CHECK_OFFSET(guest_ds_base, 472);
666 CHECK_OFFSET(guest_fs_base, 480);
667 CHECK_OFFSET(guest_gs_base, 488);
668 CHECK_OFFSET(guest_ldtr_base, 496);
669 CHECK_OFFSET(guest_tr_base, 504);
670 CHECK_OFFSET(guest_gdtr_base, 512);
671 CHECK_OFFSET(guest_idtr_base, 520);
672 CHECK_OFFSET(guest_dr7, 528);
673 CHECK_OFFSET(guest_rsp, 536);
674 CHECK_OFFSET(guest_rip, 544);
675 CHECK_OFFSET(guest_rflags, 552);
676 CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
677 CHECK_OFFSET(guest_sysenter_esp, 568);
678 CHECK_OFFSET(guest_sysenter_eip, 576);
679 CHECK_OFFSET(host_cr0, 584);
680 CHECK_OFFSET(host_cr3, 592);
681 CHECK_OFFSET(host_cr4, 600);
682 CHECK_OFFSET(host_fs_base, 608);
683 CHECK_OFFSET(host_gs_base, 616);
684 CHECK_OFFSET(host_tr_base, 624);
685 CHECK_OFFSET(host_gdtr_base, 632);
686 CHECK_OFFSET(host_idtr_base, 640);
687 CHECK_OFFSET(host_ia32_sysenter_esp, 648);
688 CHECK_OFFSET(host_ia32_sysenter_eip, 656);
689 CHECK_OFFSET(host_rsp, 664);
690 CHECK_OFFSET(host_rip, 672);
691 CHECK_OFFSET(pin_based_vm_exec_control, 744);
692 CHECK_OFFSET(cpu_based_vm_exec_control, 748);
693 CHECK_OFFSET(exception_bitmap, 752);
694 CHECK_OFFSET(page_fault_error_code_mask, 756);
695 CHECK_OFFSET(page_fault_error_code_match, 760);
696 CHECK_OFFSET(cr3_target_count, 764);
697 CHECK_OFFSET(vm_exit_controls, 768);
698 CHECK_OFFSET(vm_exit_msr_store_count, 772);
699 CHECK_OFFSET(vm_exit_msr_load_count, 776);
700 CHECK_OFFSET(vm_entry_controls, 780);
701 CHECK_OFFSET(vm_entry_msr_load_count, 784);
702 CHECK_OFFSET(vm_entry_intr_info_field, 788);
703 CHECK_OFFSET(vm_entry_exception_error_code, 792);
704 CHECK_OFFSET(vm_entry_instruction_len, 796);
705 CHECK_OFFSET(tpr_threshold, 800);
706 CHECK_OFFSET(secondary_vm_exec_control, 804);
707 CHECK_OFFSET(vm_instruction_error, 808);
708 CHECK_OFFSET(vm_exit_reason, 812);
709 CHECK_OFFSET(vm_exit_intr_info, 816);
710 CHECK_OFFSET(vm_exit_intr_error_code, 820);
711 CHECK_OFFSET(idt_vectoring_info_field, 824);
712 CHECK_OFFSET(idt_vectoring_error_code, 828);
713 CHECK_OFFSET(vm_exit_instruction_len, 832);
714 CHECK_OFFSET(vmx_instruction_info, 836);
715 CHECK_OFFSET(guest_es_limit, 840);
716 CHECK_OFFSET(guest_cs_limit, 844);
717 CHECK_OFFSET(guest_ss_limit, 848);
718 CHECK_OFFSET(guest_ds_limit, 852);
719 CHECK_OFFSET(guest_fs_limit, 856);
720 CHECK_OFFSET(guest_gs_limit, 860);
721 CHECK_OFFSET(guest_ldtr_limit, 864);
722 CHECK_OFFSET(guest_tr_limit, 868);
723 CHECK_OFFSET(guest_gdtr_limit, 872);
724 CHECK_OFFSET(guest_idtr_limit, 876);
725 CHECK_OFFSET(guest_es_ar_bytes, 880);
726 CHECK_OFFSET(guest_cs_ar_bytes, 884);
727 CHECK_OFFSET(guest_ss_ar_bytes, 888);
728 CHECK_OFFSET(guest_ds_ar_bytes, 892);
729 CHECK_OFFSET(guest_fs_ar_bytes, 896);
730 CHECK_OFFSET(guest_gs_ar_bytes, 900);
731 CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
732 CHECK_OFFSET(guest_tr_ar_bytes, 908);
733 CHECK_OFFSET(guest_interruptibility_info, 912);
734 CHECK_OFFSET(guest_activity_state, 916);
735 CHECK_OFFSET(guest_sysenter_cs, 920);
736 CHECK_OFFSET(host_ia32_sysenter_cs, 924);
737 CHECK_OFFSET(vmx_preemption_timer_value, 928);
738 CHECK_OFFSET(virtual_processor_id, 960);
739 CHECK_OFFSET(posted_intr_nv, 962);
740 CHECK_OFFSET(guest_es_selector, 964);
741 CHECK_OFFSET(guest_cs_selector, 966);
742 CHECK_OFFSET(guest_ss_selector, 968);
743 CHECK_OFFSET(guest_ds_selector, 970);
744 CHECK_OFFSET(guest_fs_selector, 972);
745 CHECK_OFFSET(guest_gs_selector, 974);
746 CHECK_OFFSET(guest_ldtr_selector, 976);
747 CHECK_OFFSET(guest_tr_selector, 978);
748 CHECK_OFFSET(guest_intr_status, 980);
749 CHECK_OFFSET(host_es_selector, 982);
750 CHECK_OFFSET(host_cs_selector, 984);
751 CHECK_OFFSET(host_ss_selector, 986);
752 CHECK_OFFSET(host_ds_selector, 988);
753 CHECK_OFFSET(host_fs_selector, 990);
754 CHECK_OFFSET(host_gs_selector, 992);
755 CHECK_OFFSET(host_tr_selector, 994);
756 CHECK_OFFSET(guest_pml_index, 996);
757}
758
759/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300760 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
761 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
762 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
Jim Mattsonb348e792018-05-01 15:40:27 -0700763 *
764 * IMPORTANT: Changing this value will break save/restore compatibility with
765 * older kvm releases.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300766 */
767#define VMCS12_REVISION 0x11e57ed0
768
769/*
770 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
771 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
772 * current implementation, 4K are reserved to avoid future complications.
773 */
774#define VMCS12_SIZE 0x1000
775
776/*
Jim Mattson5b157062017-12-22 12:11:12 -0800777 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
778 * supported VMCS12 field encoding.
779 */
780#define VMCS12_MAX_FIELD_INDEX 0x17
781
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100782struct nested_vmx_msrs {
783 /*
784 * We only store the "true" versions of the VMX capability MSRs. We
785 * generate the "non-true" versions by setting the must-be-1 bits
786 * according to the SDM.
787 */
788 u32 procbased_ctls_low;
789 u32 procbased_ctls_high;
790 u32 secondary_ctls_low;
791 u32 secondary_ctls_high;
792 u32 pinbased_ctls_low;
793 u32 pinbased_ctls_high;
794 u32 exit_ctls_low;
795 u32 exit_ctls_high;
796 u32 entry_ctls_low;
797 u32 entry_ctls_high;
798 u32 misc_low;
799 u32 misc_high;
800 u32 ept_caps;
801 u32 vpid_caps;
802 u64 basic;
803 u64 cr0_fixed0;
804 u64 cr0_fixed1;
805 u64 cr4_fixed0;
806 u64 cr4_fixed1;
807 u64 vmcs_enum;
808 u64 vmfunc_controls;
809};
810
Jim Mattson5b157062017-12-22 12:11:12 -0800811/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300812 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
813 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
814 */
815struct nested_vmx {
816 /* Has the level1 guest done vmxon? */
817 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400818 gpa_t vmxon_ptr;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400819 bool pml_full;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300820
821 /* The guest-physical address of the current VMCS L1 keeps for L2 */
822 gpa_t current_vmptr;
David Matlack4f2777b2016-07-13 17:16:37 -0700823 /*
824 * Cache of the guest's VMCS, existing outside of guest memory.
825 * Loaded from guest memory during VMPTRLD. Flushed to guest
David Matlack8ca44e82017-08-01 14:00:39 -0700826 * memory during VMCLEAR and VMPTRLD.
David Matlack4f2777b2016-07-13 17:16:37 -0700827 */
828 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300829 /*
Liran Alon61ada742018-06-23 02:35:08 +0300830 * Cache of the guest's shadow VMCS, existing outside of guest
831 * memory. Loaded from guest memory during VM entry. Flushed
832 * to guest memory during VM exit.
833 */
834 struct vmcs12 *cached_shadow_vmcs12;
835 /*
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +0200836 * Indicates if the shadow vmcs or enlightened vmcs must be updated
837 * with the data held by struct vmcs12.
Abel Gordon012f83c2013-04-18 14:39:25 +0300838 */
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +0200839 bool need_vmcs12_sync;
Paolo Bonzini74a497f2017-12-20 13:55:39 +0100840 bool dirty_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300841
Sean Christopherson9d6105b22018-09-26 09:23:51 -0700842 /*
843 * vmcs02 has been initialized, i.e. state that is constant for
844 * vmcs02 has been written to the backing VMCS. Initialization
845 * is delayed until L1 actually attempts to run a nested VM.
846 */
847 bool vmcs02_initialized;
848
Jim Mattson8d860bb2018-05-09 16:56:05 -0400849 bool change_vmcs01_virtual_apic_mode;
850
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +0200851 /*
852 * Enlightened VMCS has been enabled. It does not mean that L1 has to
853 * use it. However, VMX features available to L1 will be limited based
854 * on what the enlightened VMCS supports.
855 */
856 bool enlightened_vmcs_enabled;
857
Nadav Har'El644d7112011-05-25 23:12:35 +0300858 /* L2 must run next, and mustn't decide to exit to L1. */
859 bool nested_run_pending;
Jim Mattsonde3a0022017-11-27 17:22:25 -0600860
861 struct loaded_vmcs vmcs02;
862
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300863 /*
Jim Mattsonde3a0022017-11-27 17:22:25 -0600864 * Guest pages referred to in the vmcs02 with host-physical
865 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300866 */
867 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800868 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800869 struct page *pi_desc_page;
870 struct pi_desc *pi_desc;
871 bool pi_pending;
872 u16 posted_intr_nv;
Jan Kiszkaf4124502014-03-07 20:03:13 +0100873
874 struct hrtimer preemption_timer;
875 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200876
877 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
878 u64 vmcs01_debugctl;
Liran Alon62cf9bd812018-09-14 03:25:54 +0300879 u64 vmcs01_guest_bndcfgs;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800880
Wanpeng Li5c614b32015-10-13 09:18:36 -0700881 u16 vpid02;
882 u16 last_vpid;
883
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100884 struct nested_vmx_msrs msrs;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200885
886 /* SMM related state */
887 struct {
888 /* in VMX operation on SMM entry? */
889 bool vmxon;
890 /* in guest mode on SMM entry? */
891 bool guest_mode;
892 } smm;
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +0200893
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +0200894 gpa_t hv_evmcs_vmptr;
895 struct page *hv_evmcs_page;
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +0200896 struct hv_enlightened_vmcs *hv_evmcs;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300897};
898
Yang Zhang01e439b2013-04-11 19:25:12 +0800899#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800900#define POSTED_INTR_SN 1
901
Yang Zhang01e439b2013-04-11 19:25:12 +0800902/* Posted-Interrupt Descriptor */
903struct pi_desc {
904 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800905 union {
906 struct {
907 /* bit 256 - Outstanding Notification */
908 u16 on : 1,
909 /* bit 257 - Suppress Notification */
910 sn : 1,
911 /* bit 271:258 - Reserved */
912 rsvd_1 : 14;
913 /* bit 279:272 - Notification Vector */
914 u8 nv;
915 /* bit 287:280 - Reserved */
916 u8 rsvd_2;
917 /* bit 319:288 - Notification Destination */
918 u32 ndst;
919 };
920 u64 control;
921 };
922 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800923} __aligned(64);
924
Yang Zhanga20ed542013-04-11 19:25:15 +0800925static bool pi_test_and_set_on(struct pi_desc *pi_desc)
926{
927 return test_and_set_bit(POSTED_INTR_ON,
928 (unsigned long *)&pi_desc->control);
929}
930
931static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
932{
933 return test_and_clear_bit(POSTED_INTR_ON,
934 (unsigned long *)&pi_desc->control);
935}
936
937static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
938{
939 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
940}
941
Feng Wuebbfc762015-09-18 22:29:46 +0800942static inline void pi_clear_sn(struct pi_desc *pi_desc)
943{
944 return clear_bit(POSTED_INTR_SN,
945 (unsigned long *)&pi_desc->control);
946}
947
948static inline void pi_set_sn(struct pi_desc *pi_desc)
949{
950 return set_bit(POSTED_INTR_SN,
951 (unsigned long *)&pi_desc->control);
952}
953
Paolo Bonziniad361092016-09-20 16:15:05 +0200954static inline void pi_clear_on(struct pi_desc *pi_desc)
955{
956 clear_bit(POSTED_INTR_ON,
957 (unsigned long *)&pi_desc->control);
958}
959
Feng Wuebbfc762015-09-18 22:29:46 +0800960static inline int pi_test_on(struct pi_desc *pi_desc)
961{
962 return test_bit(POSTED_INTR_ON,
963 (unsigned long *)&pi_desc->control);
964}
965
966static inline int pi_test_sn(struct pi_desc *pi_desc)
967{
968 return test_bit(POSTED_INTR_SN,
969 (unsigned long *)&pi_desc->control);
970}
971
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400972struct vmx_msrs {
973 unsigned int nr;
974 struct vmx_msr_entry val[NR_AUTOLOAD_MSRS];
975};
976
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400977struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000978 struct kvm_vcpu vcpu;
Avi Kivity313dbd492008-07-17 18:04:30 +0300979 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300980 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100981 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300982 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200983 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200984 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300985 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400986 int nmsrs;
987 int save_nmsrs;
Liran Alonf48b4712018-11-20 18:03:25 +0200988 bool guest_msrs_dirty;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800989 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400990#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300991 u64 msr_host_kernel_gs_base;
992 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400993#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100994
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100995 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100996 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100997
Gleb Natapov2961e8762013-11-25 15:37:13 +0200998 u32 vm_entry_controls_shadow;
999 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +02001000 u32 secondary_exec_control;
1001
Nadav Har'Eld462b812011-05-24 15:26:10 +03001002 /*
1003 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
1004 * non-nested (L1) guest, it always points to vmcs01. For a nested
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001005 * guest (L2), it points to a different VMCS. loaded_cpu_state points
1006 * to the VMCS whose state is loaded into the CPU registers that only
1007 * need to be switched when transitioning to/from the kernel; a NULL
1008 * value indicates that host state is loaded.
Nadav Har'Eld462b812011-05-24 15:26:10 +03001009 */
1010 struct loaded_vmcs vmcs01;
1011 struct loaded_vmcs *loaded_vmcs;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001012 struct loaded_vmcs *loaded_cpu_state;
Nadav Har'Eld462b812011-05-24 15:26:10 +03001013 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +03001014 struct msr_autoload {
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04001015 struct vmx_msrs guest;
1016 struct vmx_msrs host;
Avi Kivity61d2ef22010-04-28 16:40:38 +03001017 } msr_autoload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001018
Avi Kivity9c8cba32007-11-22 11:42:59 +02001019 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001020 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001021 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03001022 struct kvm_segment segs[8];
1023 } rmode;
1024 struct {
1025 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001026 struct kvm_save_segment {
1027 u16 selector;
1028 unsigned long base;
1029 u32 limit;
1030 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03001031 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +03001032 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001033 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +03001034 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02001035
Andi Kleena0861c02009-06-08 17:37:09 +08001036 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001037
Yang Zhang01e439b2013-04-11 19:25:12 +08001038 /* Posted interrupt descriptor */
1039 struct pi_desc pi_desc;
1040
Nadav Har'Elec378ae2011-05-25 23:02:54 +03001041 /* Support for a guest hypervisor (nested VMX) */
1042 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +02001043
1044 /* Dynamic PLE window. */
1045 int ple_window;
1046 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +08001047
Sean Christophersond264ee02018-08-27 15:21:12 -07001048 bool req_immediate_exit;
1049
Kai Huang843e4332015-01-28 10:54:28 +08001050 /* Support for PML */
1051#define PML_ENTITY_NUM 512
1052 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001053
Yunhong Jiang64672c92016-06-13 14:19:59 -07001054 /* apic deadline value in host tsc */
1055 u64 hv_deadline_tsc;
1056
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001057 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +08001058
Xiao Guangrong1be0e612016-03-22 16:51:18 +08001059 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +08001060
Wanpeng Li74c55932017-11-29 01:31:20 -08001061 unsigned long host_debugctlmsr;
1062
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001063 /*
1064 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
1065 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
1066 * in msr_ia32_feature_control_valid_bits.
1067 */
Haozhong Zhang3b840802016-06-22 14:59:54 +08001068 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001069 u64 msr_ia32_feature_control_valid_bits;
Tianyu Lan877ad952018-07-19 08:40:23 +00001070 u64 ept_pointer;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001071};
1072
Avi Kivity2fb92db2011-04-27 19:42:18 +03001073enum segment_cache_field {
1074 SEG_FIELD_SEL = 0,
1075 SEG_FIELD_BASE = 1,
1076 SEG_FIELD_LIMIT = 2,
1077 SEG_FIELD_AR = 3,
1078
1079 SEG_FIELD_NR = 4
1080};
1081
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07001082static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
1083{
1084 return container_of(kvm, struct kvm_vmx, kvm);
1085}
1086
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001087static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
1088{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001089 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001090}
1091
Feng Wuefc64402015-09-18 22:29:51 +08001092static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
1093{
1094 return &(to_vmx(vcpu)->pi_desc);
1095}
1096
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001097#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +03001098#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001099#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
1100#define FIELD64(number, name) \
1101 FIELD(number, name), \
1102 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +03001103
Abel Gordon4607c2d2013-04-18 14:35:55 +03001104
Paolo Bonzini44900ba2017-12-13 12:58:02 +01001105static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +01001106#define SHADOW_FIELD_RO(x) x,
1107#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +03001108};
Bandan Dasfe2b2012014-04-21 15:20:14 -04001109static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001110 ARRAY_SIZE(shadow_read_only_fields);
1111
Paolo Bonzini44900ba2017-12-13 12:58:02 +01001112static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +01001113#define SHADOW_FIELD_RW(x) x,
1114#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +03001115};
Bandan Dasfe2b2012014-04-21 15:20:14 -04001116static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001117 ARRAY_SIZE(shadow_read_write_fields);
1118
Mathias Krause772e0312012-08-30 01:30:19 +02001119static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +03001120 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +08001121 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001122 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
1123 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
1124 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
1125 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
1126 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
1127 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
1128 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
1129 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +08001130 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -04001131 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001132 FIELD(HOST_ES_SELECTOR, host_es_selector),
1133 FIELD(HOST_CS_SELECTOR, host_cs_selector),
1134 FIELD(HOST_SS_SELECTOR, host_ss_selector),
1135 FIELD(HOST_DS_SELECTOR, host_ds_selector),
1136 FIELD(HOST_FS_SELECTOR, host_fs_selector),
1137 FIELD(HOST_GS_SELECTOR, host_gs_selector),
1138 FIELD(HOST_TR_SELECTOR, host_tr_selector),
1139 FIELD64(IO_BITMAP_A, io_bitmap_a),
1140 FIELD64(IO_BITMAP_B, io_bitmap_b),
1141 FIELD64(MSR_BITMAP, msr_bitmap),
1142 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
1143 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
1144 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
Jim Mattsonb348e792018-05-01 15:40:27 -07001145 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001146 FIELD64(TSC_OFFSET, tsc_offset),
1147 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
1148 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +08001149 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -04001150 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001151 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +08001152 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
1153 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
1154 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
1155 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -04001156 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Jim Mattsonb348e792018-05-01 15:40:27 -07001157 FIELD64(VMREAD_BITMAP, vmread_bitmap),
1158 FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001159 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001160 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
1161 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
1162 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
1163 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
1164 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
1165 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
1166 FIELD64(GUEST_PDPTR0, guest_pdptr0),
1167 FIELD64(GUEST_PDPTR1, guest_pdptr1),
1168 FIELD64(GUEST_PDPTR2, guest_pdptr2),
1169 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +01001170 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001171 FIELD64(HOST_IA32_PAT, host_ia32_pat),
1172 FIELD64(HOST_IA32_EFER, host_ia32_efer),
1173 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
1174 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
1175 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
1176 FIELD(EXCEPTION_BITMAP, exception_bitmap),
1177 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
1178 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
1179 FIELD(CR3_TARGET_COUNT, cr3_target_count),
1180 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
1181 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
1182 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
1183 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
1184 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
1185 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
1186 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
1187 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
1188 FIELD(TPR_THRESHOLD, tpr_threshold),
1189 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
1190 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
1191 FIELD(VM_EXIT_REASON, vm_exit_reason),
1192 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
1193 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
1194 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
1195 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
1196 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
1197 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
1198 FIELD(GUEST_ES_LIMIT, guest_es_limit),
1199 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
1200 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
1201 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
1202 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
1203 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
1204 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
1205 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
1206 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
1207 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
1208 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
1209 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
1210 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
1211 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
1212 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
1213 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
1214 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
1215 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
1216 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
1217 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
1218 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
1219 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +01001220 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001221 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
1222 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
1223 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
1224 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
1225 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
1226 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
1227 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
1228 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
1229 FIELD(EXIT_QUALIFICATION, exit_qualification),
1230 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
1231 FIELD(GUEST_CR0, guest_cr0),
1232 FIELD(GUEST_CR3, guest_cr3),
1233 FIELD(GUEST_CR4, guest_cr4),
1234 FIELD(GUEST_ES_BASE, guest_es_base),
1235 FIELD(GUEST_CS_BASE, guest_cs_base),
1236 FIELD(GUEST_SS_BASE, guest_ss_base),
1237 FIELD(GUEST_DS_BASE, guest_ds_base),
1238 FIELD(GUEST_FS_BASE, guest_fs_base),
1239 FIELD(GUEST_GS_BASE, guest_gs_base),
1240 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1241 FIELD(GUEST_TR_BASE, guest_tr_base),
1242 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1243 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1244 FIELD(GUEST_DR7, guest_dr7),
1245 FIELD(GUEST_RSP, guest_rsp),
1246 FIELD(GUEST_RIP, guest_rip),
1247 FIELD(GUEST_RFLAGS, guest_rflags),
1248 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1249 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1250 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1251 FIELD(HOST_CR0, host_cr0),
1252 FIELD(HOST_CR3, host_cr3),
1253 FIELD(HOST_CR4, host_cr4),
1254 FIELD(HOST_FS_BASE, host_fs_base),
1255 FIELD(HOST_GS_BASE, host_gs_base),
1256 FIELD(HOST_TR_BASE, host_tr_base),
1257 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1258 FIELD(HOST_IDTR_BASE, host_idtr_base),
1259 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1260 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1261 FIELD(HOST_RSP, host_rsp),
1262 FIELD(HOST_RIP, host_rip),
1263};
Nadav Har'El22bd0352011-05-25 23:05:57 +03001264
1265static inline short vmcs_field_to_offset(unsigned long field)
1266{
Dan Williams085331d2018-01-31 17:47:03 -08001267 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1268 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001269 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001270
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001271 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -08001272 return -ENOENT;
1273
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001274 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -08001275 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -08001276 return -ENOENT;
1277
Linus Torvalds15303ba2018-02-10 13:16:35 -08001278 index = array_index_nospec(index, size);
1279 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -08001280 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001281 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -08001282 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +03001283}
1284
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001285static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1286{
David Matlack4f2777b2016-07-13 17:16:37 -07001287 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001288}
1289
Liran Alon61ada742018-06-23 02:35:08 +03001290static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu)
1291{
1292 return to_vmx(vcpu)->nested.cached_shadow_vmcs12;
1293}
1294
Peter Feiner995f00a2017-06-30 17:26:32 -07001295static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03001296static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -07001297static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +08001298static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +03001299static void vmx_set_segment(struct kvm_vcpu *vcpu,
1300 struct kvm_segment *var, int seg);
1301static void vmx_get_segment(struct kvm_vcpu *vcpu,
1302 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +02001303static bool guest_state_valid(struct kvm_vcpu *vcpu);
1304static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +03001305static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001306static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1307static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1308static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1309 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001310static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Yi Wang1e4329ee2018-11-08 11:22:21 +08001311static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
Ashok Raj15d45072018-02-01 22:59:43 +01001312 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +03001313
Avi Kivity6aa8b732006-12-10 02:21:36 -08001314static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1315static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001316/*
1317 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1318 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1319 */
1320static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001321
Feng Wubf9f6ac2015-09-18 22:29:55 +08001322/*
1323 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1324 * can find which vCPU should be waken up.
1325 */
1326static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1327static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1328
Radim Krčmář23611332016-09-29 22:41:33 +02001329enum {
Radim Krčmář23611332016-09-29 22:41:33 +02001330 VMX_VMREAD_BITMAP,
1331 VMX_VMWRITE_BITMAP,
1332 VMX_BITMAP_NR
1333};
1334
1335static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1336
Radim Krčmář23611332016-09-29 22:41:33 +02001337#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1338#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +03001339
Avi Kivity110312c2010-12-21 12:54:20 +02001340static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001341static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001342
Sheng Yang2384d2b2008-01-17 15:14:33 +08001343static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1344static DEFINE_SPINLOCK(vmx_vpid_lock);
1345
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001346static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001347 int size;
1348 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001349 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001350 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001351 u32 pin_based_exec_ctrl;
1352 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001353 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001354 u32 vmexit_ctrl;
1355 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +01001356 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001357} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001358
Hannes Ederefff9e52008-11-28 17:02:06 +01001359static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001360 u32 ept;
1361 u32 vpid;
1362} vmx_capability;
1363
Avi Kivity6aa8b732006-12-10 02:21:36 -08001364#define VMX_SEGMENT_FIELD(seg) \
1365 [VCPU_SREG_##seg] = { \
1366 .selector = GUEST_##seg##_SELECTOR, \
1367 .base = GUEST_##seg##_BASE, \
1368 .limit = GUEST_##seg##_LIMIT, \
1369 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1370 }
1371
Mathias Krause772e0312012-08-30 01:30:19 +02001372static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001373 unsigned selector;
1374 unsigned base;
1375 unsigned limit;
1376 unsigned ar_bytes;
1377} kvm_vmx_segment_fields[] = {
1378 VMX_SEGMENT_FIELD(CS),
1379 VMX_SEGMENT_FIELD(DS),
1380 VMX_SEGMENT_FIELD(ES),
1381 VMX_SEGMENT_FIELD(FS),
1382 VMX_SEGMENT_FIELD(GS),
1383 VMX_SEGMENT_FIELD(SS),
1384 VMX_SEGMENT_FIELD(TR),
1385 VMX_SEGMENT_FIELD(LDTR),
1386};
1387
Avi Kivity26bb0982009-09-07 11:14:12 +03001388static u64 host_efer;
1389
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001390static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1391
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001392/*
Brian Gerst8c065852010-07-17 09:03:26 -04001393 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001394 * away by decrementing the array size.
1395 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001396static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001397#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001398 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001399#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001400 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001401};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001402
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001403DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1404
1405#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1406
1407#define KVM_EVMCS_VERSION 1
1408
Vitaly Kuznetsov5d7a6442018-10-16 18:50:00 +02001409/*
1410 * Enlightened VMCSv1 doesn't support these:
1411 *
1412 * POSTED_INTR_NV = 0x00000002,
1413 * GUEST_INTR_STATUS = 0x00000810,
1414 * APIC_ACCESS_ADDR = 0x00002014,
1415 * POSTED_INTR_DESC_ADDR = 0x00002016,
1416 * EOI_EXIT_BITMAP0 = 0x0000201c,
1417 * EOI_EXIT_BITMAP1 = 0x0000201e,
1418 * EOI_EXIT_BITMAP2 = 0x00002020,
1419 * EOI_EXIT_BITMAP3 = 0x00002022,
1420 * GUEST_PML_INDEX = 0x00000812,
1421 * PML_ADDRESS = 0x0000200e,
1422 * VM_FUNCTION_CONTROL = 0x00002018,
1423 * EPTP_LIST_ADDRESS = 0x00002024,
1424 * VMREAD_BITMAP = 0x00002026,
1425 * VMWRITE_BITMAP = 0x00002028,
1426 *
1427 * TSC_MULTIPLIER = 0x00002032,
1428 * PLE_GAP = 0x00004020,
1429 * PLE_WINDOW = 0x00004022,
1430 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1431 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1432 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1433 *
1434 * Currently unsupported in KVM:
1435 * GUEST_IA32_RTIT_CTL = 0x00002814,
1436 */
1437#define EVMCS1_UNSUPPORTED_PINCTRL (PIN_BASED_POSTED_INTR | \
1438 PIN_BASED_VMX_PREEMPTION_TIMER)
1439#define EVMCS1_UNSUPPORTED_2NDEXEC \
1440 (SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | \
1441 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | \
1442 SECONDARY_EXEC_APIC_REGISTER_VIRT | \
1443 SECONDARY_EXEC_ENABLE_PML | \
1444 SECONDARY_EXEC_ENABLE_VMFUNC | \
1445 SECONDARY_EXEC_SHADOW_VMCS | \
1446 SECONDARY_EXEC_TSC_SCALING | \
1447 SECONDARY_EXEC_PAUSE_LOOP_EXITING)
1448#define EVMCS1_UNSUPPORTED_VMEXIT_CTRL (VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
1449#define EVMCS1_UNSUPPORTED_VMENTRY_CTRL (VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
1450#define EVMCS1_UNSUPPORTED_VMFUNC (VMX_VMFUNC_EPTP_SWITCHING)
1451
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001452#if IS_ENABLED(CONFIG_HYPERV)
1453static bool __read_mostly enlightened_vmcs = true;
1454module_param(enlightened_vmcs, bool, 0444);
1455
1456static inline void evmcs_write64(unsigned long field, u64 value)
1457{
1458 u16 clean_field;
1459 int offset = get_evmcs_offset(field, &clean_field);
1460
1461 if (offset < 0)
1462 return;
1463
1464 *(u64 *)((char *)current_evmcs + offset) = value;
1465
1466 current_evmcs->hv_clean_fields &= ~clean_field;
1467}
1468
1469static inline void evmcs_write32(unsigned long field, u32 value)
1470{
1471 u16 clean_field;
1472 int offset = get_evmcs_offset(field, &clean_field);
1473
1474 if (offset < 0)
1475 return;
1476
1477 *(u32 *)((char *)current_evmcs + offset) = value;
1478 current_evmcs->hv_clean_fields &= ~clean_field;
1479}
1480
1481static inline void evmcs_write16(unsigned long field, u16 value)
1482{
1483 u16 clean_field;
1484 int offset = get_evmcs_offset(field, &clean_field);
1485
1486 if (offset < 0)
1487 return;
1488
1489 *(u16 *)((char *)current_evmcs + offset) = value;
1490 current_evmcs->hv_clean_fields &= ~clean_field;
1491}
1492
1493static inline u64 evmcs_read64(unsigned long field)
1494{
1495 int offset = get_evmcs_offset(field, NULL);
1496
1497 if (offset < 0)
1498 return 0;
1499
1500 return *(u64 *)((char *)current_evmcs + offset);
1501}
1502
1503static inline u32 evmcs_read32(unsigned long field)
1504{
1505 int offset = get_evmcs_offset(field, NULL);
1506
1507 if (offset < 0)
1508 return 0;
1509
1510 return *(u32 *)((char *)current_evmcs + offset);
1511}
1512
1513static inline u16 evmcs_read16(unsigned long field)
1514{
1515 int offset = get_evmcs_offset(field, NULL);
1516
1517 if (offset < 0)
1518 return 0;
1519
1520 return *(u16 *)((char *)current_evmcs + offset);
1521}
1522
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001523static inline void evmcs_touch_msr_bitmap(void)
1524{
1525 if (unlikely(!current_evmcs))
1526 return;
1527
1528 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1529 current_evmcs->hv_clean_fields &=
1530 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1531}
1532
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001533static void evmcs_load(u64 phys_addr)
1534{
1535 struct hv_vp_assist_page *vp_ap =
1536 hv_get_vp_assist_page(smp_processor_id());
1537
1538 vp_ap->current_nested_vmcs = phys_addr;
1539 vp_ap->enlighten_vmentry = 1;
1540}
1541
1542static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1543{
Vitaly Kuznetsov5d7a6442018-10-16 18:50:00 +02001544 vmcs_conf->pin_based_exec_ctrl &= ~EVMCS1_UNSUPPORTED_PINCTRL;
1545 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~EVMCS1_UNSUPPORTED_2NDEXEC;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001546
Vitaly Kuznetsov5d7a6442018-10-16 18:50:00 +02001547 vmcs_conf->vmexit_ctrl &= ~EVMCS1_UNSUPPORTED_VMEXIT_CTRL;
1548 vmcs_conf->vmentry_ctrl &= ~EVMCS1_UNSUPPORTED_VMENTRY_CTRL;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001549
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001550}
Tianyu Lan877ad952018-07-19 08:40:23 +00001551
1552/* check_ept_pointer() should be under protection of ept_pointer_lock. */
1553static void check_ept_pointer_match(struct kvm *kvm)
1554{
1555 struct kvm_vcpu *vcpu;
1556 u64 tmp_eptp = INVALID_PAGE;
1557 int i;
1558
1559 kvm_for_each_vcpu(i, vcpu, kvm) {
1560 if (!VALID_PAGE(tmp_eptp)) {
1561 tmp_eptp = to_vmx(vcpu)->ept_pointer;
1562 } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
1563 to_kvm_vmx(kvm)->ept_pointers_match
1564 = EPT_POINTERS_MISMATCH;
1565 return;
1566 }
1567 }
1568
1569 to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
1570}
1571
1572static int vmx_hv_remote_flush_tlb(struct kvm *kvm)
1573{
Lan Tianyua5c214d2018-10-13 22:54:05 +08001574 struct kvm_vcpu *vcpu;
1575 int ret = -ENOTSUPP, i;
Tianyu Lan877ad952018-07-19 08:40:23 +00001576
1577 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1578
1579 if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
1580 check_ept_pointer_match(kvm);
1581
Vitaly Kuznetsov5f8bb002018-10-11 12:03:12 +02001582 /*
1583 * FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE hypercall needs the address of the
1584 * base of EPT PML4 table, strip off EPT configuration information.
1585 */
Tianyu Lan877ad952018-07-19 08:40:23 +00001586 if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
Lan Tianyua5c214d2018-10-13 22:54:05 +08001587 kvm_for_each_vcpu(i, vcpu, kvm)
1588 ret |= hyperv_flush_guest_mapping(
Linus Torvalds0d1e8b82018-10-25 17:57:35 -07001589 to_vmx(kvm_get_vcpu(kvm, i))->ept_pointer & PAGE_MASK);
Lan Tianyua5c214d2018-10-13 22:54:05 +08001590 } else {
1591 ret = hyperv_flush_guest_mapping(
Linus Torvalds0d1e8b82018-10-25 17:57:35 -07001592 to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer & PAGE_MASK);
Tianyu Lan877ad952018-07-19 08:40:23 +00001593 }
Tianyu Lan877ad952018-07-19 08:40:23 +00001594
Tianyu Lan877ad952018-07-19 08:40:23 +00001595 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1596 return ret;
1597}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001598#else /* !IS_ENABLED(CONFIG_HYPERV) */
1599static inline void evmcs_write64(unsigned long field, u64 value) {}
1600static inline void evmcs_write32(unsigned long field, u32 value) {}
1601static inline void evmcs_write16(unsigned long field, u16 value) {}
1602static inline u64 evmcs_read64(unsigned long field) { return 0; }
1603static inline u32 evmcs_read32(unsigned long field) { return 0; }
1604static inline u16 evmcs_read16(unsigned long field) { return 0; }
1605static inline void evmcs_load(u64 phys_addr) {}
1606static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001607static inline void evmcs_touch_msr_bitmap(void) {}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001608#endif /* IS_ENABLED(CONFIG_HYPERV) */
1609
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02001610static int nested_enable_evmcs(struct kvm_vcpu *vcpu,
1611 uint16_t *vmcs_version)
1612{
1613 struct vcpu_vmx *vmx = to_vmx(vcpu);
1614
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02001615 /*
1616 * vmcs_version represents the range of supported Enlightened VMCS
1617 * versions: lower 8 bits is the minimal version, higher 8 bits is the
1618 * maximum supported version. KVM supports versions from 1 to
1619 * KVM_EVMCS_VERSION.
1620 */
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02001621 if (vmcs_version)
1622 *vmcs_version = (KVM_EVMCS_VERSION << 8) | 1;
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02001623
Liran Alon7f9ad1d2018-11-17 14:05:06 +02001624 /* We don't support disabling the feature for simplicity. */
1625 if (vmx->nested.enlightened_vmcs_enabled)
1626 return 0;
1627
1628 vmx->nested.enlightened_vmcs_enabled = true;
1629
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02001630 vmx->nested.msrs.pinbased_ctls_high &= ~EVMCS1_UNSUPPORTED_PINCTRL;
1631 vmx->nested.msrs.entry_ctls_high &= ~EVMCS1_UNSUPPORTED_VMENTRY_CTRL;
1632 vmx->nested.msrs.exit_ctls_high &= ~EVMCS1_UNSUPPORTED_VMEXIT_CTRL;
1633 vmx->nested.msrs.secondary_ctls_high &= ~EVMCS1_UNSUPPORTED_2NDEXEC;
1634 vmx->nested.msrs.vmfunc_controls &= ~EVMCS1_UNSUPPORTED_VMFUNC;
1635
1636 return 0;
1637}
1638
Jan Kiszka5bb16012016-02-09 20:14:21 +01001639static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001640{
1641 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1642 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001643 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1644}
1645
Jan Kiszka6f054852016-02-09 20:15:18 +01001646static inline bool is_debug(u32 intr_info)
1647{
1648 return is_exception_n(intr_info, DB_VECTOR);
1649}
1650
1651static inline bool is_breakpoint(u32 intr_info)
1652{
1653 return is_exception_n(intr_info, BP_VECTOR);
1654}
1655
Jan Kiszka5bb16012016-02-09 20:14:21 +01001656static inline bool is_page_fault(u32 intr_info)
1657{
1658 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001659}
1660
Gui Jianfeng31299942010-03-15 17:29:09 +08001661static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001662{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001663 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001664}
1665
Liran Alon9e869482018-03-12 13:12:51 +02001666static inline bool is_gp_fault(u32 intr_info)
1667{
1668 return is_exception_n(intr_info, GP_VECTOR);
1669}
1670
Gui Jianfeng31299942010-03-15 17:29:09 +08001671static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001672{
1673 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1674 INTR_INFO_VALID_MASK)) ==
1675 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1676}
1677
Linus Torvalds32d43cd2018-03-20 12:16:59 -07001678/* Undocumented: icebp/int1 */
1679static inline bool is_icebp(u32 intr_info)
1680{
1681 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1682 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1683}
1684
Gui Jianfeng31299942010-03-15 17:29:09 +08001685static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001686{
Sheng Yang04547152009-04-01 15:52:31 +08001687 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001688}
1689
Gui Jianfeng31299942010-03-15 17:29:09 +08001690static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001691{
Sheng Yang04547152009-04-01 15:52:31 +08001692 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001693}
1694
Paolo Bonzini35754c92015-07-29 12:05:37 +02001695static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001696{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001697 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001698}
1699
Gui Jianfeng31299942010-03-15 17:29:09 +08001700static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001701{
Sheng Yang04547152009-04-01 15:52:31 +08001702 return vmcs_config.cpu_based_exec_ctrl &
1703 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001704}
1705
Avi Kivity774ead32007-12-26 13:57:04 +02001706static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001707{
Sheng Yang04547152009-04-01 15:52:31 +08001708 return vmcs_config.cpu_based_2nd_exec_ctrl &
1709 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1710}
1711
Yang Zhang8d146952013-01-25 10:18:50 +08001712static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1713{
1714 return vmcs_config.cpu_based_2nd_exec_ctrl &
1715 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1716}
1717
Yang Zhang83d4c282013-01-25 10:18:49 +08001718static inline bool cpu_has_vmx_apic_register_virt(void)
1719{
1720 return vmcs_config.cpu_based_2nd_exec_ctrl &
1721 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1722}
1723
Yang Zhangc7c9c562013-01-25 10:18:51 +08001724static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1725{
1726 return vmcs_config.cpu_based_2nd_exec_ctrl &
1727 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1728}
1729
Sean Christopherson0b665d32018-08-14 09:33:34 -07001730static inline bool cpu_has_vmx_encls_vmexit(void)
1731{
1732 return vmcs_config.cpu_based_2nd_exec_ctrl &
1733 SECONDARY_EXEC_ENCLS_EXITING;
1734}
1735
Yunhong Jiang64672c92016-06-13 14:19:59 -07001736/*
1737 * Comment's format: document - errata name - stepping - processor name.
1738 * Refer from
1739 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1740 */
1741static u32 vmx_preemption_cpu_tfms[] = {
1742/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
17430x000206E6,
1744/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1745/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1746/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
17470x00020652,
1748/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
17490x00020655,
1750/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1751/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1752/*
1753 * 320767.pdf - AAP86 - B1 -
1754 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1755 */
17560x000106E5,
1757/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
17580x000106A0,
1759/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
17600x000106A1,
1761/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
17620x000106A4,
1763 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1764 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1765 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
17660x000106A5,
1767};
1768
1769static inline bool cpu_has_broken_vmx_preemption_timer(void)
1770{
1771 u32 eax = cpuid_eax(0x00000001), i;
1772
1773 /* Clear the reserved bits */
1774 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001775 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001776 if (eax == vmx_preemption_cpu_tfms[i])
1777 return true;
1778
1779 return false;
1780}
1781
1782static inline bool cpu_has_vmx_preemption_timer(void)
1783{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001784 return vmcs_config.pin_based_exec_ctrl &
1785 PIN_BASED_VMX_PREEMPTION_TIMER;
1786}
1787
Yang Zhang01e439b2013-04-11 19:25:12 +08001788static inline bool cpu_has_vmx_posted_intr(void)
1789{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001790 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1791 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001792}
1793
1794static inline bool cpu_has_vmx_apicv(void)
1795{
1796 return cpu_has_vmx_apic_register_virt() &&
1797 cpu_has_vmx_virtual_intr_delivery() &&
1798 cpu_has_vmx_posted_intr();
1799}
1800
Sheng Yang04547152009-04-01 15:52:31 +08001801static inline bool cpu_has_vmx_flexpriority(void)
1802{
1803 return cpu_has_vmx_tpr_shadow() &&
1804 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001805}
1806
Marcelo Tosattie7997942009-06-11 12:07:40 -03001807static inline bool cpu_has_vmx_ept_execute_only(void)
1808{
Gui Jianfeng31299942010-03-15 17:29:09 +08001809 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001810}
1811
Marcelo Tosattie7997942009-06-11 12:07:40 -03001812static inline bool cpu_has_vmx_ept_2m_page(void)
1813{
Gui Jianfeng31299942010-03-15 17:29:09 +08001814 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001815}
1816
Sheng Yang878403b2010-01-05 19:02:29 +08001817static inline bool cpu_has_vmx_ept_1g_page(void)
1818{
Gui Jianfeng31299942010-03-15 17:29:09 +08001819 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001820}
1821
Sheng Yang4bc9b982010-06-02 14:05:24 +08001822static inline bool cpu_has_vmx_ept_4levels(void)
1823{
1824 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1825}
1826
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001827static inline bool cpu_has_vmx_ept_mt_wb(void)
1828{
1829 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1830}
1831
Yu Zhang855feb62017-08-24 20:27:55 +08001832static inline bool cpu_has_vmx_ept_5levels(void)
1833{
1834 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1835}
1836
Xudong Hao83c3a332012-05-28 19:33:35 +08001837static inline bool cpu_has_vmx_ept_ad_bits(void)
1838{
1839 return vmx_capability.ept & VMX_EPT_AD_BIT;
1840}
1841
Gui Jianfeng31299942010-03-15 17:29:09 +08001842static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001843{
Gui Jianfeng31299942010-03-15 17:29:09 +08001844 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001845}
1846
Gui Jianfeng31299942010-03-15 17:29:09 +08001847static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001848{
Gui Jianfeng31299942010-03-15 17:29:09 +08001849 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001850}
1851
Liran Aloncd9a4912018-05-22 17:16:15 +03001852static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1853{
1854 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1855}
1856
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001857static inline bool cpu_has_vmx_invvpid_single(void)
1858{
1859 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1860}
1861
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001862static inline bool cpu_has_vmx_invvpid_global(void)
1863{
1864 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1865}
1866
Wanpeng Li08d839c2017-03-23 05:30:08 -07001867static inline bool cpu_has_vmx_invvpid(void)
1868{
1869 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1870}
1871
Gui Jianfeng31299942010-03-15 17:29:09 +08001872static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001873{
Sheng Yang04547152009-04-01 15:52:31 +08001874 return vmcs_config.cpu_based_2nd_exec_ctrl &
1875 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001876}
1877
Gui Jianfeng31299942010-03-15 17:29:09 +08001878static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001879{
1880 return vmcs_config.cpu_based_2nd_exec_ctrl &
1881 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1882}
1883
Gui Jianfeng31299942010-03-15 17:29:09 +08001884static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001885{
1886 return vmcs_config.cpu_based_2nd_exec_ctrl &
1887 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1888}
1889
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001890static inline bool cpu_has_vmx_basic_inout(void)
1891{
1892 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1893}
1894
Paolo Bonzini35754c92015-07-29 12:05:37 +02001895static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001896{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001897 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001898}
1899
Gui Jianfeng31299942010-03-15 17:29:09 +08001900static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001901{
Sheng Yang04547152009-04-01 15:52:31 +08001902 return vmcs_config.cpu_based_2nd_exec_ctrl &
1903 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001904}
1905
Gui Jianfeng31299942010-03-15 17:29:09 +08001906static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001907{
1908 return vmcs_config.cpu_based_2nd_exec_ctrl &
1909 SECONDARY_EXEC_RDTSCP;
1910}
1911
Mao, Junjiead756a12012-07-02 01:18:48 +00001912static inline bool cpu_has_vmx_invpcid(void)
1913{
1914 return vmcs_config.cpu_based_2nd_exec_ctrl &
1915 SECONDARY_EXEC_ENABLE_INVPCID;
1916}
1917
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001918static inline bool cpu_has_virtual_nmis(void)
1919{
1920 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1921}
1922
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001923static inline bool cpu_has_vmx_wbinvd_exit(void)
1924{
1925 return vmcs_config.cpu_based_2nd_exec_ctrl &
1926 SECONDARY_EXEC_WBINVD_EXITING;
1927}
1928
Abel Gordonabc4fc52013-04-18 14:35:25 +03001929static inline bool cpu_has_vmx_shadow_vmcs(void)
1930{
1931 u64 vmx_msr;
1932 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1933 /* check if the cpu supports writing r/o exit information fields */
1934 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1935 return false;
1936
1937 return vmcs_config.cpu_based_2nd_exec_ctrl &
1938 SECONDARY_EXEC_SHADOW_VMCS;
1939}
1940
Kai Huang843e4332015-01-28 10:54:28 +08001941static inline bool cpu_has_vmx_pml(void)
1942{
1943 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1944}
1945
Haozhong Zhang64903d62015-10-20 15:39:09 +08001946static inline bool cpu_has_vmx_tsc_scaling(void)
1947{
1948 return vmcs_config.cpu_based_2nd_exec_ctrl &
1949 SECONDARY_EXEC_TSC_SCALING;
1950}
1951
Bandan Das2a499e42017-08-03 15:54:41 -04001952static inline bool cpu_has_vmx_vmfunc(void)
1953{
1954 return vmcs_config.cpu_based_2nd_exec_ctrl &
1955 SECONDARY_EXEC_ENABLE_VMFUNC;
1956}
1957
Sean Christopherson64f7a112018-04-30 10:01:06 -07001958static bool vmx_umip_emulated(void)
1959{
1960 return vmcs_config.cpu_based_2nd_exec_ctrl &
1961 SECONDARY_EXEC_DESC;
1962}
1963
Sheng Yang04547152009-04-01 15:52:31 +08001964static inline bool report_flexpriority(void)
1965{
1966 return flexpriority_enabled;
1967}
1968
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001969static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1970{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001971 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001972}
1973
Jim Mattsonf4160e42018-05-29 09:11:33 -07001974/*
1975 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1976 * to modify any valid field of the VMCS, or are the VM-exit
1977 * information fields read-only?
1978 */
1979static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1980{
1981 return to_vmx(vcpu)->nested.msrs.misc_low &
1982 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1983}
1984
Marc Orr04473782018-06-20 17:21:29 -07001985static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
1986{
1987 return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
1988}
1989
1990static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
1991{
1992 return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
1993 CPU_BASED_MONITOR_TRAP_FLAG;
1994}
1995
Liran Alonfa97d7d2018-07-18 14:07:59 +02001996static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu)
1997{
1998 return to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
1999 SECONDARY_EXEC_SHADOW_VMCS;
2000}
2001
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002002static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
2003{
2004 return vmcs12->cpu_based_vm_exec_control & bit;
2005}
2006
2007static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
2008{
2009 return (vmcs12->cpu_based_vm_exec_control &
2010 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
2011 (vmcs12->secondary_vm_exec_control & bit);
2012}
2013
Jan Kiszkaf4124502014-03-07 20:03:13 +01002014static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
2015{
2016 return vmcs12->pin_based_vm_exec_control &
2017 PIN_BASED_VMX_PREEMPTION_TIMER;
2018}
2019
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05002020static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
2021{
2022 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
2023}
2024
2025static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
2026{
2027 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
2028}
2029
Nadav Har'El155a97a2013-08-05 11:07:16 +03002030static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
2031{
2032 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
2033}
2034
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002035static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
2036{
Paolo Bonzini3db13482017-08-24 14:48:03 +02002037 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002038}
2039
Bandan Dasc5f983f2017-05-05 15:25:14 -04002040static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
2041{
2042 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
2043}
2044
Wincy Vanf2b93282015-02-03 23:56:03 +08002045static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
2046{
2047 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
2048}
2049
Wanpeng Li5c614b32015-10-13 09:18:36 -07002050static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
2051{
2052 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
2053}
2054
Wincy Van82f0dd42015-02-03 23:57:18 +08002055static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
2056{
2057 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
2058}
2059
Wincy Van608406e2015-02-03 23:57:51 +08002060static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
2061{
2062 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
2063}
2064
Wincy Van705699a2015-02-03 23:58:17 +08002065static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
2066{
2067 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
2068}
2069
Bandan Das27c42a12017-08-03 15:54:42 -04002070static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
2071{
2072 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
2073}
2074
Bandan Das41ab9372017-08-03 15:54:43 -04002075static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
2076{
2077 return nested_cpu_has_vmfunc(vmcs12) &&
2078 (vmcs12->vm_function_control &
2079 VMX_VMFUNC_EPTP_SWITCHING);
2080}
2081
Liran Alonf792d272018-06-23 02:35:05 +03002082static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12)
2083{
2084 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS);
2085}
2086
Jim Mattsonef85b672016-12-12 11:01:37 -08002087static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03002088{
2089 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08002090 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03002091}
2092
Jan Kiszka533558b2014-01-04 18:47:20 +01002093static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
2094 u32 exit_intr_info,
2095 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03002096
Rusty Russell8b9cf982007-07-30 16:31:43 +10002097static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08002098{
2099 int i;
2100
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002101 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03002102 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03002103 return i;
2104 return -1;
2105}
2106
Uros Bizjak5ebb2722018-10-11 19:40:43 +02002107static inline void __invvpid(unsigned long ext, u16 vpid, gva_t gva)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002108{
2109 struct {
2110 u64 vpid : 16;
2111 u64 rsvd : 48;
2112 u64 gva;
2113 } operand = { vpid, 0, gva };
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002114 bool error;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002115
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002116 asm volatile (__ex("invvpid %2, %1") CC_SET(na)
2117 : CC_OUT(na) (error) : "r"(ext), "m"(operand));
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002118 BUG_ON(error);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002119}
2120
Uros Bizjak5ebb2722018-10-11 19:40:43 +02002121static inline void __invept(unsigned long ext, u64 eptp, gpa_t gpa)
Sheng Yang14394422008-04-28 12:24:45 +08002122{
2123 struct {
2124 u64 eptp, gpa;
2125 } operand = {eptp, gpa};
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002126 bool error;
Sheng Yang14394422008-04-28 12:24:45 +08002127
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002128 asm volatile (__ex("invept %2, %1") CC_SET(na)
2129 : CC_OUT(na) (error) : "r"(ext), "m"(operand));
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002130 BUG_ON(error);
Sheng Yang14394422008-04-28 12:24:45 +08002131}
2132
Avi Kivity26bb0982009-09-07 11:14:12 +03002133static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03002134{
2135 int i;
2136
Rusty Russell8b9cf982007-07-30 16:31:43 +10002137 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03002138 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002139 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00002140 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08002141}
2142
Avi Kivity6aa8b732006-12-10 02:21:36 -08002143static void vmcs_clear(struct vmcs *vmcs)
2144{
2145 u64 phys_addr = __pa(vmcs);
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002146 bool error;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002147
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002148 asm volatile (__ex("vmclear %1") CC_SET(na)
2149 : CC_OUT(na) (error) : "m"(phys_addr));
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002150 if (unlikely(error))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002151 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
2152 vmcs, phys_addr);
2153}
2154
Nadav Har'Eld462b812011-05-24 15:26:10 +03002155static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
2156{
2157 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07002158 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
2159 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002160 loaded_vmcs->cpu = -1;
2161 loaded_vmcs->launched = 0;
2162}
2163
Dongxiao Xu7725b892010-05-11 18:29:38 +08002164static void vmcs_load(struct vmcs *vmcs)
2165{
2166 u64 phys_addr = __pa(vmcs);
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002167 bool error;
Dongxiao Xu7725b892010-05-11 18:29:38 +08002168
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002169 if (static_branch_unlikely(&enable_evmcs))
2170 return evmcs_load(phys_addr);
2171
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002172 asm volatile (__ex("vmptrld %1") CC_SET(na)
2173 : CC_OUT(na) (error) : "m"(phys_addr));
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002174 if (unlikely(error))
Nadav Har'El2844d842011-05-25 23:16:40 +03002175 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08002176 vmcs, phys_addr);
2177}
2178
Dave Young2965faa2015-09-09 15:38:55 -07002179#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002180/*
2181 * This bitmap is used to indicate whether the vmclear
2182 * operation is enabled on all cpus. All disabled by
2183 * default.
2184 */
2185static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
2186
2187static inline void crash_enable_local_vmclear(int cpu)
2188{
2189 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
2190}
2191
2192static inline void crash_disable_local_vmclear(int cpu)
2193{
2194 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
2195}
2196
2197static inline int crash_local_vmclear_enabled(int cpu)
2198{
2199 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
2200}
2201
2202static void crash_vmclear_local_loaded_vmcss(void)
2203{
2204 int cpu = raw_smp_processor_id();
2205 struct loaded_vmcs *v;
2206
2207 if (!crash_local_vmclear_enabled(cpu))
2208 return;
2209
2210 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
2211 loaded_vmcss_on_cpu_link)
2212 vmcs_clear(v->vmcs);
2213}
2214#else
2215static inline void crash_enable_local_vmclear(int cpu) { }
2216static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07002217#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002218
Nadav Har'Eld462b812011-05-24 15:26:10 +03002219static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002220{
Nadav Har'Eld462b812011-05-24 15:26:10 +03002221 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08002222 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002223
Nadav Har'Eld462b812011-05-24 15:26:10 +03002224 if (loaded_vmcs->cpu != cpu)
2225 return; /* vcpu migration can race with cpu offline */
2226 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002227 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002228 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002229 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002230
2231 /*
2232 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
2233 * is before setting loaded_vmcs->vcpu to -1 which is done in
2234 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
2235 * then adds the vmcs into percpu list before it is deleted.
2236 */
2237 smp_wmb();
2238
Nadav Har'Eld462b812011-05-24 15:26:10 +03002239 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002240 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002241}
2242
Nadav Har'Eld462b812011-05-24 15:26:10 +03002243static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002244{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08002245 int cpu = loaded_vmcs->cpu;
2246
2247 if (cpu != -1)
2248 smp_call_function_single(cpu,
2249 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002250}
2251
Junaid Shahidfaff8752018-06-29 13:10:05 -07002252static inline bool vpid_sync_vcpu_addr(int vpid, gva_t addr)
2253{
2254 if (vpid == 0)
2255 return true;
2256
2257 if (cpu_has_vmx_invvpid_individual_addr()) {
2258 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, vpid, addr);
2259 return true;
2260 }
2261
2262 return false;
2263}
2264
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002265static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002266{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002267 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002268 return;
2269
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08002270 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002271 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002272}
2273
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002274static inline void vpid_sync_vcpu_global(void)
2275{
2276 if (cpu_has_vmx_invvpid_global())
2277 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
2278}
2279
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002280static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002281{
2282 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002283 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002284 else
2285 vpid_sync_vcpu_global();
2286}
2287
Sheng Yang14394422008-04-28 12:24:45 +08002288static inline void ept_sync_global(void)
2289{
David Hildenbrandf5f51582017-08-24 20:51:30 +02002290 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08002291}
2292
2293static inline void ept_sync_context(u64 eptp)
2294{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02002295 if (cpu_has_vmx_invept_context())
2296 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
2297 else
2298 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08002299}
2300
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002301static __always_inline void vmcs_check16(unsigned long field)
2302{
2303 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2304 "16-bit accessor invalid for 64-bit field");
2305 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2306 "16-bit accessor invalid for 64-bit high field");
2307 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2308 "16-bit accessor invalid for 32-bit high field");
2309 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2310 "16-bit accessor invalid for natural width field");
2311}
2312
2313static __always_inline void vmcs_check32(unsigned long field)
2314{
2315 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2316 "32-bit accessor invalid for 16-bit field");
2317 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2318 "32-bit accessor invalid for natural width field");
2319}
2320
2321static __always_inline void vmcs_check64(unsigned long field)
2322{
2323 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2324 "64-bit accessor invalid for 16-bit field");
2325 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2326 "64-bit accessor invalid for 64-bit high field");
2327 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2328 "64-bit accessor invalid for 32-bit field");
2329 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2330 "64-bit accessor invalid for natural width field");
2331}
2332
2333static __always_inline void vmcs_checkl(unsigned long field)
2334{
2335 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2336 "Natural width accessor invalid for 16-bit field");
2337 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2338 "Natural width accessor invalid for 64-bit field");
2339 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2340 "Natural width accessor invalid for 64-bit high field");
2341 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2342 "Natural width accessor invalid for 32-bit field");
2343}
2344
2345static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002346{
Avi Kivity5e520e62011-05-15 10:13:12 -04002347 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002348
Uros Bizjak44c2d662018-10-11 19:40:45 +02002349 asm volatile (__ex_clear("vmread %1, %0", "%k0")
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002350 : "=r"(value) : "r"(field));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002351 return value;
2352}
2353
Avi Kivity96304212011-05-15 10:13:13 -04002354static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002355{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002356 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002357 if (static_branch_unlikely(&enable_evmcs))
2358 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002359 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002360}
2361
Avi Kivity96304212011-05-15 10:13:13 -04002362static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002363{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002364 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002365 if (static_branch_unlikely(&enable_evmcs))
2366 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002367 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002368}
2369
Avi Kivity96304212011-05-15 10:13:13 -04002370static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002371{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002372 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002373 if (static_branch_unlikely(&enable_evmcs))
2374 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002375#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002376 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002377#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002378 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002379#endif
2380}
2381
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002382static __always_inline unsigned long vmcs_readl(unsigned long field)
2383{
2384 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002385 if (static_branch_unlikely(&enable_evmcs))
2386 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002387 return __vmcs_readl(field);
2388}
2389
Avi Kivitye52de1b2007-01-05 16:36:56 -08002390static noinline void vmwrite_error(unsigned long field, unsigned long value)
2391{
2392 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2393 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2394 dump_stack();
2395}
2396
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002397static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002398{
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002399 bool error;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002400
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002401 asm volatile (__ex("vmwrite %2, %1") CC_SET(na)
2402 : CC_OUT(na) (error) : "r"(field), "rm"(value));
Avi Kivitye52de1b2007-01-05 16:36:56 -08002403 if (unlikely(error))
2404 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002405}
2406
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002407static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002408{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002409 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002410 if (static_branch_unlikely(&enable_evmcs))
2411 return evmcs_write16(field, value);
2412
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002413 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002414}
2415
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002416static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002417{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002418 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002419 if (static_branch_unlikely(&enable_evmcs))
2420 return evmcs_write32(field, value);
2421
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002422 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002423}
2424
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002425static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002426{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002427 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002428 if (static_branch_unlikely(&enable_evmcs))
2429 return evmcs_write64(field, value);
2430
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002431 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03002432#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002433 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002434 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002435#endif
2436}
2437
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002438static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002439{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002440 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002441 if (static_branch_unlikely(&enable_evmcs))
2442 return evmcs_write64(field, value);
2443
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002444 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002445}
2446
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002447static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002448{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002449 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2450 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002451 if (static_branch_unlikely(&enable_evmcs))
2452 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2453
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002454 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2455}
2456
2457static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2458{
2459 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2460 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002461 if (static_branch_unlikely(&enable_evmcs))
2462 return evmcs_write32(field, evmcs_read32(field) | mask);
2463
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002464 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002465}
2466
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002467static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2468{
2469 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2470}
2471
Gleb Natapov2961e8762013-11-25 15:37:13 +02002472static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2473{
2474 vmcs_write32(VM_ENTRY_CONTROLS, val);
2475 vmx->vm_entry_controls_shadow = val;
2476}
2477
2478static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2479{
2480 if (vmx->vm_entry_controls_shadow != val)
2481 vm_entry_controls_init(vmx, val);
2482}
2483
2484static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2485{
2486 return vmx->vm_entry_controls_shadow;
2487}
2488
2489
2490static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2491{
2492 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2493}
2494
2495static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2496{
2497 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2498}
2499
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002500static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2501{
2502 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2503}
2504
Gleb Natapov2961e8762013-11-25 15:37:13 +02002505static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2506{
2507 vmcs_write32(VM_EXIT_CONTROLS, val);
2508 vmx->vm_exit_controls_shadow = val;
2509}
2510
2511static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2512{
2513 if (vmx->vm_exit_controls_shadow != val)
2514 vm_exit_controls_init(vmx, val);
2515}
2516
2517static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2518{
2519 return vmx->vm_exit_controls_shadow;
2520}
2521
2522
2523static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2524{
2525 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2526}
2527
2528static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2529{
2530 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2531}
2532
Avi Kivity2fb92db2011-04-27 19:42:18 +03002533static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2534{
2535 vmx->segment_cache.bitmask = 0;
2536}
2537
2538static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2539 unsigned field)
2540{
2541 bool ret;
2542 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2543
2544 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2545 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2546 vmx->segment_cache.bitmask = 0;
2547 }
2548 ret = vmx->segment_cache.bitmask & mask;
2549 vmx->segment_cache.bitmask |= mask;
2550 return ret;
2551}
2552
2553static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2554{
2555 u16 *p = &vmx->segment_cache.seg[seg].selector;
2556
2557 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2558 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2559 return *p;
2560}
2561
2562static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2563{
2564 ulong *p = &vmx->segment_cache.seg[seg].base;
2565
2566 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2567 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2568 return *p;
2569}
2570
2571static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2572{
2573 u32 *p = &vmx->segment_cache.seg[seg].limit;
2574
2575 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2576 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2577 return *p;
2578}
2579
2580static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2581{
2582 u32 *p = &vmx->segment_cache.seg[seg].ar;
2583
2584 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2585 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2586 return *p;
2587}
2588
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002589static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2590{
2591 u32 eb;
2592
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002593 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002594 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002595 /*
2596 * Guest access to VMware backdoor ports could legitimately
2597 * trigger #GP because of TSS I/O permission bitmap.
2598 * We intercept those #GP and allow access to them anyway
2599 * as VMware does.
2600 */
2601 if (enable_vmware_backdoor)
2602 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002603 if ((vcpu->guest_debug &
2604 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2605 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2606 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002607 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002608 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002609 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002610 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002611
2612 /* When we are running a nested L2 guest and L1 specified for it a
2613 * certain exception bitmap, we must trap the same exceptions and pass
2614 * them to L1. When running L2, we will only handle the exceptions
2615 * specified above if L1 did not want them.
2616 */
2617 if (is_guest_mode(vcpu))
2618 eb |= get_vmcs12(vcpu)->exception_bitmap;
2619
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002620 vmcs_write32(EXCEPTION_BITMAP, eb);
2621}
2622
Ashok Raj15d45072018-02-01 22:59:43 +01002623/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002624 * Check if MSR is intercepted for currently loaded MSR bitmap.
2625 */
2626static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2627{
2628 unsigned long *msr_bitmap;
2629 int f = sizeof(unsigned long);
2630
2631 if (!cpu_has_vmx_msr_bitmap())
2632 return true;
2633
2634 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2635
2636 if (msr <= 0x1fff) {
2637 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2638 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2639 msr &= 0x1fff;
2640 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2641 }
2642
2643 return true;
2644}
2645
2646/*
Ashok Raj15d45072018-02-01 22:59:43 +01002647 * Check if MSR is intercepted for L01 MSR bitmap.
2648 */
2649static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2650{
2651 unsigned long *msr_bitmap;
2652 int f = sizeof(unsigned long);
2653
2654 if (!cpu_has_vmx_msr_bitmap())
2655 return true;
2656
2657 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2658
2659 if (msr <= 0x1fff) {
2660 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2661 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2662 msr &= 0x1fff;
2663 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2664 }
2665
2666 return true;
2667}
2668
Gleb Natapov2961e8762013-11-25 15:37:13 +02002669static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2670 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002671{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002672 vm_entry_controls_clearbit(vmx, entry);
2673 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002674}
2675
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002676static int find_msr(struct vmx_msrs *m, unsigned int msr)
2677{
2678 unsigned int i;
2679
2680 for (i = 0; i < m->nr; ++i) {
2681 if (m->val[i].index == msr)
2682 return i;
2683 }
2684 return -ENOENT;
2685}
2686
Avi Kivity61d2ef22010-04-28 16:40:38 +03002687static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2688{
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002689 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002690 struct msr_autoload *m = &vmx->msr_autoload;
2691
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002692 switch (msr) {
2693 case MSR_EFER:
2694 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002695 clear_atomic_switch_msr_special(vmx,
2696 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002697 VM_EXIT_LOAD_IA32_EFER);
2698 return;
2699 }
2700 break;
2701 case MSR_CORE_PERF_GLOBAL_CTRL:
2702 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002703 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002704 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2705 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2706 return;
2707 }
2708 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002709 }
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002710 i = find_msr(&m->guest, msr);
2711 if (i < 0)
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002712 goto skip_guest;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002713 --m->guest.nr;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002714 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002715 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Avi Kivity110312c2010-12-21 12:54:20 +02002716
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002717skip_guest:
2718 i = find_msr(&m->host, msr);
2719 if (i < 0)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002720 return;
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002721
2722 --m->host.nr;
2723 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002724 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002725}
2726
Gleb Natapov2961e8762013-11-25 15:37:13 +02002727static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2728 unsigned long entry, unsigned long exit,
2729 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2730 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002731{
2732 vmcs_write64(guest_val_vmcs, guest_val);
Sean Christopherson5a5e8a12018-09-26 09:23:56 -07002733 if (host_val_vmcs != HOST_IA32_EFER)
2734 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002735 vm_entry_controls_setbit(vmx, entry);
2736 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002737}
2738
Avi Kivity61d2ef22010-04-28 16:40:38 +03002739static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002740 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002741{
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002742 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002743 struct msr_autoload *m = &vmx->msr_autoload;
2744
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002745 switch (msr) {
2746 case MSR_EFER:
2747 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002748 add_atomic_switch_msr_special(vmx,
2749 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002750 VM_EXIT_LOAD_IA32_EFER,
2751 GUEST_IA32_EFER,
2752 HOST_IA32_EFER,
2753 guest_val, host_val);
2754 return;
2755 }
2756 break;
2757 case MSR_CORE_PERF_GLOBAL_CTRL:
2758 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002759 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002760 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2761 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2762 GUEST_IA32_PERF_GLOBAL_CTRL,
2763 HOST_IA32_PERF_GLOBAL_CTRL,
2764 guest_val, host_val);
2765 return;
2766 }
2767 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002768 case MSR_IA32_PEBS_ENABLE:
2769 /* PEBS needs a quiescent period after being disabled (to write
2770 * a record). Disabling PEBS through VMX MSR swapping doesn't
2771 * provide that period, so a CPU could write host's record into
2772 * guest's memory.
2773 */
2774 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002775 }
2776
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002777 i = find_msr(&m->guest, msr);
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002778 if (!entry_only)
2779 j = find_msr(&m->host, msr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002780
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002781 if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002782 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002783 "Can't add msr %x\n", msr);
2784 return;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002785 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002786 if (i < 0) {
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002787 i = m->guest.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002788 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002789 }
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002790 m->guest.val[i].index = msr;
2791 m->guest.val[i].value = guest_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002792
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002793 if (entry_only)
2794 return;
2795
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002796 if (j < 0) {
2797 j = m->host.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002798 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002799 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002800 m->host.val[j].index = msr;
2801 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002802}
2803
Avi Kivity92c0d902009-10-29 11:00:16 +02002804static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002805{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002806 u64 guest_efer = vmx->vcpu.arch.efer;
2807 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002808
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002809 if (!enable_ept) {
2810 /*
2811 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2812 * host CPUID is more efficient than testing guest CPUID
2813 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2814 */
2815 if (boot_cpu_has(X86_FEATURE_SMEP))
2816 guest_efer |= EFER_NX;
2817 else if (!(guest_efer & EFER_NX))
2818 ignore_bits |= EFER_NX;
2819 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002820
Avi Kivity51c6cf62007-08-29 03:48:05 +03002821 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002822 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002823 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002824 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002825#ifdef CONFIG_X86_64
2826 ignore_bits |= EFER_LMA | EFER_LME;
2827 /* SCE is meaningful only in long mode on Intel */
2828 if (guest_efer & EFER_LMA)
2829 ignore_bits &= ~(u64)EFER_SCE;
2830#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002831
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002832 /*
2833 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2834 * On CPUs that support "load IA32_EFER", always switch EFER
2835 * atomically, since it's faster than switching it manually.
2836 */
2837 if (cpu_has_load_ia32_efer ||
2838 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002839 if (!(guest_efer & EFER_LMA))
2840 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002841 if (guest_efer != host_efer)
2842 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002843 guest_efer, host_efer, false);
Sean Christopherson02343cf2018-09-26 09:23:43 -07002844 else
2845 clear_atomic_switch_msr(vmx, MSR_EFER);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002846 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002847 } else {
Sean Christopherson02343cf2018-09-26 09:23:43 -07002848 clear_atomic_switch_msr(vmx, MSR_EFER);
2849
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002850 guest_efer &= ~ignore_bits;
2851 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002852
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002853 vmx->guest_msrs[efer_offset].data = guest_efer;
2854 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2855
2856 return true;
2857 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002858}
2859
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002860#ifdef CONFIG_X86_32
2861/*
2862 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2863 * VMCS rather than the segment table. KVM uses this helper to figure
2864 * out the current bases to poke them into the VMCS before entry.
2865 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002866static unsigned long segment_base(u16 selector)
2867{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002868 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002869 unsigned long v;
2870
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002871 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002872 return 0;
2873
Thomas Garnier45fc8752017-03-14 10:05:08 -07002874 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002875
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002876 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002877 u16 ldt_selector = kvm_read_ldt();
2878
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002879 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002880 return 0;
2881
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002882 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002883 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002884 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002885 return v;
2886}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002887#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002888
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002889static void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002890{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002891 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersond7ee0392018-07-23 12:32:47 -07002892 struct vmcs_host_state *host_state;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002893#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002894 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002895#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002896 unsigned long fs_base, gs_base;
2897 u16 fs_sel, gs_sel;
Avi Kivity26bb0982009-09-07 11:14:12 +03002898 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002899
Sean Christophersond264ee02018-08-27 15:21:12 -07002900 vmx->req_immediate_exit = false;
2901
Liran Alonf48b4712018-11-20 18:03:25 +02002902 /*
2903 * Note that guest MSRs to be saved/restored can also be changed
2904 * when guest state is loaded. This happens when guest transitions
2905 * to/from long-mode by setting MSR_EFER.LMA.
2906 */
2907 if (!vmx->loaded_cpu_state || vmx->guest_msrs_dirty) {
2908 vmx->guest_msrs_dirty = false;
2909 for (i = 0; i < vmx->save_nmsrs; ++i)
2910 kvm_set_shared_msr(vmx->guest_msrs[i].index,
2911 vmx->guest_msrs[i].data,
2912 vmx->guest_msrs[i].mask);
2913
2914 }
2915
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002916 if (vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002917 return;
2918
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002919 vmx->loaded_cpu_state = vmx->loaded_vmcs;
Sean Christophersond7ee0392018-07-23 12:32:47 -07002920 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002921
Avi Kivity33ed6322007-05-02 16:54:03 +03002922 /*
2923 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2924 * allow segment selectors with cpl > 0 or ti == 1.
2925 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07002926 host_state->ldt_sel = kvm_read_ldt();
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002927
2928#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002929 savesegment(ds, host_state->ds_sel);
2930 savesegment(es, host_state->es_sel);
Sean Christophersone368b872018-07-23 12:32:41 -07002931
2932 gs_base = cpu_kernelmode_gs_base(cpu);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002933 if (likely(is_64bit_mm(current->mm))) {
2934 save_fsgs_for_kvm();
Sean Christophersone368b872018-07-23 12:32:41 -07002935 fs_sel = current->thread.fsindex;
2936 gs_sel = current->thread.gsindex;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002937 fs_base = current->thread.fsbase;
Sean Christophersone368b872018-07-23 12:32:41 -07002938 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002939 } else {
Sean Christophersone368b872018-07-23 12:32:41 -07002940 savesegment(fs, fs_sel);
2941 savesegment(gs, gs_sel);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002942 fs_base = read_msr(MSR_FS_BASE);
Sean Christophersone368b872018-07-23 12:32:41 -07002943 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
Avi Kivity33ed6322007-05-02 16:54:03 +03002944 }
2945
Paolo Bonzini4679b612018-09-24 17:23:01 +02002946 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002947#else
Sean Christophersone368b872018-07-23 12:32:41 -07002948 savesegment(fs, fs_sel);
2949 savesegment(gs, gs_sel);
2950 fs_base = segment_base(fs_sel);
2951 gs_base = segment_base(gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002952#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002953
Sean Christopherson8f21a0b2018-07-23 12:32:49 -07002954 if (unlikely(fs_sel != host_state->fs_sel)) {
2955 if (!(fs_sel & 7))
2956 vmcs_write16(HOST_FS_SELECTOR, fs_sel);
2957 else
2958 vmcs_write16(HOST_FS_SELECTOR, 0);
2959 host_state->fs_sel = fs_sel;
2960 }
2961 if (unlikely(gs_sel != host_state->gs_sel)) {
2962 if (!(gs_sel & 7))
2963 vmcs_write16(HOST_GS_SELECTOR, gs_sel);
2964 else
2965 vmcs_write16(HOST_GS_SELECTOR, 0);
2966 host_state->gs_sel = gs_sel;
2967 }
Sean Christopherson5e079c72018-07-23 12:32:50 -07002968 if (unlikely(fs_base != host_state->fs_base)) {
2969 vmcs_writel(HOST_FS_BASE, fs_base);
2970 host_state->fs_base = fs_base;
2971 }
2972 if (unlikely(gs_base != host_state->gs_base)) {
2973 vmcs_writel(HOST_GS_BASE, gs_base);
2974 host_state->gs_base = gs_base;
2975 }
Avi Kivity33ed6322007-05-02 16:54:03 +03002976}
2977
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002978static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002979{
Sean Christophersond7ee0392018-07-23 12:32:47 -07002980 struct vmcs_host_state *host_state;
2981
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002982 if (!vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002983 return;
2984
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002985 WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs);
Sean Christophersond7ee0392018-07-23 12:32:47 -07002986 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002987
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002988 ++vmx->vcpu.stat.host_state_reload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002989 vmx->loaded_cpu_state = NULL;
2990
Avi Kivityc8770e72010-11-11 12:37:26 +02002991#ifdef CONFIG_X86_64
Paolo Bonzini4679b612018-09-24 17:23:01 +02002992 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivityc8770e72010-11-11 12:37:26 +02002993#endif
Sean Christophersond7ee0392018-07-23 12:32:47 -07002994 if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
2995 kvm_load_ldt(host_state->ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002996#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002997 load_gs_index(host_state->gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002998#else
Sean Christophersond7ee0392018-07-23 12:32:47 -07002999 loadsegment(gs, host_state->gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03003000#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03003001 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07003002 if (host_state->fs_sel & 7)
3003 loadsegment(fs, host_state->fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03003004#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07003005 if (unlikely(host_state->ds_sel | host_state->es_sel)) {
3006 loadsegment(ds, host_state->ds_sel);
3007 loadsegment(es, host_state->es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03003008 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03003009#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08003010 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03003011#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02003012 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03003013#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07003014 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03003015}
3016
Sean Christopherson678e3152018-07-23 12:32:43 -07003017#ifdef CONFIG_X86_64
3018static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
Avi Kivitya9b21b62008-06-24 11:48:49 +03003019{
Paolo Bonzini4679b612018-09-24 17:23:01 +02003020 preempt_disable();
3021 if (vmx->loaded_cpu_state)
3022 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
3023 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07003024 return vmx->msr_guest_kernel_gs_base;
Avi Kivitya9b21b62008-06-24 11:48:49 +03003025}
3026
Sean Christopherson678e3152018-07-23 12:32:43 -07003027static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
3028{
Paolo Bonzini4679b612018-09-24 17:23:01 +02003029 preempt_disable();
3030 if (vmx->loaded_cpu_state)
3031 wrmsrl(MSR_KERNEL_GS_BASE, data);
3032 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07003033 vmx->msr_guest_kernel_gs_base = data;
3034}
3035#endif
3036
Feng Wu28b835d2015-09-18 22:29:54 +08003037static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
3038{
3039 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
3040 struct pi_desc old, new;
3041 unsigned int dest;
3042
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003043 /*
3044 * In case of hot-plug or hot-unplug, we may have to undo
3045 * vmx_vcpu_pi_put even if there is no assigned device. And we
3046 * always keep PI.NDST up to date for simplicity: it makes the
3047 * code easier, and CPU migration is not a fast path.
3048 */
3049 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08003050 return;
3051
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003052 /*
3053 * First handle the simple case where no cmpxchg is necessary; just
3054 * allow posting non-urgent interrupts.
3055 *
3056 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
3057 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
3058 * expects the VCPU to be on the blocked_vcpu_list that matches
3059 * PI.NDST.
3060 */
3061 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
3062 vcpu->cpu == cpu) {
3063 pi_clear_sn(pi_desc);
3064 return;
3065 }
3066
3067 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08003068 do {
3069 old.control = new.control = pi_desc->control;
3070
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003071 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08003072
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003073 if (x2apic_enabled())
3074 new.ndst = dest;
3075 else
3076 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08003077
Feng Wu28b835d2015-09-18 22:29:54 +08003078 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02003079 } while (cmpxchg64(&pi_desc->control, old.control,
3080 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08003081}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08003082
Peter Feinerc95ba922016-08-17 09:36:47 -07003083static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
3084{
3085 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
3086 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
3087}
3088
Avi Kivity6aa8b732006-12-10 02:21:36 -08003089/*
3090 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
3091 * vcpu mutex is already taken.
3092 */
Avi Kivity15ad7142007-07-11 18:17:21 +03003093static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003094{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003095 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003096 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003097
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003098 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003099 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003100 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003101 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08003102
3103 /*
3104 * Read loaded_vmcs->cpu should be before fetching
3105 * loaded_vmcs->loaded_vmcss_on_cpu_link.
3106 * See the comments in __loaded_vmcs_clear().
3107 */
3108 smp_rmb();
3109
Nadav Har'Eld462b812011-05-24 15:26:10 +03003110 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
3111 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003112 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003113 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003114 }
3115
3116 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
3117 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
3118 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01003119 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003120 }
3121
3122 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07003123 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003124 unsigned long sysenter_esp;
3125
3126 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003127
Avi Kivity6aa8b732006-12-10 02:21:36 -08003128 /*
3129 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08003130 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08003131 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08003132 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01003133 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07003134 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003135
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08003136 /*
3137 * VM exits change the host TR limit to 0x67 after a VM
3138 * exit. This is okay, since 0x67 covers everything except
3139 * the IO bitmap and have have code to handle the IO bitmap
3140 * being lost after a VM exit.
3141 */
3142 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
3143
Avi Kivity6aa8b732006-12-10 02:21:36 -08003144 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
3145 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08003146
Nadav Har'Eld462b812011-05-24 15:26:10 +03003147 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003148 }
Feng Wu28b835d2015-09-18 22:29:54 +08003149
Owen Hofmann2680d6d2016-03-01 13:36:13 -08003150 /* Setup TSC multiplier */
3151 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07003152 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
3153 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08003154
Feng Wu28b835d2015-09-18 22:29:54 +08003155 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08003156 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08003157 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08003158}
3159
3160static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
3161{
3162 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
3163
3164 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08003165 !irq_remapping_cap(IRQ_POSTING_CAP) ||
3166 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08003167 return;
3168
3169 /* Set SN when the vCPU is preempted */
3170 if (vcpu->preempted)
3171 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003172}
3173
3174static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
3175{
Feng Wu28b835d2015-09-18 22:29:54 +08003176 vmx_vcpu_pi_put(vcpu);
3177
Sean Christopherson6d6095b2018-07-23 12:32:44 -07003178 vmx_prepare_switch_to_host(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003179}
3180
Wanpeng Lif244dee2017-07-20 01:11:54 -07003181static bool emulation_required(struct kvm_vcpu *vcpu)
3182{
3183 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3184}
3185
Avi Kivityedcafe32009-12-30 18:07:40 +02003186static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
3187
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03003188/*
3189 * Return the cr0 value that a nested guest would read. This is a combination
3190 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
3191 * its hypervisor (cr0_read_shadow).
3192 */
3193static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
3194{
3195 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
3196 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
3197}
3198static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
3199{
3200 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
3201 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
3202}
3203
Avi Kivity6aa8b732006-12-10 02:21:36 -08003204static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
3205{
Avi Kivity78ac8b42010-04-08 18:19:35 +03003206 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03003207
Avi Kivity6de12732011-03-07 12:51:22 +02003208 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
3209 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3210 rflags = vmcs_readl(GUEST_RFLAGS);
3211 if (to_vmx(vcpu)->rmode.vm86_active) {
3212 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3213 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
3214 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
3215 }
3216 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003217 }
Avi Kivity6de12732011-03-07 12:51:22 +02003218 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003219}
3220
3221static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
3222{
Wanpeng Lif244dee2017-07-20 01:11:54 -07003223 unsigned long old_rflags = vmx_get_rflags(vcpu);
3224
Avi Kivity6de12732011-03-07 12:51:22 +02003225 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3226 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003227 if (to_vmx(vcpu)->rmode.vm86_active) {
3228 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003229 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003230 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003231 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07003232
3233 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
3234 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003235}
3236
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003237static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003238{
3239 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3240 int ret = 0;
3241
3242 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01003243 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003244 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01003245 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003246
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003247 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003248}
3249
3250static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
3251{
3252 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3253 u32 interruptibility = interruptibility_old;
3254
3255 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
3256
Jan Kiszka48005f62010-02-19 19:38:07 +01003257 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003258 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01003259 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003260 interruptibility |= GUEST_INTR_STATE_STI;
3261
3262 if ((interruptibility != interruptibility_old))
3263 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
3264}
3265
Avi Kivity6aa8b732006-12-10 02:21:36 -08003266static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
3267{
3268 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003269
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003270 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003271 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003272 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003273
Glauber Costa2809f5d2009-05-12 16:21:05 -04003274 /* skipping an emulated instruction also counts */
3275 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003276}
3277
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003278static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3279 unsigned long exit_qual)
3280{
3281 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3282 unsigned int nr = vcpu->arch.exception.nr;
3283 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3284
3285 if (vcpu->arch.exception.has_error_code) {
3286 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3287 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3288 }
3289
3290 if (kvm_exception_is_soft(nr))
3291 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3292 else
3293 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3294
3295 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3296 vmx_get_nmi_mask(vcpu))
3297 intr_info |= INTR_INFO_UNBLOCK_NMI;
3298
3299 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3300}
3301
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003302/*
3303 * KVM wants to inject page-faults which it got to the guest. This function
3304 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003305 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003306static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003307{
3308 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003309 unsigned int nr = vcpu->arch.exception.nr;
Jim Mattsonda998b42018-10-16 14:29:22 -07003310 bool has_payload = vcpu->arch.exception.has_payload;
3311 unsigned long payload = vcpu->arch.exception.payload;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003312
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003313 if (nr == PF_VECTOR) {
3314 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003315 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003316 return 1;
3317 }
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003318 if (nested_vmx_is_page_fault_vmexit(vmcs12,
3319 vcpu->arch.exception.error_code)) {
Jim Mattsonda998b42018-10-16 14:29:22 -07003320 *exit_qual = has_payload ? payload : vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003321 return 1;
3322 }
Jim Mattsonf10c7292018-10-16 14:29:23 -07003323 } else if (vmcs12->exception_bitmap & (1u << nr)) {
3324 if (nr == DB_VECTOR) {
3325 if (!has_payload) {
3326 payload = vcpu->arch.dr6;
3327 payload &= ~(DR6_FIXED_1 | DR6_BT);
3328 payload ^= DR6_RTM;
Jim Mattsoncfb634f2018-09-21 10:36:17 -07003329 }
Jim Mattsonf10c7292018-10-16 14:29:23 -07003330 *exit_qual = payload;
3331 } else
3332 *exit_qual = 0;
3333 return 1;
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003334 }
3335
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003336 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003337}
3338
Wanpeng Licaa057a2018-03-12 04:53:03 -07003339static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3340{
3341 /*
3342 * Ensure that we clear the HLT state in the VMCS. We don't need to
3343 * explicitly skip the instruction because if the HLT state is set,
3344 * then the instruction is already executing and RIP has already been
3345 * advanced.
3346 */
3347 if (kvm_hlt_in_guest(vcpu->kvm) &&
3348 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3349 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3350}
3351
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003352static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02003353{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003354 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003355 unsigned nr = vcpu->arch.exception.nr;
3356 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003357 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003358 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003359
Jim Mattsonda998b42018-10-16 14:29:22 -07003360 kvm_deliver_exception_payload(vcpu);
3361
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003362 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003363 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003364 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3365 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003366
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003367 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003368 int inc_eip = 0;
3369 if (kvm_exception_is_soft(nr))
3370 inc_eip = vcpu->arch.event_exit_inst_len;
3371 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003372 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003373 return;
3374 }
3375
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003376 WARN_ON_ONCE(vmx->emulation_required);
3377
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003378 if (kvm_exception_is_soft(nr)) {
3379 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3380 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003381 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3382 } else
3383 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3384
3385 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003386
3387 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003388}
3389
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003390static bool vmx_rdtscp_supported(void)
3391{
3392 return cpu_has_vmx_rdtscp();
3393}
3394
Mao, Junjiead756a12012-07-02 01:18:48 +00003395static bool vmx_invpcid_supported(void)
3396{
Junaid Shahideb4b2482018-06-27 14:59:14 -07003397 return cpu_has_vmx_invpcid();
Mao, Junjiead756a12012-07-02 01:18:48 +00003398}
3399
Avi Kivity6aa8b732006-12-10 02:21:36 -08003400/*
Eddie Donga75beee2007-05-17 18:55:15 +03003401 * Swap MSR entry in host/guest MSR entry array.
3402 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003403static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003404{
Avi Kivity26bb0982009-09-07 11:14:12 +03003405 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003406
3407 tmp = vmx->guest_msrs[to];
3408 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3409 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003410}
3411
3412/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003413 * Set up the vmcs to automatically save and restore system
3414 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3415 * mode, as fiddling with msrs is very expensive.
3416 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003417static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003418{
Avi Kivity26bb0982009-09-07 11:14:12 +03003419 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003420
Eddie Donga75beee2007-05-17 18:55:15 +03003421 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003422#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003423 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003424 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003425 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003426 move_msr_up(vmx, index, save_nmsrs++);
3427 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003428 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003429 move_msr_up(vmx, index, save_nmsrs++);
3430 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003431 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003432 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003433 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003434 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003435 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003436 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003437 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003438 * if efer.sce is enabled.
3439 */
Brian Gerst8c065852010-07-17 09:03:26 -04003440 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003441 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003442 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003443 }
Eddie Donga75beee2007-05-17 18:55:15 +03003444#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003445 index = __find_msr_index(vmx, MSR_EFER);
3446 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003447 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003448
Avi Kivity26bb0982009-09-07 11:14:12 +03003449 vmx->save_nmsrs = save_nmsrs;
Liran Alonf48b4712018-11-20 18:03:25 +02003450 vmx->guest_msrs_dirty = true;
Avi Kivity58972972009-02-24 22:26:47 +02003451
Yang Zhang8d146952013-01-25 10:18:50 +08003452 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003453 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003454}
3455
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003456static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003457{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003458 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003459
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003460 if (is_guest_mode(vcpu) &&
3461 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3462 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3463
3464 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003465}
3466
Leonid Shatz326e7422018-11-06 12:14:25 +02003467static u64 vmx_write_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003468{
Leonid Shatz326e7422018-11-06 12:14:25 +02003469 u64 active_offset = offset;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003470 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003471 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003472 * We're here if L1 chose not to trap WRMSR to TSC. According
3473 * to the spec, this should set L1's TSC; The offset that L1
3474 * set for L2 remains unchanged, and still needs to be added
3475 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003476 */
Leonid Shatz326e7422018-11-06 12:14:25 +02003477 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3478 if (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING))
3479 active_offset += vmcs12->tsc_offset;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003480 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003481 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3482 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003483 }
Leonid Shatz326e7422018-11-06 12:14:25 +02003484
3485 vmcs_write64(TSC_OFFSET, active_offset);
3486 return active_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003487}
3488
Nadav Har'El801d3422011-05-25 23:02:23 +03003489/*
3490 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3491 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3492 * all guests if the "nested" module option is off, and can also be disabled
3493 * for a single guest by disabling its VMX cpuid bit.
3494 */
3495static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3496{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003497 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003498}
3499
Avi Kivity6aa8b732006-12-10 02:21:36 -08003500/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003501 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3502 * returned for the various VMX controls MSRs when nested VMX is enabled.
3503 * The same values should also be used to verify that vmcs12 control fields are
3504 * valid during nested entry from L1 to L2.
3505 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3506 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3507 * bit in the high half is on if the corresponding bit in the control field
3508 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003509 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003510static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003511{
Paolo Bonzini13893092018-02-26 13:40:09 +01003512 if (!nested) {
3513 memset(msrs, 0, sizeof(*msrs));
3514 return;
3515 }
3516
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003517 /*
3518 * Note that as a general rule, the high half of the MSRs (bits in
3519 * the control fields which may be 1) should be initialized by the
3520 * intersection of the underlying hardware's MSR (i.e., features which
3521 * can be supported) and the list of features we want to expose -
3522 * because they are known to be properly supported in our code.
3523 * Also, usually, the low half of the MSRs (bits which must be 1) can
3524 * be set to 0, meaning that L1 may turn off any of these bits. The
3525 * reason is that if one of these bits is necessary, it will appear
3526 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3527 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003528 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003529 * These rules have exceptions below.
3530 */
3531
3532 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003533 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003534 msrs->pinbased_ctls_low,
3535 msrs->pinbased_ctls_high);
3536 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003537 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003538 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003539 PIN_BASED_EXT_INTR_MASK |
3540 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003541 PIN_BASED_VIRTUAL_NMIS |
3542 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003543 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003544 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003545 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003546
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003547 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003548 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003549 msrs->exit_ctls_low,
3550 msrs->exit_ctls_high);
3551 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003552 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003553
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003554 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003555#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003556 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003557#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01003558 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003559 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003560 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003561 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003562 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3563
Jan Kiszka2996fca2014-06-16 13:59:43 +02003564 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003565 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003566
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003567 /* entry controls */
3568 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003569 msrs->entry_ctls_low,
3570 msrs->entry_ctls_high);
3571 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003572 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003573 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003574#ifdef CONFIG_X86_64
3575 VM_ENTRY_IA32E_MODE |
3576#endif
3577 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003578 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003579 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Jan Kiszka57435342013-08-06 10:39:56 +02003580
Jan Kiszka2996fca2014-06-16 13:59:43 +02003581 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003582 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003583
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003584 /* cpu-based controls */
3585 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003586 msrs->procbased_ctls_low,
3587 msrs->procbased_ctls_high);
3588 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003589 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003590 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003591 CPU_BASED_VIRTUAL_INTR_PENDING |
3592 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003593 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3594 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3595 CPU_BASED_CR3_STORE_EXITING |
3596#ifdef CONFIG_X86_64
3597 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3598#endif
3599 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003600 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3601 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3602 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3603 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003604 /*
3605 * We can allow some features even when not supported by the
3606 * hardware. For example, L1 can specify an MSR bitmap - and we
3607 * can use it to avoid exits to L1 - even when L0 runs L2
3608 * without MSR bitmaps.
3609 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003610 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003611 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003612 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003613
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003614 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003615 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003616 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3617
Paolo Bonzini80154d72017-08-24 13:55:35 +02003618 /*
3619 * secondary cpu-based controls. Do not include those that
3620 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3621 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003622 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003623 msrs->secondary_ctls_low,
3624 msrs->secondary_ctls_high);
3625 msrs->secondary_ctls_low = 0;
3626 msrs->secondary_ctls_high &=
Paolo Bonzini1b073042016-10-25 16:06:30 +02003627 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003628 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003629 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003630 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003631 SECONDARY_EXEC_WBINVD_EXITING;
Paolo Bonzini2cf7ea92018-10-03 10:34:00 +02003632
Liran Alon32c7acf2018-06-23 02:35:11 +03003633 /*
3634 * We can emulate "VMCS shadowing," even if the hardware
3635 * doesn't support it.
3636 */
3637 msrs->secondary_ctls_high |=
3638 SECONDARY_EXEC_SHADOW_VMCS;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003639
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003640 if (enable_ept) {
3641 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003642 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003643 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003644 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003645 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003646 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003647 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003648 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003649 msrs->ept_caps &= vmx_capability.ept;
3650 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003651 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3652 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003653 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003654 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003655 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003656 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003657 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003658 }
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003659
Bandan Das27c42a12017-08-03 15:54:42 -04003660 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003661 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003662 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003663 /*
3664 * Advertise EPTP switching unconditionally
3665 * since we emulate it
3666 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003667 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003668 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003669 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003670 }
3671
Paolo Bonzinief697a72016-03-18 16:58:38 +01003672 /*
3673 * Old versions of KVM use the single-context version without
3674 * checking for support, so declare that it is supported even
3675 * though it is treated as global context. The alternative is
3676 * not failing the single-context invvpid, and it is worse.
3677 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003678 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003679 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003680 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003681 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003682 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003683 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003684
Radim Krčmář0790ec12015-03-17 14:02:32 +01003685 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003686 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003687 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3688
Paolo Bonzini2cf7ea92018-10-03 10:34:00 +02003689 if (flexpriority_enabled)
3690 msrs->secondary_ctls_high |=
3691 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
3692
Jan Kiszkac18911a2013-03-13 16:06:41 +01003693 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003694 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003695 msrs->misc_low,
3696 msrs->misc_high);
3697 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3698 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003699 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003700 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003701 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003702 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003703
3704 /*
3705 * This MSR reports some information about VMX support. We
3706 * should return information about the VMX we emulate for the
3707 * guest, and the VMCS structure we give it - not about the
3708 * VMX support of the underlying hardware.
3709 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003710 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003711 VMCS12_REVISION |
3712 VMX_BASIC_TRUE_CTLS |
3713 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3714 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3715
3716 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003717 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003718
3719 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003720 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003721 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3722 * We picked the standard core2 setting.
3723 */
3724#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3725#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003726 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3727 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003728
3729 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003730 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3731 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003732
3733 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003734 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003735}
3736
David Matlack38991522016-11-29 18:14:08 -08003737/*
3738 * if fixed0[i] == 1: val[i] must be 1
3739 * if fixed1[i] == 0: val[i] must be 0
3740 */
3741static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3742{
3743 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003744}
3745
3746static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3747{
David Matlack38991522016-11-29 18:14:08 -08003748 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003749}
3750
3751static inline u64 vmx_control_msr(u32 low, u32 high)
3752{
3753 return low | ((u64)high << 32);
3754}
3755
David Matlack62cc6b9d2016-11-29 18:14:07 -08003756static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3757{
3758 superset &= mask;
3759 subset &= mask;
3760
3761 return (superset | subset) == superset;
3762}
3763
3764static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3765{
3766 const u64 feature_and_reserved =
3767 /* feature (except bit 48; see below) */
3768 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3769 /* reserved */
3770 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003771 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003772
3773 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3774 return -EINVAL;
3775
3776 /*
3777 * KVM does not emulate a version of VMX that constrains physical
3778 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3779 */
3780 if (data & BIT_ULL(48))
3781 return -EINVAL;
3782
3783 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3784 vmx_basic_vmcs_revision_id(data))
3785 return -EINVAL;
3786
3787 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3788 return -EINVAL;
3789
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003790 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003791 return 0;
3792}
3793
3794static int
3795vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3796{
3797 u64 supported;
3798 u32 *lowp, *highp;
3799
3800 switch (msr_index) {
3801 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003802 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3803 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003804 break;
3805 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003806 lowp = &vmx->nested.msrs.procbased_ctls_low;
3807 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003808 break;
3809 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003810 lowp = &vmx->nested.msrs.exit_ctls_low;
3811 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003812 break;
3813 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003814 lowp = &vmx->nested.msrs.entry_ctls_low;
3815 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003816 break;
3817 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003818 lowp = &vmx->nested.msrs.secondary_ctls_low;
3819 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003820 break;
3821 default:
3822 BUG();
3823 }
3824
3825 supported = vmx_control_msr(*lowp, *highp);
3826
3827 /* Check must-be-1 bits are still 1. */
3828 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3829 return -EINVAL;
3830
3831 /* Check must-be-0 bits are still 0. */
3832 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3833 return -EINVAL;
3834
3835 *lowp = data;
3836 *highp = data >> 32;
3837 return 0;
3838}
3839
3840static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3841{
3842 const u64 feature_and_reserved_bits =
3843 /* feature */
3844 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3845 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3846 /* reserved */
3847 GENMASK_ULL(13, 9) | BIT_ULL(31);
3848 u64 vmx_misc;
3849
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003850 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3851 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003852
3853 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3854 return -EINVAL;
3855
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003856 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003857 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3858 vmx_misc_preemption_timer_rate(data) !=
3859 vmx_misc_preemption_timer_rate(vmx_misc))
3860 return -EINVAL;
3861
3862 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3863 return -EINVAL;
3864
3865 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3866 return -EINVAL;
3867
3868 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3869 return -EINVAL;
3870
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003871 vmx->nested.msrs.misc_low = data;
3872 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003873
3874 /*
3875 * If L1 has read-only VM-exit information fields, use the
3876 * less permissive vmx_vmwrite_bitmap to specify write
3877 * permissions for the shadow VMCS.
3878 */
3879 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3880 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3881
David Matlack62cc6b9d2016-11-29 18:14:07 -08003882 return 0;
3883}
3884
3885static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3886{
3887 u64 vmx_ept_vpid_cap;
3888
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003889 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3890 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003891
3892 /* Every bit is either reserved or a feature bit. */
3893 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3894 return -EINVAL;
3895
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003896 vmx->nested.msrs.ept_caps = data;
3897 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003898 return 0;
3899}
3900
3901static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3902{
3903 u64 *msr;
3904
3905 switch (msr_index) {
3906 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003907 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003908 break;
3909 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003910 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003911 break;
3912 default:
3913 BUG();
3914 }
3915
3916 /*
3917 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3918 * must be 1 in the restored value.
3919 */
3920 if (!is_bitwise_subset(data, *msr, -1ULL))
3921 return -EINVAL;
3922
3923 *msr = data;
3924 return 0;
3925}
3926
3927/*
3928 * Called when userspace is restoring VMX MSRs.
3929 *
3930 * Returns 0 on success, non-0 otherwise.
3931 */
3932static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3933{
3934 struct vcpu_vmx *vmx = to_vmx(vcpu);
3935
Jim Mattsona943ac52018-05-29 09:11:32 -07003936 /*
3937 * Don't allow changes to the VMX capability MSRs while the vCPU
3938 * is in VMX operation.
3939 */
3940 if (vmx->nested.vmxon)
3941 return -EBUSY;
3942
David Matlack62cc6b9d2016-11-29 18:14:07 -08003943 switch (msr_index) {
3944 case MSR_IA32_VMX_BASIC:
3945 return vmx_restore_vmx_basic(vmx, data);
3946 case MSR_IA32_VMX_PINBASED_CTLS:
3947 case MSR_IA32_VMX_PROCBASED_CTLS:
3948 case MSR_IA32_VMX_EXIT_CTLS:
3949 case MSR_IA32_VMX_ENTRY_CTLS:
3950 /*
3951 * The "non-true" VMX capability MSRs are generated from the
3952 * "true" MSRs, so we do not support restoring them directly.
3953 *
3954 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3955 * should restore the "true" MSRs with the must-be-1 bits
3956 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3957 * DEFAULT SETTINGS".
3958 */
3959 return -EINVAL;
3960 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3961 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3962 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3963 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3964 case MSR_IA32_VMX_PROCBASED_CTLS2:
3965 return vmx_restore_control_msr(vmx, msr_index, data);
3966 case MSR_IA32_VMX_MISC:
3967 return vmx_restore_vmx_misc(vmx, data);
3968 case MSR_IA32_VMX_CR0_FIXED0:
3969 case MSR_IA32_VMX_CR4_FIXED0:
3970 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3971 case MSR_IA32_VMX_CR0_FIXED1:
3972 case MSR_IA32_VMX_CR4_FIXED1:
3973 /*
3974 * These MSRs are generated based on the vCPU's CPUID, so we
3975 * do not support restoring them directly.
3976 */
3977 return -EINVAL;
3978 case MSR_IA32_VMX_EPT_VPID_CAP:
3979 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3980 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003981 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003982 return 0;
3983 default:
3984 /*
3985 * The rest of the VMX capability MSRs do not support restore.
3986 */
3987 return -EINVAL;
3988 }
3989}
3990
Jan Kiszkacae50132014-01-04 18:47:22 +01003991/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003992static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003993{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003994 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003995 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003996 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003997 break;
3998 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3999 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08004000 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004001 msrs->pinbased_ctls_low,
4002 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08004003 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
4004 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004005 break;
4006 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
4007 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08004008 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004009 msrs->procbased_ctls_low,
4010 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08004011 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
4012 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004013 break;
4014 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
4015 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08004016 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004017 msrs->exit_ctls_low,
4018 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08004019 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
4020 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004021 break;
4022 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
4023 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08004024 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004025 msrs->entry_ctls_low,
4026 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08004027 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
4028 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004029 break;
4030 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08004031 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004032 msrs->misc_low,
4033 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004034 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004035 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004036 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004037 break;
4038 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004039 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004040 break;
4041 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004042 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004043 break;
4044 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004045 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004046 break;
4047 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004048 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004049 break;
4050 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08004051 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004052 msrs->secondary_ctls_low,
4053 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004054 break;
4055 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004056 *pdata = msrs->ept_caps |
4057 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004058 break;
Bandan Das27c42a12017-08-03 15:54:42 -04004059 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004060 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04004061 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004062 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004063 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08004064 }
4065
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004066 return 0;
4067}
4068
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004069static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
4070 uint64_t val)
4071{
4072 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
4073
4074 return !(val & ~valid_bits);
4075}
4076
Tom Lendacky801e4592018-02-21 13:39:51 -06004077static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
4078{
Paolo Bonzini13893092018-02-26 13:40:09 +01004079 switch (msr->index) {
4080 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4081 if (!nested)
4082 return 1;
4083 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
4084 default:
4085 return 1;
4086 }
4087
4088 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06004089}
4090
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004091/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08004092 * Reads an msr value (of 'msr_index') into 'pdata'.
4093 * Returns 0 on success, non-0 otherwise.
4094 * Assumes vcpu_load() was already called.
4095 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004096static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004097{
Borislav Petkova6cb0992017-12-20 12:50:28 +01004098 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004099 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004100
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004101 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004102#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004103 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004104 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004105 break;
4106 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004107 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004108 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03004109 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07004110 msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
Avi Kivity44ea2b12009-09-06 15:55:37 +03004111 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03004112#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08004113 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004114 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004115 case MSR_IA32_SPEC_CTRL:
4116 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004117 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4118 return 1;
4119
4120 msr_info->data = to_vmx(vcpu)->spec_ctrl;
4121 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004122 case MSR_IA32_ARCH_CAPABILITIES:
4123 if (!msr_info->host_initiated &&
4124 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
4125 return 1;
4126 msr_info->data = to_vmx(vcpu)->arch_capabilities;
4127 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004128 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004129 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004130 break;
4131 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004132 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004133 break;
4134 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004135 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004136 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004137 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004138 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004139 (!msr_info->host_initiated &&
4140 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004141 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004142 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004143 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004144 case MSR_IA32_MCG_EXT_CTL:
4145 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01004146 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08004147 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01004148 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004149 msr_info->data = vcpu->arch.mcg_ext_ctl;
4150 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004151 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01004152 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01004153 break;
4154 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4155 if (!nested_vmx_allowed(vcpu))
4156 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004157 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
4158 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08004159 case MSR_IA32_XSS:
4160 if (!vmx_xsaves_supported())
4161 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004162 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08004163 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004164 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004165 if (!msr_info->host_initiated &&
4166 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004167 return 1;
4168 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004169 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01004170 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004171 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004172 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004173 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004174 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004175 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004176 }
4177
Avi Kivity6aa8b732006-12-10 02:21:36 -08004178 return 0;
4179}
4180
Jan Kiszkacae50132014-01-04 18:47:22 +01004181static void vmx_leave_nested(struct kvm_vcpu *vcpu);
4182
Avi Kivity6aa8b732006-12-10 02:21:36 -08004183/*
4184 * Writes msr value into into the appropriate "register".
4185 * Returns 0 on success, non-0 otherwise.
4186 * Assumes vcpu_load() was already called.
4187 */
Will Auld8fe8ab42012-11-29 12:42:12 -08004188static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004189{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004190 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004191 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03004192 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08004193 u32 msr_index = msr_info->index;
4194 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03004195
Avi Kivity6aa8b732006-12-10 02:21:36 -08004196 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08004197 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08004198 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03004199 break;
Avi Kivity16175a72009-03-23 22:13:44 +02004200#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004201 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004202 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004203 vmcs_writel(GUEST_FS_BASE, data);
4204 break;
4205 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004206 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004207 vmcs_writel(GUEST_GS_BASE, data);
4208 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03004209 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07004210 vmx_write_guest_kernel_gs_base(vmx, data);
Avi Kivity44ea2b12009-09-06 15:55:37 +03004211 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004212#endif
4213 case MSR_IA32_SYSENTER_CS:
4214 vmcs_write32(GUEST_SYSENTER_CS, data);
4215 break;
4216 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004217 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004218 break;
4219 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004220 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004221 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004222 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004223 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004224 (!msr_info->host_initiated &&
4225 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004226 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08004227 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07004228 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004229 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004230 vmcs_write64(GUEST_BNDCFGS, data);
4231 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004232 case MSR_IA32_SPEC_CTRL:
4233 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004234 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4235 return 1;
4236
4237 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02004238 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004239 return 1;
4240
4241 vmx->spec_ctrl = data;
4242
4243 if (!data)
4244 break;
4245
4246 /*
4247 * For non-nested:
4248 * When it's written (to non-zero) for the first time, pass
4249 * it through.
4250 *
4251 * For nested:
4252 * The handling of the MSR bitmap for L2 guests is done in
4253 * nested_vmx_merge_msr_bitmap. We should not touch the
4254 * vmcs02.msr_bitmap here since it gets completely overwritten
4255 * in the merging. We update the vmcs01 here for L1 as well
4256 * since it will end up touching the MSR anyway now.
4257 */
4258 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
4259 MSR_IA32_SPEC_CTRL,
4260 MSR_TYPE_RW);
4261 break;
Ashok Raj15d45072018-02-01 22:59:43 +01004262 case MSR_IA32_PRED_CMD:
4263 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01004264 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4265 return 1;
4266
4267 if (data & ~PRED_CMD_IBPB)
4268 return 1;
4269
4270 if (!data)
4271 break;
4272
4273 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4274
4275 /*
4276 * For non-nested:
4277 * When it's written (to non-zero) for the first time, pass
4278 * it through.
4279 *
4280 * For nested:
4281 * The handling of the MSR bitmap for L2 guests is done in
4282 * nested_vmx_merge_msr_bitmap. We should not touch the
4283 * vmcs02.msr_bitmap here since it gets completely overwritten
4284 * in the merging.
4285 */
4286 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
4287 MSR_TYPE_W);
4288 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004289 case MSR_IA32_ARCH_CAPABILITIES:
4290 if (!msr_info->host_initiated)
4291 return 1;
4292 vmx->arch_capabilities = data;
4293 break;
Sheng Yang468d4722008-10-09 16:01:55 +08004294 case MSR_IA32_CR_PAT:
4295 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03004296 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4297 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004298 vmcs_write64(GUEST_IA32_PAT, data);
4299 vcpu->arch.pat = data;
4300 break;
4301 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004302 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004303 break;
Will Auldba904632012-11-29 12:42:50 -08004304 case MSR_IA32_TSC_ADJUST:
4305 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004306 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004307 case MSR_IA32_MCG_EXT_CTL:
4308 if ((!msr_info->host_initiated &&
4309 !(to_vmx(vcpu)->msr_ia32_feature_control &
4310 FEATURE_CONTROL_LMCE)) ||
4311 (data & ~MCG_EXT_CTL_LMCE_EN))
4312 return 1;
4313 vcpu->arch.mcg_ext_ctl = data;
4314 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004315 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004316 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08004317 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01004318 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
4319 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08004320 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01004321 if (msr_info->host_initiated && data == 0)
4322 vmx_leave_nested(vcpu);
4323 break;
4324 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08004325 if (!msr_info->host_initiated)
4326 return 1; /* they are read-only */
4327 if (!nested_vmx_allowed(vcpu))
4328 return 1;
4329 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08004330 case MSR_IA32_XSS:
4331 if (!vmx_xsaves_supported())
4332 return 1;
4333 /*
4334 * The only supported bit as of Skylake is bit 8, but
4335 * it is not supported on KVM.
4336 */
4337 if (data != 0)
4338 return 1;
4339 vcpu->arch.ia32_xss = data;
4340 if (vcpu->arch.ia32_xss != host_xss)
4341 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04004342 vcpu->arch.ia32_xss, host_xss, false);
Wanpeng Li20300092014-12-02 19:14:59 +08004343 else
4344 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4345 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004346 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004347 if (!msr_info->host_initiated &&
4348 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004349 return 1;
4350 /* Check reserved bit, higher 32 bits should be zero */
4351 if ((data >> 32) != 0)
4352 return 1;
4353 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004354 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004355 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004356 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004357 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004358 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004359 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4360 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004361 ret = kvm_set_shared_msr(msr->index, msr->data,
4362 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004363 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004364 if (ret)
4365 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004366 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004367 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004368 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004369 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004370 }
4371
Eddie Dong2cc51562007-05-21 07:28:09 +03004372 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004373}
4374
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004375static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004376{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004377 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4378 switch (reg) {
4379 case VCPU_REGS_RSP:
4380 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4381 break;
4382 case VCPU_REGS_RIP:
4383 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4384 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004385 case VCPU_EXREG_PDPTR:
4386 if (enable_ept)
4387 ept_save_pdptrs(vcpu);
4388 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004389 default:
4390 break;
4391 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004392}
4393
Avi Kivity6aa8b732006-12-10 02:21:36 -08004394static __init int cpu_has_kvm_support(void)
4395{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004396 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004397}
4398
4399static __init int vmx_disabled_by_bios(void)
4400{
4401 u64 msr;
4402
4403 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004404 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004405 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004406 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4407 && tboot_enabled())
4408 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004409 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004410 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004411 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004412 && !tboot_enabled()) {
4413 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004414 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004415 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004416 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004417 /* launched w/o TXT and VMX disabled */
4418 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4419 && !tboot_enabled())
4420 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004421 }
4422
4423 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004424}
4425
Dongxiao Xu7725b892010-05-11 18:29:38 +08004426static void kvm_cpu_vmxon(u64 addr)
4427{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004428 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004429 intel_pt_handle_vmx(1);
4430
Uros Bizjak4b1e5472018-10-11 19:40:44 +02004431 asm volatile ("vmxon %0" : : "m"(addr));
Dongxiao Xu7725b892010-05-11 18:29:38 +08004432}
4433
Radim Krčmář13a34e02014-08-28 15:13:03 +02004434static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004435{
4436 int cpu = raw_smp_processor_id();
4437 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004438 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004439
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004440 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004441 return -EBUSY;
4442
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004443 /*
4444 * This can happen if we hot-added a CPU but failed to allocate
4445 * VP assist page for it.
4446 */
4447 if (static_branch_unlikely(&enable_evmcs) &&
4448 !hv_get_vp_assist_page(cpu))
4449 return -EFAULT;
4450
Nadav Har'Eld462b812011-05-24 15:26:10 +03004451 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004452 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4453 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004454
4455 /*
4456 * Now we can enable the vmclear operation in kdump
4457 * since the loaded_vmcss_on_cpu list on this cpu
4458 * has been initialized.
4459 *
4460 * Though the cpu is not in VMX operation now, there
4461 * is no problem to enable the vmclear operation
4462 * for the loaded_vmcss_on_cpu list is empty!
4463 */
4464 crash_enable_local_vmclear(cpu);
4465
Avi Kivity6aa8b732006-12-10 02:21:36 -08004466 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004467
4468 test_bits = FEATURE_CONTROL_LOCKED;
4469 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4470 if (tboot_enabled())
4471 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4472
4473 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004474 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004475 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4476 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004477 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004478 if (enable_ept)
4479 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004480
4481 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004482}
4483
Nadav Har'Eld462b812011-05-24 15:26:10 +03004484static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004485{
4486 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004487 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004488
Nadav Har'Eld462b812011-05-24 15:26:10 +03004489 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4490 loaded_vmcss_on_cpu_link)
4491 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004492}
4493
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004494
4495/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4496 * tricks.
4497 */
4498static void kvm_cpu_vmxoff(void)
4499{
Uros Bizjak4b1e5472018-10-11 19:40:44 +02004500 asm volatile (__ex("vmxoff"));
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004501
4502 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004503 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004504}
4505
Radim Krčmář13a34e02014-08-28 15:13:03 +02004506static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004507{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004508 vmclear_local_loaded_vmcss();
4509 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004510}
4511
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004512static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004513 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004514{
4515 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004516 u32 ctl = ctl_min | ctl_opt;
4517
4518 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4519
4520 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4521 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4522
4523 /* Ensure minimum (required) set of control bits are supported. */
4524 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004525 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004526
4527 *result = ctl;
4528 return 0;
4529}
4530
Avi Kivity110312c2010-12-21 12:54:20 +02004531static __init bool allow_1_setting(u32 msr, u32 ctl)
4532{
4533 u32 vmx_msr_low, vmx_msr_high;
4534
4535 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4536 return vmx_msr_high & ctl;
4537}
4538
Yang, Sheng002c7f72007-07-31 14:23:01 +03004539static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004540{
4541 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004542 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004543 u32 _pin_based_exec_control = 0;
4544 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004545 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004546 u32 _vmexit_control = 0;
4547 u32 _vmentry_control = 0;
4548
Paolo Bonzini13893092018-02-26 13:40:09 +01004549 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304550 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004551#ifdef CONFIG_X86_64
4552 CPU_BASED_CR8_LOAD_EXITING |
4553 CPU_BASED_CR8_STORE_EXITING |
4554#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004555 CPU_BASED_CR3_LOAD_EXITING |
4556 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e22017-12-12 16:44:21 +08004557 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004558 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004559 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004560 CPU_BASED_MWAIT_EXITING |
4561 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004562 CPU_BASED_INVLPG_EXITING |
4563 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004564
Sheng Yangf78e0e22007-10-29 09:40:42 +08004565 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004566 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004567 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004568 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4569 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004570 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004571#ifdef CONFIG_X86_64
4572 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4573 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4574 ~CPU_BASED_CR8_STORE_EXITING;
4575#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004576 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004577 min2 = 0;
4578 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004579 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004580 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004581 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004582 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004583 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004584 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004585 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004586 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004587 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004588 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004589 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004590 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004591 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004592 SECONDARY_EXEC_RDSEED_EXITING |
4593 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004594 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004595 SECONDARY_EXEC_TSC_SCALING |
Sean Christopherson0b665d32018-08-14 09:33:34 -07004596 SECONDARY_EXEC_ENABLE_VMFUNC |
4597 SECONDARY_EXEC_ENCLS_EXITING;
Sheng Yangd56f5462008-04-25 10:13:16 +08004598 if (adjust_vmx_controls(min2, opt2,
4599 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004600 &_cpu_based_2nd_exec_control) < 0)
4601 return -EIO;
4602 }
4603#ifndef CONFIG_X86_64
4604 if (!(_cpu_based_2nd_exec_control &
4605 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4606 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4607#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004608
4609 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4610 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004611 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004612 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4613 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004614
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004615 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4616 &vmx_capability.ept, &vmx_capability.vpid);
4617
Sheng Yangd56f5462008-04-25 10:13:16 +08004618 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004619 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4620 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004621 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4622 CPU_BASED_CR3_STORE_EXITING |
4623 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004624 } else if (vmx_capability.ept) {
4625 vmx_capability.ept = 0;
4626 pr_warn_once("EPT CAP should not exist if not support "
4627 "1-setting enable EPT VM-execution control\n");
4628 }
4629 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4630 vmx_capability.vpid) {
4631 vmx_capability.vpid = 0;
4632 pr_warn_once("VPID CAP should not exist if not support "
4633 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004634 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004635
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004636 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004637#ifdef CONFIG_X86_64
4638 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4639#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004640 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004641 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004642 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4643 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004644 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004645
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004646 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4647 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4648 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004649 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4650 &_pin_based_exec_control) < 0)
4651 return -EIO;
4652
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004653 if (cpu_has_broken_vmx_preemption_timer())
4654 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004655 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004656 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004657 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4658
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004659 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004660 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004661 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4662 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004663 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004664
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004665 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004666
4667 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4668 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004669 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004670
4671#ifdef CONFIG_X86_64
4672 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4673 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004674 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004675#endif
4676
4677 /* Require Write-Back (WB) memory type for VMCS accesses. */
4678 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004679 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004680
Yang, Sheng002c7f72007-07-31 14:23:01 +03004681 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004682 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004683 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004684
Liran Alon2307af12018-06-29 22:59:04 +03004685 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004686
Yang, Sheng002c7f72007-07-31 14:23:01 +03004687 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4688 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004689 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004690 vmcs_conf->vmexit_ctrl = _vmexit_control;
4691 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004692
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004693 if (static_branch_unlikely(&enable_evmcs))
4694 evmcs_sanitize_exec_ctrls(vmcs_conf);
4695
Avi Kivity110312c2010-12-21 12:54:20 +02004696 cpu_has_load_ia32_efer =
4697 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4698 VM_ENTRY_LOAD_IA32_EFER)
4699 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4700 VM_EXIT_LOAD_IA32_EFER);
4701
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004702 cpu_has_load_perf_global_ctrl =
4703 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4704 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4705 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4706 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4707
4708 /*
4709 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004710 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004711 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4712 *
4713 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4714 *
4715 * AAK155 (model 26)
4716 * AAP115 (model 30)
4717 * AAT100 (model 37)
4718 * BC86,AAY89,BD102 (model 44)
4719 * BA97 (model 46)
4720 *
4721 */
4722 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4723 switch (boot_cpu_data.x86_model) {
4724 case 26:
4725 case 30:
4726 case 37:
4727 case 44:
4728 case 46:
4729 cpu_has_load_perf_global_ctrl = false;
4730 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4731 "does not work properly. Using workaround\n");
4732 break;
4733 default:
4734 break;
4735 }
4736 }
4737
Borislav Petkov782511b2016-04-04 22:25:03 +02004738 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004739 rdmsrl(MSR_IA32_XSS, host_xss);
4740
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004741 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004742}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004743
Liran Alon491a6032018-06-23 02:35:12 +03004744static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004745{
4746 int node = cpu_to_node(cpu);
4747 struct page *pages;
4748 struct vmcs *vmcs;
4749
Vlastimil Babka96db8002015-09-08 15:03:50 -07004750 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004751 if (!pages)
4752 return NULL;
4753 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004754 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03004755
4756 /* KVM supports Enlightened VMCS v1 only */
4757 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004758 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03004759 else
Liran Alon392b2f22018-06-23 02:35:01 +03004760 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004761
Liran Alon491a6032018-06-23 02:35:12 +03004762 if (shadow)
4763 vmcs->hdr.shadow_vmcs = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004764 return vmcs;
4765}
4766
Avi Kivity6aa8b732006-12-10 02:21:36 -08004767static void free_vmcs(struct vmcs *vmcs)
4768{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004769 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004770}
4771
Nadav Har'Eld462b812011-05-24 15:26:10 +03004772/*
4773 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4774 */
4775static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4776{
4777 if (!loaded_vmcs->vmcs)
4778 return;
4779 loaded_vmcs_clear(loaded_vmcs);
4780 free_vmcs(loaded_vmcs->vmcs);
4781 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004782 if (loaded_vmcs->msr_bitmap)
4783 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004784 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004785}
4786
Liran Alon491a6032018-06-23 02:35:12 +03004787static struct vmcs *alloc_vmcs(bool shadow)
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004788{
Liran Alon491a6032018-06-23 02:35:12 +03004789 return alloc_vmcs_cpu(shadow, raw_smp_processor_id());
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004790}
4791
4792static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4793{
Liran Alon491a6032018-06-23 02:35:12 +03004794 loaded_vmcs->vmcs = alloc_vmcs(false);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004795 if (!loaded_vmcs->vmcs)
4796 return -ENOMEM;
4797
4798 loaded_vmcs->shadow_vmcs = NULL;
4799 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004800
4801 if (cpu_has_vmx_msr_bitmap()) {
4802 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4803 if (!loaded_vmcs->msr_bitmap)
4804 goto out_vmcs;
4805 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004806
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004807 if (IS_ENABLED(CONFIG_HYPERV) &&
4808 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004809 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4810 struct hv_enlightened_vmcs *evmcs =
4811 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4812
4813 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4814 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004815 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07004816
4817 memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
4818
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004819 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004820
4821out_vmcs:
4822 free_loaded_vmcs(loaded_vmcs);
4823 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004824}
4825
Sam Ravnborg39959582007-06-01 00:47:13 -07004826static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004827{
4828 int cpu;
4829
Zachary Amsden3230bb42009-09-29 11:38:37 -10004830 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004831 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004832 per_cpu(vmxarea, cpu) = NULL;
4833 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004834}
4835
Jim Mattsond37f4262017-12-22 12:12:16 -08004836enum vmcs_field_width {
4837 VMCS_FIELD_WIDTH_U16 = 0,
4838 VMCS_FIELD_WIDTH_U64 = 1,
4839 VMCS_FIELD_WIDTH_U32 = 2,
4840 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004841};
4842
Jim Mattsond37f4262017-12-22 12:12:16 -08004843static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004844{
4845 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004846 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004847 return (field >> 13) & 0x3 ;
4848}
4849
4850static inline int vmcs_field_readonly(unsigned long field)
4851{
4852 return (((field >> 10) & 0x3) == 1);
4853}
4854
Bandan Dasfe2b2012014-04-21 15:20:14 -04004855static void init_vmcs_shadow_fields(void)
4856{
4857 int i, j;
4858
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004859 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4860 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004861 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004862 (i + 1 == max_shadow_read_only_fields ||
4863 shadow_read_only_fields[i + 1] != field + 1))
4864 pr_err("Missing field from shadow_read_only_field %x\n",
4865 field + 1);
4866
4867 clear_bit(field, vmx_vmread_bitmap);
4868#ifdef CONFIG_X86_64
4869 if (field & 1)
4870 continue;
4871#endif
4872 if (j < i)
4873 shadow_read_only_fields[j] = field;
4874 j++;
4875 }
4876 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004877
4878 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004879 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004880 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004881 (i + 1 == max_shadow_read_write_fields ||
4882 shadow_read_write_fields[i + 1] != field + 1))
4883 pr_err("Missing field from shadow_read_write_field %x\n",
4884 field + 1);
4885
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004886 /*
4887 * PML and the preemption timer can be emulated, but the
4888 * processor cannot vmwrite to fields that don't exist
4889 * on bare metal.
4890 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004891 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004892 case GUEST_PML_INDEX:
4893 if (!cpu_has_vmx_pml())
4894 continue;
4895 break;
4896 case VMX_PREEMPTION_TIMER_VALUE:
4897 if (!cpu_has_vmx_preemption_timer())
4898 continue;
4899 break;
4900 case GUEST_INTR_STATUS:
4901 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004902 continue;
4903 break;
4904 default:
4905 break;
4906 }
4907
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004908 clear_bit(field, vmx_vmwrite_bitmap);
4909 clear_bit(field, vmx_vmread_bitmap);
4910#ifdef CONFIG_X86_64
4911 if (field & 1)
4912 continue;
4913#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004914 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004915 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004916 j++;
4917 }
4918 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004919}
4920
Avi Kivity6aa8b732006-12-10 02:21:36 -08004921static __init int alloc_kvm_area(void)
4922{
4923 int cpu;
4924
Zachary Amsden3230bb42009-09-29 11:38:37 -10004925 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004926 struct vmcs *vmcs;
4927
Liran Alon491a6032018-06-23 02:35:12 +03004928 vmcs = alloc_vmcs_cpu(false, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004929 if (!vmcs) {
4930 free_kvm_area();
4931 return -ENOMEM;
4932 }
4933
Liran Alon2307af12018-06-29 22:59:04 +03004934 /*
4935 * When eVMCS is enabled, alloc_vmcs_cpu() sets
4936 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
4937 * revision_id reported by MSR_IA32_VMX_BASIC.
4938 *
4939 * However, even though not explictly documented by
4940 * TLFS, VMXArea passed as VMXON argument should
4941 * still be marked with revision_id reported by
4942 * physical CPU.
4943 */
4944 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004945 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004946
Avi Kivity6aa8b732006-12-10 02:21:36 -08004947 per_cpu(vmxarea, cpu) = vmcs;
4948 }
4949 return 0;
4950}
4951
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004952static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004953 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004954{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004955 if (!emulate_invalid_guest_state) {
4956 /*
4957 * CS and SS RPL should be equal during guest entry according
4958 * to VMX spec, but in reality it is not always so. Since vcpu
4959 * is in the middle of the transition from real mode to
4960 * protected mode it is safe to assume that RPL 0 is a good
4961 * default value.
4962 */
4963 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004964 save->selector &= ~SEGMENT_RPL_MASK;
4965 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004966 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004967 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004968 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004969}
4970
4971static void enter_pmode(struct kvm_vcpu *vcpu)
4972{
4973 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004974 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004975
Gleb Natapovd99e4152012-12-20 16:57:45 +02004976 /*
4977 * Update real mode segment cache. It may be not up-to-date if sement
4978 * register was written while vcpu was in a guest mode.
4979 */
4980 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4981 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4982 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4983 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4984 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4985 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4986
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004987 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004988
Avi Kivity2fb92db2011-04-27 19:42:18 +03004989 vmx_segment_cache_clear(vmx);
4990
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004991 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004992
4993 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004994 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4995 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004996 vmcs_writel(GUEST_RFLAGS, flags);
4997
Rusty Russell66aee912007-07-17 23:34:16 +10004998 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4999 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005000
5001 update_exception_bitmap(vcpu);
5002
Gleb Natapov91b0aa22013-01-21 15:36:47 +02005003 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
5004 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
5005 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
5006 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
5007 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
5008 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005009}
5010
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005011static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005012{
Mathias Krause772e0312012-08-30 01:30:19 +02005013 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02005014 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005015
Gleb Natapovd99e4152012-12-20 16:57:45 +02005016 var.dpl = 0x3;
5017 if (seg == VCPU_SREG_CS)
5018 var.type = 0x3;
5019
5020 if (!emulate_invalid_guest_state) {
5021 var.selector = var.base >> 4;
5022 var.base = var.base & 0xffff0;
5023 var.limit = 0xffff;
5024 var.g = 0;
5025 var.db = 0;
5026 var.present = 1;
5027 var.s = 1;
5028 var.l = 0;
5029 var.unusable = 0;
5030 var.type = 0x3;
5031 var.avl = 0;
5032 if (save->base & 0xf)
5033 printk_once(KERN_WARNING "kvm: segment base is not "
5034 "paragraph aligned when entering "
5035 "protected mode (seg=%d)", seg);
5036 }
5037
5038 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05005039 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005040 vmcs_write32(sf->limit, var.limit);
5041 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005042}
5043
5044static void enter_rmode(struct kvm_vcpu *vcpu)
5045{
5046 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03005047 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005048 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005049
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005050 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
5051 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
5052 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
5053 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
5054 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005055 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
5056 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005057
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005058 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005059
Gleb Natapov776e58e2011-03-13 12:34:27 +02005060 /*
5061 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01005062 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02005063 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005064 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02005065 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
5066 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02005067
Avi Kivity2fb92db2011-04-27 19:42:18 +03005068 vmx_segment_cache_clear(vmx);
5069
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005070 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005071 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005072 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5073
5074 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03005075 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005076
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01005077 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005078
5079 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10005080 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005081 update_exception_bitmap(vcpu);
5082
Gleb Natapovd99e4152012-12-20 16:57:45 +02005083 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
5084 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
5085 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
5086 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
5087 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
5088 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03005089
Eddie Dong8668a3c2007-10-10 14:26:45 +08005090 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005091}
5092
Amit Shah401d10d2009-02-20 22:53:37 +05305093static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
5094{
5095 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03005096 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
5097
5098 if (!msr)
5099 return;
Amit Shah401d10d2009-02-20 22:53:37 +05305100
Avi Kivityf6801df2010-01-21 15:31:50 +02005101 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05305102 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02005103 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05305104 msr->data = efer;
5105 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02005106 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05305107
5108 msr->data = efer & ~EFER_LME;
5109 }
5110 setup_msrs(vmx);
5111}
5112
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005113#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005114
5115static void enter_lmode(struct kvm_vcpu *vcpu)
5116{
5117 u32 guest_tr_ar;
5118
Avi Kivity2fb92db2011-04-27 19:42:18 +03005119 vmx_segment_cache_clear(to_vmx(vcpu));
5120
Avi Kivity6aa8b732006-12-10 02:21:36 -08005121 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005122 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02005123 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
5124 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005125 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005126 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
5127 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005128 }
Avi Kivityda38f432010-07-06 11:30:49 +03005129 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005130}
5131
5132static void exit_lmode(struct kvm_vcpu *vcpu)
5133{
Gleb Natapov2961e8762013-11-25 15:37:13 +02005134 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03005135 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005136}
5137
5138#endif
5139
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005140static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
5141 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005142{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005143 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005144 if (!VALID_PAGE(vcpu->arch.mmu->root_hpa))
Xiao Guangrongdd180b32010-07-03 16:02:42 +08005145 return;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005146 ept_sync_context(construct_eptp(vcpu,
5147 vcpu->arch.mmu->root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07005148 } else {
5149 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08005150 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08005151}
5152
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005153static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005154{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005155 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005156}
5157
Junaid Shahidfaff8752018-06-29 13:10:05 -07005158static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
5159{
5160 int vpid = to_vmx(vcpu)->vpid;
5161
5162 if (!vpid_sync_vcpu_addr(vpid, addr))
5163 vpid_sync_context(vpid);
5164
5165 /*
5166 * If VPIDs are not supported or enabled, then the above is a no-op.
5167 * But we don't really need a TLB flush in that case anyway, because
5168 * each VM entry/exit includes an implicit flush when VPID is 0.
5169 */
5170}
5171
Avi Kivitye8467fd2009-12-29 18:43:06 +02005172static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
5173{
5174 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
5175
5176 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
5177 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
5178}
5179
Avi Kivityaff48ba2010-12-05 18:56:11 +02005180static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
5181{
Sean Christophersonb4d18512018-03-05 12:04:40 -08005182 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02005183 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
5184 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
5185}
5186
Anthony Liguori25c4c272007-04-27 09:29:21 +03005187static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08005188{
Avi Kivityfc78f512009-12-07 12:16:48 +02005189 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
5190
5191 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
5192 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08005193}
5194
Sheng Yang14394422008-04-28 12:24:45 +08005195static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
5196{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005197 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5198
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005199 if (!test_bit(VCPU_EXREG_PDPTR,
5200 (unsigned long *)&vcpu->arch.regs_dirty))
5201 return;
5202
Sheng Yang14394422008-04-28 12:24:45 +08005203 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005204 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
5205 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
5206 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
5207 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08005208 }
5209}
5210
Avi Kivity8f5d5492009-05-31 18:41:29 +03005211static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
5212{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005213 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5214
Avi Kivity8f5d5492009-05-31 18:41:29 +03005215 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005216 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
5217 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
5218 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
5219 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005220 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005221
5222 __set_bit(VCPU_EXREG_PDPTR,
5223 (unsigned long *)&vcpu->arch.regs_avail);
5224 __set_bit(VCPU_EXREG_PDPTR,
5225 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005226}
5227
David Matlack38991522016-11-29 18:14:08 -08005228static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5229{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005230 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5231 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005232 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5233
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005234 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08005235 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5236 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5237 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
5238
5239 return fixed_bits_valid(val, fixed0, fixed1);
5240}
5241
5242static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5243{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005244 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5245 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005246
5247 return fixed_bits_valid(val, fixed0, fixed1);
5248}
5249
5250static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
5251{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005252 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
5253 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005254
5255 return fixed_bits_valid(val, fixed0, fixed1);
5256}
5257
5258/* No difference in the restrictions on guest and host CR4 in VMX operation. */
5259#define nested_guest_cr4_valid nested_cr4_valid
5260#define nested_host_cr4_valid nested_cr4_valid
5261
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005262static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08005263
5264static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
5265 unsigned long cr0,
5266 struct kvm_vcpu *vcpu)
5267{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03005268 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
5269 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005270 if (!(cr0 & X86_CR0_PG)) {
5271 /* From paging/starting to nonpaging */
5272 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005273 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08005274 (CPU_BASED_CR3_LOAD_EXITING |
5275 CPU_BASED_CR3_STORE_EXITING));
5276 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005277 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005278 } else if (!is_paging(vcpu)) {
5279 /* From nonpaging to paging */
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 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08005287
5288 if (!(cr0 & X86_CR0_WP))
5289 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08005290}
5291
Avi Kivity6aa8b732006-12-10 02:21:36 -08005292static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
5293{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005294 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005295 unsigned long hw_cr0;
5296
Sean Christopherson3de63472018-07-13 08:42:30 -07005297 hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005298 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02005299 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02005300 else {
Gleb Natapov50378782013-02-04 16:00:28 +02005301 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005302
Gleb Natapov218e7632013-01-21 15:36:45 +02005303 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
5304 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005305
Gleb Natapov218e7632013-01-21 15:36:45 +02005306 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
5307 enter_rmode(vcpu);
5308 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005309
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005310#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02005311 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10005312 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005313 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10005314 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005315 exit_lmode(vcpu);
5316 }
5317#endif
5318
Sean Christophersonb4d18512018-03-05 12:04:40 -08005319 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08005320 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
5321
Avi Kivity6aa8b732006-12-10 02:21:36 -08005322 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08005323 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005324 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02005325
5326 /* depends on vcpu->arch.cr0 to be set to a new value */
5327 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005328}
5329
Yu Zhang855feb62017-08-24 20:27:55 +08005330static int get_ept_level(struct kvm_vcpu *vcpu)
5331{
5332 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
5333 return 5;
5334 return 4;
5335}
5336
Peter Feiner995f00a2017-06-30 17:26:32 -07005337static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08005338{
Yu Zhang855feb62017-08-24 20:27:55 +08005339 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08005340
Yu Zhang855feb62017-08-24 20:27:55 +08005341 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08005342
Peter Feiner995f00a2017-06-30 17:26:32 -07005343 if (enable_ept_ad_bits &&
5344 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02005345 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08005346 eptp |= (root_hpa & PAGE_MASK);
5347
5348 return eptp;
5349}
5350
Avi Kivity6aa8b732006-12-10 02:21:36 -08005351static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5352{
Tianyu Lan877ad952018-07-19 08:40:23 +00005353 struct kvm *kvm = vcpu->kvm;
Sheng Yang14394422008-04-28 12:24:45 +08005354 unsigned long guest_cr3;
5355 u64 eptp;
5356
5357 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02005358 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07005359 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08005360 vmcs_write64(EPT_POINTER, eptp);
Tianyu Lan877ad952018-07-19 08:40:23 +00005361
5362 if (kvm_x86_ops->tlb_remote_flush) {
5363 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5364 to_vmx(vcpu)->ept_pointer = eptp;
5365 to_kvm_vmx(kvm)->ept_pointers_match
5366 = EPT_POINTERS_CHECK;
5367 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5368 }
5369
Sean Christophersone90008d2018-03-05 12:04:37 -08005370 if (enable_unrestricted_guest || is_paging(vcpu) ||
5371 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02005372 guest_cr3 = kvm_read_cr3(vcpu);
5373 else
Tianyu Lan877ad952018-07-19 08:40:23 +00005374 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005375 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005376 }
5377
Sheng Yang14394422008-04-28 12:24:45 +08005378 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005379}
5380
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005381static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005382{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005383 /*
5384 * Pass through host's Machine Check Enable value to hw_cr4, which
5385 * is in force while we are in guest mode. Do not let guests control
5386 * this bit, even if host CR4.MCE == 0.
5387 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005388 unsigned long hw_cr4;
5389
5390 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5391 if (enable_unrestricted_guest)
5392 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5393 else if (to_vmx(vcpu)->rmode.vm86_active)
5394 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5395 else
5396 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005397
Sean Christopherson64f7a112018-04-30 10:01:06 -07005398 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5399 if (cr4 & X86_CR4_UMIP) {
5400 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005401 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005402 hw_cr4 &= ~X86_CR4_UMIP;
5403 } else if (!is_guest_mode(vcpu) ||
5404 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5405 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5406 SECONDARY_EXEC_DESC);
5407 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005408
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005409 if (cr4 & X86_CR4_VMXE) {
5410 /*
5411 * To use VMXON (and later other VMX instructions), a guest
5412 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5413 * So basically the check on whether to allow nested VMX
Paolo Bonzini5bea5122018-09-18 15:19:17 +02005414 * is here. We operate under the default treatment of SMM,
5415 * so VMX cannot be enabled under SMM.
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005416 */
Paolo Bonzini5bea5122018-09-18 15:19:17 +02005417 if (!nested_vmx_allowed(vcpu) || is_smm(vcpu))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005418 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005419 }
David Matlack38991522016-11-29 18:14:08 -08005420
5421 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005422 return 1;
5423
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005424 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005425
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005426 if (!enable_unrestricted_guest) {
5427 if (enable_ept) {
5428 if (!is_paging(vcpu)) {
5429 hw_cr4 &= ~X86_CR4_PAE;
5430 hw_cr4 |= X86_CR4_PSE;
5431 } else if (!(cr4 & X86_CR4_PAE)) {
5432 hw_cr4 &= ~X86_CR4_PAE;
5433 }
5434 }
5435
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005436 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005437 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5438 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5439 * to be manually disabled when guest switches to non-paging
5440 * mode.
5441 *
5442 * If !enable_unrestricted_guest, the CPU is always running
5443 * with CR0.PG=1 and CR4 needs to be modified.
5444 * If enable_unrestricted_guest, the CPU automatically
5445 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005446 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005447 if (!is_paging(vcpu))
5448 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5449 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005450
Sheng Yang14394422008-04-28 12:24:45 +08005451 vmcs_writel(CR4_READ_SHADOW, cr4);
5452 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005453 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005454}
5455
Avi Kivity6aa8b732006-12-10 02:21:36 -08005456static void vmx_get_segment(struct kvm_vcpu *vcpu,
5457 struct kvm_segment *var, int seg)
5458{
Avi Kivitya9179492011-01-03 14:28:52 +02005459 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005460 u32 ar;
5461
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005462 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005463 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005464 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005465 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005466 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005467 var->base = vmx_read_guest_seg_base(vmx, seg);
5468 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5469 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005470 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005471 var->base = vmx_read_guest_seg_base(vmx, seg);
5472 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5473 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5474 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005475 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005476 var->type = ar & 15;
5477 var->s = (ar >> 4) & 1;
5478 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005479 /*
5480 * Some userspaces do not preserve unusable property. Since usable
5481 * segment has to be present according to VMX spec we can use present
5482 * property to amend userspace bug by making unusable segment always
5483 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5484 * segment as unusable.
5485 */
5486 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005487 var->avl = (ar >> 12) & 1;
5488 var->l = (ar >> 13) & 1;
5489 var->db = (ar >> 14) & 1;
5490 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005491}
5492
Avi Kivitya9179492011-01-03 14:28:52 +02005493static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5494{
Avi Kivitya9179492011-01-03 14:28:52 +02005495 struct kvm_segment s;
5496
5497 if (to_vmx(vcpu)->rmode.vm86_active) {
5498 vmx_get_segment(vcpu, &s, seg);
5499 return s.base;
5500 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005501 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005502}
5503
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005504static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005505{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005506 struct vcpu_vmx *vmx = to_vmx(vcpu);
5507
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005508 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005509 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005510 else {
5511 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005512 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005513 }
Avi Kivity69c73022011-03-07 15:26:44 +02005514}
5515
Avi Kivity653e3102007-05-07 10:55:37 +03005516static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005517{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005518 u32 ar;
5519
Avi Kivityf0495f92012-06-07 17:06:10 +03005520 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005521 ar = 1 << 16;
5522 else {
5523 ar = var->type & 15;
5524 ar |= (var->s & 1) << 4;
5525 ar |= (var->dpl & 3) << 5;
5526 ar |= (var->present & 1) << 7;
5527 ar |= (var->avl & 1) << 12;
5528 ar |= (var->l & 1) << 13;
5529 ar |= (var->db & 1) << 14;
5530 ar |= (var->g & 1) << 15;
5531 }
Avi Kivity653e3102007-05-07 10:55:37 +03005532
5533 return ar;
5534}
5535
5536static void vmx_set_segment(struct kvm_vcpu *vcpu,
5537 struct kvm_segment *var, int seg)
5538{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005539 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005540 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005541
Avi Kivity2fb92db2011-04-27 19:42:18 +03005542 vmx_segment_cache_clear(vmx);
5543
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005544 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5545 vmx->rmode.segs[seg] = *var;
5546 if (seg == VCPU_SREG_TR)
5547 vmcs_write16(sf->selector, var->selector);
5548 else if (var->s)
5549 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005550 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005551 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005552
Avi Kivity653e3102007-05-07 10:55:37 +03005553 vmcs_writel(sf->base, var->base);
5554 vmcs_write32(sf->limit, var->limit);
5555 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005556
5557 /*
5558 * Fix the "Accessed" bit in AR field of segment registers for older
5559 * qemu binaries.
5560 * IA32 arch specifies that at the time of processor reset the
5561 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005562 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005563 * state vmexit when "unrestricted guest" mode is turned on.
5564 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5565 * tree. Newer qemu binaries with that qemu fix would not need this
5566 * kvm hack.
5567 */
5568 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005569 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005570
Gleb Natapovf924d662012-12-12 19:10:55 +02005571 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005572
5573out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005574 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005575}
5576
Avi Kivity6aa8b732006-12-10 02:21:36 -08005577static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5578{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005579 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005580
5581 *db = (ar >> 14) & 1;
5582 *l = (ar >> 13) & 1;
5583}
5584
Gleb Natapov89a27f42010-02-16 10:51:48 +02005585static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005586{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005587 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5588 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005589}
5590
Gleb Natapov89a27f42010-02-16 10:51:48 +02005591static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005592{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005593 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5594 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005595}
5596
Gleb Natapov89a27f42010-02-16 10:51:48 +02005597static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005598{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005599 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5600 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005601}
5602
Gleb Natapov89a27f42010-02-16 10:51:48 +02005603static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005604{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005605 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5606 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005607}
5608
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005609static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5610{
5611 struct kvm_segment var;
5612 u32 ar;
5613
5614 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005615 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005616 if (seg == VCPU_SREG_CS)
5617 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005618 ar = vmx_segment_access_rights(&var);
5619
5620 if (var.base != (var.selector << 4))
5621 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005622 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005623 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005624 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005625 return false;
5626
5627 return true;
5628}
5629
5630static bool code_segment_valid(struct kvm_vcpu *vcpu)
5631{
5632 struct kvm_segment cs;
5633 unsigned int cs_rpl;
5634
5635 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005636 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005637
Avi Kivity1872a3f2009-01-04 23:26:52 +02005638 if (cs.unusable)
5639 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005640 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005641 return false;
5642 if (!cs.s)
5643 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005644 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005645 if (cs.dpl > cs_rpl)
5646 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005647 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005648 if (cs.dpl != cs_rpl)
5649 return false;
5650 }
5651 if (!cs.present)
5652 return false;
5653
5654 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5655 return true;
5656}
5657
5658static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5659{
5660 struct kvm_segment ss;
5661 unsigned int ss_rpl;
5662
5663 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005664 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005665
Avi Kivity1872a3f2009-01-04 23:26:52 +02005666 if (ss.unusable)
5667 return true;
5668 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005669 return false;
5670 if (!ss.s)
5671 return false;
5672 if (ss.dpl != ss_rpl) /* DPL != RPL */
5673 return false;
5674 if (!ss.present)
5675 return false;
5676
5677 return true;
5678}
5679
5680static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5681{
5682 struct kvm_segment var;
5683 unsigned int rpl;
5684
5685 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005686 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005687
Avi Kivity1872a3f2009-01-04 23:26:52 +02005688 if (var.unusable)
5689 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005690 if (!var.s)
5691 return false;
5692 if (!var.present)
5693 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005694 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005695 if (var.dpl < rpl) /* DPL < RPL */
5696 return false;
5697 }
5698
5699 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5700 * rights flags
5701 */
5702 return true;
5703}
5704
5705static bool tr_valid(struct kvm_vcpu *vcpu)
5706{
5707 struct kvm_segment tr;
5708
5709 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5710
Avi Kivity1872a3f2009-01-04 23:26:52 +02005711 if (tr.unusable)
5712 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005713 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005714 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005715 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005716 return false;
5717 if (!tr.present)
5718 return false;
5719
5720 return true;
5721}
5722
5723static bool ldtr_valid(struct kvm_vcpu *vcpu)
5724{
5725 struct kvm_segment ldtr;
5726
5727 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5728
Avi Kivity1872a3f2009-01-04 23:26:52 +02005729 if (ldtr.unusable)
5730 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005731 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005732 return false;
5733 if (ldtr.type != 2)
5734 return false;
5735 if (!ldtr.present)
5736 return false;
5737
5738 return true;
5739}
5740
5741static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5742{
5743 struct kvm_segment cs, ss;
5744
5745 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5746 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5747
Nadav Amitb32a9912015-03-29 16:33:04 +03005748 return ((cs.selector & SEGMENT_RPL_MASK) ==
5749 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005750}
5751
5752/*
5753 * Check if guest state is valid. Returns true if valid, false if
5754 * not.
5755 * We assume that registers are always usable
5756 */
5757static bool guest_state_valid(struct kvm_vcpu *vcpu)
5758{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005759 if (enable_unrestricted_guest)
5760 return true;
5761
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005762 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005763 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005764 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5765 return false;
5766 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5767 return false;
5768 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5769 return false;
5770 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5771 return false;
5772 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5773 return false;
5774 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5775 return false;
5776 } else {
5777 /* protected mode guest state checks */
5778 if (!cs_ss_rpl_check(vcpu))
5779 return false;
5780 if (!code_segment_valid(vcpu))
5781 return false;
5782 if (!stack_segment_valid(vcpu))
5783 return false;
5784 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5785 return false;
5786 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5787 return false;
5788 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5789 return false;
5790 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5791 return false;
5792 if (!tr_valid(vcpu))
5793 return false;
5794 if (!ldtr_valid(vcpu))
5795 return false;
5796 }
5797 /* TODO:
5798 * - Add checks on RIP
5799 * - Add checks on RFLAGS
5800 */
5801
5802 return true;
5803}
5804
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005805static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5806{
5807 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5808}
5809
Mike Dayd77c26f2007-10-08 09:02:08 -04005810static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005811{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005812 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005813 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005814 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005815
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005816 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005817 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005818 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5819 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005820 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005821 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005822 r = kvm_write_guest_page(kvm, fn++, &data,
5823 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005824 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005825 goto out;
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 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5830 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005831 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005832 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005833 r = kvm_write_guest_page(kvm, fn, &data,
5834 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5835 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005836out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005837 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005838 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005839}
5840
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005841static int init_rmode_identity_map(struct kvm *kvm)
5842{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005843 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005844 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005845 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005846 u32 tmp;
5847
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005848 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005849 mutex_lock(&kvm->slots_lock);
5850
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005851 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005852 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005853
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005854 if (!kvm_vmx->ept_identity_map_addr)
5855 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5856 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005857
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005858 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005859 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005860 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005861 goto out2;
5862
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005863 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005864 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5865 if (r < 0)
5866 goto out;
5867 /* Set up identity-mapping pagetable for EPT in real mode */
5868 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5869 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5870 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5871 r = kvm_write_guest_page(kvm, identity_map_pfn,
5872 &tmp, i * sizeof(tmp), sizeof(tmp));
5873 if (r < 0)
5874 goto out;
5875 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005876 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005877
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005878out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005879 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005880
5881out2:
5882 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005883 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005884}
5885
Avi Kivity6aa8b732006-12-10 02:21:36 -08005886static void seg_setup(int seg)
5887{
Mathias Krause772e0312012-08-30 01:30:19 +02005888 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005889 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005890
5891 vmcs_write16(sf->selector, 0);
5892 vmcs_writel(sf->base, 0);
5893 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005894 ar = 0x93;
5895 if (seg == VCPU_SREG_CS)
5896 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005897
5898 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005899}
5900
Sheng Yangf78e0e22007-10-29 09:40:42 +08005901static int alloc_apic_access_page(struct kvm *kvm)
5902{
Xiao Guangrong44841412012-09-07 14:14:20 +08005903 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005904 int r = 0;
5905
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005906 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005907 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005908 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005909 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5910 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005911 if (r)
5912 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005913
Tang Chen73a6d942014-09-11 13:38:00 +08005914 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005915 if (is_error_page(page)) {
5916 r = -EFAULT;
5917 goto out;
5918 }
5919
Tang Chenc24ae0d2014-09-24 15:57:58 +08005920 /*
5921 * Do not pin the page in memory, so that memory hot-unplug
5922 * is able to migrate it.
5923 */
5924 put_page(page);
5925 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005926out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005927 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005928 return r;
5929}
5930
Wanpeng Li991e7a02015-09-16 17:30:05 +08005931static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005932{
5933 int vpid;
5934
Avi Kivity919818a2009-03-23 18:01:29 +02005935 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005936 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005937 spin_lock(&vmx_vpid_lock);
5938 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005939 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005940 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005941 else
5942 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005943 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005944 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005945}
5946
Wanpeng Li991e7a02015-09-16 17:30:05 +08005947static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005948{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005949 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005950 return;
5951 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005952 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005953 spin_unlock(&vmx_vpid_lock);
5954}
5955
Yi Wang1e4329ee2018-11-08 11:22:21 +08005956static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005957 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005958{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005959 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005960
5961 if (!cpu_has_vmx_msr_bitmap())
5962 return;
5963
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005964 if (static_branch_unlikely(&enable_evmcs))
5965 evmcs_touch_msr_bitmap();
5966
Sheng Yang25c5f222008-03-28 13:18:56 +08005967 /*
5968 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5969 * have the write-low and read-high bitmap offsets the wrong way round.
5970 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5971 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005972 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005973 if (type & MSR_TYPE_R)
5974 /* read-low */
5975 __clear_bit(msr, msr_bitmap + 0x000 / f);
5976
5977 if (type & MSR_TYPE_W)
5978 /* write-low */
5979 __clear_bit(msr, msr_bitmap + 0x800 / f);
5980
Sheng Yang25c5f222008-03-28 13:18:56 +08005981 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5982 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005983 if (type & MSR_TYPE_R)
5984 /* read-high */
5985 __clear_bit(msr, msr_bitmap + 0x400 / f);
5986
5987 if (type & MSR_TYPE_W)
5988 /* write-high */
5989 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5990
5991 }
5992}
5993
Yi Wang1e4329ee2018-11-08 11:22:21 +08005994static __always_inline void vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005995 u32 msr, int type)
5996{
5997 int f = sizeof(unsigned long);
5998
5999 if (!cpu_has_vmx_msr_bitmap())
6000 return;
6001
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02006002 if (static_branch_unlikely(&enable_evmcs))
6003 evmcs_touch_msr_bitmap();
6004
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006005 /*
6006 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
6007 * have the write-low and read-high bitmap offsets the wrong way round.
6008 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
6009 */
6010 if (msr <= 0x1fff) {
6011 if (type & MSR_TYPE_R)
6012 /* read-low */
6013 __set_bit(msr, msr_bitmap + 0x000 / f);
6014
6015 if (type & MSR_TYPE_W)
6016 /* write-low */
6017 __set_bit(msr, msr_bitmap + 0x800 / f);
6018
6019 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
6020 msr &= 0x1fff;
6021 if (type & MSR_TYPE_R)
6022 /* read-high */
6023 __set_bit(msr, msr_bitmap + 0x400 / f);
6024
6025 if (type & MSR_TYPE_W)
6026 /* write-high */
6027 __set_bit(msr, msr_bitmap + 0xc00 / f);
6028
6029 }
6030}
6031
Yi Wang1e4329ee2018-11-08 11:22:21 +08006032static __always_inline void vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006033 u32 msr, int type, bool value)
6034{
6035 if (value)
6036 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
6037 else
6038 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
6039}
6040
Wincy Vanf2b93282015-02-03 23:56:03 +08006041/*
6042 * If a msr is allowed by L0, we should check whether it is allowed by L1.
6043 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
6044 */
6045static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
6046 unsigned long *msr_bitmap_nested,
6047 u32 msr, int type)
6048{
6049 int f = sizeof(unsigned long);
6050
Wincy Vanf2b93282015-02-03 23:56:03 +08006051 /*
6052 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
6053 * have the write-low and read-high bitmap offsets the wrong way round.
6054 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
6055 */
6056 if (msr <= 0x1fff) {
6057 if (type & MSR_TYPE_R &&
6058 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
6059 /* read-low */
6060 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
6061
6062 if (type & MSR_TYPE_W &&
6063 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
6064 /* write-low */
6065 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
6066
6067 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
6068 msr &= 0x1fff;
6069 if (type & MSR_TYPE_R &&
6070 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
6071 /* read-high */
6072 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
6073
6074 if (type & MSR_TYPE_W &&
6075 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
6076 /* write-high */
6077 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
6078
6079 }
6080}
6081
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006082static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02006083{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006084 u8 mode = 0;
6085
6086 if (cpu_has_secondary_exec_ctrls() &&
6087 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
6088 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
6089 mode |= MSR_BITMAP_MODE_X2APIC;
6090 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
6091 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
6092 }
6093
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006094 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08006095}
6096
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006097#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
6098
6099static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
6100 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08006101{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006102 int msr;
6103
6104 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
6105 unsigned word = msr / BITS_PER_LONG;
6106 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
6107 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006108 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006109
6110 if (mode & MSR_BITMAP_MODE_X2APIC) {
6111 /*
6112 * TPR reads and writes can be virtualized even if virtual interrupt
6113 * delivery is not in use.
6114 */
6115 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
6116 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
6117 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
6118 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
6119 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
6120 }
6121 }
6122}
6123
6124static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
6125{
6126 struct vcpu_vmx *vmx = to_vmx(vcpu);
6127 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
6128 u8 mode = vmx_msr_bitmap_mode(vcpu);
6129 u8 changed = mode ^ vmx->msr_bitmap_mode;
6130
6131 if (!changed)
6132 return;
6133
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006134 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
6135 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
6136
6137 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02006138}
6139
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05006140static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02006141{
Andrey Smetanind62caab2015-11-10 15:36:33 +03006142 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02006143}
6144
David Matlackc9f04402017-08-01 14:00:40 -07006145static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
6146{
6147 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6148 gfn_t gfn;
6149
6150 /*
6151 * Don't need to mark the APIC access page dirty; it is never
6152 * written to by the CPU during APIC virtualization.
6153 */
6154
6155 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
6156 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
6157 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6158 }
6159
6160 if (nested_cpu_has_posted_intr(vmcs12)) {
6161 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
6162 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6163 }
6164}
6165
6166
David Hildenbrand6342c502017-01-25 11:58:58 +01006167static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08006168{
6169 struct vcpu_vmx *vmx = to_vmx(vcpu);
6170 int max_irr;
6171 void *vapic_page;
6172 u16 status;
6173
David Matlackc9f04402017-08-01 14:00:40 -07006174 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
6175 return;
Wincy Van705699a2015-02-03 23:58:17 +08006176
David Matlackc9f04402017-08-01 14:00:40 -07006177 vmx->nested.pi_pending = false;
6178 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
6179 return;
Wincy Van705699a2015-02-03 23:58:17 +08006180
David Matlackc9f04402017-08-01 14:00:40 -07006181 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
6182 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08006183 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02006184 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
6185 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08006186 kunmap(vmx->nested.virtual_apic_page);
6187
6188 status = vmcs_read16(GUEST_INTR_STATUS);
6189 if ((u8)max_irr > ((u8)status & 0xff)) {
6190 status &= ~0xff;
6191 status |= (u8)max_irr;
6192 vmcs_write16(GUEST_INTR_STATUS, status);
6193 }
6194 }
David Matlackc9f04402017-08-01 14:00:40 -07006195
6196 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006197}
6198
Paolo Bonzini7e712682018-10-03 13:44:26 +02006199static u8 vmx_get_rvi(void)
6200{
6201 return vmcs_read16(GUEST_INTR_STATUS) & 0xff;
6202}
6203
Liran Alone6c67d82018-09-04 10:56:52 +03006204static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
6205{
6206 struct vcpu_vmx *vmx = to_vmx(vcpu);
6207 void *vapic_page;
6208 u32 vppr;
6209 int rvi;
6210
6211 if (WARN_ON_ONCE(!is_guest_mode(vcpu)) ||
6212 !nested_cpu_has_vid(get_vmcs12(vcpu)) ||
6213 WARN_ON_ONCE(!vmx->nested.virtual_apic_page))
6214 return false;
6215
Paolo Bonzini7e712682018-10-03 13:44:26 +02006216 rvi = vmx_get_rvi();
Liran Alone6c67d82018-09-04 10:56:52 +03006217
6218 vapic_page = kmap(vmx->nested.virtual_apic_page);
6219 vppr = *((u32 *)(vapic_page + APIC_PROCPRI));
6220 kunmap(vmx->nested.virtual_apic_page);
6221
6222 return ((rvi & 0xf0) > (vppr & 0xf0));
6223}
6224
Wincy Van06a55242017-04-28 13:13:59 +08006225static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
6226 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006227{
6228#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08006229 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
6230
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006231 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08006232 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006233 * The vector of interrupt to be delivered to vcpu had
6234 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08006235 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006236 * Following cases will be reached in this block, and
6237 * we always send a notification event in all cases as
6238 * explained below.
6239 *
6240 * Case 1: vcpu keeps in non-root mode. Sending a
6241 * notification event posts the interrupt to vcpu.
6242 *
6243 * Case 2: vcpu exits to root mode and is still
6244 * runnable. PIR will be synced to vIRR before the
6245 * next vcpu entry. Sending a notification event in
6246 * this case has no effect, as vcpu is not in root
6247 * mode.
6248 *
6249 * Case 3: vcpu exits to root mode and is blocked.
6250 * vcpu_block() has already synced PIR to vIRR and
6251 * never blocks vcpu if vIRR is not cleared. Therefore,
6252 * a blocked vcpu here does not wait for any requested
6253 * interrupts in PIR, and sending a notification event
6254 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08006255 */
Feng Wu28b835d2015-09-18 22:29:54 +08006256
Wincy Van06a55242017-04-28 13:13:59 +08006257 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006258 return true;
6259 }
6260#endif
6261 return false;
6262}
6263
Wincy Van705699a2015-02-03 23:58:17 +08006264static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
6265 int vector)
6266{
6267 struct vcpu_vmx *vmx = to_vmx(vcpu);
6268
6269 if (is_guest_mode(vcpu) &&
6270 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08006271 /*
6272 * If a posted intr is not recognized by hardware,
6273 * we will accomplish it in the next vmentry.
6274 */
6275 vmx->nested.pi_pending = true;
6276 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02006277 /* the PIR and ON have been set by L1. */
6278 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
6279 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006280 return 0;
6281 }
6282 return -1;
6283}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006284/*
Yang Zhanga20ed542013-04-11 19:25:15 +08006285 * Send interrupt to vcpu via posted interrupt way.
6286 * 1. If target vcpu is running(non-root mode), send posted interrupt
6287 * notification to vcpu and hardware will sync PIR to vIRR atomically.
6288 * 2. If target vcpu isn't running(root mode), kick it to pick up the
6289 * interrupt from PIR in next vmentry.
6290 */
6291static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
6292{
6293 struct vcpu_vmx *vmx = to_vmx(vcpu);
6294 int r;
6295
Wincy Van705699a2015-02-03 23:58:17 +08006296 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
6297 if (!r)
6298 return;
6299
Yang Zhanga20ed542013-04-11 19:25:15 +08006300 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
6301 return;
6302
Paolo Bonzinib95234c2016-12-19 13:57:33 +01006303 /* If a previous notification has sent the IPI, nothing to do. */
6304 if (pi_test_and_set_on(&vmx->pi_desc))
6305 return;
6306
Wincy Van06a55242017-04-28 13:13:59 +08006307 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08006308 kvm_vcpu_kick(vcpu);
6309}
6310
Avi Kivity6aa8b732006-12-10 02:21:36 -08006311/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006312 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
6313 * will not change in the lifetime of the guest.
6314 * Note that host-state that does change is set elsewhere. E.g., host-state
6315 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
6316 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006317static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006318{
6319 u32 low32, high32;
6320 unsigned long tmpl;
6321 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006322 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006323
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07006324 cr0 = read_cr0();
6325 WARN_ON(cr0 & X86_CR0_TS);
6326 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006327
6328 /*
6329 * Save the most likely value for this task's CR3 in the VMCS.
6330 * We can't use __get_current_cr3_fast() because we're not atomic.
6331 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07006332 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006333 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Sean Christophersond7ee0392018-07-23 12:32:47 -07006334 vmx->loaded_vmcs->host_state.cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006335
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006336 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07006337 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006338 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07006339 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006340
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006341 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006342#ifdef CONFIG_X86_64
6343 /*
6344 * Load null selectors, so we can avoid reloading them in
Sean Christopherson6d6095b2018-07-23 12:32:44 -07006345 * vmx_prepare_switch_to_host(), in case userspace uses
6346 * the null selectors too (the expected case).
Avi Kivityb2da15a2012-05-13 19:53:24 +03006347 */
6348 vmcs_write16(HOST_DS_SELECTOR, 0);
6349 vmcs_write16(HOST_ES_SELECTOR, 0);
6350#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006351 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6352 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006353#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006354 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6355 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
6356
Juergen Gross87930012017-09-04 12:25:27 +02006357 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006358 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006359 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006360
Avi Kivity83287ea422012-09-16 15:10:57 +03006361 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006362
6363 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
6364 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
6365 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
6366 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
6367
6368 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
6369 rdmsr(MSR_IA32_CR_PAT, low32, high32);
6370 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
6371 }
Sean Christopherson5a5e8a12018-09-26 09:23:56 -07006372
6373 if (cpu_has_load_ia32_efer)
6374 vmcs_write64(HOST_IA32_EFER, host_efer);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006375}
6376
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006377static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6378{
6379 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6380 if (enable_ept)
6381 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006382 if (is_guest_mode(&vmx->vcpu))
6383 vmx->vcpu.arch.cr4_guest_owned_bits &=
6384 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006385 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6386}
6387
Yang Zhang01e439b2013-04-11 19:25:12 +08006388static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6389{
6390 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6391
Andrey Smetanind62caab2015-11-10 15:36:33 +03006392 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006393 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006394
6395 if (!enable_vnmi)
6396 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6397
Yunhong Jiang64672c92016-06-13 14:19:59 -07006398 /* Enable the preemption timer dynamically */
6399 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006400 return pin_based_exec_ctrl;
6401}
6402
Andrey Smetanind62caab2015-11-10 15:36:33 +03006403static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6404{
6405 struct vcpu_vmx *vmx = to_vmx(vcpu);
6406
6407 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006408 if (cpu_has_secondary_exec_ctrls()) {
6409 if (kvm_vcpu_apicv_active(vcpu))
6410 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6411 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6412 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6413 else
6414 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6415 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6416 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6417 }
6418
6419 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006420 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006421}
6422
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006423static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6424{
6425 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006426
6427 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6428 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6429
Paolo Bonzini35754c92015-07-29 12:05:37 +02006430 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006431 exec_control &= ~CPU_BASED_TPR_SHADOW;
6432#ifdef CONFIG_X86_64
6433 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6434 CPU_BASED_CR8_LOAD_EXITING;
6435#endif
6436 }
6437 if (!enable_ept)
6438 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6439 CPU_BASED_CR3_LOAD_EXITING |
6440 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006441 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6442 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6443 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006444 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6445 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006446 return exec_control;
6447}
6448
Jim Mattson45ec3682017-08-23 16:32:04 -07006449static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006450{
Jim Mattson45ec3682017-08-23 16:32:04 -07006451 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006452 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006453}
6454
Jim Mattson75f4fc82017-08-23 16:32:03 -07006455static bool vmx_rdseed_supported(void)
6456{
6457 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006458 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006459}
6460
Paolo Bonzini80154d72017-08-24 13:55:35 +02006461static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006462{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006463 struct kvm_vcpu *vcpu = &vmx->vcpu;
6464
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006465 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006466
Paolo Bonzini80154d72017-08-24 13:55:35 +02006467 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006468 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6469 if (vmx->vpid == 0)
6470 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6471 if (!enable_ept) {
6472 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6473 enable_unrestricted_guest = 0;
6474 }
6475 if (!enable_unrestricted_guest)
6476 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006477 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006478 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006479 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006480 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6481 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006482 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006483
6484 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6485 * in vmx_set_cr4. */
6486 exec_control &= ~SECONDARY_EXEC_DESC;
6487
Abel Gordonabc4fc52013-04-18 14:35:25 +03006488 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6489 (handle_vmptrld).
6490 We can NOT enable shadow_vmcs here because we don't have yet
6491 a current VMCS12
6492 */
6493 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006494
6495 if (!enable_pml)
6496 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006497
Paolo Bonzini3db13482017-08-24 14:48:03 +02006498 if (vmx_xsaves_supported()) {
6499 /* Exposing XSAVES only when XSAVE is exposed */
6500 bool xsaves_enabled =
6501 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6502 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6503
6504 if (!xsaves_enabled)
6505 exec_control &= ~SECONDARY_EXEC_XSAVES;
6506
6507 if (nested) {
6508 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006509 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006510 SECONDARY_EXEC_XSAVES;
6511 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006512 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006513 ~SECONDARY_EXEC_XSAVES;
6514 }
6515 }
6516
Paolo Bonzini80154d72017-08-24 13:55:35 +02006517 if (vmx_rdtscp_supported()) {
6518 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6519 if (!rdtscp_enabled)
6520 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6521
6522 if (nested) {
6523 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006524 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006525 SECONDARY_EXEC_RDTSCP;
6526 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006527 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006528 ~SECONDARY_EXEC_RDTSCP;
6529 }
6530 }
6531
6532 if (vmx_invpcid_supported()) {
6533 /* Exposing INVPCID only when PCID is exposed */
6534 bool invpcid_enabled =
6535 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6536 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6537
6538 if (!invpcid_enabled) {
6539 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6540 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6541 }
6542
6543 if (nested) {
6544 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006545 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006546 SECONDARY_EXEC_ENABLE_INVPCID;
6547 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006548 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006549 ~SECONDARY_EXEC_ENABLE_INVPCID;
6550 }
6551 }
6552
Jim Mattson45ec3682017-08-23 16:32:04 -07006553 if (vmx_rdrand_supported()) {
6554 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6555 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006556 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006557
6558 if (nested) {
6559 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006560 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006561 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006562 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006563 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006564 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006565 }
6566 }
6567
Jim Mattson75f4fc82017-08-23 16:32:03 -07006568 if (vmx_rdseed_supported()) {
6569 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6570 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006571 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006572
6573 if (nested) {
6574 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006575 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006576 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006577 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006578 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006579 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006580 }
6581 }
6582
Paolo Bonzini80154d72017-08-24 13:55:35 +02006583 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006584}
6585
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006586static void ept_set_mmio_spte_mask(void)
6587{
6588 /*
6589 * EPT Misconfigurations can be generated if the value of bits 2:0
6590 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006591 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006592 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6593 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006594}
6595
Wanpeng Lif53cd632014-12-02 19:14:58 +08006596#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006597/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006598 * Sets up the vmcs for emulated real mode.
6599 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006600static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006601{
Avi Kivity6aa8b732006-12-10 02:21:36 -08006602 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006603
Abel Gordon4607c2d2013-04-18 14:35:55 +03006604 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006605 /*
6606 * At vCPU creation, "VMWRITE to any supported field
6607 * in the VMCS" is supported, so use the more
6608 * permissive vmx_vmread_bitmap to specify both read
6609 * and write permissions for the shadow VMCS.
6610 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006611 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006612 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006613 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006614 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006615 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006616
Avi Kivity6aa8b732006-12-10 02:21:36 -08006617 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6618
Avi Kivity6aa8b732006-12-10 02:21:36 -08006619 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006620 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006621 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006622
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006623 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006624
Dan Williamsdfa169b2016-06-02 11:17:24 -07006625 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006626 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006627 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006628 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006629 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006630
Andrey Smetanind62caab2015-11-10 15:36:33 +03006631 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006632 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6633 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6634 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6635 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6636
6637 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006638
Li RongQing0bcf2612015-12-03 13:29:34 +08006639 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006640 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006641 }
6642
Wanpeng Lib31c1142018-03-12 04:53:04 -07006643 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006644 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006645 vmx->ple_window = ple_window;
6646 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006647 }
6648
Xiao Guangrongc3707952011-07-12 03:28:04 +08006649 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6650 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006651 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6652
Avi Kivity9581d442010-10-19 16:46:55 +02006653 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6654 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006655 vmx_set_constant_host_state(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006656 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6657 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08006658
Bandan Das2a499e42017-08-03 15:54:41 -04006659 if (cpu_has_vmx_vmfunc())
6660 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6661
Eddie Dong2cc51562007-05-21 07:28:09 +03006662 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6663 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006664 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03006665 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006666 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006667
Radim Krčmář74545702015-04-27 15:11:25 +02006668 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6669 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006670
Paolo Bonzini03916db2014-07-24 14:21:57 +02006671 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006672 u32 index = vmx_msr_index[i];
6673 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006674 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006675
6676 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6677 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006678 if (wrmsr_safe(index, data_low, data_high) < 0)
6679 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006680 vmx->guest_msrs[j].index = i;
6681 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006682 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006683 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006684 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006685
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02006686 vmx->arch_capabilities = kvm_get_arch_capabilities();
Gleb Natapov2961e8762013-11-25 15:37:13 +02006687
6688 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006689
6690 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006691 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006692
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006693 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6694 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6695
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006696 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006697
Wanpeng Lif53cd632014-12-02 19:14:58 +08006698 if (vmx_xsaves_supported())
6699 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6700
Peter Feiner4e595162016-07-07 14:49:58 -07006701 if (enable_pml) {
Peter Feiner4e595162016-07-07 14:49:58 -07006702 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6703 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6704 }
Sean Christopherson0b665d32018-08-14 09:33:34 -07006705
6706 if (cpu_has_vmx_encls_vmexit())
6707 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006708}
6709
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006710static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006711{
6712 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006713 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006714 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006715
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006716 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006717 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006718
Wanpeng Li518e7b92018-02-28 14:03:31 +08006719 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006720 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006721 kvm_set_cr8(vcpu, 0);
6722
6723 if (!init_event) {
6724 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6725 MSR_IA32_APICBASE_ENABLE;
6726 if (kvm_vcpu_is_reset_bsp(vcpu))
6727 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6728 apic_base_msr.host_initiated = true;
6729 kvm_set_apic_base(vcpu, &apic_base_msr);
6730 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006731
Avi Kivity2fb92db2011-04-27 19:42:18 +03006732 vmx_segment_cache_clear(vmx);
6733
Avi Kivity5706be02008-08-20 15:07:31 +03006734 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006735 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006736 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006737
6738 seg_setup(VCPU_SREG_DS);
6739 seg_setup(VCPU_SREG_ES);
6740 seg_setup(VCPU_SREG_FS);
6741 seg_setup(VCPU_SREG_GS);
6742 seg_setup(VCPU_SREG_SS);
6743
6744 vmcs_write16(GUEST_TR_SELECTOR, 0);
6745 vmcs_writel(GUEST_TR_BASE, 0);
6746 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6747 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6748
6749 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6750 vmcs_writel(GUEST_LDTR_BASE, 0);
6751 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6752 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6753
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006754 if (!init_event) {
6755 vmcs_write32(GUEST_SYSENTER_CS, 0);
6756 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6757 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6758 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6759 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006760
Wanpeng Lic37c2872017-11-20 14:52:21 -08006761 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006762 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006763
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006764 vmcs_writel(GUEST_GDTR_BASE, 0);
6765 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6766
6767 vmcs_writel(GUEST_IDTR_BASE, 0);
6768 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6769
Anthony Liguori443381a2010-12-06 10:53:38 -06006770 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006771 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006772 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006773 if (kvm_mpx_supported())
6774 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006775
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006776 setup_msrs(vmx);
6777
Avi Kivity6aa8b732006-12-10 02:21:36 -08006778 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6779
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006780 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006781 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006782 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006783 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006784 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006785 vmcs_write32(TPR_THRESHOLD, 0);
6786 }
6787
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006788 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006789
Sheng Yang2384d2b2008-01-17 15:14:33 +08006790 if (vmx->vpid != 0)
6791 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6792
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006793 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006794 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006795 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006796 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006797 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006798
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006799 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006800
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006801 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006802 if (init_event)
6803 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006804}
6805
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006806/*
6807 * In nested virtualization, check if L1 asked to exit on external interrupts.
6808 * For most existing hypervisors, this will always return true.
6809 */
6810static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6811{
6812 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6813 PIN_BASED_EXT_INTR_MASK;
6814}
6815
Bandan Das77b0f5d2014-04-19 18:17:45 -04006816/*
6817 * In nested virtualization, check if L1 has set
6818 * VM_EXIT_ACK_INTR_ON_EXIT
6819 */
6820static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6821{
6822 return get_vmcs12(vcpu)->vm_exit_controls &
6823 VM_EXIT_ACK_INTR_ON_EXIT;
6824}
6825
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006826static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6827{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006828 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006829}
6830
Jan Kiszkac9a79532014-03-07 20:03:15 +01006831static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006832{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006833 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6834 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006835}
6836
Jan Kiszkac9a79532014-03-07 20:03:15 +01006837static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006838{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006839 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006840 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006841 enable_irq_window(vcpu);
6842 return;
6843 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006844
Paolo Bonzini47c01522016-12-19 11:44:07 +01006845 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6846 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006847}
6848
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006849static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006850{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006851 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006852 uint32_t intr;
6853 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006854
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006855 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006856
Avi Kivityfa89a812008-09-01 15:57:51 +03006857 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006858 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006859 int inc_eip = 0;
6860 if (vcpu->arch.interrupt.soft)
6861 inc_eip = vcpu->arch.event_exit_inst_len;
6862 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006863 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006864 return;
6865 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006866 intr = irq | INTR_INFO_VALID_MASK;
6867 if (vcpu->arch.interrupt.soft) {
6868 intr |= INTR_TYPE_SOFT_INTR;
6869 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6870 vmx->vcpu.arch.event_exit_inst_len);
6871 } else
6872 intr |= INTR_TYPE_EXT_INTR;
6873 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006874
6875 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006876}
6877
Sheng Yangf08864b2008-05-15 18:23:25 +08006878static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6879{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006880 struct vcpu_vmx *vmx = to_vmx(vcpu);
6881
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006882 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006883 /*
6884 * Tracking the NMI-blocked state in software is built upon
6885 * finding the next open IRQ window. This, in turn, depends on
6886 * well-behaving guests: They have to keep IRQs disabled at
6887 * least as long as the NMI handler runs. Otherwise we may
6888 * cause NMI nesting, maybe breaking the guest. But as this is
6889 * highly unlikely, we can live with the residual risk.
6890 */
6891 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6892 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6893 }
6894
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006895 ++vcpu->stat.nmi_injections;
6896 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006897
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006898 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006899 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006900 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006901 return;
6902 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006903
Sheng Yangf08864b2008-05-15 18:23:25 +08006904 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6905 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006906
6907 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006908}
6909
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006910static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6911{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006912 struct vcpu_vmx *vmx = to_vmx(vcpu);
6913 bool masked;
6914
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006915 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006916 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006917 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006918 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006919 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6920 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6921 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006922}
6923
6924static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6925{
6926 struct vcpu_vmx *vmx = to_vmx(vcpu);
6927
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006928 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006929 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6930 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6931 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6932 }
6933 } else {
6934 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6935 if (masked)
6936 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6937 GUEST_INTR_STATE_NMI);
6938 else
6939 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6940 GUEST_INTR_STATE_NMI);
6941 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006942}
6943
Jan Kiszka2505dc92013-04-14 12:12:47 +02006944static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6945{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006946 if (to_vmx(vcpu)->nested.nested_run_pending)
6947 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006948
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006949 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006950 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6951 return 0;
6952
Jan Kiszka2505dc92013-04-14 12:12:47 +02006953 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6954 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6955 | GUEST_INTR_STATE_NMI));
6956}
6957
Gleb Natapov78646122009-03-23 12:12:11 +02006958static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6959{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006960 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6961 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006962 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6963 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006964}
6965
Izik Eiduscbc94022007-10-25 00:29:55 +02006966static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6967{
6968 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006969
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006970 if (enable_unrestricted_guest)
6971 return 0;
6972
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006973 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6974 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006975 if (ret)
6976 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006977 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006978 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006979}
6980
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006981static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6982{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006983 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006984 return 0;
6985}
6986
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006987static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006988{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006989 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006990 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006991 /*
6992 * Update instruction length as we may reinject the exception
6993 * from user space while in guest debugging mode.
6994 */
6995 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6996 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006997 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006998 return false;
6999 /* fall through */
7000 case DB_VECTOR:
7001 if (vcpu->guest_debug &
7002 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
7003 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007004 /* fall through */
7005 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02007006 case OF_VECTOR:
7007 case BR_VECTOR:
7008 case UD_VECTOR:
7009 case DF_VECTOR:
7010 case SS_VECTOR:
7011 case GP_VECTOR:
7012 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02007013 return true;
7014 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02007015 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02007016 return false;
7017}
7018
7019static int handle_rmode_exception(struct kvm_vcpu *vcpu,
7020 int vec, u32 err_code)
7021{
7022 /*
7023 * Instruction with address size override prefix opcode 0x67
7024 * Cause the #SS fault with 0 error code in VM86 mode.
7025 */
7026 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007027 if (kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE) {
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02007028 if (vcpu->arch.halt_request) {
7029 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007030 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02007031 }
7032 return 1;
7033 }
7034 return 0;
7035 }
7036
7037 /*
7038 * Forward all other exceptions that are valid in real mode.
7039 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
7040 * the required debugging infrastructure rework.
7041 */
7042 kvm_queue_exception(vcpu, vec);
7043 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007044}
7045
Andi Kleena0861c02009-06-08 17:37:09 +08007046/*
7047 * Trigger machine check on the host. We assume all the MSRs are already set up
7048 * by the CPU and that we still run on the same CPU as the MCE occurred on.
7049 * We pass a fake environment to the machine check handler because we want
7050 * the guest to be always treated like user space, no matter what context
7051 * it used internally.
7052 */
7053static void kvm_machine_check(void)
7054{
7055#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
7056 struct pt_regs regs = {
7057 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
7058 .flags = X86_EFLAGS_IF,
7059 };
7060
7061 do_machine_check(&regs, 0);
7062#endif
7063}
7064
Avi Kivity851ba692009-08-24 11:10:17 +03007065static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08007066{
7067 /* already handled by vcpu_run */
7068 return 1;
7069}
7070
Avi Kivity851ba692009-08-24 11:10:17 +03007071static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007072{
Avi Kivity1155f762007-11-22 11:30:47 +02007073 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007074 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007075 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007076 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007077 u32 vect_info;
7078 enum emulation_result er;
7079
Avi Kivity1155f762007-11-22 11:30:47 +02007080 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02007081 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007082
Andi Kleena0861c02009-06-08 17:37:09 +08007083 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03007084 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08007085
Jim Mattsonef85b672016-12-12 11:01:37 -08007086 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02007087 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03007088
Wanpeng Li082d06e2018-04-03 16:28:48 -07007089 if (is_invalid_opcode(intr_info))
7090 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05007091
Avi Kivity6aa8b732006-12-10 02:21:36 -08007092 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06007093 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007094 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007095
Liran Alon9e869482018-03-12 13:12:51 +02007096 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
7097 WARN_ON_ONCE(!enable_vmware_backdoor);
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007098 er = kvm_emulate_instruction(vcpu,
Liran Alon9e869482018-03-12 13:12:51 +02007099 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
7100 if (er == EMULATE_USER_EXIT)
7101 return 0;
7102 else if (er != EMULATE_DONE)
7103 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
7104 return 1;
7105 }
7106
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007107 /*
7108 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
7109 * MMIO, it is better to report an internal error.
7110 * See the comments in vmx_handle_exit.
7111 */
7112 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
7113 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
7114 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7115 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02007116 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007117 vcpu->run->internal.data[0] = vect_info;
7118 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02007119 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007120 return 0;
7121 }
7122
Avi Kivity6aa8b732006-12-10 02:21:36 -08007123 if (is_page_fault(intr_info)) {
7124 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07007125 /* EPT won't cause page fault directly */
7126 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02007127 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007128 }
7129
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007130 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02007131
7132 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
7133 return handle_rmode_exception(vcpu, ex_no, error_code);
7134
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007135 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01007136 case AC_VECTOR:
7137 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
7138 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007139 case DB_VECTOR:
7140 dr6 = vmcs_readl(EXIT_QUALIFICATION);
7141 if (!(vcpu->guest_debug &
7142 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01007143 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007144 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07007145 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01007146 skip_emulated_instruction(vcpu);
7147
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007148 kvm_queue_exception(vcpu, DB_VECTOR);
7149 return 1;
7150 }
7151 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
7152 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
7153 /* fall through */
7154 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01007155 /*
7156 * Update instruction length as we may reinject #BP from
7157 * user space while in guest debugging mode. Reading it for
7158 * #DB as well causes no harm, it is not used in that case.
7159 */
7160 vmx->vcpu.arch.event_exit_inst_len =
7161 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007162 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03007163 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007164 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
7165 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007166 break;
7167 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007168 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
7169 kvm_run->ex.exception = ex_no;
7170 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007171 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007172 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007173 return 0;
7174}
7175
Avi Kivity851ba692009-08-24 11:10:17 +03007176static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007177{
Avi Kivity1165f5f2007-04-19 17:27:43 +03007178 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007179 return 1;
7180}
7181
Avi Kivity851ba692009-08-24 11:10:17 +03007182static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08007183{
Avi Kivity851ba692009-08-24 11:10:17 +03007184 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07007185 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08007186 return 0;
7187}
Avi Kivity6aa8b732006-12-10 02:21:36 -08007188
Avi Kivity851ba692009-08-24 11:10:17 +03007189static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007190{
He, Qingbfdaab02007-09-12 14:18:28 +08007191 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08007192 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02007193 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007194
He, Qingbfdaab02007-09-12 14:18:28 +08007195 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02007196 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03007197
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007198 ++vcpu->stat.io_exits;
7199
Sean Christopherson432baf62018-03-08 08:57:26 -08007200 if (string)
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007201 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007202
7203 port = exit_qualification >> 16;
7204 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08007205 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007206
Sean Christophersondca7f122018-03-08 08:57:27 -08007207 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007208}
7209
Ingo Molnar102d8322007-02-19 14:37:47 +02007210static void
7211vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
7212{
7213 /*
7214 * Patch in the VMCALL instruction:
7215 */
7216 hypercall[0] = 0x0f;
7217 hypercall[1] = 0x01;
7218 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02007219}
7220
Guo Chao0fa06072012-06-28 15:16:19 +08007221/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007222static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
7223{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007224 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007225 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7226 unsigned long orig_val = val;
7227
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007228 /*
7229 * We get here when L2 changed cr0 in a way that did not change
7230 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007231 * but did change L0 shadowed bits. So we first calculate the
7232 * effective cr0 value that L1 would like to write into the
7233 * hardware. It consists of the L2-owned bits from the new
7234 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007235 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007236 val = (val & ~vmcs12->cr0_guest_host_mask) |
7237 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
7238
David Matlack38991522016-11-29 18:14:08 -08007239 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007240 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007241
7242 if (kvm_set_cr0(vcpu, val))
7243 return 1;
7244 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007245 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007246 } else {
7247 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08007248 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007249 return 1;
David Matlack38991522016-11-29 18:14:08 -08007250
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007251 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007252 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007253}
7254
7255static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
7256{
7257 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007258 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7259 unsigned long orig_val = val;
7260
7261 /* analogously to handle_set_cr0 */
7262 val = (val & ~vmcs12->cr4_guest_host_mask) |
7263 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
7264 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007265 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007266 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007267 return 0;
7268 } else
7269 return kvm_set_cr4(vcpu, val);
7270}
7271
Paolo Bonzini0367f202016-07-12 10:44:55 +02007272static int handle_desc(struct kvm_vcpu *vcpu)
7273{
7274 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007275 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02007276}
7277
Avi Kivity851ba692009-08-24 11:10:17 +03007278static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007279{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007280 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007281 int cr;
7282 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03007283 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007284 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007285
He, Qingbfdaab02007-09-12 14:18:28 +08007286 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007287 cr = exit_qualification & 15;
7288 reg = (exit_qualification >> 8) & 15;
7289 switch ((exit_qualification >> 4) & 3) {
7290 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03007291 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007292 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007293 switch (cr) {
7294 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007295 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007296 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007297 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007298 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03007299 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007300 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007301 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007302 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007303 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007304 case 8: {
7305 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03007306 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01007307 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007308 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02007309 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08007310 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007311 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007312 return ret;
7313 /*
7314 * TODO: we might be squashing a
7315 * KVM_GUESTDBG_SINGLESTEP-triggered
7316 * KVM_EXIT_DEBUG here.
7317 */
Avi Kivity851ba692009-08-24 11:10:17 +03007318 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007319 return 0;
7320 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02007321 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007322 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03007323 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08007324 WARN_ONCE(1, "Guest should always own CR0.TS");
7325 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02007326 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08007327 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007328 case 1: /*mov from cr*/
7329 switch (cr) {
7330 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007331 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02007332 val = kvm_read_cr3(vcpu);
7333 kvm_register_write(vcpu, reg, val);
7334 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007335 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007336 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007337 val = kvm_get_cr8(vcpu);
7338 kvm_register_write(vcpu, reg, val);
7339 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007340 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007341 }
7342 break;
7343 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02007344 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02007345 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02007346 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007347
Kyle Huey6affcbe2016-11-29 12:40:40 -08007348 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007349 default:
7350 break;
7351 }
Avi Kivity851ba692009-08-24 11:10:17 +03007352 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03007353 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08007354 (int)(exit_qualification >> 4) & 3, cr);
7355 return 0;
7356}
7357
Avi Kivity851ba692009-08-24 11:10:17 +03007358static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007359{
He, Qingbfdaab02007-09-12 14:18:28 +08007360 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007361 int dr, dr7, reg;
7362
7363 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7364 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
7365
7366 /* First, if DR does not exist, trigger UD */
7367 if (!kvm_require_dr(vcpu, dr))
7368 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007369
Jan Kiszkaf2483412010-01-20 18:20:20 +01007370 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03007371 if (!kvm_require_cpl(vcpu, 0))
7372 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007373 dr7 = vmcs_readl(GUEST_DR7);
7374 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007375 /*
7376 * As the vm-exit takes precedence over the debug trap, we
7377 * need to emulate the latter, either for the host or the
7378 * guest debugging itself.
7379 */
7380 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007381 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007382 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007383 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007384 vcpu->run->debug.arch.exception = DB_VECTOR;
7385 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007386 return 0;
7387 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007388 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007389 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007390 kvm_queue_exception(vcpu, DB_VECTOR);
7391 return 1;
7392 }
7393 }
7394
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007395 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007396 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7397 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007398
7399 /*
7400 * No more DR vmexits; force a reload of the debug registers
7401 * and reenter on this instruction. The next vmexit will
7402 * retrieve the full state of the debug registers.
7403 */
7404 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7405 return 1;
7406 }
7407
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007408 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7409 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007410 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007411
7412 if (kvm_get_dr(vcpu, dr, &val))
7413 return 1;
7414 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007415 } else
Nadav Amit57773922014-06-18 17:19:23 +03007416 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007417 return 1;
7418
Kyle Huey6affcbe2016-11-29 12:40:40 -08007419 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007420}
7421
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007422static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7423{
7424 return vcpu->arch.dr6;
7425}
7426
7427static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7428{
7429}
7430
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007431static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7432{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007433 get_debugreg(vcpu->arch.db[0], 0);
7434 get_debugreg(vcpu->arch.db[1], 1);
7435 get_debugreg(vcpu->arch.db[2], 2);
7436 get_debugreg(vcpu->arch.db[3], 3);
7437 get_debugreg(vcpu->arch.dr6, 6);
7438 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7439
7440 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007441 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007442}
7443
Gleb Natapov020df072010-04-13 10:05:23 +03007444static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7445{
7446 vmcs_writel(GUEST_DR7, val);
7447}
7448
Avi Kivity851ba692009-08-24 11:10:17 +03007449static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007450{
Kyle Huey6a908b62016-11-29 12:40:37 -08007451 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007452}
7453
Avi Kivity851ba692009-08-24 11:10:17 +03007454static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007455{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007456 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007457 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007458
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007459 msr_info.index = ecx;
7460 msr_info.host_initiated = false;
7461 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007462 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007463 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007464 return 1;
7465 }
7466
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007467 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007468
Avi Kivity6aa8b732006-12-10 02:21:36 -08007469 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007470 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7471 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007472 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007473}
7474
Avi Kivity851ba692009-08-24 11:10:17 +03007475static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007476{
Will Auld8fe8ab42012-11-29 12:42:12 -08007477 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007478 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7479 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7480 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007481
Will Auld8fe8ab42012-11-29 12:42:12 -08007482 msr.data = data;
7483 msr.index = ecx;
7484 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007485 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007486 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007487 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007488 return 1;
7489 }
7490
Avi Kivity59200272010-01-25 19:47:02 +02007491 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007492 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007493}
7494
Avi Kivity851ba692009-08-24 11:10:17 +03007495static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007496{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007497 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007498 return 1;
7499}
7500
Avi Kivity851ba692009-08-24 11:10:17 +03007501static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007502{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007503 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7504 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007505
Avi Kivity3842d132010-07-27 12:30:24 +03007506 kvm_make_request(KVM_REQ_EVENT, vcpu);
7507
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007508 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007509 return 1;
7510}
7511
Avi Kivity851ba692009-08-24 11:10:17 +03007512static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007513{
Avi Kivityd3bef152007-06-05 15:53:05 +03007514 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007515}
7516
Avi Kivity851ba692009-08-24 11:10:17 +03007517static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007518{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007519 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007520}
7521
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007522static int handle_invd(struct kvm_vcpu *vcpu)
7523{
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007524 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007525}
7526
Avi Kivity851ba692009-08-24 11:10:17 +03007527static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007528{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007529 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007530
7531 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007532 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007533}
7534
Avi Kivityfee84b02011-11-10 14:57:25 +02007535static int handle_rdpmc(struct kvm_vcpu *vcpu)
7536{
7537 int err;
7538
7539 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007540 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007541}
7542
Avi Kivity851ba692009-08-24 11:10:17 +03007543static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007544{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007545 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007546}
7547
Dexuan Cui2acf9232010-06-10 11:27:12 +08007548static int handle_xsetbv(struct kvm_vcpu *vcpu)
7549{
7550 u64 new_bv = kvm_read_edx_eax(vcpu);
7551 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7552
7553 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007554 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007555 return 1;
7556}
7557
Wanpeng Lif53cd632014-12-02 19:14:58 +08007558static int handle_xsaves(struct kvm_vcpu *vcpu)
7559{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007560 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007561 WARN(1, "this should never happen\n");
7562 return 1;
7563}
7564
7565static int handle_xrstors(struct kvm_vcpu *vcpu)
7566{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007567 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007568 WARN(1, "this should never happen\n");
7569 return 1;
7570}
7571
Avi Kivity851ba692009-08-24 11:10:17 +03007572static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007573{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007574 if (likely(fasteoi)) {
7575 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7576 int access_type, offset;
7577
7578 access_type = exit_qualification & APIC_ACCESS_TYPE;
7579 offset = exit_qualification & APIC_ACCESS_OFFSET;
7580 /*
7581 * Sane guest uses MOV to write EOI, with written value
7582 * not cared. So make a short-circuit here by avoiding
7583 * heavy instruction emulation.
7584 */
7585 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7586 (offset == APIC_EOI)) {
7587 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007588 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007589 }
7590 }
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007591 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007592}
7593
Yang Zhangc7c9c562013-01-25 10:18:51 +08007594static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7595{
7596 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7597 int vector = exit_qualification & 0xff;
7598
7599 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7600 kvm_apic_set_eoi_accelerated(vcpu, vector);
7601 return 1;
7602}
7603
Yang Zhang83d4c282013-01-25 10:18:49 +08007604static int handle_apic_write(struct kvm_vcpu *vcpu)
7605{
7606 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7607 u32 offset = exit_qualification & 0xfff;
7608
7609 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7610 kvm_apic_write_nodecode(vcpu, offset);
7611 return 1;
7612}
7613
Avi Kivity851ba692009-08-24 11:10:17 +03007614static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007615{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007616 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007617 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007618 bool has_error_code = false;
7619 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007620 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007621 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007622
7623 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007624 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007625 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007626
7627 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7628
7629 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007630 if (reason == TASK_SWITCH_GATE && idt_v) {
7631 switch (type) {
7632 case INTR_TYPE_NMI_INTR:
7633 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007634 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007635 break;
7636 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007637 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007638 kvm_clear_interrupt_queue(vcpu);
7639 break;
7640 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007641 if (vmx->idt_vectoring_info &
7642 VECTORING_INFO_DELIVER_CODE_MASK) {
7643 has_error_code = true;
7644 error_code =
7645 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7646 }
7647 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007648 case INTR_TYPE_SOFT_EXCEPTION:
7649 kvm_clear_exception_queue(vcpu);
7650 break;
7651 default:
7652 break;
7653 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007654 }
Izik Eidus37817f22008-03-24 23:14:53 +02007655 tss_selector = exit_qualification;
7656
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007657 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7658 type != INTR_TYPE_EXT_INTR &&
7659 type != INTR_TYPE_NMI_INTR))
7660 skip_emulated_instruction(vcpu);
7661
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007662 if (kvm_task_switch(vcpu, tss_selector,
7663 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7664 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007665 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7666 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7667 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007668 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007669 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007670
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007671 /*
7672 * TODO: What about debug traps on tss switch?
7673 * Are we supposed to inject them and update dr6?
7674 */
7675
7676 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007677}
7678
Avi Kivity851ba692009-08-24 11:10:17 +03007679static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007680{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007681 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007682 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007683 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007684
Sheng Yangf9c617f2009-03-25 10:08:52 +08007685 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007686
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007687 /*
7688 * EPT violation happened while executing iret from NMI,
7689 * "blocked by NMI" bit has to be set before next VM entry.
7690 * There are errata that may cause this bit to not be set:
7691 * AAK134, BY25.
7692 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007693 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007694 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007695 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007696 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7697
Sheng Yang14394422008-04-28 12:24:45 +08007698 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007699 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007700
Junaid Shahid27959a42016-12-06 16:46:10 -08007701 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007702 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007703 ? PFERR_USER_MASK : 0;
7704 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007705 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007706 ? PFERR_WRITE_MASK : 0;
7707 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007708 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007709 ? PFERR_FETCH_MASK : 0;
7710 /* ept page table entry is present? */
7711 error_code |= (exit_qualification &
7712 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7713 EPT_VIOLATION_EXECUTABLE))
7714 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007715
Paolo Bonzinieebed242016-11-28 14:39:58 +01007716 error_code |= (exit_qualification & 0x100) != 0 ?
7717 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007718
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007719 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007720 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007721}
7722
Avi Kivity851ba692009-08-24 11:10:17 +03007723static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007724{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007725 gpa_t gpa;
7726
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007727 /*
7728 * A nested guest cannot optimize MMIO vmexits, because we have an
7729 * nGPA here instead of the required GPA.
7730 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007731 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007732 if (!is_guest_mode(vcpu) &&
7733 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007734 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007735 /*
7736 * Doing kvm_skip_emulated_instruction() depends on undefined
7737 * behavior: Intel's manual doesn't mandate
7738 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7739 * occurs and while on real hardware it was observed to be set,
7740 * other hypervisors (namely Hyper-V) don't set it, we end up
7741 * advancing IP with some random value. Disable fast mmio when
7742 * running nested and keep it for real hardware in hope that
7743 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7744 */
7745 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7746 return kvm_skip_emulated_instruction(vcpu);
7747 else
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007748 return kvm_emulate_instruction(vcpu, EMULTYPE_SKIP) ==
Sean Christophersonc4409902018-08-23 13:56:46 -07007749 EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007750 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007751
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007752 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007753}
7754
Avi Kivity851ba692009-08-24 11:10:17 +03007755static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007756{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007757 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007758 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7759 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007760 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007761 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007762
7763 return 1;
7764}
7765
Mohammed Gamal80ced182009-09-01 12:48:18 +02007766static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007767{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007768 struct vcpu_vmx *vmx = to_vmx(vcpu);
7769 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007770 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007771 u32 cpu_exec_ctrl;
7772 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007773 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007774
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007775 /*
7776 * We should never reach the point where we are emulating L2
7777 * due to invalid guest state as that means we incorrectly
7778 * allowed a nested VMEntry with an invalid vmcs12.
7779 */
7780 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7781
Avi Kivity49e9d552010-09-19 14:34:08 +02007782 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7783 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007784
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007785 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007786 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007787 return handle_interrupt_window(&vmx->vcpu);
7788
Radim Krčmář72875d82017-04-26 22:32:19 +02007789 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007790 return 1;
7791
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007792 err = kvm_emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007793
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007794 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007795 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007796 ret = 0;
7797 goto out;
7798 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007799
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007800 if (err != EMULATE_DONE)
7801 goto emulation_error;
7802
7803 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7804 vcpu->arch.exception.pending)
7805 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007806
Gleb Natapov8d76c492013-05-08 18:38:44 +03007807 if (vcpu->arch.halt_request) {
7808 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007809 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007810 goto out;
7811 }
7812
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007813 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007814 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007815 if (need_resched())
7816 schedule();
7817 }
7818
Mohammed Gamal80ced182009-09-01 12:48:18 +02007819out:
7820 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007821
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007822emulation_error:
7823 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7824 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7825 vcpu->run->internal.ndata = 0;
7826 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007827}
7828
7829static void grow_ple_window(struct kvm_vcpu *vcpu)
7830{
7831 struct vcpu_vmx *vmx = to_vmx(vcpu);
7832 int old = vmx->ple_window;
7833
Babu Mogerc8e88712018-03-16 16:37:24 -04007834 vmx->ple_window = __grow_ple_window(old, ple_window,
7835 ple_window_grow,
7836 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007837
7838 if (vmx->ple_window != old)
7839 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007840
7841 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007842}
7843
7844static void shrink_ple_window(struct kvm_vcpu *vcpu)
7845{
7846 struct vcpu_vmx *vmx = to_vmx(vcpu);
7847 int old = vmx->ple_window;
7848
Babu Mogerc8e88712018-03-16 16:37:24 -04007849 vmx->ple_window = __shrink_ple_window(old, ple_window,
7850 ple_window_shrink,
7851 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007852
7853 if (vmx->ple_window != old)
7854 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007855
7856 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007857}
7858
7859/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007860 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7861 */
7862static void wakeup_handler(void)
7863{
7864 struct kvm_vcpu *vcpu;
7865 int cpu = smp_processor_id();
7866
7867 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7868 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7869 blocked_vcpu_list) {
7870 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7871
7872 if (pi_test_on(pi_desc) == 1)
7873 kvm_vcpu_kick(vcpu);
7874 }
7875 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7876}
7877
Peng Haoe01bca22018-04-07 05:47:32 +08007878static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007879{
7880 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7881 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7882 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7883 0ull, VMX_EPT_EXECUTABLE_MASK,
7884 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007885 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007886
7887 ept_set_mmio_spte_mask();
7888 kvm_enable_tdp();
7889}
7890
Tiejun Chenf2c76482014-10-28 10:14:47 +08007891static __init int hardware_setup(void)
7892{
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007893 unsigned long host_bndcfgs;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007894 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007895
7896 rdmsrl_safe(MSR_EFER, &host_efer);
7897
7898 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7899 kvm_define_shared_msr(i, vmx_msr_index[i]);
7900
Radim Krčmář23611332016-09-29 22:41:33 +02007901 for (i = 0; i < VMX_BITMAP_NR; i++) {
7902 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7903 if (!vmx_bitmap[i])
7904 goto out;
7905 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007906
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007907 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7908 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7909
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007910 if (setup_vmcs_config(&vmcs_config) < 0) {
7911 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007912 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007913 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007914
7915 if (boot_cpu_has(X86_FEATURE_NX))
7916 kvm_enable_efer_bits(EFER_NX);
7917
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007918 if (boot_cpu_has(X86_FEATURE_MPX)) {
7919 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7920 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7921 }
7922
Wanpeng Li08d839c2017-03-23 05:30:08 -07007923 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7924 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007925 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007926
Tiejun Chenf2c76482014-10-28 10:14:47 +08007927 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007928 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007929 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007930 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007931 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007932
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007933 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007934 enable_ept_ad_bits = 0;
7935
Wanpeng Li8ad81822017-10-09 15:51:53 -07007936 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007937 enable_unrestricted_guest = 0;
7938
Paolo Bonziniad15a292015-01-30 16:18:49 +01007939 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007940 flexpriority_enabled = 0;
7941
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007942 if (!cpu_has_virtual_nmis())
7943 enable_vnmi = 0;
7944
Paolo Bonziniad15a292015-01-30 16:18:49 +01007945 /*
7946 * set_apic_access_page_addr() is used to reload apic access
7947 * page upon invalidation. No need to do anything if not
7948 * using the APIC_ACCESS_ADDR VMCS field.
7949 */
7950 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007951 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007952
7953 if (!cpu_has_vmx_tpr_shadow())
7954 kvm_x86_ops->update_cr8_intercept = NULL;
7955
7956 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7957 kvm_disable_largepages();
7958
Tianyu Lan877ad952018-07-19 08:40:23 +00007959#if IS_ENABLED(CONFIG_HYPERV)
7960 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
7961 && enable_ept)
7962 kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb;
7963#endif
7964
Wanpeng Li0f107682017-09-28 18:06:24 -07007965 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007966 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007967 ple_window = 0;
7968 ple_window_grow = 0;
7969 ple_window_max = 0;
7970 ple_window_shrink = 0;
7971 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007972
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007973 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007974 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007975 kvm_x86_ops->sync_pir_to_irr = NULL;
7976 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007977
Haozhong Zhang64903d62015-10-20 15:39:09 +08007978 if (cpu_has_vmx_tsc_scaling()) {
7979 kvm_has_tsc_control = true;
7980 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7981 kvm_tsc_scaling_ratio_frac_bits = 48;
7982 }
7983
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007984 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7985
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007986 if (enable_ept)
7987 vmx_enable_tdp();
7988 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007989 kvm_disable_tdp();
7990
Jim Mattson8fcc4b52018-07-10 11:27:20 +02007991 if (!nested) {
7992 kvm_x86_ops->get_nested_state = NULL;
7993 kvm_x86_ops->set_nested_state = NULL;
7994 }
7995
Kai Huang843e4332015-01-28 10:54:28 +08007996 /*
7997 * Only enable PML when hardware supports PML feature, and both EPT
7998 * and EPT A/D bit features are enabled -- PML depends on them to work.
7999 */
8000 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
8001 enable_pml = 0;
8002
8003 if (!enable_pml) {
8004 kvm_x86_ops->slot_enable_log_dirty = NULL;
8005 kvm_x86_ops->slot_disable_log_dirty = NULL;
8006 kvm_x86_ops->flush_log_dirty = NULL;
8007 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
8008 }
8009
Sean Christophersond264ee02018-08-27 15:21:12 -07008010 if (!cpu_has_vmx_preemption_timer())
8011 kvm_x86_ops->request_immediate_exit = __kvm_request_immediate_exit;
8012
Yunhong Jiang64672c92016-06-13 14:19:59 -07008013 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
8014 u64 vmx_msr;
8015
8016 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
8017 cpu_preemption_timer_multi =
8018 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
8019 } else {
8020 kvm_x86_ops->set_hv_timer = NULL;
8021 kvm_x86_ops->cancel_hv_timer = NULL;
8022 }
8023
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01008024 if (!cpu_has_vmx_shadow_vmcs())
8025 enable_shadow_vmcs = 0;
8026 if (enable_shadow_vmcs)
8027 init_vmcs_shadow_fields();
8028
Feng Wubf9f6ac2015-09-18 22:29:55 +08008029 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01008030 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008031
Ashok Rajc45dcc72016-06-22 14:59:56 +08008032 kvm_mce_cap_supported |= MCG_LMCE_P;
8033
Tiejun Chenf2c76482014-10-28 10:14:47 +08008034 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008035
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008036out:
Radim Krčmář23611332016-09-29 22:41:33 +02008037 for (i = 0; i < VMX_BITMAP_NR; i++)
8038 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008039
8040 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08008041}
8042
8043static __exit void hardware_unsetup(void)
8044{
Radim Krčmář23611332016-09-29 22:41:33 +02008045 int i;
8046
8047 for (i = 0; i < VMX_BITMAP_NR; i++)
8048 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008049
Tiejun Chenf2c76482014-10-28 10:14:47 +08008050 free_kvm_area();
8051}
8052
Avi Kivity6aa8b732006-12-10 02:21:36 -08008053/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008054 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
8055 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
8056 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03008057static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008058{
Wanpeng Lib31c1142018-03-12 04:53:04 -07008059 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02008060 grow_ple_window(vcpu);
8061
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08008062 /*
8063 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
8064 * VM-execution control is ignored if CPL > 0. OTOH, KVM
8065 * never set PAUSE_EXITING and just set PLE if supported,
8066 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
8067 */
8068 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008069 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008070}
8071
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008072static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08008073{
Kyle Huey6affcbe2016-11-29 12:40:40 -08008074 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08008075}
8076
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008077static int handle_mwait(struct kvm_vcpu *vcpu)
8078{
8079 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
8080 return handle_nop(vcpu);
8081}
8082
Jim Mattson45ec3682017-08-23 16:32:04 -07008083static int handle_invalid_op(struct kvm_vcpu *vcpu)
8084{
8085 kvm_queue_exception(vcpu, UD_VECTOR);
8086 return 1;
8087}
8088
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008089static int handle_monitor_trap(struct kvm_vcpu *vcpu)
8090{
8091 return 1;
8092}
8093
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008094static int handle_monitor(struct kvm_vcpu *vcpu)
8095{
8096 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
8097 return handle_nop(vcpu);
8098}
8099
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008100/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008101 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008102 * set the success or error code of an emulated VMX instruction (as specified
8103 * by Vol 2B, VMX Instruction Reference, "Conventions"), and skip the emulated
8104 * instruction.
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008105 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008106static int nested_vmx_succeed(struct kvm_vcpu *vcpu)
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008107{
8108 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
8109 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8110 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008111 return kvm_skip_emulated_instruction(vcpu);
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008112}
8113
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008114static int nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008115{
8116 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8117 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
8118 X86_EFLAGS_SF | X86_EFLAGS_OF))
8119 | X86_EFLAGS_CF);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008120 return kvm_skip_emulated_instruction(vcpu);
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008121}
8122
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008123static int nested_vmx_failValid(struct kvm_vcpu *vcpu,
8124 u32 vm_instruction_error)
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008125{
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02008126 struct vcpu_vmx *vmx = to_vmx(vcpu);
8127
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008128 /*
8129 * failValid writes the error number to the current VMCS, which
8130 * can't be done if there isn't a current VMCS.
8131 */
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02008132 if (vmx->nested.current_vmptr == -1ull && !vmx->nested.hv_evmcs)
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008133 return nested_vmx_failInvalid(vcpu);
8134
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008135 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8136 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8137 X86_EFLAGS_SF | X86_EFLAGS_OF))
8138 | X86_EFLAGS_ZF);
8139 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
8140 /*
8141 * We don't need to force a shadow sync because
8142 * VM_INSTRUCTION_ERROR is not shadowed
8143 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008144 return kvm_skip_emulated_instruction(vcpu);
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008145}
Abel Gordon145c28d2013-04-18 14:36:55 +03008146
Wincy Vanff651cb2014-12-11 08:52:58 +03008147static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
8148{
8149 /* TODO: not to reset guest simply here. */
8150 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02008151 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03008152}
8153
Jan Kiszkaf4124502014-03-07 20:03:13 +01008154static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
8155{
8156 struct vcpu_vmx *vmx =
8157 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
8158
8159 vmx->nested.preemption_timer_expired = true;
8160 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
8161 kvm_vcpu_kick(&vmx->vcpu);
8162
8163 return HRTIMER_NORESTART;
8164}
8165
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008166/*
Bandan Das19677e32014-05-06 02:19:15 -04008167 * Decode the memory-address operand of a vmx instruction, as recorded on an
8168 * exit caused by such an instruction (run by a guest hypervisor).
8169 * On success, returns 0. When the operand is invalid, returns 1 and throws
8170 * #UD or #GP.
8171 */
8172static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
8173 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008174 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04008175{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008176 gva_t off;
8177 bool exn;
8178 struct kvm_segment s;
8179
Bandan Das19677e32014-05-06 02:19:15 -04008180 /*
8181 * According to Vol. 3B, "Information for VM Exits Due to Instruction
8182 * Execution", on an exit, vmx_instruction_info holds most of the
8183 * addressing components of the operand. Only the displacement part
8184 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
8185 * For how an actual address is calculated from all these components,
8186 * refer to Vol. 1, "Operand Addressing".
8187 */
8188 int scaling = vmx_instruction_info & 3;
8189 int addr_size = (vmx_instruction_info >> 7) & 7;
8190 bool is_reg = vmx_instruction_info & (1u << 10);
8191 int seg_reg = (vmx_instruction_info >> 15) & 7;
8192 int index_reg = (vmx_instruction_info >> 18) & 0xf;
8193 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
8194 int base_reg = (vmx_instruction_info >> 23) & 0xf;
8195 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
8196
8197 if (is_reg) {
8198 kvm_queue_exception(vcpu, UD_VECTOR);
8199 return 1;
8200 }
8201
8202 /* Addr = segment_base + offset */
8203 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008204 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04008205 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008206 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04008207 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008208 off += kvm_register_read(vcpu, index_reg)<<scaling;
8209 vmx_get_segment(vcpu, &s, seg_reg);
8210 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04008211
8212 if (addr_size == 1) /* 32 bit */
8213 *ret &= 0xffffffff;
8214
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008215 /* Checks for #GP/#SS exceptions. */
8216 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008217 if (is_long_mode(vcpu)) {
8218 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
8219 * non-canonical form. This is the only check on the memory
8220 * destination for long mode!
8221 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08008222 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008223 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008224 /* Protected mode: apply checks for segment validity in the
8225 * following order:
8226 * - segment type check (#GP(0) may be thrown)
8227 * - usability check (#GP(0)/#SS(0))
8228 * - limit check (#GP(0)/#SS(0))
8229 */
8230 if (wr)
8231 /* #GP(0) if the destination operand is located in a
8232 * read-only data segment or any code segment.
8233 */
8234 exn = ((s.type & 0xa) == 0 || (s.type & 8));
8235 else
8236 /* #GP(0) if the source operand is located in an
8237 * execute-only code segment
8238 */
8239 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008240 if (exn) {
8241 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
8242 return 1;
8243 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008244 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
8245 */
8246 exn = (s.unusable != 0);
8247 /* Protected mode: #GP(0)/#SS(0) if the memory
8248 * operand is outside the segment limit.
8249 */
8250 exn = exn || (off + sizeof(u64) > s.limit);
8251 }
8252 if (exn) {
8253 kvm_queue_exception_e(vcpu,
8254 seg_reg == VCPU_SREG_SS ?
8255 SS_VECTOR : GP_VECTOR,
8256 0);
8257 return 1;
8258 }
8259
Bandan Das19677e32014-05-06 02:19:15 -04008260 return 0;
8261}
8262
Radim Krčmářcbf71272017-05-19 15:48:51 +02008263static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04008264{
8265 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04008266 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04008267
8268 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008269 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04008270 return 1;
8271
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008272 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04008273 kvm_inject_page_fault(vcpu, &e);
8274 return 1;
8275 }
8276
Bandan Das3573e222014-05-06 02:19:16 -04008277 return 0;
8278}
8279
Liran Alonabfc52c2018-06-23 02:35:13 +03008280/*
8281 * Allocate a shadow VMCS and associate it with the currently loaded
8282 * VMCS, unless such a shadow VMCS already exists. The newly allocated
8283 * VMCS is also VMCLEARed, so that it is ready for use.
8284 */
8285static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
8286{
8287 struct vcpu_vmx *vmx = to_vmx(vcpu);
8288 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
8289
8290 /*
8291 * We should allocate a shadow vmcs for vmcs01 only when L1
8292 * executes VMXON and free it when L1 executes VMXOFF.
8293 * As it is invalid to execute VMXON twice, we shouldn't reach
8294 * here when vmcs01 already have an allocated shadow vmcs.
8295 */
8296 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
8297
8298 if (!loaded_vmcs->shadow_vmcs) {
8299 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
8300 if (loaded_vmcs->shadow_vmcs)
8301 vmcs_clear(loaded_vmcs->shadow_vmcs);
8302 }
8303 return loaded_vmcs->shadow_vmcs;
8304}
8305
Jim Mattsone29acc52016-11-30 12:03:43 -08008306static int enter_vmx_operation(struct kvm_vcpu *vcpu)
8307{
8308 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008309 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08008310
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008311 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
8312 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06008313 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08008314
8315 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8316 if (!vmx->nested.cached_vmcs12)
8317 goto out_cached_vmcs12;
8318
Liran Alon61ada742018-06-23 02:35:08 +03008319 vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8320 if (!vmx->nested.cached_shadow_vmcs12)
8321 goto out_cached_shadow_vmcs12;
8322
Liran Alonabfc52c2018-06-23 02:35:13 +03008323 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
8324 goto out_shadow_vmcs;
Jim Mattsone29acc52016-11-30 12:03:43 -08008325
Jim Mattsone29acc52016-11-30 12:03:43 -08008326 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
8327 HRTIMER_MODE_REL_PINNED);
8328 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
8329
Roman Kagan63aff652018-07-19 21:59:07 +03008330 vmx->nested.vpid02 = allocate_vpid();
8331
Sean Christopherson9d6105b22018-09-26 09:23:51 -07008332 vmx->nested.vmcs02_initialized = false;
Jim Mattsone29acc52016-11-30 12:03:43 -08008333 vmx->nested.vmxon = true;
8334 return 0;
8335
8336out_shadow_vmcs:
Liran Alon61ada742018-06-23 02:35:08 +03008337 kfree(vmx->nested.cached_shadow_vmcs12);
8338
8339out_cached_shadow_vmcs12:
Jim Mattsone29acc52016-11-30 12:03:43 -08008340 kfree(vmx->nested.cached_vmcs12);
8341
8342out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06008343 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08008344
Jim Mattsonde3a0022017-11-27 17:22:25 -06008345out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08008346 return -ENOMEM;
8347}
8348
Bandan Das3573e222014-05-06 02:19:16 -04008349/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008350 * Emulate the VMXON instruction.
8351 * Currently, we just remember that VMX is active, and do not save or even
8352 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
8353 * do not currently need to store anything in that guest-allocated memory
8354 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
8355 * argument is different from the VMXON pointer (which the spec says they do).
8356 */
8357static int handle_vmon(struct kvm_vcpu *vcpu)
8358{
Jim Mattsone29acc52016-11-30 12:03:43 -08008359 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008360 gpa_t vmptr;
8361 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008362 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008363 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
8364 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008365
Jim Mattson70f3aac2017-04-26 08:53:46 -07008366 /*
8367 * The Intel VMX Instruction Reference lists a bunch of bits that are
8368 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
8369 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
8370 * Otherwise, we should fail with #UD. But most faulting conditions
8371 * have already been checked by hardware, prior to the VM-exit for
8372 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
8373 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008374 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07008375 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008376 kvm_queue_exception(vcpu, UD_VECTOR);
8377 return 1;
8378 }
8379
Felix Wilhelm727ba742018-06-11 09:43:44 +02008380 /* CPL=0 must be checked manually. */
8381 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008382 kvm_inject_gp(vcpu, 0);
Felix Wilhelm727ba742018-06-11 09:43:44 +02008383 return 1;
8384 }
8385
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008386 if (vmx->nested.vmxon)
8387 return nested_vmx_failValid(vcpu,
8388 VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008389
Haozhong Zhang3b840802016-06-22 14:59:54 +08008390 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008391 != VMXON_NEEDED_FEATURES) {
8392 kvm_inject_gp(vcpu, 0);
8393 return 1;
8394 }
8395
Radim Krčmářcbf71272017-05-19 15:48:51 +02008396 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08008397 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008398
8399 /*
8400 * SDM 3: 24.11.5
8401 * The first 4 bytes of VMXON region contain the supported
8402 * VMCS revision identifier
8403 *
8404 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8405 * which replaces physical address width with 32
8406 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008407 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu)))
8408 return nested_vmx_failInvalid(vcpu);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008409
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008410 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008411 if (is_error_page(page))
8412 return nested_vmx_failInvalid(vcpu);
8413
Radim Krčmářcbf71272017-05-19 15:48:51 +02008414 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8415 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008416 kvm_release_page_clean(page);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008417 return nested_vmx_failInvalid(vcpu);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008418 }
8419 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008420 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008421
8422 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08008423 ret = enter_vmx_operation(vcpu);
8424 if (ret)
8425 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008426
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008427 return nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008428}
8429
8430/*
8431 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8432 * for running VMX instructions (except VMXON, whose prerequisites are
8433 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008434 * Note that many of these exceptions have priority over VM exits, so they
8435 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008436 */
8437static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8438{
Jim Mattson70f3aac2017-04-26 08:53:46 -07008439 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008440 kvm_queue_exception(vcpu, UD_VECTOR);
8441 return 0;
8442 }
Jim Mattsone49fcb82018-07-27 13:44:45 -07008443
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008444 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008445 kvm_inject_gp(vcpu, 0);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008446 return 0;
8447 }
8448
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008449 return 1;
8450}
8451
David Matlack8ca44e82017-08-01 14:00:39 -07008452static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8453{
8454 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8455 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8456}
8457
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02008458static inline void nested_release_evmcs(struct kvm_vcpu *vcpu)
Abel Gordone7953d72013-04-18 14:37:55 +03008459{
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02008460 struct vcpu_vmx *vmx = to_vmx(vcpu);
8461
8462 if (!vmx->nested.hv_evmcs)
8463 return;
8464
8465 kunmap(vmx->nested.hv_evmcs_page);
8466 kvm_release_page_dirty(vmx->nested.hv_evmcs_page);
8467 vmx->nested.hv_evmcs_vmptr = -1ull;
8468 vmx->nested.hv_evmcs_page = NULL;
8469 vmx->nested.hv_evmcs = NULL;
8470}
8471
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008472static inline void nested_release_vmcs12(struct kvm_vcpu *vcpu)
Abel Gordone7953d72013-04-18 14:37:55 +03008473{
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008474 struct vcpu_vmx *vmx = to_vmx(vcpu);
8475
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008476 if (vmx->nested.current_vmptr == -1ull)
8477 return;
8478
Abel Gordon012f83c2013-04-18 14:39:25 +03008479 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008480 /* copy to memory all shadowed fields in case
8481 they were modified */
8482 copy_shadow_to_vmcs12(vmx);
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +02008483 vmx->nested.need_vmcs12_sync = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008484 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008485 }
Wincy Van705699a2015-02-03 23:58:17 +08008486 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008487
8488 /* Flush VMCS12 to guest memory */
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008489 kvm_vcpu_write_guest_page(vcpu,
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008490 vmx->nested.current_vmptr >> PAGE_SHIFT,
8491 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008492
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008493 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
8494
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008495 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008496}
8497
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008498/*
8499 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8500 * just stops using VMX.
8501 */
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008502static void free_nested(struct kvm_vcpu *vcpu)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008503{
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008504 struct vcpu_vmx *vmx = to_vmx(vcpu);
8505
Wanpeng Lib7455822017-11-22 14:04:00 -08008506 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008507 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008508
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008509 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008510 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008511 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008512 vmx->nested.posted_intr_nv = -1;
8513 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008514 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008515 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008516 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8517 free_vmcs(vmx->vmcs01.shadow_vmcs);
8518 vmx->vmcs01.shadow_vmcs = NULL;
8519 }
David Matlack4f2777b2016-07-13 17:16:37 -07008520 kfree(vmx->nested.cached_vmcs12);
Liran Alon61ada742018-06-23 02:35:08 +03008521 kfree(vmx->nested.cached_shadow_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008522 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008523 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008524 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008525 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008526 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008527 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008528 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008529 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008530 }
Wincy Van705699a2015-02-03 23:58:17 +08008531 if (vmx->nested.pi_desc_page) {
8532 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008533 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008534 vmx->nested.pi_desc_page = NULL;
8535 vmx->nested.pi_desc = NULL;
8536 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008537
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008538 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
8539
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02008540 nested_release_evmcs(vcpu);
8541
Jim Mattsonde3a0022017-11-27 17:22:25 -06008542 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008543}
8544
8545/* Emulate the VMXOFF instruction */
8546static int handle_vmoff(struct kvm_vcpu *vcpu)
8547{
8548 if (!nested_vmx_check_permission(vcpu))
8549 return 1;
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008550 free_nested(vcpu);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008551 return nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008552}
8553
Nadav Har'El27d6c862011-05-25 23:06:59 +03008554/* Emulate the VMCLEAR instruction */
8555static int handle_vmclear(struct kvm_vcpu *vcpu)
8556{
8557 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008558 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008559 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008560
8561 if (!nested_vmx_check_permission(vcpu))
8562 return 1;
8563
Radim Krčmářcbf71272017-05-19 15:48:51 +02008564 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008565 return 1;
8566
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008567 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu)))
8568 return nested_vmx_failValid(vcpu,
8569 VMXERR_VMCLEAR_INVALID_ADDRESS);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008570
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008571 if (vmptr == vmx->nested.vmxon_ptr)
8572 return nested_vmx_failValid(vcpu,
8573 VMXERR_VMCLEAR_VMXON_POINTER);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008574
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02008575 if (vmx->nested.hv_evmcs_page) {
8576 if (vmptr == vmx->nested.hv_evmcs_vmptr)
8577 nested_release_evmcs(vcpu);
8578 } else {
8579 if (vmptr == vmx->nested.current_vmptr)
8580 nested_release_vmcs12(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008581
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02008582 kvm_vcpu_write_guest(vcpu,
8583 vmptr + offsetof(struct vmcs12,
8584 launch_state),
8585 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008586 }
8587
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008588 return nested_vmx_succeed(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008589}
8590
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008591static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8592
8593/* Emulate the VMLAUNCH instruction */
8594static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8595{
8596 return nested_vmx_run(vcpu, true);
8597}
8598
8599/* Emulate the VMRESUME instruction */
8600static int handle_vmresume(struct kvm_vcpu *vcpu)
8601{
8602
8603 return nested_vmx_run(vcpu, false);
8604}
8605
Nadav Har'El49f705c2011-05-25 23:08:30 +03008606/*
8607 * Read a vmcs12 field. Since these can have varying lengths and we return
8608 * one type, we chose the biggest type (u64) and zero-extend the return value
8609 * to that size. Note that the caller, handle_vmread, might need to use only
8610 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8611 * 64-bit fields are to be returned).
8612 */
Liran Alone2536742018-06-23 02:35:02 +03008613static inline int vmcs12_read_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008614 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008615{
8616 short offset = vmcs_field_to_offset(field);
8617 char *p;
8618
8619 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008620 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008621
Liran Alone2536742018-06-23 02:35:02 +03008622 p = (char *)vmcs12 + offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008623
Jim Mattsond37f4262017-12-22 12:12:16 -08008624 switch (vmcs_field_width(field)) {
8625 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008626 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008627 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008628 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008629 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008630 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008631 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008632 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008633 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008634 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008635 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008636 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008637 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008638 WARN_ON(1);
8639 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008640 }
8641}
8642
Abel Gordon20b97fe2013-04-18 14:36:25 +03008643
Liran Alone2536742018-06-23 02:35:02 +03008644static inline int vmcs12_write_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008645 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008646 short offset = vmcs_field_to_offset(field);
Liran Alone2536742018-06-23 02:35:02 +03008647 char *p = (char *)vmcs12 + offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008648 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008649 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008650
Jim Mattsond37f4262017-12-22 12:12:16 -08008651 switch (vmcs_field_width(field)) {
8652 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008653 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008654 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008655 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008656 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008657 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008658 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008659 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008660 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008661 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008662 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008663 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008664 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008665 WARN_ON(1);
8666 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008667 }
8668
8669}
8670
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +02008671static int copy_enlightened_to_vmcs12(struct vcpu_vmx *vmx)
8672{
8673 struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
8674 struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
8675
8676 /* HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE */
8677 vmcs12->tpr_threshold = evmcs->tpr_threshold;
8678 vmcs12->guest_rip = evmcs->guest_rip;
8679
8680 if (unlikely(!(evmcs->hv_clean_fields &
8681 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC))) {
8682 vmcs12->guest_rsp = evmcs->guest_rsp;
8683 vmcs12->guest_rflags = evmcs->guest_rflags;
8684 vmcs12->guest_interruptibility_info =
8685 evmcs->guest_interruptibility_info;
8686 }
8687
8688 if (unlikely(!(evmcs->hv_clean_fields &
8689 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) {
8690 vmcs12->cpu_based_vm_exec_control =
8691 evmcs->cpu_based_vm_exec_control;
8692 }
8693
8694 if (unlikely(!(evmcs->hv_clean_fields &
8695 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) {
8696 vmcs12->exception_bitmap = evmcs->exception_bitmap;
8697 }
8698
8699 if (unlikely(!(evmcs->hv_clean_fields &
8700 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_ENTRY))) {
8701 vmcs12->vm_entry_controls = evmcs->vm_entry_controls;
8702 }
8703
8704 if (unlikely(!(evmcs->hv_clean_fields &
8705 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT))) {
8706 vmcs12->vm_entry_intr_info_field =
8707 evmcs->vm_entry_intr_info_field;
8708 vmcs12->vm_entry_exception_error_code =
8709 evmcs->vm_entry_exception_error_code;
8710 vmcs12->vm_entry_instruction_len =
8711 evmcs->vm_entry_instruction_len;
8712 }
8713
8714 if (unlikely(!(evmcs->hv_clean_fields &
8715 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) {
8716 vmcs12->host_ia32_pat = evmcs->host_ia32_pat;
8717 vmcs12->host_ia32_efer = evmcs->host_ia32_efer;
8718 vmcs12->host_cr0 = evmcs->host_cr0;
8719 vmcs12->host_cr3 = evmcs->host_cr3;
8720 vmcs12->host_cr4 = evmcs->host_cr4;
8721 vmcs12->host_ia32_sysenter_esp = evmcs->host_ia32_sysenter_esp;
8722 vmcs12->host_ia32_sysenter_eip = evmcs->host_ia32_sysenter_eip;
8723 vmcs12->host_rip = evmcs->host_rip;
8724 vmcs12->host_ia32_sysenter_cs = evmcs->host_ia32_sysenter_cs;
8725 vmcs12->host_es_selector = evmcs->host_es_selector;
8726 vmcs12->host_cs_selector = evmcs->host_cs_selector;
8727 vmcs12->host_ss_selector = evmcs->host_ss_selector;
8728 vmcs12->host_ds_selector = evmcs->host_ds_selector;
8729 vmcs12->host_fs_selector = evmcs->host_fs_selector;
8730 vmcs12->host_gs_selector = evmcs->host_gs_selector;
8731 vmcs12->host_tr_selector = evmcs->host_tr_selector;
8732 }
8733
8734 if (unlikely(!(evmcs->hv_clean_fields &
8735 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) {
8736 vmcs12->pin_based_vm_exec_control =
8737 evmcs->pin_based_vm_exec_control;
8738 vmcs12->vm_exit_controls = evmcs->vm_exit_controls;
8739 vmcs12->secondary_vm_exec_control =
8740 evmcs->secondary_vm_exec_control;
8741 }
8742
8743 if (unlikely(!(evmcs->hv_clean_fields &
8744 HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP))) {
8745 vmcs12->io_bitmap_a = evmcs->io_bitmap_a;
8746 vmcs12->io_bitmap_b = evmcs->io_bitmap_b;
8747 }
8748
8749 if (unlikely(!(evmcs->hv_clean_fields &
8750 HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP))) {
8751 vmcs12->msr_bitmap = evmcs->msr_bitmap;
8752 }
8753
8754 if (unlikely(!(evmcs->hv_clean_fields &
8755 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2))) {
8756 vmcs12->guest_es_base = evmcs->guest_es_base;
8757 vmcs12->guest_cs_base = evmcs->guest_cs_base;
8758 vmcs12->guest_ss_base = evmcs->guest_ss_base;
8759 vmcs12->guest_ds_base = evmcs->guest_ds_base;
8760 vmcs12->guest_fs_base = evmcs->guest_fs_base;
8761 vmcs12->guest_gs_base = evmcs->guest_gs_base;
8762 vmcs12->guest_ldtr_base = evmcs->guest_ldtr_base;
8763 vmcs12->guest_tr_base = evmcs->guest_tr_base;
8764 vmcs12->guest_gdtr_base = evmcs->guest_gdtr_base;
8765 vmcs12->guest_idtr_base = evmcs->guest_idtr_base;
8766 vmcs12->guest_es_limit = evmcs->guest_es_limit;
8767 vmcs12->guest_cs_limit = evmcs->guest_cs_limit;
8768 vmcs12->guest_ss_limit = evmcs->guest_ss_limit;
8769 vmcs12->guest_ds_limit = evmcs->guest_ds_limit;
8770 vmcs12->guest_fs_limit = evmcs->guest_fs_limit;
8771 vmcs12->guest_gs_limit = evmcs->guest_gs_limit;
8772 vmcs12->guest_ldtr_limit = evmcs->guest_ldtr_limit;
8773 vmcs12->guest_tr_limit = evmcs->guest_tr_limit;
8774 vmcs12->guest_gdtr_limit = evmcs->guest_gdtr_limit;
8775 vmcs12->guest_idtr_limit = evmcs->guest_idtr_limit;
8776 vmcs12->guest_es_ar_bytes = evmcs->guest_es_ar_bytes;
8777 vmcs12->guest_cs_ar_bytes = evmcs->guest_cs_ar_bytes;
8778 vmcs12->guest_ss_ar_bytes = evmcs->guest_ss_ar_bytes;
8779 vmcs12->guest_ds_ar_bytes = evmcs->guest_ds_ar_bytes;
8780 vmcs12->guest_fs_ar_bytes = evmcs->guest_fs_ar_bytes;
8781 vmcs12->guest_gs_ar_bytes = evmcs->guest_gs_ar_bytes;
8782 vmcs12->guest_ldtr_ar_bytes = evmcs->guest_ldtr_ar_bytes;
8783 vmcs12->guest_tr_ar_bytes = evmcs->guest_tr_ar_bytes;
8784 vmcs12->guest_es_selector = evmcs->guest_es_selector;
8785 vmcs12->guest_cs_selector = evmcs->guest_cs_selector;
8786 vmcs12->guest_ss_selector = evmcs->guest_ss_selector;
8787 vmcs12->guest_ds_selector = evmcs->guest_ds_selector;
8788 vmcs12->guest_fs_selector = evmcs->guest_fs_selector;
8789 vmcs12->guest_gs_selector = evmcs->guest_gs_selector;
8790 vmcs12->guest_ldtr_selector = evmcs->guest_ldtr_selector;
8791 vmcs12->guest_tr_selector = evmcs->guest_tr_selector;
8792 }
8793
8794 if (unlikely(!(evmcs->hv_clean_fields &
8795 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2))) {
8796 vmcs12->tsc_offset = evmcs->tsc_offset;
8797 vmcs12->virtual_apic_page_addr = evmcs->virtual_apic_page_addr;
8798 vmcs12->xss_exit_bitmap = evmcs->xss_exit_bitmap;
8799 }
8800
8801 if (unlikely(!(evmcs->hv_clean_fields &
8802 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR))) {
8803 vmcs12->cr0_guest_host_mask = evmcs->cr0_guest_host_mask;
8804 vmcs12->cr4_guest_host_mask = evmcs->cr4_guest_host_mask;
8805 vmcs12->cr0_read_shadow = evmcs->cr0_read_shadow;
8806 vmcs12->cr4_read_shadow = evmcs->cr4_read_shadow;
8807 vmcs12->guest_cr0 = evmcs->guest_cr0;
8808 vmcs12->guest_cr3 = evmcs->guest_cr3;
8809 vmcs12->guest_cr4 = evmcs->guest_cr4;
8810 vmcs12->guest_dr7 = evmcs->guest_dr7;
8811 }
8812
8813 if (unlikely(!(evmcs->hv_clean_fields &
8814 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER))) {
8815 vmcs12->host_fs_base = evmcs->host_fs_base;
8816 vmcs12->host_gs_base = evmcs->host_gs_base;
8817 vmcs12->host_tr_base = evmcs->host_tr_base;
8818 vmcs12->host_gdtr_base = evmcs->host_gdtr_base;
8819 vmcs12->host_idtr_base = evmcs->host_idtr_base;
8820 vmcs12->host_rsp = evmcs->host_rsp;
8821 }
8822
8823 if (unlikely(!(evmcs->hv_clean_fields &
8824 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT))) {
8825 vmcs12->ept_pointer = evmcs->ept_pointer;
8826 vmcs12->virtual_processor_id = evmcs->virtual_processor_id;
8827 }
8828
8829 if (unlikely(!(evmcs->hv_clean_fields &
8830 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1))) {
8831 vmcs12->vmcs_link_pointer = evmcs->vmcs_link_pointer;
8832 vmcs12->guest_ia32_debugctl = evmcs->guest_ia32_debugctl;
8833 vmcs12->guest_ia32_pat = evmcs->guest_ia32_pat;
8834 vmcs12->guest_ia32_efer = evmcs->guest_ia32_efer;
8835 vmcs12->guest_pdptr0 = evmcs->guest_pdptr0;
8836 vmcs12->guest_pdptr1 = evmcs->guest_pdptr1;
8837 vmcs12->guest_pdptr2 = evmcs->guest_pdptr2;
8838 vmcs12->guest_pdptr3 = evmcs->guest_pdptr3;
8839 vmcs12->guest_pending_dbg_exceptions =
8840 evmcs->guest_pending_dbg_exceptions;
8841 vmcs12->guest_sysenter_esp = evmcs->guest_sysenter_esp;
8842 vmcs12->guest_sysenter_eip = evmcs->guest_sysenter_eip;
8843 vmcs12->guest_bndcfgs = evmcs->guest_bndcfgs;
8844 vmcs12->guest_activity_state = evmcs->guest_activity_state;
8845 vmcs12->guest_sysenter_cs = evmcs->guest_sysenter_cs;
8846 }
8847
8848 /*
8849 * Not used?
8850 * vmcs12->vm_exit_msr_store_addr = evmcs->vm_exit_msr_store_addr;
8851 * vmcs12->vm_exit_msr_load_addr = evmcs->vm_exit_msr_load_addr;
8852 * vmcs12->vm_entry_msr_load_addr = evmcs->vm_entry_msr_load_addr;
8853 * vmcs12->cr3_target_value0 = evmcs->cr3_target_value0;
8854 * vmcs12->cr3_target_value1 = evmcs->cr3_target_value1;
8855 * vmcs12->cr3_target_value2 = evmcs->cr3_target_value2;
8856 * vmcs12->cr3_target_value3 = evmcs->cr3_target_value3;
8857 * vmcs12->page_fault_error_code_mask =
8858 * evmcs->page_fault_error_code_mask;
8859 * vmcs12->page_fault_error_code_match =
8860 * evmcs->page_fault_error_code_match;
8861 * vmcs12->cr3_target_count = evmcs->cr3_target_count;
8862 * vmcs12->vm_exit_msr_store_count = evmcs->vm_exit_msr_store_count;
8863 * vmcs12->vm_exit_msr_load_count = evmcs->vm_exit_msr_load_count;
8864 * vmcs12->vm_entry_msr_load_count = evmcs->vm_entry_msr_load_count;
8865 */
8866
8867 /*
8868 * Read only fields:
8869 * vmcs12->guest_physical_address = evmcs->guest_physical_address;
8870 * vmcs12->vm_instruction_error = evmcs->vm_instruction_error;
8871 * vmcs12->vm_exit_reason = evmcs->vm_exit_reason;
8872 * vmcs12->vm_exit_intr_info = evmcs->vm_exit_intr_info;
8873 * vmcs12->vm_exit_intr_error_code = evmcs->vm_exit_intr_error_code;
8874 * vmcs12->idt_vectoring_info_field = evmcs->idt_vectoring_info_field;
8875 * vmcs12->idt_vectoring_error_code = evmcs->idt_vectoring_error_code;
8876 * vmcs12->vm_exit_instruction_len = evmcs->vm_exit_instruction_len;
8877 * vmcs12->vmx_instruction_info = evmcs->vmx_instruction_info;
8878 * vmcs12->exit_qualification = evmcs->exit_qualification;
8879 * vmcs12->guest_linear_address = evmcs->guest_linear_address;
8880 *
8881 * Not present in struct vmcs12:
8882 * vmcs12->exit_io_instruction_ecx = evmcs->exit_io_instruction_ecx;
8883 * vmcs12->exit_io_instruction_esi = evmcs->exit_io_instruction_esi;
8884 * vmcs12->exit_io_instruction_edi = evmcs->exit_io_instruction_edi;
8885 * vmcs12->exit_io_instruction_eip = evmcs->exit_io_instruction_eip;
8886 */
8887
8888 return 0;
8889}
8890
8891static int copy_vmcs12_to_enlightened(struct vcpu_vmx *vmx)
8892{
8893 struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
8894 struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
8895
8896 /*
8897 * Should not be changed by KVM:
8898 *
8899 * evmcs->host_es_selector = vmcs12->host_es_selector;
8900 * evmcs->host_cs_selector = vmcs12->host_cs_selector;
8901 * evmcs->host_ss_selector = vmcs12->host_ss_selector;
8902 * evmcs->host_ds_selector = vmcs12->host_ds_selector;
8903 * evmcs->host_fs_selector = vmcs12->host_fs_selector;
8904 * evmcs->host_gs_selector = vmcs12->host_gs_selector;
8905 * evmcs->host_tr_selector = vmcs12->host_tr_selector;
8906 * evmcs->host_ia32_pat = vmcs12->host_ia32_pat;
8907 * evmcs->host_ia32_efer = vmcs12->host_ia32_efer;
8908 * evmcs->host_cr0 = vmcs12->host_cr0;
8909 * evmcs->host_cr3 = vmcs12->host_cr3;
8910 * evmcs->host_cr4 = vmcs12->host_cr4;
8911 * evmcs->host_ia32_sysenter_esp = vmcs12->host_ia32_sysenter_esp;
8912 * evmcs->host_ia32_sysenter_eip = vmcs12->host_ia32_sysenter_eip;
8913 * evmcs->host_rip = vmcs12->host_rip;
8914 * evmcs->host_ia32_sysenter_cs = vmcs12->host_ia32_sysenter_cs;
8915 * evmcs->host_fs_base = vmcs12->host_fs_base;
8916 * evmcs->host_gs_base = vmcs12->host_gs_base;
8917 * evmcs->host_tr_base = vmcs12->host_tr_base;
8918 * evmcs->host_gdtr_base = vmcs12->host_gdtr_base;
8919 * evmcs->host_idtr_base = vmcs12->host_idtr_base;
8920 * evmcs->host_rsp = vmcs12->host_rsp;
8921 * sync_vmcs12() doesn't read these:
8922 * evmcs->io_bitmap_a = vmcs12->io_bitmap_a;
8923 * evmcs->io_bitmap_b = vmcs12->io_bitmap_b;
8924 * evmcs->msr_bitmap = vmcs12->msr_bitmap;
8925 * evmcs->ept_pointer = vmcs12->ept_pointer;
8926 * evmcs->xss_exit_bitmap = vmcs12->xss_exit_bitmap;
8927 * evmcs->vm_exit_msr_store_addr = vmcs12->vm_exit_msr_store_addr;
8928 * evmcs->vm_exit_msr_load_addr = vmcs12->vm_exit_msr_load_addr;
8929 * evmcs->vm_entry_msr_load_addr = vmcs12->vm_entry_msr_load_addr;
8930 * evmcs->cr3_target_value0 = vmcs12->cr3_target_value0;
8931 * evmcs->cr3_target_value1 = vmcs12->cr3_target_value1;
8932 * evmcs->cr3_target_value2 = vmcs12->cr3_target_value2;
8933 * evmcs->cr3_target_value3 = vmcs12->cr3_target_value3;
8934 * evmcs->tpr_threshold = vmcs12->tpr_threshold;
8935 * evmcs->virtual_processor_id = vmcs12->virtual_processor_id;
8936 * evmcs->exception_bitmap = vmcs12->exception_bitmap;
8937 * evmcs->vmcs_link_pointer = vmcs12->vmcs_link_pointer;
8938 * evmcs->pin_based_vm_exec_control = vmcs12->pin_based_vm_exec_control;
8939 * evmcs->vm_exit_controls = vmcs12->vm_exit_controls;
8940 * evmcs->secondary_vm_exec_control = vmcs12->secondary_vm_exec_control;
8941 * evmcs->page_fault_error_code_mask =
8942 * vmcs12->page_fault_error_code_mask;
8943 * evmcs->page_fault_error_code_match =
8944 * vmcs12->page_fault_error_code_match;
8945 * evmcs->cr3_target_count = vmcs12->cr3_target_count;
8946 * evmcs->virtual_apic_page_addr = vmcs12->virtual_apic_page_addr;
8947 * evmcs->tsc_offset = vmcs12->tsc_offset;
8948 * evmcs->guest_ia32_debugctl = vmcs12->guest_ia32_debugctl;
8949 * evmcs->cr0_guest_host_mask = vmcs12->cr0_guest_host_mask;
8950 * evmcs->cr4_guest_host_mask = vmcs12->cr4_guest_host_mask;
8951 * evmcs->cr0_read_shadow = vmcs12->cr0_read_shadow;
8952 * evmcs->cr4_read_shadow = vmcs12->cr4_read_shadow;
8953 * evmcs->vm_exit_msr_store_count = vmcs12->vm_exit_msr_store_count;
8954 * evmcs->vm_exit_msr_load_count = vmcs12->vm_exit_msr_load_count;
8955 * evmcs->vm_entry_msr_load_count = vmcs12->vm_entry_msr_load_count;
8956 *
8957 * Not present in struct vmcs12:
8958 * evmcs->exit_io_instruction_ecx = vmcs12->exit_io_instruction_ecx;
8959 * evmcs->exit_io_instruction_esi = vmcs12->exit_io_instruction_esi;
8960 * evmcs->exit_io_instruction_edi = vmcs12->exit_io_instruction_edi;
8961 * evmcs->exit_io_instruction_eip = vmcs12->exit_io_instruction_eip;
8962 */
8963
8964 evmcs->guest_es_selector = vmcs12->guest_es_selector;
8965 evmcs->guest_cs_selector = vmcs12->guest_cs_selector;
8966 evmcs->guest_ss_selector = vmcs12->guest_ss_selector;
8967 evmcs->guest_ds_selector = vmcs12->guest_ds_selector;
8968 evmcs->guest_fs_selector = vmcs12->guest_fs_selector;
8969 evmcs->guest_gs_selector = vmcs12->guest_gs_selector;
8970 evmcs->guest_ldtr_selector = vmcs12->guest_ldtr_selector;
8971 evmcs->guest_tr_selector = vmcs12->guest_tr_selector;
8972
8973 evmcs->guest_es_limit = vmcs12->guest_es_limit;
8974 evmcs->guest_cs_limit = vmcs12->guest_cs_limit;
8975 evmcs->guest_ss_limit = vmcs12->guest_ss_limit;
8976 evmcs->guest_ds_limit = vmcs12->guest_ds_limit;
8977 evmcs->guest_fs_limit = vmcs12->guest_fs_limit;
8978 evmcs->guest_gs_limit = vmcs12->guest_gs_limit;
8979 evmcs->guest_ldtr_limit = vmcs12->guest_ldtr_limit;
8980 evmcs->guest_tr_limit = vmcs12->guest_tr_limit;
8981 evmcs->guest_gdtr_limit = vmcs12->guest_gdtr_limit;
8982 evmcs->guest_idtr_limit = vmcs12->guest_idtr_limit;
8983
8984 evmcs->guest_es_ar_bytes = vmcs12->guest_es_ar_bytes;
8985 evmcs->guest_cs_ar_bytes = vmcs12->guest_cs_ar_bytes;
8986 evmcs->guest_ss_ar_bytes = vmcs12->guest_ss_ar_bytes;
8987 evmcs->guest_ds_ar_bytes = vmcs12->guest_ds_ar_bytes;
8988 evmcs->guest_fs_ar_bytes = vmcs12->guest_fs_ar_bytes;
8989 evmcs->guest_gs_ar_bytes = vmcs12->guest_gs_ar_bytes;
8990 evmcs->guest_ldtr_ar_bytes = vmcs12->guest_ldtr_ar_bytes;
8991 evmcs->guest_tr_ar_bytes = vmcs12->guest_tr_ar_bytes;
8992
8993 evmcs->guest_es_base = vmcs12->guest_es_base;
8994 evmcs->guest_cs_base = vmcs12->guest_cs_base;
8995 evmcs->guest_ss_base = vmcs12->guest_ss_base;
8996 evmcs->guest_ds_base = vmcs12->guest_ds_base;
8997 evmcs->guest_fs_base = vmcs12->guest_fs_base;
8998 evmcs->guest_gs_base = vmcs12->guest_gs_base;
8999 evmcs->guest_ldtr_base = vmcs12->guest_ldtr_base;
9000 evmcs->guest_tr_base = vmcs12->guest_tr_base;
9001 evmcs->guest_gdtr_base = vmcs12->guest_gdtr_base;
9002 evmcs->guest_idtr_base = vmcs12->guest_idtr_base;
9003
9004 evmcs->guest_ia32_pat = vmcs12->guest_ia32_pat;
9005 evmcs->guest_ia32_efer = vmcs12->guest_ia32_efer;
9006
9007 evmcs->guest_pdptr0 = vmcs12->guest_pdptr0;
9008 evmcs->guest_pdptr1 = vmcs12->guest_pdptr1;
9009 evmcs->guest_pdptr2 = vmcs12->guest_pdptr2;
9010 evmcs->guest_pdptr3 = vmcs12->guest_pdptr3;
9011
9012 evmcs->guest_pending_dbg_exceptions =
9013 vmcs12->guest_pending_dbg_exceptions;
9014 evmcs->guest_sysenter_esp = vmcs12->guest_sysenter_esp;
9015 evmcs->guest_sysenter_eip = vmcs12->guest_sysenter_eip;
9016
9017 evmcs->guest_activity_state = vmcs12->guest_activity_state;
9018 evmcs->guest_sysenter_cs = vmcs12->guest_sysenter_cs;
9019
9020 evmcs->guest_cr0 = vmcs12->guest_cr0;
9021 evmcs->guest_cr3 = vmcs12->guest_cr3;
9022 evmcs->guest_cr4 = vmcs12->guest_cr4;
9023 evmcs->guest_dr7 = vmcs12->guest_dr7;
9024
9025 evmcs->guest_physical_address = vmcs12->guest_physical_address;
9026
9027 evmcs->vm_instruction_error = vmcs12->vm_instruction_error;
9028 evmcs->vm_exit_reason = vmcs12->vm_exit_reason;
9029 evmcs->vm_exit_intr_info = vmcs12->vm_exit_intr_info;
9030 evmcs->vm_exit_intr_error_code = vmcs12->vm_exit_intr_error_code;
9031 evmcs->idt_vectoring_info_field = vmcs12->idt_vectoring_info_field;
9032 evmcs->idt_vectoring_error_code = vmcs12->idt_vectoring_error_code;
9033 evmcs->vm_exit_instruction_len = vmcs12->vm_exit_instruction_len;
9034 evmcs->vmx_instruction_info = vmcs12->vmx_instruction_info;
9035
9036 evmcs->exit_qualification = vmcs12->exit_qualification;
9037
9038 evmcs->guest_linear_address = vmcs12->guest_linear_address;
9039 evmcs->guest_rsp = vmcs12->guest_rsp;
9040 evmcs->guest_rflags = vmcs12->guest_rflags;
9041
9042 evmcs->guest_interruptibility_info =
9043 vmcs12->guest_interruptibility_info;
9044 evmcs->cpu_based_vm_exec_control = vmcs12->cpu_based_vm_exec_control;
9045 evmcs->vm_entry_controls = vmcs12->vm_entry_controls;
9046 evmcs->vm_entry_intr_info_field = vmcs12->vm_entry_intr_info_field;
9047 evmcs->vm_entry_exception_error_code =
9048 vmcs12->vm_entry_exception_error_code;
9049 evmcs->vm_entry_instruction_len = vmcs12->vm_entry_instruction_len;
9050
9051 evmcs->guest_rip = vmcs12->guest_rip;
9052
9053 evmcs->guest_bndcfgs = vmcs12->guest_bndcfgs;
9054
9055 return 0;
9056}
9057
Jim Mattsonf4160e42018-05-29 09:11:33 -07009058/*
9059 * Copy the writable VMCS shadow fields back to the VMCS12, in case
9060 * they have been modified by the L1 guest. Note that the "read-only"
9061 * VM-exit information fields are actually writable if the vCPU is
9062 * configured to support "VMWRITE to any supported field in the VMCS."
9063 */
Abel Gordon16f5b902013-04-18 14:38:25 +03009064static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
9065{
Jim Mattsonf4160e42018-05-29 09:11:33 -07009066 const u16 *fields[] = {
9067 shadow_read_write_fields,
9068 shadow_read_only_fields
9069 };
9070 const int max_fields[] = {
9071 max_shadow_read_write_fields,
9072 max_shadow_read_only_fields
9073 };
9074 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03009075 unsigned long field;
9076 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07009077 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03009078
Jan Kiszka282da872014-10-08 18:05:39 +02009079 preempt_disable();
9080
Abel Gordon16f5b902013-04-18 14:38:25 +03009081 vmcs_load(shadow_vmcs);
9082
Jim Mattsonf4160e42018-05-29 09:11:33 -07009083 for (q = 0; q < ARRAY_SIZE(fields); q++) {
9084 for (i = 0; i < max_fields[q]; i++) {
9085 field = fields[q][i];
9086 field_value = __vmcs_readl(field);
Liran Alone2536742018-06-23 02:35:02 +03009087 vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
Jim Mattsonf4160e42018-05-29 09:11:33 -07009088 }
9089 /*
9090 * Skip the VM-exit information fields if they are read-only.
9091 */
9092 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
9093 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03009094 }
9095
9096 vmcs_clear(shadow_vmcs);
9097 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02009098
9099 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03009100}
9101
Abel Gordonc3114422013-04-18 14:38:55 +03009102static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
9103{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01009104 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02009105 shadow_read_write_fields,
9106 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03009107 };
Mathias Krausec2bae892013-06-26 20:36:21 +02009108 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03009109 max_shadow_read_write_fields,
9110 max_shadow_read_only_fields
9111 };
9112 int i, q;
9113 unsigned long field;
9114 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07009115 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03009116
9117 vmcs_load(shadow_vmcs);
9118
Mathias Krausec2bae892013-06-26 20:36:21 +02009119 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03009120 for (i = 0; i < max_fields[q]; i++) {
9121 field = fields[q][i];
Liran Alone2536742018-06-23 02:35:02 +03009122 vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01009123 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03009124 }
9125 }
9126
9127 vmcs_clear(shadow_vmcs);
9128 vmcs_load(vmx->loaded_vmcs->vmcs);
9129}
9130
Nadav Har'El49f705c2011-05-25 23:08:30 +03009131static int handle_vmread(struct kvm_vcpu *vcpu)
9132{
9133 unsigned long field;
9134 u64 field_value;
9135 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9136 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9137 gva_t gva = 0;
Liran Alon6d894f42018-06-23 02:35:09 +03009138 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03009139
Kyle Hueyeb277562016-11-29 12:40:39 -08009140 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03009141 return 1;
9142
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009143 if (to_vmx(vcpu)->nested.current_vmptr == -1ull)
9144 return nested_vmx_failInvalid(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08009145
Liran Alon6d894f42018-06-23 02:35:09 +03009146 if (!is_guest_mode(vcpu))
9147 vmcs12 = get_vmcs12(vcpu);
9148 else {
9149 /*
9150 * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
9151 * to shadowed-field sets the ALU flags for VMfailInvalid.
9152 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009153 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull)
9154 return nested_vmx_failInvalid(vcpu);
Liran Alon6d894f42018-06-23 02:35:09 +03009155 vmcs12 = get_shadow_vmcs12(vcpu);
9156 }
9157
Nadav Har'El49f705c2011-05-25 23:08:30 +03009158 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03009159 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03009160 /* Read the field, zero-extended to a u64 field_value */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009161 if (vmcs12_read_any(vmcs12, field, &field_value) < 0)
9162 return nested_vmx_failValid(vcpu,
9163 VMXERR_UNSUPPORTED_VMCS_COMPONENT);
9164
Nadav Har'El49f705c2011-05-25 23:08:30 +03009165 /*
9166 * Now copy part of this value to register or memory, as requested.
9167 * Note that the number of bits actually copied is 32 or 64 depending
9168 * on the guest's mode (32 or 64 bit), not on the given field's length.
9169 */
9170 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03009171 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03009172 field_value);
9173 } else {
9174 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00009175 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03009176 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02009177 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02009178 kvm_write_guest_virt_system(vcpu, gva, &field_value,
9179 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03009180 }
9181
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009182 return nested_vmx_succeed(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03009183}
9184
9185
9186static int handle_vmwrite(struct kvm_vcpu *vcpu)
9187{
9188 unsigned long field;
9189 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01009190 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03009191 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9192 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01009193
Nadav Har'El49f705c2011-05-25 23:08:30 +03009194 /* The value to write might be 32 or 64 bits, depending on L1's long
9195 * mode, and eventually we need to write that into a field of several
9196 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08009197 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03009198 * bits into the vmcs12 field.
9199 */
9200 u64 field_value = 0;
9201 struct x86_exception e;
Liran Alon6d894f42018-06-23 02:35:09 +03009202 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03009203
Kyle Hueyeb277562016-11-29 12:40:39 -08009204 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03009205 return 1;
9206
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009207 if (vmx->nested.current_vmptr == -1ull)
9208 return nested_vmx_failInvalid(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08009209
Nadav Har'El49f705c2011-05-25 23:08:30 +03009210 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03009211 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009212 (((vmx_instruction_info) >> 3) & 0xf));
9213 else {
9214 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00009215 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03009216 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02009217 if (kvm_read_guest_virt(vcpu, gva, &field_value,
9218 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03009219 kvm_inject_page_fault(vcpu, &e);
9220 return 1;
9221 }
9222 }
9223
9224
Nadav Amit27e6fb52014-06-18 17:19:26 +03009225 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07009226 /*
9227 * If the vCPU supports "VMWRITE to any supported field in the
9228 * VMCS," then the "read-only" fields are actually read/write.
9229 */
9230 if (vmcs_field_readonly(field) &&
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009231 !nested_cpu_has_vmwrite_any_field(vcpu))
9232 return nested_vmx_failValid(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009233 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Nadav Har'El49f705c2011-05-25 23:08:30 +03009234
Liran Alon6d894f42018-06-23 02:35:09 +03009235 if (!is_guest_mode(vcpu))
9236 vmcs12 = get_vmcs12(vcpu);
9237 else {
9238 /*
9239 * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
9240 * to shadowed-field sets the ALU flags for VMfailInvalid.
9241 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009242 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull)
9243 return nested_vmx_failInvalid(vcpu);
Liran Alon6d894f42018-06-23 02:35:09 +03009244 vmcs12 = get_shadow_vmcs12(vcpu);
Liran Alon6d894f42018-06-23 02:35:09 +03009245 }
9246
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009247 if (vmcs12_write_any(vmcs12, field, field_value) < 0)
9248 return nested_vmx_failValid(vcpu,
9249 VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Nadav Har'El49f705c2011-05-25 23:08:30 +03009250
Liran Alon6d894f42018-06-23 02:35:09 +03009251 /*
9252 * Do not track vmcs12 dirty-state if in guest-mode
9253 * as we actually dirty shadow vmcs12 instead of vmcs12.
9254 */
9255 if (!is_guest_mode(vcpu)) {
9256 switch (field) {
Paolo Bonzini74a497f2017-12-20 13:55:39 +01009257#define SHADOW_FIELD_RW(x) case x:
9258#include "vmx_shadow_fields.h"
Liran Alon6d894f42018-06-23 02:35:09 +03009259 /*
9260 * The fields that can be updated by L1 without a vmexit are
9261 * always updated in the vmcs02, the others go down the slow
9262 * path of prepare_vmcs02.
9263 */
9264 break;
9265 default:
9266 vmx->nested.dirty_vmcs12 = true;
9267 break;
9268 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01009269 }
9270
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009271 return nested_vmx_succeed(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03009272}
9273
Jim Mattsona8bc2842016-11-30 12:03:44 -08009274static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
9275{
9276 vmx->nested.current_vmptr = vmptr;
9277 if (enable_shadow_vmcs) {
9278 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
9279 SECONDARY_EXEC_SHADOW_VMCS);
9280 vmcs_write64(VMCS_LINK_POINTER,
9281 __pa(vmx->vmcs01.shadow_vmcs));
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +02009282 vmx->nested.need_vmcs12_sync = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08009283 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01009284 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08009285}
9286
Nadav Har'El63846662011-05-25 23:07:29 +03009287/* Emulate the VMPTRLD instruction */
9288static int handle_vmptrld(struct kvm_vcpu *vcpu)
9289{
9290 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03009291 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03009292
9293 if (!nested_vmx_check_permission(vcpu))
9294 return 1;
9295
Radim Krčmářcbf71272017-05-19 15:48:51 +02009296 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03009297 return 1;
9298
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009299 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu)))
9300 return nested_vmx_failValid(vcpu,
9301 VMXERR_VMPTRLD_INVALID_ADDRESS);
Radim Krčmářcbf71272017-05-19 15:48:51 +02009302
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009303 if (vmptr == vmx->nested.vmxon_ptr)
9304 return nested_vmx_failValid(vcpu,
9305 VMXERR_VMPTRLD_VMXON_POINTER);
Radim Krčmářcbf71272017-05-19 15:48:51 +02009306
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02009307 /* Forbid normal VMPTRLD if Enlightened version was used */
9308 if (vmx->nested.hv_evmcs)
9309 return 1;
Nadav Har'El0140cae2011-05-25 23:06:28 +03009310
Nadav Har'El63846662011-05-25 23:07:29 +03009311 if (vmx->nested.current_vmptr != vmptr) {
9312 struct vmcs12 *new_vmcs12;
9313 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009314 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009315 if (is_error_page(page))
9316 return nested_vmx_failInvalid(vcpu);
9317
Nadav Har'El63846662011-05-25 23:07:29 +03009318 new_vmcs12 = kmap(page);
Liran Alon392b2f22018-06-23 02:35:01 +03009319 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
Liran Alonfa97d7d2018-07-18 14:07:59 +02009320 (new_vmcs12->hdr.shadow_vmcs &&
9321 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
Nadav Har'El63846662011-05-25 23:07:29 +03009322 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02009323 kvm_release_page_clean(page);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009324 return nested_vmx_failValid(vcpu,
Nadav Har'El63846662011-05-25 23:07:29 +03009325 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Nadav Har'El63846662011-05-25 23:07:29 +03009326 }
Nadav Har'El63846662011-05-25 23:07:29 +03009327
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02009328 nested_release_vmcs12(vcpu);
9329
David Matlack4f2777b2016-07-13 17:16:37 -07009330 /*
9331 * Load VMCS12 from guest memory since it is not already
9332 * cached.
9333 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02009334 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
9335 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02009336 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02009337
Jim Mattsona8bc2842016-11-30 12:03:44 -08009338 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03009339 }
9340
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009341 return nested_vmx_succeed(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03009342}
9343
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02009344/*
9345 * This is an equivalent of the nested hypervisor executing the vmptrld
9346 * instruction.
9347 */
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02009348static int nested_vmx_handle_enlightened_vmptrld(struct kvm_vcpu *vcpu,
9349 bool from_launch)
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02009350{
9351 struct vcpu_vmx *vmx = to_vmx(vcpu);
9352 struct hv_vp_assist_page assist_page;
9353
9354 if (likely(!vmx->nested.enlightened_vmcs_enabled))
9355 return 1;
9356
9357 if (unlikely(!kvm_hv_get_assist_page(vcpu, &assist_page)))
9358 return 1;
9359
9360 if (unlikely(!assist_page.enlighten_vmentry))
9361 return 1;
9362
9363 if (unlikely(assist_page.current_nested_vmcs !=
9364 vmx->nested.hv_evmcs_vmptr)) {
9365
9366 if (!vmx->nested.hv_evmcs)
9367 vmx->nested.current_vmptr = -1ull;
9368
9369 nested_release_evmcs(vcpu);
9370
9371 vmx->nested.hv_evmcs_page = kvm_vcpu_gpa_to_page(
9372 vcpu, assist_page.current_nested_vmcs);
9373
9374 if (unlikely(is_error_page(vmx->nested.hv_evmcs_page)))
9375 return 0;
9376
9377 vmx->nested.hv_evmcs = kmap(vmx->nested.hv_evmcs_page);
9378
Liran Alon72aeb602018-11-01 10:57:39 +02009379 /*
9380 * Currently, KVM only supports eVMCS version 1
9381 * (== KVM_EVMCS_VERSION) and thus we expect guest to set this
9382 * value to first u32 field of eVMCS which should specify eVMCS
9383 * VersionNumber.
9384 *
9385 * Guest should be aware of supported eVMCS versions by host by
9386 * examining CPUID.0x4000000A.EAX[0:15]. Host userspace VMM is
9387 * expected to set this CPUID leaf according to the value
9388 * returned in vmcs_version from nested_enable_evmcs().
9389 *
9390 * However, it turns out that Microsoft Hyper-V fails to comply
9391 * to their own invented interface: When Hyper-V use eVMCS, it
9392 * just sets first u32 field of eVMCS to revision_id specified
9393 * in MSR_IA32_VMX_BASIC. Instead of used eVMCS version number
9394 * which is one of the supported versions specified in
9395 * CPUID.0x4000000A.EAX[0:15].
9396 *
9397 * To overcome Hyper-V bug, we accept here either a supported
9398 * eVMCS version or VMCS12 revision_id as valid values for first
9399 * u32 field of eVMCS.
9400 */
9401 if ((vmx->nested.hv_evmcs->revision_id != KVM_EVMCS_VERSION) &&
9402 (vmx->nested.hv_evmcs->revision_id != VMCS12_REVISION)) {
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02009403 nested_release_evmcs(vcpu);
9404 return 0;
9405 }
9406
9407 vmx->nested.dirty_vmcs12 = true;
9408 /*
9409 * As we keep L2 state for one guest only 'hv_clean_fields' mask
9410 * can't be used when we switch between them. Reset it here for
9411 * simplicity.
9412 */
9413 vmx->nested.hv_evmcs->hv_clean_fields &=
9414 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
9415 vmx->nested.hv_evmcs_vmptr = assist_page.current_nested_vmcs;
9416
9417 /*
9418 * Unlike normal vmcs12, enlightened vmcs12 is not fully
9419 * reloaded from guest's memory (read only fields, fields not
9420 * present in struct hv_enlightened_vmcs, ...). Make sure there
9421 * are no leftovers.
9422 */
Liran Alon52ad7eb2018-11-13 17:44:46 +02009423 if (from_launch) {
9424 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9425 memset(vmcs12, 0, sizeof(*vmcs12));
9426 vmcs12->hdr.revision_id = VMCS12_REVISION;
9427 }
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02009428
9429 }
9430 return 1;
Nadav Har'El0140cae2011-05-25 23:06:28 +03009431}
9432
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009433/* Emulate the VMPTRST instruction */
9434static int handle_vmptrst(struct kvm_vcpu *vcpu)
9435{
Sean Christopherson0a06d422018-07-19 10:31:00 -07009436 unsigned long exit_qual = vmcs_readl(EXIT_QUALIFICATION);
9437 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9438 gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009439 struct x86_exception e;
Sean Christopherson0a06d422018-07-19 10:31:00 -07009440 gva_t gva;
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009441
9442 if (!nested_vmx_check_permission(vcpu))
9443 return 1;
9444
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02009445 if (unlikely(to_vmx(vcpu)->nested.hv_evmcs))
9446 return 1;
9447
Sean Christopherson0a06d422018-07-19 10:31:00 -07009448 if (get_vmx_mem_address(vcpu, exit_qual, instr_info, true, &gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009449 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02009450 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Sean Christopherson0a06d422018-07-19 10:31:00 -07009451 if (kvm_write_guest_virt_system(vcpu, gva, (void *)&current_vmptr,
9452 sizeof(gpa_t), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009453 kvm_inject_page_fault(vcpu, &e);
9454 return 1;
9455 }
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009456 return nested_vmx_succeed(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009457}
9458
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009459/* Emulate the INVEPT instruction */
9460static int handle_invept(struct kvm_vcpu *vcpu)
9461{
Wincy Vanb9c237b2015-02-03 23:56:30 +08009462 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009463 u32 vmx_instruction_info, types;
9464 unsigned long type;
9465 gva_t gva;
9466 struct x86_exception e;
9467 struct {
9468 u64 eptp, gpa;
9469 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009470
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009471 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08009472 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009473 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009474 kvm_queue_exception(vcpu, UD_VECTOR);
9475 return 1;
9476 }
9477
9478 if (!nested_vmx_check_permission(vcpu))
9479 return 1;
9480
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009481 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03009482 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009483
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009484 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009485
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009486 if (type >= 32 || !(types & (1 << type)))
9487 return nested_vmx_failValid(vcpu,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009488 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009489
9490 /* According to the Intel VMX instruction reference, the memory
9491 * operand is read even if it isn't needed (e.g., for type==global)
9492 */
9493 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00009494 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009495 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02009496 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009497 kvm_inject_page_fault(vcpu, &e);
9498 return 1;
9499 }
9500
9501 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009502 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04009503 /*
9504 * TODO: track mappings and invalidate
9505 * single context requests appropriately
9506 */
9507 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009508 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04009509 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009510 break;
9511 default:
9512 BUG_ON(1);
9513 break;
9514 }
9515
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009516 return nested_vmx_succeed(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009517}
9518
Liran Alon3d5bdae2018-10-08 23:42:18 +03009519static u16 nested_get_vpid02(struct kvm_vcpu *vcpu)
9520{
9521 struct vcpu_vmx *vmx = to_vmx(vcpu);
9522
9523 return vmx->nested.vpid02 ? vmx->nested.vpid02 : vmx->vpid;
Nadav Har'El0140cae2011-05-25 23:06:28 +03009524}
9525
Petr Matouseka642fc32014-09-23 20:22:30 +02009526static int handle_invvpid(struct kvm_vcpu *vcpu)
9527{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009528 struct vcpu_vmx *vmx = to_vmx(vcpu);
9529 u32 vmx_instruction_info;
9530 unsigned long type, types;
9531 gva_t gva;
9532 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07009533 struct {
9534 u64 vpid;
9535 u64 gla;
9536 } operand;
Liran Alon3d5bdae2018-10-08 23:42:18 +03009537 u16 vpid02;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009538
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009539 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009540 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009541 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009542 kvm_queue_exception(vcpu, UD_VECTOR);
9543 return 1;
9544 }
9545
9546 if (!nested_vmx_check_permission(vcpu))
9547 return 1;
9548
9549 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9550 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9551
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009552 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009553 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009554
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009555 if (type >= 32 || !(types & (1 << type)))
9556 return nested_vmx_failValid(vcpu,
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009557 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009558
9559 /* according to the intel vmx instruction reference, the memory
9560 * operand is read even if it isn't needed (e.g., for type==global)
9561 */
9562 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9563 vmx_instruction_info, false, &gva))
9564 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02009565 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009566 kvm_inject_page_fault(vcpu, &e);
9567 return 1;
9568 }
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009569 if (operand.vpid >> 16)
9570 return nested_vmx_failValid(vcpu,
Jim Mattson40352602017-06-28 09:37:37 -07009571 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009572
Liran Alon3d5bdae2018-10-08 23:42:18 +03009573 vpid02 = nested_get_vpid02(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009574 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009575 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03009576 if (!operand.vpid ||
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009577 is_noncanonical_address(operand.gla, vcpu))
9578 return nested_vmx_failValid(vcpu,
Jim Mattson40352602017-06-28 09:37:37 -07009579 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Liran Alon3d5bdae2018-10-08 23:42:18 +03009580 if (cpu_has_vmx_invvpid_individual_addr()) {
Liran Aloncd9a4912018-05-22 17:16:15 +03009581 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
Liran Alon3d5bdae2018-10-08 23:42:18 +03009582 vpid02, operand.gla);
Liran Aloncd9a4912018-05-22 17:16:15 +03009583 } else
Liran Alon327c0722018-10-08 23:42:19 +03009584 __vmx_flush_tlb(vcpu, vpid02, false);
Liran Aloncd9a4912018-05-22 17:16:15 +03009585 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01009586 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009587 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009588 if (!operand.vpid)
9589 return nested_vmx_failValid(vcpu,
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009590 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Liran Alon327c0722018-10-08 23:42:19 +03009591 __vmx_flush_tlb(vcpu, vpid02, false);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009592 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009593 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Alon327c0722018-10-08 23:42:19 +03009594 __vmx_flush_tlb(vcpu, vpid02, false);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009595 break;
9596 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009597 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009598 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009599 }
9600
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009601 return nested_vmx_succeed(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02009602}
9603
Junaid Shahideb4b2482018-06-27 14:59:14 -07009604static int handle_invpcid(struct kvm_vcpu *vcpu)
9605{
9606 u32 vmx_instruction_info;
9607 unsigned long type;
9608 bool pcid_enabled;
9609 gva_t gva;
9610 struct x86_exception e;
Junaid Shahidb94742c2018-06-27 14:59:20 -07009611 unsigned i;
9612 unsigned long roots_to_free = 0;
Junaid Shahideb4b2482018-06-27 14:59:14 -07009613 struct {
9614 u64 pcid;
9615 u64 gla;
9616 } operand;
9617
9618 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
9619 kvm_queue_exception(vcpu, UD_VECTOR);
9620 return 1;
9621 }
9622
9623 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9624 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9625
9626 if (type > 3) {
9627 kvm_inject_gp(vcpu, 0);
9628 return 1;
9629 }
9630
9631 /* According to the Intel instruction reference, the memory operand
9632 * is read even if it isn't needed (e.g., for type==all)
9633 */
9634 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9635 vmx_instruction_info, false, &gva))
9636 return 1;
9637
9638 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
9639 kvm_inject_page_fault(vcpu, &e);
9640 return 1;
9641 }
9642
9643 if (operand.pcid >> 12 != 0) {
9644 kvm_inject_gp(vcpu, 0);
9645 return 1;
9646 }
9647
9648 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
9649
9650 switch (type) {
9651 case INVPCID_TYPE_INDIV_ADDR:
9652 if ((!pcid_enabled && (operand.pcid != 0)) ||
9653 is_noncanonical_address(operand.gla, vcpu)) {
9654 kvm_inject_gp(vcpu, 0);
9655 return 1;
9656 }
9657 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
9658 return kvm_skip_emulated_instruction(vcpu);
9659
9660 case INVPCID_TYPE_SINGLE_CTXT:
9661 if (!pcid_enabled && (operand.pcid != 0)) {
9662 kvm_inject_gp(vcpu, 0);
9663 return 1;
9664 }
9665
9666 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
9667 kvm_mmu_sync_roots(vcpu);
9668 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
9669 }
9670
Junaid Shahidb94742c2018-06-27 14:59:20 -07009671 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02009672 if (kvm_get_pcid(vcpu, vcpu->arch.mmu->prev_roots[i].cr3)
Junaid Shahidb94742c2018-06-27 14:59:20 -07009673 == operand.pcid)
9674 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
Junaid Shahidade61e22018-06-27 14:59:15 -07009675
Vitaly Kuznetsov6a82cd12018-10-08 21:28:07 +02009676 kvm_mmu_free_roots(vcpu, vcpu->arch.mmu, roots_to_free);
Junaid Shahideb4b2482018-06-27 14:59:14 -07009677 /*
Junaid Shahidb94742c2018-06-27 14:59:20 -07009678 * If neither the current cr3 nor any of the prev_roots use the
Junaid Shahidade61e22018-06-27 14:59:15 -07009679 * given PCID, then nothing needs to be done here because a
9680 * resync will happen anyway before switching to any other CR3.
Junaid Shahideb4b2482018-06-27 14:59:14 -07009681 */
9682
9683 return kvm_skip_emulated_instruction(vcpu);
9684
9685 case INVPCID_TYPE_ALL_NON_GLOBAL:
9686 /*
9687 * Currently, KVM doesn't mark global entries in the shadow
9688 * page tables, so a non-global flush just degenerates to a
9689 * global flush. If needed, we could optimize this later by
9690 * keeping track of global entries in shadow page tables.
9691 */
9692
9693 /* fall-through */
9694 case INVPCID_TYPE_ALL_INCL_GLOBAL:
9695 kvm_mmu_unload(vcpu);
9696 return kvm_skip_emulated_instruction(vcpu);
9697
9698 default:
9699 BUG(); /* We have already checked above that type <= 3 */
9700 }
9701}
9702
Kai Huang843e4332015-01-28 10:54:28 +08009703static int handle_pml_full(struct kvm_vcpu *vcpu)
9704{
9705 unsigned long exit_qualification;
9706
9707 trace_kvm_pml_full(vcpu->vcpu_id);
9708
9709 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9710
9711 /*
9712 * PML buffer FULL happened while executing iret from NMI,
9713 * "blocked by NMI" bit has to be set before next VM entry.
9714 */
9715 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009716 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08009717 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
9718 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9719 GUEST_INTR_STATE_NMI);
9720
9721 /*
9722 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
9723 * here.., and there's no userspace involvement needed for PML.
9724 */
9725 return 1;
9726}
9727
Yunhong Jiang64672c92016-06-13 14:19:59 -07009728static int handle_preemption_timer(struct kvm_vcpu *vcpu)
9729{
Sean Christophersond264ee02018-08-27 15:21:12 -07009730 if (!to_vmx(vcpu)->req_immediate_exit)
9731 kvm_lapic_expired_hv_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -07009732 return 1;
9733}
9734
Bandan Das41ab9372017-08-03 15:54:43 -04009735static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
9736{
9737 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04009738 int maxphyaddr = cpuid_maxphyaddr(vcpu);
9739
9740 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009741 switch (address & VMX_EPTP_MT_MASK) {
9742 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009743 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009744 return false;
9745 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02009746 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009747 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009748 return false;
9749 break;
9750 default:
9751 return false;
9752 }
9753
David Hildenbrandbb97a012017-08-10 23:15:28 +02009754 /* only 4 levels page-walk length are valid */
9755 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04009756 return false;
9757
9758 /* Reserved bits should not be set */
9759 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
9760 return false;
9761
9762 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009763 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009764 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009765 return false;
9766 }
9767
9768 return true;
9769}
9770
9771static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
9772 struct vmcs12 *vmcs12)
9773{
9774 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
9775 u64 address;
9776 bool accessed_dirty;
9777 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
9778
9779 if (!nested_cpu_has_eptp_switching(vmcs12) ||
9780 !nested_cpu_has_ept(vmcs12))
9781 return 1;
9782
9783 if (index >= VMFUNC_EPTP_ENTRIES)
9784 return 1;
9785
9786
9787 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
9788 &address, index * 8, 8))
9789 return 1;
9790
David Hildenbrandbb97a012017-08-10 23:15:28 +02009791 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04009792
9793 /*
9794 * If the (L2) guest does a vmfunc to the currently
9795 * active ept pointer, we don't have to do anything else
9796 */
9797 if (vmcs12->ept_pointer != address) {
9798 if (!valid_ept_address(vcpu, address))
9799 return 1;
9800
9801 kvm_mmu_unload(vcpu);
9802 mmu->ept_ad = accessed_dirty;
Vitaly Kuznetsov36d95942018-10-08 21:28:10 +02009803 mmu->mmu_role.base.ad_disabled = !accessed_dirty;
Bandan Das41ab9372017-08-03 15:54:43 -04009804 vmcs12->ept_pointer = address;
9805 /*
9806 * TODO: Check what's the correct approach in case
9807 * mmu reload fails. Currently, we just let the next
9808 * reload potentially fail
9809 */
9810 kvm_mmu_reload(vcpu);
9811 }
9812
9813 return 0;
9814}
9815
Bandan Das2a499e42017-08-03 15:54:41 -04009816static int handle_vmfunc(struct kvm_vcpu *vcpu)
9817{
Bandan Das27c42a12017-08-03 15:54:42 -04009818 struct vcpu_vmx *vmx = to_vmx(vcpu);
9819 struct vmcs12 *vmcs12;
9820 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
9821
9822 /*
9823 * VMFUNC is only supported for nested guests, but we always enable the
9824 * secondary control for simplicity; for non-nested mode, fake that we
9825 * didn't by injecting #UD.
9826 */
9827 if (!is_guest_mode(vcpu)) {
9828 kvm_queue_exception(vcpu, UD_VECTOR);
9829 return 1;
9830 }
9831
9832 vmcs12 = get_vmcs12(vcpu);
9833 if ((vmcs12->vm_function_control & (1 << function)) == 0)
9834 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04009835
9836 switch (function) {
9837 case 0:
9838 if (nested_vmx_eptp_switching(vcpu, vmcs12))
9839 goto fail;
9840 break;
9841 default:
9842 goto fail;
9843 }
9844 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04009845
9846fail:
9847 nested_vmx_vmexit(vcpu, vmx->exit_reason,
9848 vmcs_read32(VM_EXIT_INTR_INFO),
9849 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04009850 return 1;
9851}
9852
Sean Christopherson0b665d32018-08-14 09:33:34 -07009853static int handle_encls(struct kvm_vcpu *vcpu)
9854{
9855 /*
9856 * SGX virtualization is not yet supported. There is no software
9857 * enable bit for SGX, so we have to trap ENCLS and inject a #UD
9858 * to prevent the guest from executing ENCLS.
9859 */
9860 kvm_queue_exception(vcpu, UD_VECTOR);
9861 return 1;
9862}
9863
Nadav Har'El0140cae2011-05-25 23:06:28 +03009864/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08009865 * The exit handlers return 1 if the exit was handled fully and guest execution
9866 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
9867 * to be done to userspace and return 0.
9868 */
Mathias Krause772e0312012-08-30 01:30:19 +02009869static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08009870 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
9871 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08009872 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08009873 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009874 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009875 [EXIT_REASON_CR_ACCESS] = handle_cr,
9876 [EXIT_REASON_DR_ACCESS] = handle_dr,
9877 [EXIT_REASON_CPUID] = handle_cpuid,
9878 [EXIT_REASON_MSR_READ] = handle_rdmsr,
9879 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
9880 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
9881 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02009882 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03009883 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02009884 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02009885 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03009886 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009887 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03009888 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009889 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009890 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009891 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009892 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03009893 [EXIT_REASON_VMOFF] = handle_vmoff,
9894 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08009895 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
9896 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08009897 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08009898 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02009899 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08009900 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02009901 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08009902 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02009903 [EXIT_REASON_GDTR_IDTR] = handle_desc,
9904 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03009905 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
9906 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08009907 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009908 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009909 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009910 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009911 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02009912 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07009913 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07009914 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08009915 [EXIT_REASON_XSAVES] = handle_xsaves,
9916 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08009917 [EXIT_REASON_PML_FULL] = handle_pml_full,
Junaid Shahideb4b2482018-06-27 14:59:14 -07009918 [EXIT_REASON_INVPCID] = handle_invpcid,
Bandan Das2a499e42017-08-03 15:54:41 -04009919 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07009920 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Sean Christopherson0b665d32018-08-14 09:33:34 -07009921 [EXIT_REASON_ENCLS] = handle_encls,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009922};
9923
9924static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04009925 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009926
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009927static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
9928 struct vmcs12 *vmcs12)
9929{
9930 unsigned long exit_qualification;
9931 gpa_t bitmap, last_bitmap;
9932 unsigned int port;
9933 int size;
9934 u8 b;
9935
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009936 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05009937 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009938
9939 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9940
9941 port = exit_qualification >> 16;
9942 size = (exit_qualification & 7) + 1;
9943
9944 last_bitmap = (gpa_t)-1;
9945 b = -1;
9946
9947 while (size > 0) {
9948 if (port < 0x8000)
9949 bitmap = vmcs12->io_bitmap_a;
9950 else if (port < 0x10000)
9951 bitmap = vmcs12->io_bitmap_b;
9952 else
Joe Perches1d804d02015-03-30 16:46:09 -07009953 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009954 bitmap += (port & 0x7fff) / 8;
9955
9956 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009957 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009958 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009959 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07009960 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009961
9962 port++;
9963 size--;
9964 last_bitmap = bitmap;
9965 }
9966
Joe Perches1d804d02015-03-30 16:46:09 -07009967 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009968}
9969
Nadav Har'El644d7112011-05-25 23:12:35 +03009970/*
9971 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
9972 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
9973 * disinterest in the current event (read or write a specific MSR) by using an
9974 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
9975 */
9976static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
9977 struct vmcs12 *vmcs12, u32 exit_reason)
9978{
9979 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
9980 gpa_t bitmap;
9981
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01009982 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07009983 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009984
9985 /*
9986 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
9987 * for the four combinations of read/write and low/high MSR numbers.
9988 * First we need to figure out which of the four to use:
9989 */
9990 bitmap = vmcs12->msr_bitmap;
9991 if (exit_reason == EXIT_REASON_MSR_WRITE)
9992 bitmap += 2048;
9993 if (msr_index >= 0xc0000000) {
9994 msr_index -= 0xc0000000;
9995 bitmap += 1024;
9996 }
9997
9998 /* Then read the msr_index'th bit from this bitmap: */
9999 if (msr_index < 1024*8) {
10000 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010001 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -070010002 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010003 return 1 & (b >> (msr_index & 7));
10004 } else
Joe Perches1d804d02015-03-30 16:46:09 -070010005 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +030010006}
10007
10008/*
10009 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
10010 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
10011 * intercept (via guest_host_mask etc.) the current event.
10012 */
10013static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
10014 struct vmcs12 *vmcs12)
10015{
10016 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
10017 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +020010018 int reg;
10019 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +030010020
10021 switch ((exit_qualification >> 4) & 3) {
10022 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +020010023 reg = (exit_qualification >> 8) & 15;
10024 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +030010025 switch (cr) {
10026 case 0:
10027 if (vmcs12->cr0_guest_host_mask &
10028 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -070010029 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010030 break;
10031 case 3:
10032 if ((vmcs12->cr3_target_count >= 1 &&
10033 vmcs12->cr3_target_value0 == val) ||
10034 (vmcs12->cr3_target_count >= 2 &&
10035 vmcs12->cr3_target_value1 == val) ||
10036 (vmcs12->cr3_target_count >= 3 &&
10037 vmcs12->cr3_target_value2 == val) ||
10038 (vmcs12->cr3_target_count >= 4 &&
10039 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -070010040 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +030010041 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -070010042 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010043 break;
10044 case 4:
10045 if (vmcs12->cr4_guest_host_mask &
10046 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -070010047 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010048 break;
10049 case 8:
10050 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -070010051 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010052 break;
10053 }
10054 break;
10055 case 2: /* clts */
10056 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
10057 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -070010058 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010059 break;
10060 case 1: /* mov from cr */
10061 switch (cr) {
10062 case 3:
10063 if (vmcs12->cpu_based_vm_exec_control &
10064 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -070010065 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010066 break;
10067 case 8:
10068 if (vmcs12->cpu_based_vm_exec_control &
10069 CPU_BASED_CR8_STORE_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 3: /* lmsw */
10075 /*
10076 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
10077 * cr0. Other attempted changes are ignored, with no exit.
10078 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +020010079 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +030010080 if (vmcs12->cr0_guest_host_mask & 0xe &
10081 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -070010082 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010083 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
10084 !(vmcs12->cr0_read_shadow & 0x1) &&
10085 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -070010086 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010087 break;
10088 }
Joe Perches1d804d02015-03-30 16:46:09 -070010089 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +030010090}
10091
Liran Alona7cde482018-06-23 02:35:10 +030010092static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
10093 struct vmcs12 *vmcs12, gpa_t bitmap)
10094{
10095 u32 vmx_instruction_info;
10096 unsigned long field;
10097 u8 b;
10098
10099 if (!nested_cpu_has_shadow_vmcs(vmcs12))
10100 return true;
10101
10102 /* Decode instruction info and find the field to access */
10103 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
10104 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
10105
10106 /* Out-of-range fields always cause a VM exit from L2 to L1 */
10107 if (field >> 15)
10108 return true;
10109
10110 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
10111 return true;
10112
10113 return 1 & (b >> (field & 7));
10114}
10115
Nadav Har'El644d7112011-05-25 23:12:35 +030010116/*
10117 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
10118 * should handle it ourselves in L0 (and then continue L2). Only call this
10119 * when in is_guest_mode (L2).
10120 */
Paolo Bonzini7313c692017-07-27 10:31:25 +020010121static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +030010122{
Nadav Har'El644d7112011-05-25 23:12:35 +030010123 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10124 struct vcpu_vmx *vmx = to_vmx(vcpu);
10125 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10126
Jim Mattson4f350c62017-09-14 16:31:44 -070010127 if (vmx->nested.nested_run_pending)
10128 return false;
10129
10130 if (unlikely(vmx->fail)) {
10131 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
10132 vmcs_read32(VM_INSTRUCTION_ERROR));
10133 return true;
10134 }
Jan Kiszka542060e2014-01-04 18:47:21 +010010135
David Matlackc9f04402017-08-01 14:00:40 -070010136 /*
10137 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -060010138 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
10139 * Page). The CPU may write to these pages via their host
10140 * physical address while L2 is running, bypassing any
10141 * address-translation-based dirty tracking (e.g. EPT write
10142 * protection).
David Matlackc9f04402017-08-01 14:00:40 -070010143 *
10144 * Mark them dirty on every exit from L2 to prevent them from
10145 * getting out of sync with dirty tracking.
10146 */
10147 nested_mark_vmcs12_pages_dirty(vcpu);
10148
Jim Mattson4f350c62017-09-14 16:31:44 -070010149 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
10150 vmcs_readl(EXIT_QUALIFICATION),
10151 vmx->idt_vectoring_info,
10152 intr_info,
10153 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
10154 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +030010155
10156 switch (exit_reason) {
10157 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -080010158 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -070010159 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +030010160 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -070010161 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Jan Kiszka6f054852016-02-09 20:15:18 +010010162 else if (is_debug(intr_info) &&
10163 vcpu->guest_debug &
10164 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
10165 return false;
10166 else if (is_breakpoint(intr_info) &&
10167 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
10168 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +030010169 return vmcs12->exception_bitmap &
10170 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
10171 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -070010172 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +030010173 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -070010174 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010175 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +020010176 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +030010177 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +020010178 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +030010179 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -070010180 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010181 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -070010182 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010183 case EXIT_REASON_HLT:
10184 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
10185 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -070010186 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010187 case EXIT_REASON_INVLPG:
10188 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
10189 case EXIT_REASON_RDPMC:
10190 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +020010191 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +020010192 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +020010193 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +020010194 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +010010195 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +030010196 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
Liran Alona7cde482018-06-23 02:35:10 +030010197 case EXIT_REASON_VMREAD:
10198 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
10199 vmcs12->vmread_bitmap);
10200 case EXIT_REASON_VMWRITE:
10201 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
10202 vmcs12->vmwrite_bitmap);
Nadav Har'El644d7112011-05-25 23:12:35 +030010203 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
10204 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
Liran Alona7cde482018-06-23 02:35:10 +030010205 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
Nadav Har'El644d7112011-05-25 23:12:35 +030010206 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +020010207 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +030010208 /*
10209 * VMX instructions trap unconditionally. This allows L1 to
10210 * emulate them for its L2 guest, i.e., allows 3-level nesting!
10211 */
Joe Perches1d804d02015-03-30 16:46:09 -070010212 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010213 case EXIT_REASON_CR_ACCESS:
10214 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
10215 case EXIT_REASON_DR_ACCESS:
10216 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
10217 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +010010218 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +020010219 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
10220 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +030010221 case EXIT_REASON_MSR_READ:
10222 case EXIT_REASON_MSR_WRITE:
10223 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
10224 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -070010225 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010226 case EXIT_REASON_MWAIT_INSTRUCTION:
10227 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +030010228 case EXIT_REASON_MONITOR_TRAP_FLAG:
10229 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +030010230 case EXIT_REASON_MONITOR_INSTRUCTION:
10231 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
10232 case EXIT_REASON_PAUSE_INSTRUCTION:
10233 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
10234 nested_cpu_has2(vmcs12,
10235 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
10236 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -070010237 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +030010238 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010239 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +030010240 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +080010241 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +080010242 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -040010243 /*
10244 * The controls for "virtualize APIC accesses," "APIC-
10245 * register virtualization," and "virtual-interrupt
10246 * delivery" only come from vmcs12.
10247 */
Joe Perches1d804d02015-03-30 16:46:09 -070010248 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010249 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +030010250 /*
10251 * L0 always deals with the EPT violation. If nested EPT is
10252 * used, and the nested mmu code discovers that the address is
10253 * missing in the guest EPT table (EPT12), the EPT violation
10254 * will be injected with nested_ept_inject_page_fault()
10255 */
Joe Perches1d804d02015-03-30 16:46:09 -070010256 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +030010257 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +030010258 /*
10259 * L2 never uses directly L1's EPT, but rather L0's own EPT
10260 * table (shadow on EPT) or a merged EPT table that L0 built
10261 * (EPT on EPT). So any problems with the structure of the
10262 * table is L0's fault.
10263 */
Joe Perches1d804d02015-03-30 16:46:09 -070010264 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +020010265 case EXIT_REASON_INVPCID:
10266 return
10267 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
10268 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +030010269 case EXIT_REASON_WBINVD:
10270 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
10271 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -070010272 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +080010273 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
10274 /*
10275 * This should never happen, since it is not possible to
10276 * set XSS to a non-zero value---neither in L1 nor in L2.
10277 * If if it were, XSS would have to be checked against
10278 * the XSS exit bitmap in vmcs12.
10279 */
10280 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +080010281 case EXIT_REASON_PREEMPTION_TIMER:
10282 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +020010283 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -040010284 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +020010285 return false;
Bandan Das2a499e42017-08-03 15:54:41 -040010286 case EXIT_REASON_VMFUNC:
10287 /* VM functions are emulated through L2->L0 vmexits. */
10288 return false;
Sean Christopherson0b665d32018-08-14 09:33:34 -070010289 case EXIT_REASON_ENCLS:
10290 /* SGX is never exposed to L1 */
10291 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +030010292 default:
Joe Perches1d804d02015-03-30 16:46:09 -070010293 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010294 }
10295}
10296
Paolo Bonzini7313c692017-07-27 10:31:25 +020010297static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
10298{
10299 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10300
10301 /*
10302 * At this point, the exit interruption info in exit_intr_info
10303 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
10304 * we need to query the in-kernel LAPIC.
10305 */
10306 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
10307 if ((exit_intr_info &
10308 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
10309 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
10310 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10311 vmcs12->vm_exit_intr_error_code =
10312 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
10313 }
10314
10315 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
10316 vmcs_readl(EXIT_QUALIFICATION));
10317 return 1;
10318}
10319
Avi Kivity586f9602010-11-18 13:09:54 +020010320static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
10321{
10322 *info1 = vmcs_readl(EXIT_QUALIFICATION);
10323 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
10324}
10325
Kai Huanga3eaa862015-11-04 13:46:05 +080010326static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +080010327{
Kai Huanga3eaa862015-11-04 13:46:05 +080010328 if (vmx->pml_pg) {
10329 __free_page(vmx->pml_pg);
10330 vmx->pml_pg = NULL;
10331 }
Kai Huang843e4332015-01-28 10:54:28 +080010332}
10333
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010334static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +080010335{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010336 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +080010337 u64 *pml_buf;
10338 u16 pml_idx;
10339
10340 pml_idx = vmcs_read16(GUEST_PML_INDEX);
10341
10342 /* Do nothing if PML buffer is empty */
10343 if (pml_idx == (PML_ENTITY_NUM - 1))
10344 return;
10345
10346 /* PML index always points to next available PML buffer entity */
10347 if (pml_idx >= PML_ENTITY_NUM)
10348 pml_idx = 0;
10349 else
10350 pml_idx++;
10351
10352 pml_buf = page_address(vmx->pml_pg);
10353 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
10354 u64 gpa;
10355
10356 gpa = pml_buf[pml_idx];
10357 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010358 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +080010359 }
10360
10361 /* reset PML index */
10362 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
10363}
10364
10365/*
10366 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
10367 * Called before reporting dirty_bitmap to userspace.
10368 */
10369static void kvm_flush_pml_buffers(struct kvm *kvm)
10370{
10371 int i;
10372 struct kvm_vcpu *vcpu;
10373 /*
10374 * We only need to kick vcpu out of guest mode here, as PML buffer
10375 * is flushed at beginning of all VMEXITs, and it's obvious that only
10376 * vcpus running in guest are possible to have unflushed GPAs in PML
10377 * buffer.
10378 */
10379 kvm_for_each_vcpu(i, vcpu, kvm)
10380 kvm_vcpu_kick(vcpu);
10381}
10382
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010383static void vmx_dump_sel(char *name, uint32_t sel)
10384{
10385 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -050010386 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010387 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
10388 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
10389 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
10390}
10391
10392static void vmx_dump_dtsel(char *name, uint32_t limit)
10393{
10394 pr_err("%s limit=0x%08x, base=0x%016lx\n",
10395 name, vmcs_read32(limit),
10396 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
10397}
10398
10399static void dump_vmcs(void)
10400{
10401 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
10402 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
10403 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
10404 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
10405 u32 secondary_exec_control = 0;
10406 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +010010407 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010408 int i, n;
10409
10410 if (cpu_has_secondary_exec_ctrls())
10411 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10412
10413 pr_err("*** Guest State ***\n");
10414 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
10415 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
10416 vmcs_readl(CR0_GUEST_HOST_MASK));
10417 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
10418 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
10419 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
10420 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
10421 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
10422 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010423 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
10424 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
10425 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
10426 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010427 }
10428 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
10429 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
10430 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
10431 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
10432 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
10433 vmcs_readl(GUEST_SYSENTER_ESP),
10434 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
10435 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
10436 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
10437 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
10438 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
10439 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
10440 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
10441 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
10442 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
10443 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
10444 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
10445 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
10446 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010447 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
10448 efer, vmcs_read64(GUEST_IA32_PAT));
10449 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
10450 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010451 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010452 if (cpu_has_load_perf_global_ctrl &&
10453 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010454 pr_err("PerfGlobCtl = 0x%016llx\n",
10455 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010456 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010457 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010458 pr_err("Interruptibility = %08x ActivityState = %08x\n",
10459 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
10460 vmcs_read32(GUEST_ACTIVITY_STATE));
10461 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
10462 pr_err("InterruptStatus = %04x\n",
10463 vmcs_read16(GUEST_INTR_STATUS));
10464
10465 pr_err("*** Host State ***\n");
10466 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
10467 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
10468 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
10469 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
10470 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
10471 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
10472 vmcs_read16(HOST_TR_SELECTOR));
10473 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
10474 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
10475 vmcs_readl(HOST_TR_BASE));
10476 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
10477 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
10478 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
10479 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
10480 vmcs_readl(HOST_CR4));
10481 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
10482 vmcs_readl(HOST_IA32_SYSENTER_ESP),
10483 vmcs_read32(HOST_IA32_SYSENTER_CS),
10484 vmcs_readl(HOST_IA32_SYSENTER_EIP));
10485 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010486 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
10487 vmcs_read64(HOST_IA32_EFER),
10488 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010489 if (cpu_has_load_perf_global_ctrl &&
10490 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010491 pr_err("PerfGlobCtl = 0x%016llx\n",
10492 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010493
10494 pr_err("*** Control State ***\n");
10495 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
10496 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
10497 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
10498 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
10499 vmcs_read32(EXCEPTION_BITMAP),
10500 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
10501 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
10502 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
10503 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10504 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
10505 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
10506 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
10507 vmcs_read32(VM_EXIT_INTR_INFO),
10508 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
10509 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
10510 pr_err(" reason=%08x qualification=%016lx\n",
10511 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
10512 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
10513 vmcs_read32(IDT_VECTORING_INFO_FIELD),
10514 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010515 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +080010516 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010517 pr_err("TSC Multiplier = 0x%016llx\n",
10518 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010519 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
10520 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
10521 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
10522 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
10523 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010524 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010525 n = vmcs_read32(CR3_TARGET_COUNT);
10526 for (i = 0; i + 1 < n; i += 4)
10527 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
10528 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
10529 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
10530 if (i < n)
10531 pr_err("CR3 target%u=%016lx\n",
10532 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
10533 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
10534 pr_err("PLE Gap=%08x Window=%08x\n",
10535 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
10536 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
10537 pr_err("Virtual processor ID = 0x%04x\n",
10538 vmcs_read16(VIRTUAL_PROCESSOR_ID));
10539}
10540
Avi Kivity6aa8b732006-12-10 02:21:36 -080010541/*
10542 * The guest has exited. See if we can fix it or if we need userspace
10543 * assistance.
10544 */
Avi Kivity851ba692009-08-24 11:10:17 +030010545static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010546{
Avi Kivity29bd8a72007-09-10 17:27:03 +030010547 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +080010548 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +020010549 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +030010550
Paolo Bonzini8b89fe12015-12-10 18:37:32 +010010551 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
10552
Kai Huang843e4332015-01-28 10:54:28 +080010553 /*
10554 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
10555 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
10556 * querying dirty_bitmap, we only need to kick all vcpus out of guest
10557 * mode as if vcpus is in root mode, the PML buffer must has been
10558 * flushed already.
10559 */
10560 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010561 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +080010562
Mohammed Gamal80ced182009-09-01 12:48:18 +020010563 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +020010564 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +020010565 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +010010566
Paolo Bonzini7313c692017-07-27 10:31:25 +020010567 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
10568 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +030010569
Mohammed Gamal51207022010-05-31 22:40:54 +030010570 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010571 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +030010572 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10573 vcpu->run->fail_entry.hardware_entry_failure_reason
10574 = exit_reason;
10575 return 0;
10576 }
10577
Avi Kivity29bd8a72007-09-10 17:27:03 +030010578 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +030010579 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10580 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +030010581 = vmcs_read32(VM_INSTRUCTION_ERROR);
10582 return 0;
10583 }
Avi Kivity6aa8b732006-12-10 02:21:36 -080010584
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010585 /*
10586 * Note:
10587 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
10588 * delivery event since it indicates guest is accessing MMIO.
10589 * The vm-exit can be triggered again after return to guest that
10590 * will cause infinite loop.
10591 */
Mike Dayd77c26f2007-10-08 09:02:08 -040010592 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +080010593 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +020010594 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +000010595 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010596 exit_reason != EXIT_REASON_TASK_SWITCH)) {
10597 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
10598 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +020010599 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010600 vcpu->run->internal.data[0] = vectoring_info;
10601 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +020010602 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
10603 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
10604 vcpu->run->internal.ndata++;
10605 vcpu->run->internal.data[3] =
10606 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
10607 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010608 return 0;
10609 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +020010610
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010611 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010612 vmx->loaded_vmcs->soft_vnmi_blocked)) {
10613 if (vmx_interrupt_allowed(vcpu)) {
10614 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10615 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
10616 vcpu->arch.nmi_pending) {
10617 /*
10618 * This CPU don't support us in finding the end of an
10619 * NMI-blocked window if the guest runs with IRQs
10620 * disabled. So we pull the trigger after 1 s of
10621 * futile waiting, but inform the user about this.
10622 */
10623 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
10624 "state on VCPU %d after 1 s timeout\n",
10625 __func__, vcpu->vcpu_id);
10626 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10627 }
10628 }
10629
Avi Kivity6aa8b732006-12-10 02:21:36 -080010630 if (exit_reason < kvm_vmx_max_exit_handlers
10631 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +030010632 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010633 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +010010634 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
10635 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +030010636 kvm_queue_exception(vcpu, UD_VECTOR);
10637 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010638 }
Avi Kivity6aa8b732006-12-10 02:21:36 -080010639}
10640
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010641/*
10642 * Software based L1D cache flush which is used when microcode providing
10643 * the cache control MSR is not loaded.
10644 *
10645 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
10646 * flush it is required to read in 64 KiB because the replacement algorithm
10647 * is not exactly LRU. This could be sized at runtime via topology
10648 * information but as all relevant affected CPUs have 32KiB L1D cache size
10649 * there is no point in doing so.
10650 */
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010651static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010652{
10653 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010654
10655 /*
Thomas Gleixner2f055942018-07-13 16:23:17 +020010656 * This code is only executed when the the flush mode is 'cond' or
10657 * 'always'
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010658 */
Nicolai Stange427362a2018-07-21 22:25:00 +020010659 if (static_branch_likely(&vmx_l1d_flush_cond)) {
Nicolai Stange45b575c2018-07-27 13:22:16 +020010660 bool flush_l1d;
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010661
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010662 /*
Nicolai Stange45b575c2018-07-27 13:22:16 +020010663 * Clear the per-vcpu flush bit, it gets set again
10664 * either from vcpu_run() or from one of the unsafe
10665 * VMEXIT handlers.
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010666 */
Nicolai Stange45b575c2018-07-27 13:22:16 +020010667 flush_l1d = vcpu->arch.l1tf_flush_l1d;
Thomas Gleixner4c6523e2018-07-13 16:23:20 +020010668 vcpu->arch.l1tf_flush_l1d = false;
Nicolai Stange45b575c2018-07-27 13:22:16 +020010669
10670 /*
10671 * Clear the per-cpu flush bit, it gets set again from
10672 * the interrupt handlers.
10673 */
10674 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
10675 kvm_clear_cpu_l1tf_flush_l1d();
10676
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010677 if (!flush_l1d)
10678 return;
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010679 }
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010680
10681 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010682
Paolo Bonzini3fa045b2018-07-02 13:03:48 +020010683 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
10684 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
10685 return;
10686 }
10687
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010688 asm volatile(
10689 /* First ensure the pages are in the TLB */
10690 "xorl %%eax, %%eax\n"
10691 ".Lpopulate_tlb:\n\t"
Nicolai Stange288d1522018-07-18 19:07:38 +020010692 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010693 "addl $4096, %%eax\n\t"
10694 "cmpl %%eax, %[size]\n\t"
10695 "jne .Lpopulate_tlb\n\t"
10696 "xorl %%eax, %%eax\n\t"
10697 "cpuid\n\t"
10698 /* Now fill the cache */
10699 "xorl %%eax, %%eax\n"
10700 ".Lfill_cache:\n"
Nicolai Stange288d1522018-07-18 19:07:38 +020010701 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010702 "addl $64, %%eax\n\t"
10703 "cmpl %%eax, %[size]\n\t"
10704 "jne .Lfill_cache\n\t"
10705 "lfence\n"
Nicolai Stange288d1522018-07-18 19:07:38 +020010706 :: [flush_pages] "r" (vmx_l1d_flush_pages),
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010707 [size] "r" (size)
10708 : "eax", "ebx", "ecx", "edx");
10709}
10710
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010711static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010712{
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010713 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10714
10715 if (is_guest_mode(vcpu) &&
10716 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10717 return;
10718
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010719 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010720 vmcs_write32(TPR_THRESHOLD, 0);
10721 return;
10722 }
10723
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010724 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010725}
10726
Jim Mattson8d860bb2018-05-09 16:56:05 -040010727static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +080010728{
10729 u32 sec_exec_control;
10730
Jim Mattson8d860bb2018-05-09 16:56:05 -040010731 if (!lapic_in_kernel(vcpu))
10732 return;
10733
Sean Christophersonfd6b6d92018-10-01 14:25:34 -070010734 if (!flexpriority_enabled &&
10735 !cpu_has_vmx_virtualize_x2apic_mode())
10736 return;
10737
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010738 /* Postpone execution until vmcs01 is the current VMCS. */
10739 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -040010740 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010741 return;
10742 }
10743
Yang Zhang8d146952013-01-25 10:18:50 +080010744 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -040010745 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10746 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +080010747
Jim Mattson8d860bb2018-05-09 16:56:05 -040010748 switch (kvm_get_apic_mode(vcpu)) {
10749 case LAPIC_MODE_INVALID:
10750 WARN_ONCE(true, "Invalid local APIC state");
10751 case LAPIC_MODE_DISABLED:
10752 break;
10753 case LAPIC_MODE_XAPIC:
10754 if (flexpriority_enabled) {
10755 sec_exec_control |=
10756 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
10757 vmx_flush_tlb(vcpu, true);
10758 }
10759 break;
10760 case LAPIC_MODE_X2APIC:
10761 if (cpu_has_vmx_virtualize_x2apic_mode())
10762 sec_exec_control |=
10763 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
10764 break;
Yang Zhang8d146952013-01-25 10:18:50 +080010765 }
10766 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
10767
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010768 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +080010769}
10770
Tang Chen38b99172014-09-24 15:57:54 +080010771static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
10772{
Jim Mattsonab5df312018-05-09 17:02:03 -040010773 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +080010774 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -070010775 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070010776 }
Tang Chen38b99172014-09-24 15:57:54 +080010777}
10778
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010779static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010780{
10781 u16 status;
10782 u8 old;
10783
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010784 if (max_isr == -1)
10785 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +080010786
10787 status = vmcs_read16(GUEST_INTR_STATUS);
10788 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010789 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +080010790 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010791 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +080010792 vmcs_write16(GUEST_INTR_STATUS, status);
10793 }
10794}
10795
10796static void vmx_set_rvi(int vector)
10797{
10798 u16 status;
10799 u8 old;
10800
Wei Wang4114c272014-11-05 10:53:43 +080010801 if (vector == -1)
10802 vector = 0;
10803
Yang Zhangc7c9c562013-01-25 10:18:51 +080010804 status = vmcs_read16(GUEST_INTR_STATUS);
10805 old = (u8)status & 0xff;
10806 if ((u8)vector != old) {
10807 status &= ~0xff;
10808 status |= (u8)vector;
10809 vmcs_write16(GUEST_INTR_STATUS, status);
10810 }
10811}
10812
10813static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
10814{
Liran Alon851c1a182017-12-24 18:12:56 +020010815 /*
10816 * When running L2, updating RVI is only relevant when
10817 * vmcs12 virtual-interrupt-delivery enabled.
10818 * However, it can be enabled only when L1 also
10819 * intercepts external-interrupts and in that case
10820 * we should not update vmcs02 RVI but instead intercept
10821 * interrupt. Therefore, do nothing when running L2.
10822 */
10823 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +080010824 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +080010825}
10826
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010827static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010828{
10829 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010830 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +020010831 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010832
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010833 WARN_ON(!vcpu->arch.apicv_active);
10834 if (pi_test_on(&vmx->pi_desc)) {
10835 pi_clear_on(&vmx->pi_desc);
10836 /*
10837 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
10838 * But on x86 this is just a compiler barrier anyway.
10839 */
10840 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +020010841 max_irr_updated =
10842 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
10843
10844 /*
10845 * If we are running L2 and L1 has a new pending interrupt
10846 * which can be injected, we should re-evaluate
10847 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +020010848 * If L1 intercepts external-interrupts, we should
10849 * exit from L2 to L1. Otherwise, interrupt should be
10850 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +020010851 */
Liran Alon851c1a182017-12-24 18:12:56 +020010852 if (is_guest_mode(vcpu) && max_irr_updated) {
10853 if (nested_exit_on_intr(vcpu))
10854 kvm_vcpu_exiting_guest_mode(vcpu);
10855 else
10856 kvm_make_request(KVM_REQ_EVENT, vcpu);
10857 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010858 } else {
10859 max_irr = kvm_lapic_find_highest_irr(vcpu);
10860 }
10861 vmx_hwapic_irr_update(vcpu, max_irr);
10862 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010863}
10864
Paolo Bonzini7e712682018-10-03 13:44:26 +020010865static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu)
10866{
10867 u8 rvi = vmx_get_rvi();
10868 u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI);
10869
10870 return ((rvi & 0xf0) > (vppr & 0xf0));
10871}
10872
Andrey Smetanin63086302015-11-10 15:36:32 +030010873static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010874{
Andrey Smetanind62caab2015-11-10 15:36:33 +030010875 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +080010876 return;
10877
Yang Zhangc7c9c562013-01-25 10:18:51 +080010878 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
10879 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
10880 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
10881 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
10882}
10883
Paolo Bonzini967235d2016-12-19 14:03:45 +010010884static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
10885{
10886 struct vcpu_vmx *vmx = to_vmx(vcpu);
10887
10888 pi_clear_on(&vmx->pi_desc);
10889 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
10890}
10891
Avi Kivity51aa01d2010-07-20 14:31:20 +030010892static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +030010893{
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010894 u32 exit_intr_info = 0;
10895 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +020010896
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010897 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
10898 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +020010899 return;
10900
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010901 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10902 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10903 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +080010904
Wanpeng Li1261bfa2017-07-13 18:30:40 -070010905 /* if exit due to PF check for async PF */
10906 if (is_page_fault(exit_intr_info))
10907 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
10908
Andi Kleena0861c02009-06-08 17:37:09 +080010909 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010910 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
10911 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +080010912 kvm_machine_check();
10913
Gleb Natapov20f65982009-05-11 13:35:55 +030010914 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -080010915 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -070010916 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +030010917 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -070010918 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080010919 }
Avi Kivity51aa01d2010-07-20 14:31:20 +030010920}
Gleb Natapov20f65982009-05-11 13:35:55 +030010921
Yang Zhanga547c6d2013-04-11 19:25:10 +080010922static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
10923{
10924 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10925
Yang Zhanga547c6d2013-04-11 19:25:10 +080010926 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
10927 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
10928 unsigned int vector;
10929 unsigned long entry;
10930 gate_desc *desc;
10931 struct vcpu_vmx *vmx = to_vmx(vcpu);
10932#ifdef CONFIG_X86_64
10933 unsigned long tmp;
10934#endif
10935
10936 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10937 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +020010938 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010939 asm volatile(
10940#ifdef CONFIG_X86_64
10941 "mov %%" _ASM_SP ", %[sp]\n\t"
10942 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
10943 "push $%c[ss]\n\t"
10944 "push %[sp]\n\t"
10945#endif
10946 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +080010947 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010948 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +080010949 :
10950#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -060010951 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010952#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -050010953 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +080010954 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010955 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010956 [ss]"i"(__KERNEL_DS),
10957 [cs]"i"(__KERNEL_CS)
10958 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +020010959 }
Yang Zhanga547c6d2013-04-11 19:25:10 +080010960}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010961STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010962
Tom Lendackybc226f02018-05-10 22:06:39 +020010963static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010964{
Tom Lendackybc226f02018-05-10 22:06:39 +020010965 switch (index) {
10966 case MSR_IA32_SMBASE:
10967 /*
10968 * We cannot do SMM unless we can run the guest in big
10969 * real mode.
10970 */
10971 return enable_unrestricted_guest || emulate_invalid_guest_state;
10972 case MSR_AMD64_VIRT_SPEC_CTRL:
10973 /* This is AMD only. */
10974 return false;
10975 default:
10976 return true;
10977 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010978}
10979
Liu, Jinsongda8999d2014-02-24 10:55:46 +000010980static bool vmx_mpx_supported(void)
10981{
10982 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
10983 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
10984}
10985
Wanpeng Li55412b22014-12-02 19:21:30 +080010986static bool vmx_xsaves_supported(void)
10987{
10988 return vmcs_config.cpu_based_2nd_exec_ctrl &
10989 SECONDARY_EXEC_XSAVES;
10990}
10991
Avi Kivity51aa01d2010-07-20 14:31:20 +030010992static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
10993{
Avi Kivityc5ca8e52011-03-07 17:37:37 +020010994 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +030010995 bool unblock_nmi;
10996 u8 vector;
10997 bool idtv_info_valid;
10998
10999 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +030011000
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010011001 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010011002 if (vmx->loaded_vmcs->nmi_known_unmasked)
11003 return;
11004 /*
11005 * Can't use vmx->exit_intr_info since we're not sure what
11006 * the exit reason is.
11007 */
11008 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
11009 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
11010 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
11011 /*
11012 * SDM 3: 27.7.1.2 (September 2008)
11013 * Re-set bit "block by NMI" before VM entry if vmexit caused by
11014 * a guest IRET fault.
11015 * SDM 3: 23.2.2 (September 2008)
11016 * Bit 12 is undefined in any of the following cases:
11017 * If the VM exit sets the valid bit in the IDT-vectoring
11018 * information field.
11019 * If the VM exit is due to a double fault.
11020 */
11021 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
11022 vector != DF_VECTOR && !idtv_info_valid)
11023 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
11024 GUEST_INTR_STATE_NMI);
11025 else
11026 vmx->loaded_vmcs->nmi_known_unmasked =
11027 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
11028 & GUEST_INTR_STATE_NMI);
11029 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
11030 vmx->loaded_vmcs->vnmi_blocked_time +=
11031 ktime_to_ns(ktime_sub(ktime_get(),
11032 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +030011033}
11034
Jan Kiszka3ab66e82013-02-20 14:03:24 +010011035static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +030011036 u32 idt_vectoring_info,
11037 int instr_len_field,
11038 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +030011039{
Avi Kivity51aa01d2010-07-20 14:31:20 +030011040 u8 vector;
11041 int type;
11042 bool idtv_info_valid;
11043
11044 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +030011045
Jan Kiszka3ab66e82013-02-20 14:03:24 +010011046 vcpu->arch.nmi_injected = false;
11047 kvm_clear_exception_queue(vcpu);
11048 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +030011049
11050 if (!idtv_info_valid)
11051 return;
11052
Jan Kiszka3ab66e82013-02-20 14:03:24 +010011053 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +030011054
Avi Kivity668f6122008-07-02 09:28:55 +030011055 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
11056 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +030011057
Gleb Natapov64a7ec02009-03-30 16:03:29 +030011058 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +030011059 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010011060 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +030011061 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +030011062 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +030011063 * Clear bit "block by NMI" before VM entry if a NMI
11064 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +030011065 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +010011066 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +030011067 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +030011068 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010011069 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030011070 /* fall through */
11071 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +030011072 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +030011073 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +030011074 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +030011075 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +030011076 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +030011077 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +030011078 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010011079 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030011080 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +030011081 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010011082 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +030011083 break;
11084 default:
11085 break;
Avi Kivityf7d92382008-07-03 16:14:28 +030011086 }
Avi Kivitycf393f72008-07-01 16:20:21 +030011087}
11088
Avi Kivity83422e12010-07-20 14:43:23 +030011089static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
11090{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010011091 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +030011092 VM_EXIT_INSTRUCTION_LEN,
11093 IDT_VECTORING_ERROR_CODE);
11094}
11095
Avi Kivityb463a6f2010-07-20 15:06:17 +030011096static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
11097{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010011098 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +030011099 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
11100 VM_ENTRY_INSTRUCTION_LEN,
11101 VM_ENTRY_EXCEPTION_ERROR_CODE);
11102
11103 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
11104}
11105
Gleb Natapovd7cd9792011-10-05 14:01:23 +020011106static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
11107{
11108 int i, nr_msrs;
11109 struct perf_guest_switch_msr *msrs;
11110
11111 msrs = perf_guest_get_msrs(&nr_msrs);
11112
11113 if (!msrs)
11114 return;
11115
11116 for (i = 0; i < nr_msrs; i++)
11117 if (msrs[i].host == msrs[i].guest)
11118 clear_atomic_switch_msr(vmx, msrs[i].msr);
11119 else
11120 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -040011121 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +020011122}
11123
Sean Christophersonf459a702018-08-27 15:21:11 -070011124static void vmx_arm_hv_timer(struct vcpu_vmx *vmx, u32 val)
11125{
11126 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, val);
11127 if (!vmx->loaded_vmcs->hv_timer_armed)
11128 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11129 PIN_BASED_VMX_PREEMPTION_TIMER);
11130 vmx->loaded_vmcs->hv_timer_armed = true;
11131}
11132
11133static void vmx_update_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -070011134{
11135 struct vcpu_vmx *vmx = to_vmx(vcpu);
11136 u64 tscl;
11137 u32 delta_tsc;
11138
Sean Christophersond264ee02018-08-27 15:21:12 -070011139 if (vmx->req_immediate_exit) {
11140 vmx_arm_hv_timer(vmx, 0);
11141 return;
11142 }
11143
Sean Christophersonf459a702018-08-27 15:21:11 -070011144 if (vmx->hv_deadline_tsc != -1) {
11145 tscl = rdtsc();
11146 if (vmx->hv_deadline_tsc > tscl)
11147 /* set_hv_timer ensures the delta fits in 32-bits */
11148 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
11149 cpu_preemption_timer_multi);
11150 else
11151 delta_tsc = 0;
11152
11153 vmx_arm_hv_timer(vmx, delta_tsc);
Yunhong Jiang64672c92016-06-13 14:19:59 -070011154 return;
Sean Christophersonf459a702018-08-27 15:21:11 -070011155 }
Yunhong Jiang64672c92016-06-13 14:19:59 -070011156
Sean Christophersonf459a702018-08-27 15:21:11 -070011157 if (vmx->loaded_vmcs->hv_timer_armed)
11158 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11159 PIN_BASED_VMX_PREEMPTION_TIMER);
11160 vmx->loaded_vmcs->hv_timer_armed = false;
Yunhong Jiang64672c92016-06-13 14:19:59 -070011161}
11162
Lai Jiangshana3b5ba42011-02-11 14:29:40 +080011163static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080011164{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040011165 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010011166 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020011167
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010011168 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010011169 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010011170 vmx->loaded_vmcs->soft_vnmi_blocked))
11171 vmx->loaded_vmcs->entry_time = ktime_get();
11172
Avi Kivity104f2262010-11-18 13:12:52 +020011173 /* Don't enter VMX if guest state is invalid, let the exit handler
11174 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020011175 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020011176 return;
11177
Radim Krčmářa7653ec2014-08-21 18:08:07 +020011178 if (vmx->ple_window_dirty) {
11179 vmx->ple_window_dirty = false;
11180 vmcs_write32(PLE_WINDOW, vmx->ple_window);
11181 }
11182
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020011183 if (vmx->nested.need_vmcs12_sync) {
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020011184 /*
11185 * hv_evmcs may end up being not mapped after migration (when
11186 * L2 was running), map it here to make sure vmcs12 changes are
11187 * properly reflected.
11188 */
11189 if (vmx->nested.enlightened_vmcs_enabled &&
11190 !vmx->nested.hv_evmcs)
11191 nested_vmx_handle_enlightened_vmptrld(vcpu, false);
11192
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020011193 if (vmx->nested.hv_evmcs) {
11194 copy_vmcs12_to_enlightened(vmx);
11195 /* All fields are clean */
11196 vmx->nested.hv_evmcs->hv_clean_fields |=
11197 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
11198 } else {
11199 copy_vmcs12_to_shadow(vmx);
11200 }
11201 vmx->nested.need_vmcs12_sync = false;
Abel Gordon012f83c2013-04-18 14:39:25 +030011202 }
11203
Avi Kivity104f2262010-11-18 13:12:52 +020011204 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
11205 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
11206 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
11207 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
11208
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070011209 cr3 = __get_current_cr3_fast();
Sean Christophersond7ee0392018-07-23 12:32:47 -070011210 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070011211 vmcs_writel(HOST_CR3, cr3);
Sean Christophersond7ee0392018-07-23 12:32:47 -070011212 vmx->loaded_vmcs->host_state.cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070011213 }
11214
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070011215 cr4 = cr4_read_shadow();
Sean Christophersond7ee0392018-07-23 12:32:47 -070011216 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070011217 vmcs_writel(HOST_CR4, cr4);
Sean Christophersond7ee0392018-07-23 12:32:47 -070011218 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070011219 }
11220
Avi Kivity104f2262010-11-18 13:12:52 +020011221 /* When single-stepping over STI and MOV SS, we must clear the
11222 * corresponding interruptibility bits in the guest state. Otherwise
11223 * vmentry fails as it then expects bit 14 (BS) in pending debug
11224 * exceptions being set, but that's not correct for the guest debugging
11225 * case. */
11226 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
11227 vmx_set_interrupt_shadow(vcpu, 0);
11228
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020011229 if (static_cpu_has(X86_FEATURE_PKU) &&
11230 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
11231 vcpu->arch.pkru != vmx->host_pkru)
11232 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080011233
Gleb Natapovd7cd9792011-10-05 14:01:23 +020011234 atomic_switch_perf_msrs(vmx);
11235
Sean Christophersonf459a702018-08-27 15:21:11 -070011236 vmx_update_hv_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -070011237
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011238 /*
11239 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
11240 * it's non-zero. Since vmentry is serialising on affected CPUs, there
11241 * is no need to worry about the conditional branch over the wrmsr
11242 * being speculatively taken.
11243 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020011244 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011245
Nadav Har'Eld462b812011-05-24 15:26:10 +030011246 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010011247
11248 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
11249 (unsigned long)&current_evmcs->host_rsp : 0;
11250
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020011251 if (static_branch_unlikely(&vmx_l1d_should_flush))
11252 vmx_l1d_flush(vcpu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +020011253
Avi Kivity104f2262010-11-18 13:12:52 +020011254 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080011255 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030011256 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
11257 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
11258 "push %%" _ASM_CX " \n\t"
11259 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030011260 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030011261 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010011262 /* Avoid VMWRITE when Enlightened VMCS is in use */
11263 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
11264 "jz 2f \n\t"
11265 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
11266 "jmp 1f \n\t"
11267 "2: \n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +020011268 __ex("vmwrite %%" _ASM_SP ", %%" _ASM_DX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030011269 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030011270 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030011271 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
11272 "mov %%cr2, %%" _ASM_DX " \n\t"
11273 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010011274 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030011275 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010011276 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080011277 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020011278 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080011279 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030011280 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
11281 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
11282 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
11283 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
11284 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
11285 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080011286#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020011287 "mov %c[r8](%0), %%r8 \n\t"
11288 "mov %c[r9](%0), %%r9 \n\t"
11289 "mov %c[r10](%0), %%r10 \n\t"
11290 "mov %c[r11](%0), %%r11 \n\t"
11291 "mov %c[r12](%0), %%r12 \n\t"
11292 "mov %c[r13](%0), %%r13 \n\t"
11293 "mov %c[r14](%0), %%r14 \n\t"
11294 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080011295#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030011296 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030011297
Avi Kivity6aa8b732006-12-10 02:21:36 -080011298 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030011299 "jne 1f \n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +020011300 __ex("vmlaunch") "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030011301 "jmp 2f \n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +020011302 "1: " __ex("vmresume") "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030011303 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080011304 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030011305 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020011306 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080011307 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030011308 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
11309 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
11310 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
11311 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
11312 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
11313 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
11314 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080011315#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020011316 "mov %%r8, %c[r8](%0) \n\t"
11317 "mov %%r9, %c[r9](%0) \n\t"
11318 "mov %%r10, %c[r10](%0) \n\t"
11319 "mov %%r11, %c[r11](%0) \n\t"
11320 "mov %%r12, %c[r12](%0) \n\t"
11321 "mov %%r13, %c[r13](%0) \n\t"
11322 "mov %%r14, %c[r14](%0) \n\t"
11323 "mov %%r15, %c[r15](%0) \n\t"
Uros Bizjak43ce76c2018-10-17 16:46:57 +020011324 /*
11325 * Clear host registers marked as clobbered to prevent
11326 * speculative use.
11327 */
Jim Mattson0cb5b302018-01-03 14:31:38 -080011328 "xor %%r8d, %%r8d \n\t"
11329 "xor %%r9d, %%r9d \n\t"
11330 "xor %%r10d, %%r10d \n\t"
11331 "xor %%r11d, %%r11d \n\t"
11332 "xor %%r12d, %%r12d \n\t"
11333 "xor %%r13d, %%r13d \n\t"
11334 "xor %%r14d, %%r14d \n\t"
11335 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080011336#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030011337 "mov %%cr2, %%" _ASM_AX " \n\t"
11338 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030011339
Jim Mattson0cb5b302018-01-03 14:31:38 -080011340 "xor %%eax, %%eax \n\t"
11341 "xor %%ebx, %%ebx \n\t"
11342 "xor %%esi, %%esi \n\t"
11343 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030011344 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030011345 ".pushsection .rodata \n\t"
11346 ".global vmx_return \n\t"
11347 "vmx_return: " _ASM_PTR " 2b \n\t"
11348 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010011349 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030011350 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020011351 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +030011352 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080011353 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
11354 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
11355 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
11356 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
11357 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
11358 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
11359 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080011360#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080011361 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
11362 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
11363 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
11364 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
11365 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
11366 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
11367 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
11368 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080011369#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020011370 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
11371 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020011372 : "cc", "memory"
11373#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010011374 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020011375 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030011376#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010011377 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020011378#endif
11379 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080011380
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011381 /*
11382 * We do not use IBRS in the kernel. If this vCPU has used the
11383 * SPEC_CTRL MSR it may have left it on; save the value and
11384 * turn it off. This is much more efficient than blindly adding
11385 * it to the atomic save/restore list. Especially as the former
11386 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
11387 *
11388 * For non-nested case:
11389 * If the L01 MSR bitmap does not intercept the MSR, then we need to
11390 * save it.
11391 *
11392 * For nested case:
11393 * If the L02 MSR bitmap does not intercept the MSR, then we need to
11394 * save it.
11395 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010011396 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010011397 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011398
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020011399 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011400
David Woodhouse117cc7a2018-01-12 11:11:27 +000011401 /* Eliminate branch target predictions from guest mode */
11402 vmexit_fill_RSB();
11403
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010011404 /* All fields are clean at this point */
11405 if (static_branch_unlikely(&enable_evmcs))
11406 current_evmcs->hv_clean_fields |=
11407 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
11408
Gleb Natapov2a7921b2012-08-12 16:12:29 +030011409 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080011410 if (vmx->host_debugctlmsr)
11411 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030011412
Avi Kivityaa67f602012-08-01 16:48:03 +030011413#ifndef CONFIG_X86_64
11414 /*
11415 * The sysexit path does not restore ds/es, so we must set them to
11416 * a reasonable value ourselves.
11417 *
Sean Christopherson6d6095b2018-07-23 12:32:44 -070011418 * We can't defer this to vmx_prepare_switch_to_host() since that
11419 * function may be executed in interrupt context, which saves and
11420 * restore segments around it, nullifying its effect.
Avi Kivityaa67f602012-08-01 16:48:03 +030011421 */
11422 loadsegment(ds, __USER_DS);
11423 loadsegment(es, __USER_DS);
11424#endif
11425
Avi Kivity6de4f3a2009-05-31 22:58:47 +030011426 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020011427 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020011428 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030011429 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020011430 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030011431 vcpu->arch.regs_dirty = 0;
11432
Gleb Natapove0b890d2013-09-25 12:51:33 +030011433 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080011434 * eager fpu is enabled if PKEY is supported and CR4 is switched
11435 * back on host, so it is safe to read guest PKRU from current
11436 * XSAVE.
11437 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020011438 if (static_cpu_has(X86_FEATURE_PKU) &&
11439 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
11440 vcpu->arch.pkru = __read_pkru();
11441 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080011442 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080011443 }
11444
Gleb Natapove0b890d2013-09-25 12:51:33 +030011445 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070011446 vmx->idt_vectoring_info = 0;
11447
11448 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
11449 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
11450 return;
11451
11452 vmx->loaded_vmcs->launched = 1;
11453 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030011454
Avi Kivity51aa01d2010-07-20 14:31:20 +030011455 vmx_complete_atomic_exit(vmx);
11456 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030011457 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080011458}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050011459STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080011460
Sean Christopherson434a1e92018-03-20 12:17:18 -070011461static struct kvm *vmx_vm_alloc(void)
11462{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070011463 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070011464 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070011465}
11466
11467static void vmx_vm_free(struct kvm *kvm)
11468{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070011469 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070011470}
11471
David Hildenbrand1279a6b12017-03-20 10:00:08 +010011472static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020011473{
11474 struct vcpu_vmx *vmx = to_vmx(vcpu);
11475 int cpu;
11476
David Hildenbrand1279a6b12017-03-20 10:00:08 +010011477 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020011478 return;
11479
11480 cpu = get_cpu();
Paolo Bonzini4fa77342014-07-17 12:25:16 +020011481 vmx_vcpu_put(vcpu);
Sean Christophersonbd9966d2018-07-23 12:32:42 -070011482 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020011483 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020011484 put_cpu();
Sean Christophersonb7031fd2018-09-26 09:23:42 -070011485
11486 vm_entry_controls_reset_shadow(vmx);
11487 vm_exit_controls_reset_shadow(vmx);
11488 vmx_segment_cache_clear(vmx);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020011489}
11490
Jim Mattson2f1fe812016-07-08 15:36:06 -070011491/*
11492 * Ensure that the current vmcs of the logical processor is the
11493 * vmcs01 of the vcpu before calling free_nested().
11494 */
11495static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
11496{
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +020011497 vcpu_load(vcpu);
11498 vmx_switch_vmcs(vcpu, &to_vmx(vcpu)->vmcs01);
11499 free_nested(vcpu);
11500 vcpu_put(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070011501}
11502
Avi Kivity6aa8b732006-12-10 02:21:36 -080011503static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
11504{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011505 struct vcpu_vmx *vmx = to_vmx(vcpu);
11506
Kai Huang843e4332015-01-28 10:54:28 +080011507 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080011508 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080011509 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020011510 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070011511 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020011512 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011513 kfree(vmx->guest_msrs);
11514 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100011515 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080011516}
11517
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011518static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080011519{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011520 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100011521 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011522 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030011523 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080011524
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040011525 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011526 return ERR_PTR(-ENOMEM);
11527
Wanpeng Li991e7a02015-09-16 17:30:05 +080011528 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080011529
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011530 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
11531 if (err)
11532 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011533
Peter Feiner4e595162016-07-07 14:49:58 -070011534 err = -ENOMEM;
11535
11536 /*
11537 * If PML is turned on, failure on enabling PML just results in failure
11538 * of creating the vcpu, therefore we can simplify PML logic (by
11539 * avoiding dealing with cases, such as enabling PML partially on vcpus
11540 * for the guest, etc.
11541 */
11542 if (enable_pml) {
11543 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
11544 if (!vmx->pml_pg)
11545 goto uninit_vcpu;
11546 }
11547
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040011548 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020011549 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
11550 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030011551
Peter Feiner4e595162016-07-07 14:49:58 -070011552 if (!vmx->guest_msrs)
11553 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011554
Paolo Bonzinif21f1652018-01-11 12:16:15 +010011555 err = alloc_loaded_vmcs(&vmx->vmcs01);
11556 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011557 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040011558
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011559 msr_bitmap = vmx->vmcs01.msr_bitmap;
11560 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
11561 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
11562 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
11563 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
11564 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
11565 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
11566 vmx->msr_bitmap_mode = 0;
11567
Paolo Bonzinif21f1652018-01-11 12:16:15 +010011568 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030011569 cpu = get_cpu();
11570 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100011571 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020011572 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011573 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030011574 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020011575 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020011576 err = alloc_apic_access_page(kvm);
11577 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020011578 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020011579 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080011580
Sean Christophersone90008d2018-03-05 12:04:37 -080011581 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080011582 err = init_rmode_identity_map(kvm);
11583 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020011584 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080011585 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080011586
Roman Kagan63aff652018-07-19 21:59:07 +030011587 if (nested)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011588 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
11589 kvm_vcpu_apicv_active(&vmx->vcpu));
Wincy Vanb9c237b2015-02-03 23:56:30 +080011590
Wincy Van705699a2015-02-03 23:58:17 +080011591 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030011592 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030011593
Haozhong Zhang37e4c992016-06-22 14:59:55 +080011594 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
11595
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020011596 /*
11597 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
11598 * or POSTED_INTR_WAKEUP_VECTOR.
11599 */
11600 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
11601 vmx->pi_desc.sn = 1;
11602
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011603 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011604
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011605free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080011606 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011607free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011608 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070011609free_pml:
11610 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011611uninit_vcpu:
11612 kvm_vcpu_uninit(&vmx->vcpu);
11613free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080011614 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100011615 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011616 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080011617}
11618
Jiri Kosinad90a7a02018-07-13 16:23:25 +020011619#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"
11620#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 -040011621
Wanpeng Lib31c1142018-03-12 04:53:04 -070011622static int vmx_vm_init(struct kvm *kvm)
11623{
Tianyu Lan877ad952018-07-19 08:40:23 +000011624 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
11625
Wanpeng Lib31c1142018-03-12 04:53:04 -070011626 if (!ple_gap)
11627 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011628
Jiri Kosinad90a7a02018-07-13 16:23:25 +020011629 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
11630 switch (l1tf_mitigation) {
11631 case L1TF_MITIGATION_OFF:
11632 case L1TF_MITIGATION_FLUSH_NOWARN:
11633 /* 'I explicitly don't care' is set */
11634 break;
11635 case L1TF_MITIGATION_FLUSH:
11636 case L1TF_MITIGATION_FLUSH_NOSMT:
11637 case L1TF_MITIGATION_FULL:
11638 /*
11639 * Warn upon starting the first VM in a potentially
11640 * insecure environment.
11641 */
11642 if (cpu_smt_control == CPU_SMT_ENABLED)
11643 pr_warn_once(L1TF_MSG_SMT);
11644 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
11645 pr_warn_once(L1TF_MSG_L1D);
11646 break;
11647 case L1TF_MITIGATION_FULL_FORCE:
11648 /* Flush is enforced */
11649 break;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011650 }
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011651 }
Wanpeng Lib31c1142018-03-12 04:53:04 -070011652 return 0;
11653}
11654
Yang, Sheng002c7f72007-07-31 14:23:01 +030011655static void __init vmx_check_processor_compat(void *rtn)
11656{
11657 struct vmcs_config vmcs_conf;
11658
11659 *(int *)rtn = 0;
11660 if (setup_vmcs_config(&vmcs_conf) < 0)
11661 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010011662 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030011663 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
11664 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
11665 smp_processor_id());
11666 *(int *)rtn = -EIO;
11667 }
11668}
11669
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011670static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080011671{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011672 u8 cache;
11673 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011674
Sheng Yang522c68c2009-04-27 20:35:43 +080011675 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020011676 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080011677 * 2. EPT with VT-d:
11678 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020011679 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080011680 * b. VT-d with snooping control feature: snooping control feature of
11681 * VT-d engine can guarantee the cache correctness. Just set it
11682 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080011683 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080011684 * consistent with host MTRR
11685 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020011686 if (is_mmio) {
11687 cache = MTRR_TYPE_UNCACHABLE;
11688 goto exit;
11689 }
11690
11691 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011692 ipat = VMX_EPT_IPAT_BIT;
11693 cache = MTRR_TYPE_WRBACK;
11694 goto exit;
11695 }
11696
11697 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
11698 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020011699 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080011700 cache = MTRR_TYPE_WRBACK;
11701 else
11702 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011703 goto exit;
11704 }
11705
Xiao Guangrongff536042015-06-15 16:55:22 +080011706 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011707
11708exit:
11709 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080011710}
11711
Sheng Yang17cc3932010-01-05 19:02:27 +080011712static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020011713{
Sheng Yang878403b2010-01-05 19:02:29 +080011714 if (enable_ept && !cpu_has_vmx_ept_1g_page())
11715 return PT_DIRECTORY_LEVEL;
11716 else
11717 /* For shadow and EPT supported 1GB page */
11718 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020011719}
11720
Xiao Guangrongfeda8052015-09-09 14:05:55 +080011721static void vmcs_set_secondary_exec_control(u32 new_ctl)
11722{
11723 /*
11724 * These bits in the secondary execution controls field
11725 * are dynamic, the others are mostly based on the hypervisor
11726 * architecture and the guest's CPUID. Do not touch the
11727 * dynamic bits.
11728 */
11729 u32 mask =
11730 SECONDARY_EXEC_SHADOW_VMCS |
11731 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020011732 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
11733 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080011734
11735 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
11736
11737 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
11738 (new_ctl & ~mask) | (cur_ctl & mask));
11739}
11740
David Matlack8322ebb2016-11-29 18:14:09 -080011741/*
11742 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
11743 * (indicating "allowed-1") if they are supported in the guest's CPUID.
11744 */
11745static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
11746{
11747 struct vcpu_vmx *vmx = to_vmx(vcpu);
11748 struct kvm_cpuid_entry2 *entry;
11749
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011750 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
11751 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080011752
11753#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
11754 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011755 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080011756} while (0)
11757
11758 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
11759 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
11760 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
11761 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
11762 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
11763 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
11764 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
11765 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
11766 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
11767 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
11768 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
11769 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
11770 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
11771 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
11772 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
11773
11774 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
11775 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
11776 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
11777 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
11778 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010011779 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080011780
11781#undef cr4_fixed1_update
11782}
11783
Liran Alon5f76f6f2018-09-14 03:25:52 +030011784static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
11785{
11786 struct vcpu_vmx *vmx = to_vmx(vcpu);
11787
11788 if (kvm_mpx_supported()) {
11789 bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX);
11790
11791 if (mpx_enabled) {
11792 vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
11793 vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
11794 } else {
11795 vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS;
11796 vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS;
11797 }
11798 }
11799}
11800
Sheng Yang0e851882009-12-18 16:48:46 +080011801static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
11802{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011803 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011804
Paolo Bonzini80154d72017-08-24 13:55:35 +020011805 if (cpu_has_secondary_exec_ctrls()) {
11806 vmx_compute_secondary_exec_control(vmx);
11807 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011808 }
Mao, Junjiead756a12012-07-02 01:18:48 +000011809
Haozhong Zhang37e4c992016-06-22 14:59:55 +080011810 if (nested_vmx_allowed(vcpu))
11811 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11812 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
11813 else
11814 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11815 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080011816
Liran Alon5f76f6f2018-09-14 03:25:52 +030011817 if (nested_vmx_allowed(vcpu)) {
David Matlack8322ebb2016-11-29 18:14:09 -080011818 nested_vmx_cr_fixed1_bits_update(vcpu);
Liran Alon5f76f6f2018-09-14 03:25:52 +030011819 nested_vmx_entry_exit_ctls_update(vcpu);
11820 }
Sheng Yang0e851882009-12-18 16:48:46 +080011821}
11822
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011823static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
11824{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030011825 if (func == 1 && nested)
11826 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011827}
11828
Yang Zhang25d92082013-08-06 12:00:32 +030011829static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
11830 struct x86_exception *fault)
11831{
Jan Kiszka533558b2014-01-04 18:47:20 +010011832 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040011833 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010011834 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011835 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030011836
Bandan Dasc5f983f2017-05-05 15:25:14 -040011837 if (vmx->nested.pml_full) {
11838 exit_reason = EXIT_REASON_PML_FULL;
11839 vmx->nested.pml_full = false;
11840 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
11841 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010011842 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030011843 else
Jan Kiszka533558b2014-01-04 18:47:20 +010011844 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011845
11846 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030011847 vmcs12->guest_physical_address = fault->address;
11848}
11849
Peter Feiner995f00a2017-06-30 17:26:32 -070011850static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
11851{
David Hildenbrandbb97a012017-08-10 23:15:28 +020011852 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070011853}
11854
Nadav Har'El155a97a2013-08-05 11:07:16 +030011855/* Callbacks for nested_ept_init_mmu_context: */
11856
11857static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
11858{
11859 /* return the page table to be shadowed - in our case, EPT12 */
11860 return get_vmcs12(vcpu)->ept_pointer;
11861}
11862
Sean Christopherson5b8ba412018-09-26 09:23:40 -070011863static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030011864{
Paolo Bonziniad896af2013-10-02 16:56:14 +020011865 WARN_ON(mmu_is_nested(vcpu));
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011866
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +020011867 vcpu->arch.mmu = &vcpu->arch.guest_mmu;
Paolo Bonziniad896af2013-10-02 16:56:14 +020011868 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011869 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011870 VMX_EPT_EXECUTE_ONLY_BIT,
Junaid Shahid50c28f22018-06-27 14:59:11 -070011871 nested_ept_ad_enabled(vcpu),
11872 nested_ept_get_cr3(vcpu));
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020011873 vcpu->arch.mmu->set_cr3 = vmx_set_cr3;
11874 vcpu->arch.mmu->get_cr3 = nested_ept_get_cr3;
11875 vcpu->arch.mmu->inject_page_fault = nested_ept_inject_page_fault;
Vitaly Kuznetsov3dc773e2018-10-08 21:28:06 +020011876 vcpu->arch.mmu->get_pdptr = kvm_pdptr_read;
Nadav Har'El155a97a2013-08-05 11:07:16 +030011877
11878 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +030011879}
11880
11881static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
11882{
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +020011883 vcpu->arch.mmu = &vcpu->arch.root_mmu;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020011884 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +030011885}
11886
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030011887static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
11888 u16 error_code)
11889{
11890 bool inequality, bit;
11891
11892 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
11893 inequality =
11894 (error_code & vmcs12->page_fault_error_code_mask) !=
11895 vmcs12->page_fault_error_code_match;
11896 return inequality ^ bit;
11897}
11898
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011899static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
11900 struct x86_exception *fault)
11901{
11902 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11903
11904 WARN_ON(!is_guest_mode(vcpu));
11905
Wanpeng Li305d0ab2017-09-28 18:16:44 -070011906 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
11907 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020011908 vmcs12->vm_exit_intr_error_code = fault->error_code;
11909 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11910 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
11911 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
11912 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020011913 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011914 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020011915 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011916}
11917
Paolo Bonzinic9923842017-12-13 14:16:30 +010011918static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11919 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011920
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011921static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011922{
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011923 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011924 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011925 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011926 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011927
11928 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011929 /*
11930 * Translate L1 physical address to host physical
11931 * address for vmcs02. Keep the page pinned, so this
11932 * physical address remains valid. We keep a reference
11933 * to it so we can release it later.
11934 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011935 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011936 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011937 vmx->nested.apic_access_page = NULL;
11938 }
11939 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011940 /*
11941 * If translation failed, no matter: This feature asks
11942 * to exit when accessing the given address, and if it
11943 * can never be accessed, this feature won't do
11944 * anything anyway.
11945 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011946 if (!is_error_page(page)) {
11947 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011948 hpa = page_to_phys(vmx->nested.apic_access_page);
11949 vmcs_write64(APIC_ACCESS_ADDR, hpa);
11950 } else {
11951 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
11952 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
11953 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011954 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011955
11956 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011957 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011958 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011959 vmx->nested.virtual_apic_page = NULL;
11960 }
11961 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011962
11963 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011964 * If translation failed, VM entry will fail because
11965 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
11966 * Failing the vm entry is _not_ what the processor
11967 * does but it's basically the only possibility we
11968 * have. We could still enter the guest if CR8 load
11969 * exits are enabled, CR8 store exits are enabled, and
11970 * virtualize APIC access is disabled; in this case
11971 * the processor would never use the TPR shadow and we
11972 * could simply clear the bit from the execution
11973 * control. But such a configuration is useless, so
11974 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011975 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011976 if (!is_error_page(page)) {
11977 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011978 hpa = page_to_phys(vmx->nested.virtual_apic_page);
11979 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
11980 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011981 }
11982
Wincy Van705699a2015-02-03 23:58:17 +080011983 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011984 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
11985 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011986 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011987 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080011988 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011989 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
11990 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011991 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011992 vmx->nested.pi_desc_page = page;
11993 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080011994 vmx->nested.pi_desc =
11995 (struct pi_desc *)((void *)vmx->nested.pi_desc +
11996 (unsigned long)(vmcs12->posted_intr_desc_addr &
11997 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011998 vmcs_write64(POSTED_INTR_DESC_ADDR,
11999 page_to_phys(vmx->nested.pi_desc_page) +
12000 (unsigned long)(vmcs12->posted_intr_desc_addr &
12001 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080012002 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080012003 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000012004 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
12005 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012006 else
12007 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
12008 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080012009}
12010
Jan Kiszkaf4124502014-03-07 20:03:13 +010012011static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
12012{
12013 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
12014 struct vcpu_vmx *vmx = to_vmx(vcpu);
12015
Sean Christopherson4c008122018-08-27 15:21:10 -070012016 /*
12017 * A timer value of zero is architecturally guaranteed to cause
12018 * a VMExit prior to executing any instructions in the guest.
12019 */
12020 if (preemption_timeout == 0) {
Jan Kiszkaf4124502014-03-07 20:03:13 +010012021 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
12022 return;
12023 }
12024
Sean Christopherson4c008122018-08-27 15:21:10 -070012025 if (vcpu->arch.virtual_tsc_khz == 0)
12026 return;
12027
Jan Kiszkaf4124502014-03-07 20:03:13 +010012028 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
12029 preemption_timeout *= 1000000;
12030 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
12031 hrtimer_start(&vmx->nested.preemption_timer,
12032 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
12033}
12034
Jim Mattson56a20512017-07-06 16:33:06 -070012035static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
12036 struct vmcs12 *vmcs12)
12037{
12038 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
12039 return 0;
12040
12041 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
12042 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
12043 return -EINVAL;
12044
12045 return 0;
12046}
12047
Wincy Van3af18d92015-02-03 23:49:31 +080012048static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
12049 struct vmcs12 *vmcs12)
12050{
Wincy Van3af18d92015-02-03 23:49:31 +080012051 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
12052 return 0;
12053
Jim Mattson5fa99cb2017-07-06 16:33:07 -070012054 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080012055 return -EINVAL;
12056
12057 return 0;
12058}
12059
Jim Mattson712b12d2017-08-24 13:24:47 -070012060static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
12061 struct vmcs12 *vmcs12)
12062{
12063 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
12064 return 0;
12065
12066 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
12067 return -EINVAL;
12068
12069 return 0;
12070}
12071
Wincy Van3af18d92015-02-03 23:49:31 +080012072/*
12073 * Merge L0's and L1's MSR bitmap, return false to indicate that
12074 * we do not use the hardware.
12075 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010012076static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
12077 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080012078{
Wincy Van82f0dd42015-02-03 23:57:18 +080012079 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080012080 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020012081 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010012082 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010012083 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010012084 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010012085 *
12086 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
12087 * ensures that we do not accidentally generate an L02 MSR bitmap
12088 * from the L12 MSR bitmap that is too permissive.
12089 * 2. That L1 or L2s have actually used the MSR. This avoids
12090 * unnecessarily merging of the bitmap if the MSR is unused. This
12091 * works properly because we only update the L01 MSR bitmap lazily.
12092 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
12093 * updated to reflect this when L1 (or its L2s) actually write to
12094 * the MSR.
12095 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000012096 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
12097 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080012098
Paolo Bonzinic9923842017-12-13 14:16:30 +010012099 /* Nothing to do if the MSR bitmap is not in use. */
12100 if (!cpu_has_vmx_msr_bitmap() ||
12101 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
12102 return false;
12103
Ashok Raj15d45072018-02-01 22:59:43 +010012104 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010012105 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080012106 return false;
12107
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020012108 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
12109 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080012110 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010012111
Radim Krčmářd048c092016-08-08 20:16:22 +020012112 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010012113 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
12114 /*
12115 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
12116 * just lets the processor take the value from the virtual-APIC page;
12117 * take those 256 bits directly from the L1 bitmap.
12118 */
12119 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
12120 unsigned word = msr / BITS_PER_LONG;
12121 msr_bitmap_l0[word] = msr_bitmap_l1[word];
12122 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080012123 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010012124 } else {
12125 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
12126 unsigned word = msr / BITS_PER_LONG;
12127 msr_bitmap_l0[word] = ~0;
12128 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
12129 }
12130 }
12131
12132 nested_vmx_disable_intercept_for_msr(
12133 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010012134 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010012135 MSR_TYPE_W);
12136
12137 if (nested_cpu_has_vid(vmcs12)) {
12138 nested_vmx_disable_intercept_for_msr(
12139 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010012140 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010012141 MSR_TYPE_W);
12142 nested_vmx_disable_intercept_for_msr(
12143 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010012144 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010012145 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080012146 }
Ashok Raj15d45072018-02-01 22:59:43 +010012147
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010012148 if (spec_ctrl)
12149 nested_vmx_disable_intercept_for_msr(
12150 msr_bitmap_l1, msr_bitmap_l0,
12151 MSR_IA32_SPEC_CTRL,
12152 MSR_TYPE_R | MSR_TYPE_W);
12153
Ashok Raj15d45072018-02-01 22:59:43 +010012154 if (pred_cmd)
12155 nested_vmx_disable_intercept_for_msr(
12156 msr_bitmap_l1, msr_bitmap_l0,
12157 MSR_IA32_PRED_CMD,
12158 MSR_TYPE_W);
12159
Wincy Vanf2b93282015-02-03 23:56:03 +080012160 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012161 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080012162
12163 return true;
12164}
12165
Liran Alon61ada742018-06-23 02:35:08 +030012166static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
12167 struct vmcs12 *vmcs12)
12168{
12169 struct vmcs12 *shadow;
12170 struct page *page;
12171
12172 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
12173 vmcs12->vmcs_link_pointer == -1ull)
12174 return;
12175
12176 shadow = get_shadow_vmcs12(vcpu);
12177 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
12178
12179 memcpy(shadow, kmap(page), VMCS12_SIZE);
12180
12181 kunmap(page);
12182 kvm_release_page_clean(page);
12183}
12184
12185static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
12186 struct vmcs12 *vmcs12)
12187{
12188 struct vcpu_vmx *vmx = to_vmx(vcpu);
12189
12190 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
12191 vmcs12->vmcs_link_pointer == -1ull)
12192 return;
12193
12194 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
12195 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
12196}
12197
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040012198static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
12199 struct vmcs12 *vmcs12)
12200{
12201 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
12202 !page_address_valid(vcpu, vmcs12->apic_access_addr))
12203 return -EINVAL;
12204 else
12205 return 0;
12206}
12207
Wincy Vanf2b93282015-02-03 23:56:03 +080012208static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
12209 struct vmcs12 *vmcs12)
12210{
Wincy Van82f0dd42015-02-03 23:57:18 +080012211 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080012212 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080012213 !nested_cpu_has_vid(vmcs12) &&
12214 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080012215 return 0;
12216
12217 /*
12218 * If virtualize x2apic mode is enabled,
12219 * virtualize apic access must be disabled.
12220 */
Wincy Van82f0dd42015-02-03 23:57:18 +080012221 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
12222 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080012223 return -EINVAL;
12224
Wincy Van608406e2015-02-03 23:57:51 +080012225 /*
12226 * If virtual interrupt delivery is enabled,
12227 * we must exit on external interrupts.
12228 */
12229 if (nested_cpu_has_vid(vmcs12) &&
12230 !nested_exit_on_intr(vcpu))
12231 return -EINVAL;
12232
Wincy Van705699a2015-02-03 23:58:17 +080012233 /*
12234 * bits 15:8 should be zero in posted_intr_nv,
12235 * the descriptor address has been already checked
12236 * in nested_get_vmcs12_pages.
Krish Sadhukhan6de84e52018-08-23 20:03:03 -040012237 *
12238 * bits 5:0 of posted_intr_desc_addr should be zero.
Wincy Van705699a2015-02-03 23:58:17 +080012239 */
12240 if (nested_cpu_has_posted_intr(vmcs12) &&
12241 (!nested_cpu_has_vid(vmcs12) ||
12242 !nested_exit_intr_ack_set(vcpu) ||
Krish Sadhukhan6de84e52018-08-23 20:03:03 -040012243 (vmcs12->posted_intr_nv & 0xff00) ||
12244 (vmcs12->posted_intr_desc_addr & 0x3f) ||
KarimAllah Ahmed22a7cdc2018-10-20 23:42:59 +020012245 (vmcs12->posted_intr_desc_addr >> cpuid_maxphyaddr(vcpu))))
Wincy Van705699a2015-02-03 23:58:17 +080012246 return -EINVAL;
12247
Wincy Vanf2b93282015-02-03 23:56:03 +080012248 /* tpr shadow is needed by all apicv features. */
12249 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
12250 return -EINVAL;
12251
12252 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080012253}
12254
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012255static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
12256 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030012257 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030012258{
Liran Alone2536742018-06-23 02:35:02 +030012259 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030012260 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012261 u64 count, addr;
12262
Liran Alone2536742018-06-23 02:35:02 +030012263 if (vmcs12_read_any(vmcs12, count_field, &count) ||
12264 vmcs12_read_any(vmcs12, addr_field, &addr)) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012265 WARN_ON(1);
12266 return -EINVAL;
12267 }
12268 if (count == 0)
12269 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030012270 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012271 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
12272 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020012273 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012274 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
12275 addr_field, maxphyaddr, count, addr);
12276 return -EINVAL;
12277 }
12278 return 0;
12279}
12280
12281static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
12282 struct vmcs12 *vmcs12)
12283{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012284 if (vmcs12->vm_exit_msr_load_count == 0 &&
12285 vmcs12->vm_exit_msr_store_count == 0 &&
12286 vmcs12->vm_entry_msr_load_count == 0)
12287 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012288 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030012289 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012290 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030012291 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012292 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030012293 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030012294 return -EINVAL;
12295 return 0;
12296}
12297
Bandan Dasc5f983f2017-05-05 15:25:14 -040012298static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
12299 struct vmcs12 *vmcs12)
12300{
Krish Sadhukhan55c1dcd2018-09-27 14:33:27 -040012301 if (!nested_cpu_has_pml(vmcs12))
12302 return 0;
Bandan Dasc5f983f2017-05-05 15:25:14 -040012303
Krish Sadhukhan55c1dcd2018-09-27 14:33:27 -040012304 if (!nested_cpu_has_ept(vmcs12) ||
12305 !page_address_valid(vcpu, vmcs12->pml_address))
12306 return -EINVAL;
Bandan Dasc5f983f2017-05-05 15:25:14 -040012307
12308 return 0;
12309}
12310
Liran Alona8a7c022018-06-23 02:35:06 +030012311static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
12312 struct vmcs12 *vmcs12)
12313{
12314 if (!nested_cpu_has_shadow_vmcs(vmcs12))
12315 return 0;
12316
12317 if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
12318 !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
12319 return -EINVAL;
12320
12321 return 0;
12322}
12323
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012324static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
12325 struct vmx_msr_entry *e)
12326{
12327 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020012328 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012329 return -EINVAL;
12330 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
12331 e->index == MSR_IA32_UCODE_REV)
12332 return -EINVAL;
12333 if (e->reserved != 0)
12334 return -EINVAL;
12335 return 0;
12336}
12337
12338static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
12339 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030012340{
12341 if (e->index == MSR_FS_BASE ||
12342 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012343 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
12344 nested_vmx_msr_check_common(vcpu, e))
12345 return -EINVAL;
12346 return 0;
12347}
12348
12349static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
12350 struct vmx_msr_entry *e)
12351{
12352 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
12353 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030012354 return -EINVAL;
12355 return 0;
12356}
12357
12358/*
12359 * Load guest's/host's msr at nested entry/exit.
12360 * return 0 for success, entry index for failure.
12361 */
12362static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
12363{
12364 u32 i;
12365 struct vmx_msr_entry e;
12366 struct msr_data msr;
12367
12368 msr.host_initiated = false;
12369 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020012370 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
12371 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020012372 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012373 "%s cannot read MSR entry (%u, 0x%08llx)\n",
12374 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030012375 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012376 }
12377 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020012378 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012379 "%s check failed (%u, 0x%x, 0x%x)\n",
12380 __func__, i, e.index, e.reserved);
12381 goto fail;
12382 }
Wincy Vanff651cb2014-12-11 08:52:58 +030012383 msr.index = e.index;
12384 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012385 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020012386 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012387 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
12388 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030012389 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012390 }
Wincy Vanff651cb2014-12-11 08:52:58 +030012391 }
12392 return 0;
12393fail:
12394 return i + 1;
12395}
12396
12397static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
12398{
12399 u32 i;
12400 struct vmx_msr_entry e;
12401
12402 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020012403 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020012404 if (kvm_vcpu_read_guest(vcpu,
12405 gpa + i * sizeof(e),
12406 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020012407 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012408 "%s cannot read MSR entry (%u, 0x%08llx)\n",
12409 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030012410 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012411 }
12412 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020012413 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012414 "%s check failed (%u, 0x%x, 0x%x)\n",
12415 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030012416 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012417 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020012418 msr_info.host_initiated = false;
12419 msr_info.index = e.index;
12420 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020012421 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012422 "%s cannot read MSR (%u, 0x%x)\n",
12423 __func__, i, e.index);
12424 return -EINVAL;
12425 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020012426 if (kvm_vcpu_write_guest(vcpu,
12427 gpa + i * sizeof(e) +
12428 offsetof(struct vmx_msr_entry, value),
12429 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020012430 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012431 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020012432 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012433 return -EINVAL;
12434 }
Wincy Vanff651cb2014-12-11 08:52:58 +030012435 }
12436 return 0;
12437}
12438
Ladi Prosek1dc35da2016-11-30 16:03:11 +010012439static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
12440{
12441 unsigned long invalid_mask;
12442
12443 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
12444 return (val & invalid_mask) == 0;
12445}
12446
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012447/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012448 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
12449 * emulating VM entry into a guest with EPT enabled.
12450 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
12451 * is assigned to entry_failure_code on failure.
12452 */
12453static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080012454 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012455{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012456 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010012457 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012458 *entry_failure_code = ENTRY_FAIL_DEFAULT;
12459 return 1;
12460 }
12461
12462 /*
12463 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
12464 * must not be dereferenced.
12465 */
12466 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
12467 !nested_ept) {
12468 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
12469 *entry_failure_code = ENTRY_FAIL_PDPTE;
12470 return 1;
12471 }
12472 }
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012473 }
12474
Junaid Shahid50c28f22018-06-27 14:59:11 -070012475 if (!nested_ept)
Junaid Shahidade61e22018-06-27 14:59:15 -070012476 kvm_mmu_new_cr3(vcpu, cr3, false);
Junaid Shahid50c28f22018-06-27 14:59:11 -070012477
12478 vcpu->arch.cr3 = cr3;
12479 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
12480
12481 kvm_init_mmu(vcpu, false);
12482
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012483 return 0;
12484}
12485
Liran Alonefebf0a2018-10-08 23:42:20 +030012486/*
12487 * Returns if KVM is able to config CPU to tag TLB entries
12488 * populated by L2 differently than TLB entries populated
12489 * by L1.
12490 *
12491 * If L1 uses EPT, then TLB entries are tagged with different EPTP.
12492 *
12493 * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged
12494 * with different VPID (L1 entries are tagged with vmx->vpid
12495 * while L2 entries are tagged with vmx->nested.vpid02).
12496 */
12497static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010012498{
Liran Alonefebf0a2018-10-08 23:42:20 +030012499 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012500
Liran Alonefebf0a2018-10-08 23:42:20 +030012501 return nested_cpu_has_ept(vmcs12) ||
12502 (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02);
12503}
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012504
Sean Christopherson3df5c372018-09-26 09:23:44 -070012505static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
12506{
12507 if (vmx->nested.nested_run_pending &&
12508 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
12509 return vmcs12->guest_ia32_efer;
12510 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
12511 return vmx->vcpu.arch.efer | (EFER_LMA | EFER_LME);
12512 else
12513 return vmx->vcpu.arch.efer & ~(EFER_LMA | EFER_LME);
12514}
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012515
Sean Christopherson09abe322018-09-26 09:23:50 -070012516static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010012517{
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012518 /*
Sean Christopherson9d6105b22018-09-26 09:23:51 -070012519 * If vmcs02 hasn't been initialized, set the constant vmcs02 state
Sean Christopherson09abe322018-09-26 09:23:50 -070012520 * according to L0's settings (vmcs12 is irrelevant here). Host
12521 * fields that come from L0 and are not constant, e.g. HOST_CR3,
12522 * will be set as needed prior to VMLAUNCH/VMRESUME.
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012523 */
Sean Christopherson9d6105b22018-09-26 09:23:51 -070012524 if (vmx->nested.vmcs02_initialized)
Sean Christopherson09abe322018-09-26 09:23:50 -070012525 return;
Sean Christopherson9d6105b22018-09-26 09:23:51 -070012526 vmx->nested.vmcs02_initialized = true;
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012527
12528 /*
Sean Christopherson52017602018-09-26 09:23:57 -070012529 * We don't care what the EPTP value is we just need to guarantee
12530 * it's valid so we don't get a false positive when doing early
12531 * consistency checks.
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012532 */
Sean Christopherson52017602018-09-26 09:23:57 -070012533 if (enable_ept && nested_early_check)
12534 vmcs_write64(EPT_POINTER, construct_eptp(&vmx->vcpu, 0));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012535
12536 /* All VMFUNCs are currently emulated through L0 vmexits. */
12537 if (cpu_has_vmx_vmfunc())
12538 vmcs_write64(VM_FUNCTION_CONTROL, 0);
12539
Sean Christopherson09abe322018-09-26 09:23:50 -070012540 if (cpu_has_vmx_posted_intr())
12541 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
12542
12543 if (cpu_has_vmx_msr_bitmap())
12544 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
12545
12546 if (enable_pml)
12547 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012548
12549 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070012550 * Set the MSR load/store lists to match L0's settings. Only the
12551 * addresses are constant (for vmcs02), the counts can change based
12552 * on L2's behavior, e.g. switching to/from long mode.
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012553 */
12554 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040012555 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040012556 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012557
Sean Christopherson09abe322018-09-26 09:23:50 -070012558 vmx_set_constant_host_state(vmx);
12559}
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012560
Sean Christopherson09abe322018-09-26 09:23:50 -070012561static void prepare_vmcs02_early_full(struct vcpu_vmx *vmx,
12562 struct vmcs12 *vmcs12)
12563{
12564 prepare_vmcs02_constant_state(vmx);
12565
12566 vmcs_write64(VMCS_LINK_POINTER, -1ull);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012567
12568 if (enable_vpid) {
12569 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
12570 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
12571 else
12572 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
12573 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +010012574}
12575
Sean Christopherson09abe322018-09-26 09:23:50 -070012576static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012577{
Bandan Das03efce62017-05-05 15:25:15 -040012578 u32 exec_control, vmcs12_exec_ctrl;
Sean Christopherson09abe322018-09-26 09:23:50 -070012579 u64 guest_efer = nested_vmx_calc_efer(vmx, vmcs12);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012580
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020012581 if (vmx->nested.dirty_vmcs12 || vmx->nested.hv_evmcs)
Sean Christopherson09abe322018-09-26 09:23:50 -070012582 prepare_vmcs02_early_full(vmx, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080012583
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012584 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070012585 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
12586 * entry, but only if the current (host) sp changed from the value
12587 * we wrote last (vmx->host_rsp). This cache is no longer relevant
12588 * if we switch vmcs, and rather than hold a separate cache per vmcs,
Sean Christopherson52017602018-09-26 09:23:57 -070012589 * here we just force the write to happen on entry. host_rsp will
12590 * also be written unconditionally by nested_vmx_check_vmentry_hw()
12591 * if we are doing early consistency checks via hardware.
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012592 */
Sean Christopherson09abe322018-09-26 09:23:50 -070012593 vmx->host_rsp = 0;
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012594
Sean Christopherson09abe322018-09-26 09:23:50 -070012595 /*
12596 * PIN CONTROLS
12597 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010012598 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080012599
Sean Christophersonf459a702018-08-27 15:21:11 -070012600 /* Preemption timer setting is computed directly in vmx_vcpu_run. */
Paolo Bonzini93140062016-07-06 13:23:51 +020012601 exec_control |= vmcs_config.pin_based_exec_ctrl;
Sean Christophersonf459a702018-08-27 15:21:11 -070012602 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
12603 vmx->loaded_vmcs->hv_timer_armed = false;
Paolo Bonzini93140062016-07-06 13:23:51 +020012604
12605 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080012606 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080012607 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
12608 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012609 } else {
Wincy Van705699a2015-02-03 23:58:17 +080012610 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012611 }
Jan Kiszkaf4124502014-03-07 20:03:13 +010012612 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012613
Sean Christopherson09abe322018-09-26 09:23:50 -070012614 /*
12615 * EXEC CONTROLS
12616 */
12617 exec_control = vmx_exec_control(vmx); /* L0's desires */
12618 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
12619 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
12620 exec_control &= ~CPU_BASED_TPR_SHADOW;
12621 exec_control |= vmcs12->cpu_based_vm_exec_control;
Jan Kiszka0238ea92013-03-13 11:31:24 +010012622
Sean Christopherson09abe322018-09-26 09:23:50 -070012623 /*
12624 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
12625 * nested_get_vmcs12_pages can't fix it up, the illegal value
12626 * will result in a VM entry failure.
12627 */
12628 if (exec_control & CPU_BASED_TPR_SHADOW) {
12629 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
12630 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
12631 } else {
12632#ifdef CONFIG_X86_64
12633 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
12634 CPU_BASED_CR8_STORE_EXITING;
12635#endif
12636 }
12637
12638 /*
12639 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
12640 * for I/O port accesses.
12641 */
12642 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
12643 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
12644 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
12645
12646 /*
12647 * SECONDARY EXEC CONTROLS
12648 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012649 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020012650 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080012651
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012652 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020012653 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020012654 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010012655 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020012656 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020012657 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040012658 SECONDARY_EXEC_APIC_REGISTER_VIRT |
12659 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012660 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040012661 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
12662 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
12663 ~SECONDARY_EXEC_ENABLE_PML;
12664 exec_control |= vmcs12_exec_ctrl;
12665 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012666
Liran Alon32c7acf2018-06-23 02:35:11 +030012667 /* VMCS shadowing for L2 is emulated for now */
12668 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
12669
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012670 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080012671 vmcs_write16(GUEST_INTR_STATUS,
12672 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080012673
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012674 /*
12675 * Write an illegal value to APIC_ACCESS_ADDR. Later,
12676 * nested_get_vmcs12_pages will either fix it up or
12677 * remove the VM execution control.
12678 */
12679 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
12680 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
12681
Sean Christopherson0b665d32018-08-14 09:33:34 -070012682 if (exec_control & SECONDARY_EXEC_ENCLS_EXITING)
12683 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
12684
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012685 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
12686 }
12687
Jim Mattson83bafef2016-10-04 10:48:38 -070012688 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070012689 * ENTRY CONTROLS
12690 *
Sean Christopherson3df5c372018-09-26 09:23:44 -070012691 * vmcs12's VM_{ENTRY,EXIT}_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE
Sean Christopherson09abe322018-09-26 09:23:50 -070012692 * are emulated by vmx_set_efer() in prepare_vmcs02(), but speculate
12693 * on the related bits (if supported by the CPU) in the hope that
12694 * we can avoid VMWrites during vmx_set_efer().
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012695 */
Sean Christopherson3df5c372018-09-26 09:23:44 -070012696 exec_control = (vmcs12->vm_entry_controls | vmcs_config.vmentry_ctrl) &
12697 ~VM_ENTRY_IA32E_MODE & ~VM_ENTRY_LOAD_IA32_EFER;
12698 if (cpu_has_load_ia32_efer) {
12699 if (guest_efer & EFER_LMA)
12700 exec_control |= VM_ENTRY_IA32E_MODE;
12701 if (guest_efer != host_efer)
12702 exec_control |= VM_ENTRY_LOAD_IA32_EFER;
12703 }
12704 vm_entry_controls_init(vmx, exec_control);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012705
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012706 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070012707 * EXIT CONTROLS
12708 *
12709 * L2->L1 exit controls are emulated - the hardware exit is to L0 so
12710 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
12711 * bits may be modified by vmx_set_efer() in prepare_vmcs02().
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012712 */
Sean Christopherson09abe322018-09-26 09:23:50 -070012713 exec_control = vmcs_config.vmexit_ctrl;
12714 if (cpu_has_load_ia32_efer && guest_efer != host_efer)
12715 exec_control |= VM_EXIT_LOAD_IA32_EFER;
12716 vm_exit_controls_init(vmx, exec_control);
12717
12718 /*
12719 * Conceptually we want to copy the PML address and index from
12720 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
12721 * since we always flush the log on each vmexit and never change
12722 * the PML address (once set), this happens to be equivalent to
12723 * simply resetting the index in vmcs02.
12724 */
12725 if (enable_pml)
12726 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
12727
12728 /*
12729 * Interrupt/Exception Fields
12730 */
12731 if (vmx->nested.nested_run_pending) {
12732 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
12733 vmcs12->vm_entry_intr_info_field);
12734 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
12735 vmcs12->vm_entry_exception_error_code);
12736 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
12737 vmcs12->vm_entry_instruction_len);
12738 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
12739 vmcs12->guest_interruptibility_info);
12740 vmx->loaded_vmcs->nmi_known_unmasked =
12741 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattson51aa68e2017-09-12 13:02:54 -070012742 } else {
Sean Christopherson09abe322018-09-26 09:23:50 -070012743 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
12744 }
12745}
12746
12747static void prepare_vmcs02_full(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
12748{
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020012749 struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
Sean Christopherson09abe322018-09-26 09:23:50 -070012750
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020012751 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
12752 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) {
12753 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
Vitaly Kuznetsovcbe3f892018-10-19 16:16:03 +020012754 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020012755 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
12756 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
12757 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
12758 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
12759 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
12760 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
12761 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
Vitaly Kuznetsovcbe3f892018-10-19 16:16:03 +020012762 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020012763 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
12764 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
12765 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
12766 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
12767 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
12768 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
12769 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
12770 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
12771 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020012772 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
12773 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
12774 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
12775 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
12776 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
Vitaly Kuznetsovcbe3f892018-10-19 16:16:03 +020012777 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
12778 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020012779 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
12780 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
12781 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
12782 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
12783 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
12784 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
12785 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
12786 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
12787 }
12788
12789 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
12790 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1)) {
12791 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
12792 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
12793 vmcs12->guest_pending_dbg_exceptions);
12794 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
12795 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
12796
12797 /*
12798 * L1 may access the L2's PDPTR, so save them to construct
12799 * vmcs12
12800 */
12801 if (enable_ept) {
12802 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
12803 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
12804 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
12805 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
12806 }
12807 }
Sean Christopherson09abe322018-09-26 09:23:50 -070012808
12809 if (nested_cpu_has_xsaves(vmcs12))
12810 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
12811
12812 /*
12813 * Whether page-faults are trapped is determined by a combination of
12814 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
12815 * If enable_ept, L0 doesn't care about page faults and we should
12816 * set all of these to L1's desires. However, if !enable_ept, L0 does
12817 * care about (at least some) page faults, and because it is not easy
12818 * (if at all possible?) to merge L0 and L1's desires, we simply ask
12819 * to exit on each and every L2 page fault. This is done by setting
12820 * MASK=MATCH=0 and (see below) EB.PF=1.
12821 * Note that below we don't need special code to set EB.PF beyond the
12822 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
12823 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
12824 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
12825 */
12826 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
12827 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
12828 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
12829 enable_ept ? vmcs12->page_fault_error_code_match : 0);
12830
12831 if (cpu_has_vmx_apicv()) {
12832 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
12833 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
12834 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
12835 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
12836 }
12837
12838 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
12839 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
12840
12841 set_cr4_guest_host_mask(vmx);
12842
12843 if (kvm_mpx_supported()) {
12844 if (vmx->nested.nested_run_pending &&
12845 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
12846 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
12847 else
12848 vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs);
12849 }
Sean Christopherson09abe322018-09-26 09:23:50 -070012850}
12851
12852/*
12853 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
12854 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
12855 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
12856 * guest in a way that will both be appropriate to L1's requests, and our
12857 * needs. In addition to modifying the active vmcs (which is vmcs02), this
12858 * function also has additional necessary side-effects, like setting various
12859 * vcpu->arch fields.
12860 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
12861 * is assigned to entry_failure_code on failure.
12862 */
12863static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12864 u32 *entry_failure_code)
12865{
12866 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020012867 struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
Sean Christopherson09abe322018-09-26 09:23:50 -070012868
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020012869 if (vmx->nested.dirty_vmcs12 || vmx->nested.hv_evmcs) {
Sean Christopherson09abe322018-09-26 09:23:50 -070012870 prepare_vmcs02_full(vmx, vmcs12);
12871 vmx->nested.dirty_vmcs12 = false;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012872 }
12873
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012874 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070012875 * First, the fields that are shadowed. This must be kept in sync
12876 * with vmx_shadow_fields.h.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012877 */
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020012878 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
12879 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) {
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020012880 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Vitaly Kuznetsovcbe3f892018-10-19 16:16:03 +020012881 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020012882 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012883
Sean Christopherson09abe322018-09-26 09:23:50 -070012884 if (vmx->nested.nested_run_pending &&
12885 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
12886 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
12887 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
12888 } else {
12889 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
12890 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
12891 }
12892 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
12893
12894 vmx->nested.preemption_timer_expired = false;
12895 if (nested_cpu_has_preemption_timer(vmcs12))
12896 vmx_start_preemption_timer(vcpu);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012897
12898 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
12899 * bitwise-or of what L1 wants to trap for L2, and what we want to
12900 * trap. Note that CR0.TS also needs updating - we do this later.
12901 */
12902 update_exception_bitmap(vcpu);
12903 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
12904 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
12905
Jim Mattson6514dc32018-04-26 16:09:12 -070012906 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012907 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012908 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012909 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012910 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012911 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012912 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012913
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012914 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
12915
Peter Feinerc95ba922016-08-17 09:36:47 -070012916 if (kvm_has_tsc_control)
12917 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012918
12919 if (enable_vpid) {
12920 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070012921 * There is no direct mapping between vpid02 and vpid12, the
12922 * vpid02 is per-vCPU for L0 and reused while the value of
12923 * vpid12 is changed w/ one invvpid during nested vmentry.
12924 * The vpid12 is allocated by L1 for L2, so it will not
12925 * influence global bitmap(for vpid01 and vpid02 allocation)
12926 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012927 */
Liran Alonefebf0a2018-10-08 23:42:20 +030012928 if (nested_cpu_has_vpid(vmcs12) && nested_has_guest_tlb_tag(vcpu)) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070012929 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
12930 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alonefebf0a2018-10-08 23:42:20 +030012931 __vmx_flush_tlb(vcpu, nested_get_vpid02(vcpu), false);
Wanpeng Li5c614b32015-10-13 09:18:36 -070012932 }
12933 } else {
Liran Alon14389212018-10-08 23:42:17 +030012934 /*
12935 * If L1 use EPT, then L0 needs to execute INVEPT on
12936 * EPTP02 instead of EPTP01. Therefore, delay TLB
12937 * flush until vmcs02->eptp is fully updated by
12938 * KVM_REQ_LOAD_CR3. Note that this assumes
12939 * KVM_REQ_TLB_FLUSH is evaluated after
12940 * KVM_REQ_LOAD_CR3 in vcpu_enter_guest().
12941 */
12942 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Wanpeng Li5c614b32015-10-13 09:18:36 -070012943 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012944 }
12945
Sean Christopherson5b8ba412018-09-26 09:23:40 -070012946 if (nested_cpu_has_ept(vmcs12))
12947 nested_ept_init_mmu_context(vcpu);
12948 else if (nested_cpu_has2(vmcs12,
12949 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Junaid Shahida468f2d2018-04-26 13:09:50 -070012950 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030012951
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012952 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012953 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
12954 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012955 * The CR0_READ_SHADOW is what L2 should have expected to read given
12956 * the specifications by L1; It's not enough to take
12957 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
12958 * have more bits than L1 expected.
12959 */
12960 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
12961 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
12962
12963 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
12964 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
12965
Sean Christopherson09abe322018-09-26 09:23:50 -070012966 vcpu->arch.efer = nested_vmx_calc_efer(vmx, vmcs12);
Sean Christopherson3df5c372018-09-26 09:23:44 -070012967 /* Note: may modify VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
David Matlack5a6a9742016-11-29 18:14:10 -080012968 vmx_set_efer(vcpu, vcpu->arch.efer);
12969
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012970 /*
12971 * Guest state is invalid and unrestricted guest is disabled,
12972 * which means L1 attempted VMEntry to L2 with invalid state.
12973 * Fail the VMEntry.
12974 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010012975 if (vmx->emulation_required) {
12976 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012977 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010012978 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012979
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012980 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010012981 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012982 entry_failure_code))
12983 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010012984
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012985 if (!enable_ept)
12986 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
12987
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012988 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
12989 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010012990 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012991}
12992
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050012993static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
12994{
12995 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
12996 nested_cpu_has_virtual_nmis(vmcs12))
12997 return -EINVAL;
12998
12999 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
13000 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
13001 return -EINVAL;
13002
13003 return 0;
13004}
13005
Jim Mattsonca0bde22016-11-30 12:03:46 -080013006static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
13007{
13008 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson64a919f2018-09-26 09:23:39 -070013009 bool ia32e;
Jim Mattsonca0bde22016-11-30 12:03:46 -080013010
13011 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
13012 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
13013 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13014
Krish Sadhukhanba8e23d2018-09-04 14:42:58 -040013015 if (nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id)
13016 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13017
Jim Mattson56a20512017-07-06 16:33:06 -070013018 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
13019 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13020
Jim Mattsonca0bde22016-11-30 12:03:46 -080013021 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
13022 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13023
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040013024 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
13025 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13026
Jim Mattson712b12d2017-08-24 13:24:47 -070013027 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
13028 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13029
Jim Mattsonca0bde22016-11-30 12:03:46 -080013030 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
13031 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13032
13033 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
13034 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13035
Bandan Dasc5f983f2017-05-05 15:25:14 -040013036 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
13037 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13038
Liran Alona8a7c022018-06-23 02:35:06 +030013039 if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
13040 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13041
Jim Mattsonca0bde22016-11-30 12:03:46 -080013042 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010013043 vmx->nested.msrs.procbased_ctls_low,
13044 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070013045 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
13046 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010013047 vmx->nested.msrs.secondary_ctls_low,
13048 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080013049 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010013050 vmx->nested.msrs.pinbased_ctls_low,
13051 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080013052 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010013053 vmx->nested.msrs.exit_ctls_low,
13054 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080013055 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010013056 vmx->nested.msrs.entry_ctls_low,
13057 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080013058 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13059
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050013060 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080013061 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13062
Bandan Das41ab9372017-08-03 15:54:43 -040013063 if (nested_cpu_has_vmfunc(vmcs12)) {
13064 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010013065 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040013066 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13067
13068 if (nested_cpu_has_eptp_switching(vmcs12)) {
13069 if (!nested_cpu_has_ept(vmcs12) ||
13070 !page_address_valid(vcpu, vmcs12->eptp_list_address))
13071 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13072 }
13073 }
Bandan Das27c42a12017-08-03 15:54:42 -040013074
Jim Mattsonc7c2c702017-05-05 11:28:09 -070013075 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
13076 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13077
Jim Mattsonca0bde22016-11-30 12:03:46 -080013078 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
13079 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
13080 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
13081 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
13082
Marc Orr04473782018-06-20 17:21:29 -070013083 /*
Sean Christopherson64a919f2018-09-26 09:23:39 -070013084 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
13085 * IA32_EFER MSR must be 0 in the field for that register. In addition,
13086 * the values of the LMA and LME bits in the field must each be that of
13087 * the host address-space size VM-exit control.
13088 */
13089 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
13090 ia32e = (vmcs12->vm_exit_controls &
13091 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
13092 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
13093 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
13094 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
13095 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
13096 }
13097
13098 /*
Marc Orr04473782018-06-20 17:21:29 -070013099 * From the Intel SDM, volume 3:
13100 * Fields relevant to VM-entry event injection must be set properly.
13101 * These fields are the VM-entry interruption-information field, the
13102 * VM-entry exception error code, and the VM-entry instruction length.
13103 */
13104 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
13105 u32 intr_info = vmcs12->vm_entry_intr_info_field;
13106 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
13107 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
13108 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
13109 bool should_have_error_code;
13110 bool urg = nested_cpu_has2(vmcs12,
13111 SECONDARY_EXEC_UNRESTRICTED_GUEST);
13112 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
13113
13114 /* VM-entry interruption-info field: interruption type */
13115 if (intr_type == INTR_TYPE_RESERVED ||
13116 (intr_type == INTR_TYPE_OTHER_EVENT &&
13117 !nested_cpu_supports_monitor_trap_flag(vcpu)))
13118 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13119
13120 /* VM-entry interruption-info field: vector */
13121 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
13122 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
13123 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
13124 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13125
13126 /* VM-entry interruption-info field: deliver error code */
13127 should_have_error_code =
13128 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
13129 x86_exception_has_error_code(vector);
13130 if (has_error_code != should_have_error_code)
13131 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13132
13133 /* VM-entry exception error code */
13134 if (has_error_code &&
13135 vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
13136 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13137
13138 /* VM-entry interruption-info field: reserved bits */
13139 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
13140 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13141
13142 /* VM-entry instruction length */
13143 switch (intr_type) {
13144 case INTR_TYPE_SOFT_EXCEPTION:
13145 case INTR_TYPE_SOFT_INTR:
13146 case INTR_TYPE_PRIV_SW_EXCEPTION:
13147 if ((vmcs12->vm_entry_instruction_len > 15) ||
13148 (vmcs12->vm_entry_instruction_len == 0 &&
13149 !nested_cpu_has_zero_length_injection(vcpu)))
13150 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13151 }
13152 }
13153
Sean Christopherson5b8ba412018-09-26 09:23:40 -070013154 if (nested_cpu_has_ept(vmcs12) &&
13155 !valid_ept_address(vcpu, vmcs12->ept_pointer))
13156 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13157
Jim Mattsonca0bde22016-11-30 12:03:46 -080013158 return 0;
13159}
13160
Liran Alonf145d902018-06-23 02:35:07 +030013161static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
13162 struct vmcs12 *vmcs12)
13163{
13164 int r;
13165 struct page *page;
13166 struct vmcs12 *shadow;
13167
13168 if (vmcs12->vmcs_link_pointer == -1ull)
13169 return 0;
13170
13171 if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
13172 return -EINVAL;
13173
13174 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
13175 if (is_error_page(page))
13176 return -EINVAL;
13177
13178 r = 0;
13179 shadow = kmap(page);
13180 if (shadow->hdr.revision_id != VMCS12_REVISION ||
13181 shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
13182 r = -EINVAL;
13183 kunmap(page);
13184 kvm_release_page_clean(page);
13185 return r;
13186}
13187
Jim Mattsonca0bde22016-11-30 12:03:46 -080013188static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
13189 u32 *exit_qual)
13190{
13191 bool ia32e;
13192
13193 *exit_qual = ENTRY_FAIL_DEFAULT;
13194
13195 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
13196 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
13197 return 1;
13198
Liran Alonf145d902018-06-23 02:35:07 +030013199 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Jim Mattsonca0bde22016-11-30 12:03:46 -080013200 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
13201 return 1;
13202 }
13203
13204 /*
13205 * If the load IA32_EFER VM-entry control is 1, the following checks
13206 * are performed on the field for the IA32_EFER MSR:
13207 * - Bits reserved in the IA32_EFER MSR must be 0.
13208 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
13209 * the IA-32e mode guest VM-exit control. It must also be identical
13210 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
13211 * CR0.PG) is 1.
13212 */
13213 if (to_vmx(vcpu)->nested.nested_run_pending &&
13214 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
13215 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
13216 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
13217 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
13218 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
13219 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
13220 return 1;
13221 }
13222
Wanpeng Lif1b026a2017-11-05 16:54:48 -080013223 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
13224 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
13225 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
13226 return 1;
13227
Jim Mattsonca0bde22016-11-30 12:03:46 -080013228 return 0;
13229}
13230
Sean Christopherson52017602018-09-26 09:23:57 -070013231static int __noclone nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu)
13232{
13233 struct vcpu_vmx *vmx = to_vmx(vcpu);
13234 unsigned long cr3, cr4;
13235
13236 if (!nested_early_check)
13237 return 0;
13238
13239 if (vmx->msr_autoload.host.nr)
13240 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
13241 if (vmx->msr_autoload.guest.nr)
13242 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
13243
13244 preempt_disable();
13245
13246 vmx_prepare_switch_to_guest(vcpu);
13247
13248 /*
13249 * Induce a consistency check VMExit by clearing bit 1 in GUEST_RFLAGS,
13250 * which is reserved to '1' by hardware. GUEST_RFLAGS is guaranteed to
13251 * be written (by preparve_vmcs02()) before the "real" VMEnter, i.e.
13252 * there is no need to preserve other bits or save/restore the field.
13253 */
13254 vmcs_writel(GUEST_RFLAGS, 0);
13255
13256 vmcs_writel(HOST_RIP, vmx_early_consistency_check_return);
13257
13258 cr3 = __get_current_cr3_fast();
13259 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
13260 vmcs_writel(HOST_CR3, cr3);
13261 vmx->loaded_vmcs->host_state.cr3 = cr3;
13262 }
13263
13264 cr4 = cr4_read_shadow();
13265 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
13266 vmcs_writel(HOST_CR4, cr4);
13267 vmx->loaded_vmcs->host_state.cr4 = cr4;
13268 }
13269
13270 vmx->__launched = vmx->loaded_vmcs->launched;
13271
13272 asm(
13273 /* Set HOST_RSP */
Uros Bizjak4b1e5472018-10-11 19:40:44 +020013274 __ex("vmwrite %%" _ASM_SP ", %%" _ASM_DX) "\n\t"
Sean Christopherson52017602018-09-26 09:23:57 -070013275 "mov %%" _ASM_SP ", %c[host_rsp](%0)\n\t"
13276
13277 /* Check if vmlaunch of vmresume is needed */
13278 "cmpl $0, %c[launched](%0)\n\t"
13279 "je 1f\n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +020013280 __ex("vmresume") "\n\t"
Sean Christopherson52017602018-09-26 09:23:57 -070013281 "jmp 2f\n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +020013282 "1: " __ex("vmlaunch") "\n\t"
Sean Christopherson52017602018-09-26 09:23:57 -070013283 "jmp 2f\n\t"
13284 "2: "
13285
13286 /* Set vmx->fail accordingly */
13287 "setbe %c[fail](%0)\n\t"
13288
13289 ".pushsection .rodata\n\t"
13290 ".global vmx_early_consistency_check_return\n\t"
13291 "vmx_early_consistency_check_return: " _ASM_PTR " 2b\n\t"
13292 ".popsection"
13293 :
13294 : "c"(vmx), "d"((unsigned long)HOST_RSP),
13295 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
13296 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
13297 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp))
13298 : "rax", "cc", "memory"
13299 );
13300
13301 vmcs_writel(HOST_RIP, vmx_return);
13302
13303 preempt_enable();
13304
13305 if (vmx->msr_autoload.host.nr)
13306 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
13307 if (vmx->msr_autoload.guest.nr)
13308 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
13309
13310 if (vmx->fail) {
13311 WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
13312 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
13313 vmx->fail = 0;
13314 return 1;
13315 }
13316
13317 /*
13318 * VMExit clears RFLAGS.IF and DR7, even on a consistency check.
13319 */
13320 local_irq_enable();
13321 if (hw_breakpoint_active())
13322 set_debugreg(__this_cpu_read(cpu_dr7), 7);
13323
13324 /*
13325 * A non-failing VMEntry means we somehow entered guest mode with
13326 * an illegal RIP, and that's just the tip of the iceberg. There
13327 * is no telling what memory has been modified or what state has
13328 * been exposed to unknown code. Hitting this all but guarantees
13329 * a (very critical) hardware issue.
13330 */
13331 WARN_ON(!(vmcs_read32(VM_EXIT_REASON) &
13332 VMX_EXIT_REASONS_FAILED_VMENTRY));
13333
13334 return 0;
13335}
13336STACK_FRAME_NON_STANDARD(nested_vmx_check_vmentry_hw);
13337
Sean Christophersona633e412018-09-26 09:23:47 -070013338static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
13339 struct vmcs12 *vmcs12);
13340
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013341/*
Sean Christophersona633e412018-09-26 09:23:47 -070013342 * If from_vmentry is false, this is being called from state restore (either RSM
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013343 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Sean Christopherson52017602018-09-26 09:23:57 -070013344+ *
13345+ * Returns:
13346+ * 0 - success, i.e. proceed with actual VMEnter
13347+ * 1 - consistency check VMExit
13348+ * -1 - consistency check VMFail
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013349 */
Sean Christophersona633e412018-09-26 09:23:47 -070013350static int nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu,
13351 bool from_vmentry)
Jim Mattson858e25c2016-11-30 12:03:47 -080013352{
13353 struct vcpu_vmx *vmx = to_vmx(vcpu);
13354 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzini7e712682018-10-03 13:44:26 +020013355 bool evaluate_pending_interrupts;
Sean Christophersona633e412018-09-26 09:23:47 -070013356 u32 exit_reason = EXIT_REASON_INVALID_STATE;
13357 u32 exit_qual;
Jim Mattson858e25c2016-11-30 12:03:47 -080013358
Paolo Bonzini7e712682018-10-03 13:44:26 +020013359 evaluate_pending_interrupts = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
13360 (CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_VIRTUAL_NMI_PENDING);
13361 if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu))
13362 evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu);
Liran Alonb5861e52018-09-03 15:20:22 +030013363
Jim Mattson858e25c2016-11-30 12:03:47 -080013364 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
13365 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
Liran Alon62cf9bd812018-09-14 03:25:54 +030013366 if (kvm_mpx_supported() &&
13367 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
13368 vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattson858e25c2016-11-30 12:03:47 -080013369
Jim Mattsonde3a0022017-11-27 17:22:25 -060013370 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080013371
Sean Christopherson16fb9a42018-09-26 09:23:52 -070013372 prepare_vmcs02_early(vmx, vmcs12);
Jim Mattson858e25c2016-11-30 12:03:47 -080013373
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013374 if (from_vmentry) {
13375 nested_get_vmcs12_pages(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080013376
Sean Christopherson52017602018-09-26 09:23:57 -070013377 if (nested_vmx_check_vmentry_hw(vcpu)) {
13378 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
13379 return -1;
13380 }
13381
Sean Christopherson16fb9a42018-09-26 09:23:52 -070013382 if (check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
13383 goto vmentry_fail_vmexit;
13384 }
13385
13386 enter_guest_mode(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080013387 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
13388 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
13389
Sean Christophersona633e412018-09-26 09:23:47 -070013390 if (prepare_vmcs02(vcpu, vmcs12, &exit_qual))
Sean Christopherson39f9c382018-09-26 09:23:48 -070013391 goto vmentry_fail_vmexit_guest_mode;
Jim Mattson858e25c2016-11-30 12:03:47 -080013392
13393 if (from_vmentry) {
Sean Christophersona633e412018-09-26 09:23:47 -070013394 exit_reason = EXIT_REASON_MSR_LOAD_FAIL;
13395 exit_qual = nested_vmx_load_msr(vcpu,
13396 vmcs12->vm_entry_msr_load_addr,
13397 vmcs12->vm_entry_msr_load_count);
13398 if (exit_qual)
Sean Christopherson39f9c382018-09-26 09:23:48 -070013399 goto vmentry_fail_vmexit_guest_mode;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013400 } else {
13401 /*
13402 * The MMU is not initialized to point at the right entities yet and
13403 * "get pages" would need to read data from the guest (i.e. we will
13404 * need to perform gpa to hpa translation). Request a call
13405 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
13406 * have already been set at vmentry time and should not be reset.
13407 */
13408 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
13409 }
Jim Mattson858e25c2016-11-30 12:03:47 -080013410
Jim Mattson858e25c2016-11-30 12:03:47 -080013411 /*
Liran Alonb5861e52018-09-03 15:20:22 +030013412 * If L1 had a pending IRQ/NMI until it executed
13413 * VMLAUNCH/VMRESUME which wasn't delivered because it was
13414 * disallowed (e.g. interrupts disabled), L0 needs to
13415 * evaluate if this pending event should cause an exit from L2
13416 * to L1 or delivered directly to L2 (e.g. In case L1 don't
13417 * intercept EXTERNAL_INTERRUPT).
13418 *
Paolo Bonzini7e712682018-10-03 13:44:26 +020013419 * Usually this would be handled by the processor noticing an
13420 * IRQ/NMI window request, or checking RVI during evaluation of
13421 * pending virtual interrupts. However, this setting was done
13422 * on VMCS01 and now VMCS02 is active instead. Thus, we force L0
13423 * to perform pending event evaluation by requesting a KVM_REQ_EVENT.
Liran Alonb5861e52018-09-03 15:20:22 +030013424 */
Paolo Bonzini7e712682018-10-03 13:44:26 +020013425 if (unlikely(evaluate_pending_interrupts))
Liran Alonb5861e52018-09-03 15:20:22 +030013426 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alonb5861e52018-09-03 15:20:22 +030013427
13428 /*
Jim Mattson858e25c2016-11-30 12:03:47 -080013429 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
13430 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
13431 * returned as far as L1 is concerned. It will only return (and set
13432 * the success flag) when L2 exits (see nested_vmx_vmexit()).
13433 */
13434 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013435
Sean Christophersona633e412018-09-26 09:23:47 -070013436 /*
13437 * A failed consistency check that leads to a VMExit during L1's
13438 * VMEnter to L2 is a variation of a normal VMexit, as explained in
13439 * 26.7 "VM-entry failures during or after loading guest state".
13440 */
Sean Christopherson39f9c382018-09-26 09:23:48 -070013441vmentry_fail_vmexit_guest_mode:
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013442 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
13443 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
13444 leave_guest_mode(vcpu);
Sean Christopherson16fb9a42018-09-26 09:23:52 -070013445
13446vmentry_fail_vmexit:
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013447 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Sean Christophersona633e412018-09-26 09:23:47 -070013448
13449 if (!from_vmentry)
13450 return 1;
13451
Sean Christophersona633e412018-09-26 09:23:47 -070013452 load_vmcs12_host_state(vcpu, vmcs12);
13453 vmcs12->vm_exit_reason = exit_reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
13454 vmcs12->exit_qualification = exit_qual;
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020013455 if (enable_shadow_vmcs || vmx->nested.hv_evmcs)
13456 vmx->nested.need_vmcs12_sync = true;
Sean Christophersona633e412018-09-26 09:23:47 -070013457 return 1;
Jim Mattson858e25c2016-11-30 12:03:47 -080013458}
13459
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030013460/*
13461 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
13462 * for running an L2 nested guest.
13463 */
13464static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
13465{
13466 struct vmcs12 *vmcs12;
13467 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070013468 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080013469 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030013470
Kyle Hueyeb277562016-11-29 12:40:39 -080013471 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030013472 return 1;
13473
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020013474 if (!nested_vmx_handle_enlightened_vmptrld(vcpu, true))
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +020013475 return 1;
13476
13477 if (!vmx->nested.hv_evmcs && vmx->nested.current_vmptr == -1ull)
Sean Christopherson09abb5e2018-09-26 09:23:55 -070013478 return nested_vmx_failInvalid(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -080013479
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030013480 vmcs12 = get_vmcs12(vcpu);
13481
Liran Alona6192d42018-06-23 02:35:04 +030013482 /*
13483 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
13484 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
13485 * rather than RFLAGS.ZF, and no error number is stored to the
13486 * VM-instruction error field.
13487 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -070013488 if (vmcs12->hdr.shadow_vmcs)
13489 return nested_vmx_failInvalid(vcpu);
Liran Alona6192d42018-06-23 02:35:04 +030013490
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020013491 if (vmx->nested.hv_evmcs) {
13492 copy_enlightened_to_vmcs12(vmx);
13493 /* Enlightened VMCS doesn't have launch state */
13494 vmcs12->launch_state = !launch;
13495 } else if (enable_shadow_vmcs) {
Abel Gordon012f83c2013-04-18 14:39:25 +030013496 copy_shadow_to_vmcs12(vmx);
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020013497 }
Abel Gordon012f83c2013-04-18 14:39:25 +030013498
Nadav Har'El7c177932011-05-25 23:12:04 +030013499 /*
13500 * The nested entry process starts with enforcing various prerequisites
13501 * on vmcs12 as required by the Intel SDM, and act appropriately when
13502 * they fail: As the SDM explains, some conditions should cause the
13503 * instruction to fail, while others will cause the instruction to seem
13504 * to succeed, but return an EXIT_REASON_INVALID_STATE.
13505 * To speed up the normal (success) code path, we should avoid checking
13506 * for misconfigurations which will anyway be caught by the processor
13507 * when using the merged vmcs02.
13508 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -070013509 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS)
13510 return nested_vmx_failValid(vcpu,
13511 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070013512
Sean Christopherson09abb5e2018-09-26 09:23:55 -070013513 if (vmcs12->launch_state == launch)
13514 return nested_vmx_failValid(vcpu,
Nadav Har'El7c177932011-05-25 23:12:04 +030013515 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
13516 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Nadav Har'El7c177932011-05-25 23:12:04 +030013517
Jim Mattsonca0bde22016-11-30 12:03:46 -080013518 ret = check_vmentry_prereqs(vcpu, vmcs12);
Sean Christopherson09abb5e2018-09-26 09:23:55 -070013519 if (ret)
13520 return nested_vmx_failValid(vcpu, ret);
Jan Kiszka384bb782013-04-20 10:52:36 +020013521
13522 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030013523 * We're finally done with prerequisite checking, and can start with
13524 * the nested entry.
13525 */
Jim Mattson6514dc32018-04-26 16:09:12 -070013526 vmx->nested.nested_run_pending = 1;
Sean Christophersona633e412018-09-26 09:23:47 -070013527 ret = nested_vmx_enter_non_root_mode(vcpu, true);
Sean Christopherson52017602018-09-26 09:23:57 -070013528 vmx->nested.nested_run_pending = !ret;
13529 if (ret > 0)
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013530 return 1;
Sean Christopherson52017602018-09-26 09:23:57 -070013531 else if (ret)
13532 return nested_vmx_failValid(vcpu,
13533 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wincy Vanff651cb2014-12-11 08:52:58 +030013534
Paolo Bonzinic595cee2018-07-02 13:07:14 +020013535 /* Hide L1D cache contents from the nested guest. */
13536 vmx->vcpu.arch.l1tf_flush_l1d = true;
13537
Chao Gao135a06c2018-02-11 10:06:30 +080013538 /*
Sean Christophersond63907d2018-09-26 09:23:45 -070013539 * Must happen outside of nested_vmx_enter_non_root_mode() as it will
Liran Alon61ada742018-06-23 02:35:08 +030013540 * also be used as part of restoring nVMX state for
13541 * snapshot restore (migration).
13542 *
13543 * In this flow, it is assumed that vmcs12 cache was
13544 * trasferred as part of captured nVMX state and should
13545 * therefore not be read from guest memory (which may not
13546 * exist on destination host yet).
13547 */
13548 nested_cache_shadow_vmcs12(vcpu, vmcs12);
13549
13550 /*
Chao Gao135a06c2018-02-11 10:06:30 +080013551 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
13552 * by event injection, halt vcpu.
13553 */
13554 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070013555 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
13556 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060013557 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070013558 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030013559 return 1;
13560}
13561
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013562/*
13563 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
13564 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
13565 * This function returns the new value we should put in vmcs12.guest_cr0.
13566 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
13567 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
13568 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
13569 * didn't trap the bit, because if L1 did, so would L0).
13570 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
13571 * been modified by L2, and L1 knows it. So just leave the old value of
13572 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
13573 * isn't relevant, because if L0 traps this bit it can set it to anything.
13574 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
13575 * changed these bits, and therefore they need to be updated, but L0
13576 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
13577 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
13578 */
13579static inline unsigned long
13580vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
13581{
13582 return
13583 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
13584 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
13585 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
13586 vcpu->arch.cr0_guest_owned_bits));
13587}
13588
13589static inline unsigned long
13590vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
13591{
13592 return
13593 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
13594 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
13595 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
13596 vcpu->arch.cr4_guest_owned_bits));
13597}
13598
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013599static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
13600 struct vmcs12 *vmcs12)
13601{
13602 u32 idt_vectoring;
13603 unsigned int nr;
13604
Wanpeng Li664f8e22017-08-24 03:35:09 -070013605 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013606 nr = vcpu->arch.exception.nr;
13607 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
13608
13609 if (kvm_exception_is_soft(nr)) {
13610 vmcs12->vm_exit_instruction_len =
13611 vcpu->arch.event_exit_inst_len;
13612 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
13613 } else
13614 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
13615
13616 if (vcpu->arch.exception.has_error_code) {
13617 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
13618 vmcs12->idt_vectoring_error_code =
13619 vcpu->arch.exception.error_code;
13620 }
13621
13622 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010013623 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013624 vmcs12->idt_vectoring_info_field =
13625 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030013626 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013627 nr = vcpu->arch.interrupt.nr;
13628 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
13629
13630 if (vcpu->arch.interrupt.soft) {
13631 idt_vectoring |= INTR_TYPE_SOFT_INTR;
13632 vmcs12->vm_entry_instruction_len =
13633 vcpu->arch.event_exit_inst_len;
13634 } else
13635 idt_vectoring |= INTR_TYPE_EXT_INTR;
13636
13637 vmcs12->idt_vectoring_info_field = idt_vectoring;
13638 }
13639}
13640
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013641static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
13642{
13643 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070013644 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020013645 bool block_nested_events =
13646 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080013647
Wanpeng Libfcf83b2017-08-24 03:35:11 -070013648 if (vcpu->arch.exception.pending &&
13649 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020013650 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070013651 return -EBUSY;
13652 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070013653 return 0;
13654 }
13655
Jan Kiszkaf4124502014-03-07 20:03:13 +010013656 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
13657 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020013658 if (block_nested_events)
Jan Kiszkaf4124502014-03-07 20:03:13 +010013659 return -EBUSY;
13660 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
13661 return 0;
13662 }
13663
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013664 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020013665 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013666 return -EBUSY;
13667 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
13668 NMI_VECTOR | INTR_TYPE_NMI_INTR |
13669 INTR_INFO_VALID_MASK, 0);
13670 /*
13671 * The NMI-triggered VM exit counts as injection:
13672 * clear this one and block further NMIs.
13673 */
13674 vcpu->arch.nmi_pending = 0;
13675 vmx_set_nmi_mask(vcpu, true);
13676 return 0;
13677 }
13678
13679 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
13680 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020013681 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013682 return -EBUSY;
13683 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080013684 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013685 }
13686
David Hildenbrand6342c502017-01-25 11:58:58 +010013687 vmx_complete_nested_posted_interrupt(vcpu);
13688 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013689}
13690
Sean Christophersond264ee02018-08-27 15:21:12 -070013691static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)
13692{
13693 to_vmx(vcpu)->req_immediate_exit = true;
13694}
13695
Jan Kiszkaf4124502014-03-07 20:03:13 +010013696static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
13697{
13698 ktime_t remaining =
13699 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
13700 u64 value;
13701
13702 if (ktime_to_ns(remaining) <= 0)
13703 return 0;
13704
13705 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
13706 do_div(value, 1000000);
13707 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
13708}
13709
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013710/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080013711 * Update the guest state fields of vmcs12 to reflect changes that
13712 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
13713 * VM-entry controls is also updated, since this is really a guest
13714 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013715 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080013716static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013717{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013718 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
13719 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
13720
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013721 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
13722 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
13723 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
13724
13725 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
13726 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
13727 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
13728 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
13729 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
13730 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
13731 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
13732 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
13733 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
13734 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
13735 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
13736 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
13737 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
13738 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
13739 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
13740 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
13741 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
13742 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
13743 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
13744 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
13745 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
13746 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
13747 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
13748 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
13749 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
13750 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
13751 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
13752 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
13753 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
13754 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
13755 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
13756 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
13757 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
13758 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
13759 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
13760 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
13761
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013762 vmcs12->guest_interruptibility_info =
13763 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
13764 vmcs12->guest_pending_dbg_exceptions =
13765 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010013766 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
13767 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
13768 else
13769 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013770
Jan Kiszkaf4124502014-03-07 20:03:13 +010013771 if (nested_cpu_has_preemption_timer(vmcs12)) {
13772 if (vmcs12->vm_exit_controls &
13773 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
13774 vmcs12->vmx_preemption_timer_value =
13775 vmx_get_preemption_timer_value(vcpu);
13776 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
13777 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080013778
Nadav Har'El3633cfc2013-08-05 11:07:07 +030013779 /*
13780 * In some cases (usually, nested EPT), L2 is allowed to change its
13781 * own CR3 without exiting. If it has changed it, we must keep it.
13782 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
13783 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
13784 *
13785 * Additionally, restore L2's PDPTR to vmcs12.
13786 */
13787 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010013788 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030013789 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
13790 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
13791 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
13792 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
13793 }
13794
Jim Mattsond281e132017-06-01 12:44:46 -070013795 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030013796
Wincy Van608406e2015-02-03 23:57:51 +080013797 if (nested_cpu_has_vid(vmcs12))
13798 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
13799
Jan Kiszkac18911a2013-03-13 16:06:41 +010013800 vmcs12->vm_entry_controls =
13801 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020013802 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010013803
Jan Kiszka2996fca2014-06-16 13:59:43 +020013804 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
13805 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
13806 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
13807 }
13808
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013809 /* TODO: These cannot have changed unless we have MSR bitmaps and
13810 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020013811 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013812 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020013813 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
13814 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013815 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
13816 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
13817 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010013818 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010013819 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080013820}
13821
13822/*
13823 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
13824 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
13825 * and this function updates it to reflect the changes to the guest state while
13826 * L2 was running (and perhaps made some exits which were handled directly by L0
13827 * without going back to L1), and to reflect the exit reason.
13828 * Note that we do not have to copy here all VMCS fields, just those that
13829 * could have changed by the L2 guest or the exit - i.e., the guest-state and
13830 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
13831 * which already writes to vmcs12 directly.
13832 */
13833static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
13834 u32 exit_reason, u32 exit_intr_info,
13835 unsigned long exit_qualification)
13836{
13837 /* update guest state fields: */
13838 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013839
13840 /* update exit information fields: */
13841
Jan Kiszka533558b2014-01-04 18:47:20 +010013842 vmcs12->vm_exit_reason = exit_reason;
13843 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010013844 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020013845
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013846 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013847 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
13848 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
13849
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013850 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070013851 vmcs12->launch_state = 1;
13852
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013853 /* vm_entry_intr_info_field is cleared on exit. Emulate this
13854 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013855 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013856
13857 /*
13858 * Transfer the event that L0 or L1 may wanted to inject into
13859 * L2 to IDT_VECTORING_INFO_FIELD.
13860 */
13861 vmcs12_save_pending_event(vcpu, vmcs12);
13862 }
13863
13864 /*
13865 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
13866 * preserved above and would only end up incorrectly in L1.
13867 */
13868 vcpu->arch.nmi_injected = false;
13869 kvm_clear_exception_queue(vcpu);
13870 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013871}
13872
13873/*
13874 * A part of what we need to when the nested L2 guest exits and we want to
13875 * run its L1 parent, is to reset L1's guest state to the host state specified
13876 * in vmcs12.
13877 * This function is to be called not only on normal nested exit, but also on
13878 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
13879 * Failures During or After Loading Guest State").
13880 * This function should be called when the active VMCS is L1's (vmcs01).
13881 */
Jan Kiszka733568f2013-02-23 15:07:47 +010013882static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
13883 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013884{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013885 struct kvm_segment seg;
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013886 u32 entry_failure_code;
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013887
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013888 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
13889 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020013890 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013891 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
13892 else
13893 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
13894 vmx_set_efer(vcpu, vcpu->arch.efer);
13895
13896 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
13897 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070013898 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Sean Christophersoncb61de22018-09-26 09:23:53 -070013899 vmx_set_interrupt_shadow(vcpu, 0);
13900
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013901 /*
13902 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013903 * actually changed, because vmx_set_cr0 refers to efer set above.
13904 *
13905 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
13906 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013907 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013908 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020013909 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013910
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013911 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013912 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080013913 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013914
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013915 nested_ept_uninit_mmu_context(vcpu);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030013916
Liran Alon6f1e03b2018-05-22 17:16:14 +030013917 /*
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013918 * Only PDPTE load can fail as the value of cr3 was checked on entry and
13919 * couldn't have changed.
13920 */
13921 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
13922 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
13923
13924 if (!enable_ept)
13925 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
Jan Kiszka44811c02013-08-04 17:17:27 +020013926
Liran Alon6f1e03b2018-05-22 17:16:14 +030013927 /*
Liran Alonefebf0a2018-10-08 23:42:20 +030013928 * If vmcs01 doesn't use VPID, CPU flushes TLB on every
Liran Alon6f1e03b2018-05-22 17:16:14 +030013929 * VMEntry/VMExit. Thus, no need to flush TLB.
13930 *
Liran Alonefebf0a2018-10-08 23:42:20 +030013931 * If vmcs12 doesn't use VPID, L1 expects TLB to be
13932 * flushed on every VMEntry/VMExit.
Liran Alon6f1e03b2018-05-22 17:16:14 +030013933 *
Liran Alonefebf0a2018-10-08 23:42:20 +030013934 * Otherwise, we can preserve TLB entries as long as we are
13935 * able to tag L1 TLB entries differently than L2 TLB entries.
Liran Alon14389212018-10-08 23:42:17 +030013936 *
13937 * If vmcs12 uses EPT, we need to execute this flush on EPTP01
13938 * and therefore we request the TLB flush to happen only after VMCS EPTP
13939 * has been set by KVM_REQ_LOAD_CR3.
Liran Alon6f1e03b2018-05-22 17:16:14 +030013940 */
13941 if (enable_vpid &&
Liran Alonefebf0a2018-10-08 23:42:20 +030013942 (!nested_cpu_has_vpid(vmcs12) || !nested_has_guest_tlb_tag(vcpu))) {
Liran Alon14389212018-10-08 23:42:17 +030013943 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013944 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013945
13946 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
13947 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
13948 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
13949 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013950 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020013951 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
13952 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013953
Paolo Bonzini36be0b92014-02-24 12:30:04 +010013954 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
13955 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
13956 vmcs_write64(GUEST_BNDCFGS, 0);
13957
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013958 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
13959 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
13960 vcpu->arch.pat = vmcs12->host_ia32_pat;
13961 }
Jan Kiszka503cd0c2013-03-03 13:05:44 +010013962 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013963 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
13964 vmcs12->host_ia32_perf_global_ctrl);
13965
13966 /* Set L1 segment info according to Intel SDM
13967 27.5.2 Loading Host Segment and Descriptor-Table Registers */
13968 seg = (struct kvm_segment) {
13969 .base = 0,
13970 .limit = 0xFFFFFFFF,
13971 .selector = vmcs12->host_cs_selector,
13972 .type = 11,
13973 .present = 1,
13974 .s = 1,
13975 .g = 1
13976 };
13977 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
13978 seg.l = 1;
13979 else
13980 seg.db = 1;
13981 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
13982 seg = (struct kvm_segment) {
13983 .base = 0,
13984 .limit = 0xFFFFFFFF,
13985 .type = 3,
13986 .present = 1,
13987 .s = 1,
13988 .db = 1,
13989 .g = 1
13990 };
13991 seg.selector = vmcs12->host_ds_selector;
13992 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
13993 seg.selector = vmcs12->host_es_selector;
13994 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
13995 seg.selector = vmcs12->host_ss_selector;
13996 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
13997 seg.selector = vmcs12->host_fs_selector;
13998 seg.base = vmcs12->host_fs_base;
13999 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
14000 seg.selector = vmcs12->host_gs_selector;
Gleb Natapov205befd2013-08-04 15:08:06 +030014001 seg.base = vmcs12->host_gs_base;
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080014002 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
14003 seg = (struct kvm_segment) {
14004 .base = vmcs12->host_tr_base,
14005 .limit = 0x67,
14006 .selector = vmcs12->host_tr_selector,
14007 .type = 11,
14008 .present = 1
Jan Kiszka503cd0c2013-03-03 13:05:44 +010014009 };
14010 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030014011
14012 kvm_set_dr(vcpu, 7, 0x400);
14013 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030014014
Wincy Van3af18d92015-02-03 23:49:31 +080014015 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010014016 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080014017
Wincy Vanff651cb2014-12-11 08:52:58 +030014018 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
14019 vmcs12->vm_exit_msr_load_count))
14020 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030014021}
14022
Sean Christophersonbd18bff2018-08-22 14:57:07 -070014023static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx)
14024{
14025 struct shared_msr_entry *efer_msr;
14026 unsigned int i;
14027
14028 if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER)
14029 return vmcs_read64(GUEST_IA32_EFER);
14030
14031 if (cpu_has_load_ia32_efer)
14032 return host_efer;
14033
14034 for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) {
14035 if (vmx->msr_autoload.guest.val[i].index == MSR_EFER)
14036 return vmx->msr_autoload.guest.val[i].value;
14037 }
14038
14039 efer_msr = find_msr_entry(vmx, MSR_EFER);
14040 if (efer_msr)
14041 return efer_msr->data;
14042
14043 return host_efer;
14044}
14045
14046static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu)
14047{
14048 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
14049 struct vcpu_vmx *vmx = to_vmx(vcpu);
14050 struct vmx_msr_entry g, h;
14051 struct msr_data msr;
14052 gpa_t gpa;
14053 u32 i, j;
14054
14055 vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT);
14056
14057 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
14058 /*
14059 * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set
14060 * as vmcs01.GUEST_DR7 contains a userspace defined value
14061 * and vcpu->arch.dr7 is not squirreled away before the
14062 * nested VMENTER (not worth adding a variable in nested_vmx).
14063 */
14064 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
14065 kvm_set_dr(vcpu, 7, DR7_FIXED_1);
14066 else
14067 WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7)));
14068 }
14069
14070 /*
14071 * Note that calling vmx_set_{efer,cr0,cr4} is important as they
14072 * handle a variety of side effects to KVM's software model.
14073 */
14074 vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx));
14075
14076 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
14077 vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW));
14078
14079 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
14080 vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW));
14081
14082 nested_ept_uninit_mmu_context(vcpu);
14083 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
14084 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
14085
14086 /*
14087 * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs
14088 * from vmcs01 (if necessary). The PDPTRs are not loaded on
14089 * VMFail, like everything else we just need to ensure our
14090 * software model is up-to-date.
14091 */
14092 ept_save_pdptrs(vcpu);
14093
14094 kvm_mmu_reset_context(vcpu);
14095
14096 if (cpu_has_vmx_msr_bitmap())
14097 vmx_update_msr_bitmap(vcpu);
14098
14099 /*
14100 * This nasty bit of open coding is a compromise between blindly
14101 * loading L1's MSRs using the exit load lists (incorrect emulation
14102 * of VMFail), leaving the nested VM's MSRs in the software model
14103 * (incorrect behavior) and snapshotting the modified MSRs (too
14104 * expensive since the lists are unbound by hardware). For each
14105 * MSR that was (prematurely) loaded from the nested VMEntry load
14106 * list, reload it from the exit load list if it exists and differs
14107 * from the guest value. The intent is to stuff host state as
14108 * silently as possible, not to fully process the exit load list.
14109 */
14110 msr.host_initiated = false;
14111 for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) {
14112 gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g));
14113 if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) {
14114 pr_debug_ratelimited(
14115 "%s read MSR index failed (%u, 0x%08llx)\n",
14116 __func__, i, gpa);
14117 goto vmabort;
14118 }
14119
14120 for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) {
14121 gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h));
14122 if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) {
14123 pr_debug_ratelimited(
14124 "%s read MSR failed (%u, 0x%08llx)\n",
14125 __func__, j, gpa);
14126 goto vmabort;
14127 }
14128 if (h.index != g.index)
14129 continue;
14130 if (h.value == g.value)
14131 break;
14132
14133 if (nested_vmx_load_msr_check(vcpu, &h)) {
14134 pr_debug_ratelimited(
14135 "%s check failed (%u, 0x%x, 0x%x)\n",
14136 __func__, j, h.index, h.reserved);
14137 goto vmabort;
14138 }
14139
14140 msr.index = h.index;
14141 msr.data = h.value;
14142 if (kvm_set_msr(vcpu, &msr)) {
14143 pr_debug_ratelimited(
14144 "%s WRMSR failed (%u, 0x%x, 0x%llx)\n",
14145 __func__, j, h.index, h.value);
14146 goto vmabort;
14147 }
14148 }
14149 }
14150
14151 return;
14152
14153vmabort:
14154 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
14155}
14156
Nadav Har'El4704d0b2011-05-25 23:11:34 +030014157/*
14158 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
14159 * and modify vmcs12 to make it see what it would expect to see there if
14160 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
14161 */
Jan Kiszka533558b2014-01-04 18:47:20 +010014162static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
14163 u32 exit_intr_info,
14164 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030014165{
14166 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030014167 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
14168
Jan Kiszka5f3d5792013-04-14 12:12:46 +020014169 /* trying to cancel vmlaunch/vmresume is a bug */
14170 WARN_ON_ONCE(vmx->nested.nested_run_pending);
14171
Jim Mattson4f350c62017-09-14 16:31:44 -070014172 leave_guest_mode(vcpu);
14173
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020014174 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
14175 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
14176
Jim Mattson4f350c62017-09-14 16:31:44 -070014177 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014178 if (exit_reason == -1)
14179 sync_vmcs12(vcpu, vmcs12);
14180 else
14181 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
14182 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070014183
Liran Alon61ada742018-06-23 02:35:08 +030014184 /*
14185 * Must happen outside of sync_vmcs12() as it will
14186 * also be used to capture vmcs12 cache as part of
14187 * capturing nVMX state for snapshot (migration).
14188 *
14189 * Otherwise, this flush will dirty guest memory at a
14190 * point it is already assumed by user-space to be
14191 * immutable.
14192 */
14193 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
14194
Jim Mattson4f350c62017-09-14 16:31:44 -070014195 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
14196 vmcs12->vm_exit_msr_store_count))
14197 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Sean Christopherson2768c0c2018-09-26 09:23:58 -070014198 } else {
14199 /*
14200 * The only expected VM-instruction error is "VM entry with
14201 * invalid control field(s)." Anything else indicates a
14202 * problem with L0. And we should never get here with a
14203 * VMFail of any type if early consistency checks are enabled.
14204 */
14205 WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
14206 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
14207 WARN_ON_ONCE(nested_early_check);
Bandan Das77b0f5d2014-04-19 18:17:45 -040014208 }
14209
Jim Mattson4f350c62017-09-14 16:31:44 -070014210 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010014211
Paolo Bonzini93140062016-07-06 13:23:51 +020014212 /* Update any VMCS fields that might have changed while L2 ran */
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040014213 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
14214 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010014215 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Sean Christophersonf459a702018-08-27 15:21:11 -070014216
Peter Feinerc95ba922016-08-17 09:36:47 -070014217 if (kvm_has_tsc_control)
14218 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030014219
Jim Mattson8d860bb2018-05-09 16:56:05 -040014220 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
14221 vmx->nested.change_vmcs01_virtual_apic_mode = false;
14222 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070014223 } else if (!nested_cpu_has_ept(vmcs12) &&
14224 nested_cpu_has2(vmcs12,
14225 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070014226 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020014227 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030014228
14229 /* This is needed for same reason as it was needed in prepare_vmcs02 */
14230 vmx->host_rsp = 0;
14231
14232 /* Unpin physical memory we referred to in vmcs02 */
14233 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020014234 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020014235 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030014236 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080014237 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020014238 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020014239 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080014240 }
Wincy Van705699a2015-02-03 23:58:17 +080014241 if (vmx->nested.pi_desc_page) {
14242 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020014243 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080014244 vmx->nested.pi_desc_page = NULL;
14245 vmx->nested.pi_desc = NULL;
14246 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030014247
14248 /*
Tang Chen38b99172014-09-24 15:57:54 +080014249 * We are now running in L2, mmu_notifier will force to reload the
14250 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
14251 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080014252 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080014253
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020014254 if ((exit_reason != -1) && (enable_shadow_vmcs || vmx->nested.hv_evmcs))
14255 vmx->nested.need_vmcs12_sync = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010014256
14257 /* in case we halted in L2 */
14258 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070014259
14260 if (likely(!vmx->fail)) {
14261 /*
14262 * TODO: SDM says that with acknowledge interrupt on
14263 * exit, bit 31 of the VM-exit interrupt information
14264 * (valid interrupt) is always set to 1 on
14265 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
14266 * need kvm_cpu_has_interrupt(). See the commit
14267 * message for details.
14268 */
14269 if (nested_exit_intr_ack_set(vcpu) &&
14270 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
14271 kvm_cpu_has_interrupt(vcpu)) {
14272 int irq = kvm_cpu_get_interrupt(vcpu);
14273 WARN_ON(irq < 0);
14274 vmcs12->vm_exit_intr_info = irq |
14275 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
14276 }
14277
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014278 if (exit_reason != -1)
14279 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
14280 vmcs12->exit_qualification,
14281 vmcs12->idt_vectoring_info_field,
14282 vmcs12->vm_exit_intr_info,
14283 vmcs12->vm_exit_intr_error_code,
14284 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070014285
14286 load_vmcs12_host_state(vcpu, vmcs12);
14287
14288 return;
14289 }
Sean Christopherson09abb5e2018-09-26 09:23:55 -070014290
Jim Mattson4f350c62017-09-14 16:31:44 -070014291 /*
14292 * After an early L2 VM-entry failure, we're now back
14293 * in L1 which thinks it just finished a VMLAUNCH or
14294 * VMRESUME instruction, so we need to set the failure
14295 * flag and the VM-instruction error field of the VMCS
Sean Christophersoncb61de22018-09-26 09:23:53 -070014296 * accordingly, and skip the emulated instruction.
Jim Mattson4f350c62017-09-14 16:31:44 -070014297 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -070014298 (void)nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080014299
Jim Mattson4f350c62017-09-14 16:31:44 -070014300 /*
Sean Christophersonbd18bff2018-08-22 14:57:07 -070014301 * Restore L1's host state to KVM's software model. We're here
14302 * because a consistency check was caught by hardware, which
14303 * means some amount of guest state has been propagated to KVM's
14304 * model and needs to be unwound to the host's state.
Jim Mattson4f350c62017-09-14 16:31:44 -070014305 */
Sean Christophersonbd18bff2018-08-22 14:57:07 -070014306 nested_vmx_restore_host_state(vcpu);
Jim Mattson4f350c62017-09-14 16:31:44 -070014307
Jim Mattson4f350c62017-09-14 16:31:44 -070014308 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030014309}
14310
Nadav Har'El7c177932011-05-25 23:12:04 +030014311/*
Jan Kiszka42124922014-01-04 18:47:19 +010014312 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
14313 */
14314static void vmx_leave_nested(struct kvm_vcpu *vcpu)
14315{
Wanpeng Li2f707d92017-03-06 04:03:28 -080014316 if (is_guest_mode(vcpu)) {
14317 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010014318 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080014319 }
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +020014320 free_nested(vcpu);
Nadav Har'El7c177932011-05-25 23:12:04 +030014321}
14322
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020014323static int vmx_check_intercept(struct kvm_vcpu *vcpu,
14324 struct x86_instruction_info *info,
14325 enum x86_intercept_stage stage)
14326{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020014327 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
14328 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
14329
14330 /*
14331 * RDPID causes #UD if disabled through secondary execution controls.
14332 * Because it is marked as EmulateOnUD, we need to intercept it here.
14333 */
14334 if (info->intercept == x86_intercept_rdtscp &&
14335 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
14336 ctxt->exception.vector = UD_VECTOR;
14337 ctxt->exception.error_code_valid = false;
14338 return X86EMUL_PROPAGATE_FAULT;
14339 }
14340
14341 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020014342 return X86EMUL_CONTINUE;
14343}
14344
Yunhong Jiang64672c92016-06-13 14:19:59 -070014345#ifdef CONFIG_X86_64
14346/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
14347static inline int u64_shl_div_u64(u64 a, unsigned int shift,
14348 u64 divisor, u64 *result)
14349{
14350 u64 low = a << shift, high = a >> (64 - shift);
14351
14352 /* To avoid the overflow on divq */
14353 if (high >= divisor)
14354 return 1;
14355
14356 /* Low hold the result, high hold rem which is discarded */
14357 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
14358 "rm" (divisor), "0" (low), "1" (high));
14359 *result = low;
14360
14361 return 0;
14362}
14363
14364static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
14365{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020014366 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080014367 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020014368
14369 if (kvm_mwait_in_guest(vcpu->kvm))
14370 return -EOPNOTSUPP;
14371
14372 vmx = to_vmx(vcpu);
14373 tscl = rdtsc();
14374 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
14375 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080014376 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
14377
14378 if (delta_tsc > lapic_timer_advance_cycles)
14379 delta_tsc -= lapic_timer_advance_cycles;
14380 else
14381 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070014382
14383 /* Convert to host delta tsc if tsc scaling is enabled */
14384 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
14385 u64_shl_div_u64(delta_tsc,
14386 kvm_tsc_scaling_ratio_frac_bits,
14387 vcpu->arch.tsc_scaling_ratio,
14388 &delta_tsc))
14389 return -ERANGE;
14390
14391 /*
14392 * If the delta tsc can't fit in the 32 bit after the multi shift,
14393 * we can't use the preemption timer.
14394 * It's possible that it fits on later vmentries, but checking
14395 * on every vmentry is costly so we just use an hrtimer.
14396 */
14397 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
14398 return -ERANGE;
14399
14400 vmx->hv_deadline_tsc = tscl + delta_tsc;
Wanpeng Lic8533542017-06-29 06:28:09 -070014401 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070014402}
14403
14404static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
14405{
Sean Christophersonf459a702018-08-27 15:21:11 -070014406 to_vmx(vcpu)->hv_deadline_tsc = -1;
Yunhong Jiang64672c92016-06-13 14:19:59 -070014407}
14408#endif
14409
Paolo Bonzini48d89b92014-08-26 13:27:46 +020014410static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020014411{
Wanpeng Lib31c1142018-03-12 04:53:04 -070014412 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020014413 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020014414}
14415
Kai Huang843e4332015-01-28 10:54:28 +080014416static void vmx_slot_enable_log_dirty(struct kvm *kvm,
14417 struct kvm_memory_slot *slot)
14418{
14419 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
14420 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
14421}
14422
14423static void vmx_slot_disable_log_dirty(struct kvm *kvm,
14424 struct kvm_memory_slot *slot)
14425{
14426 kvm_mmu_slot_set_dirty(kvm, slot);
14427}
14428
14429static void vmx_flush_log_dirty(struct kvm *kvm)
14430{
14431 kvm_flush_pml_buffers(kvm);
14432}
14433
Bandan Dasc5f983f2017-05-05 15:25:14 -040014434static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
14435{
14436 struct vmcs12 *vmcs12;
14437 struct vcpu_vmx *vmx = to_vmx(vcpu);
14438 gpa_t gpa;
14439 struct page *page = NULL;
14440 u64 *pml_address;
14441
14442 if (is_guest_mode(vcpu)) {
14443 WARN_ON_ONCE(vmx->nested.pml_full);
14444
14445 /*
14446 * Check if PML is enabled for the nested guest.
14447 * Whether eptp bit 6 is set is already checked
14448 * as part of A/D emulation.
14449 */
14450 vmcs12 = get_vmcs12(vcpu);
14451 if (!nested_cpu_has_pml(vmcs12))
14452 return 0;
14453
Dan Carpenter47698862017-05-10 22:43:17 +030014454 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040014455 vmx->nested.pml_full = true;
14456 return 1;
14457 }
14458
14459 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
14460
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020014461 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
14462 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040014463 return 0;
14464
14465 pml_address = kmap(page);
14466 pml_address[vmcs12->guest_pml_index--] = gpa;
14467 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020014468 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040014469 }
14470
14471 return 0;
14472}
14473
Kai Huang843e4332015-01-28 10:54:28 +080014474static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
14475 struct kvm_memory_slot *memslot,
14476 gfn_t offset, unsigned long mask)
14477{
14478 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
14479}
14480
Paolo Bonzinicd39e112017-06-06 12:57:04 +020014481static void __pi_post_block(struct kvm_vcpu *vcpu)
14482{
14483 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
14484 struct pi_desc old, new;
14485 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020014486
14487 do {
14488 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020014489 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
14490 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020014491
14492 dest = cpu_physical_id(vcpu->cpu);
14493
14494 if (x2apic_enabled())
14495 new.ndst = dest;
14496 else
14497 new.ndst = (dest << 8) & 0xFF00;
14498
Paolo Bonzinicd39e112017-06-06 12:57:04 +020014499 /* set 'NV' to 'notification vector' */
14500 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020014501 } while (cmpxchg64(&pi_desc->control, old.control,
14502 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020014503
Paolo Bonzini8b306e22017-06-06 12:57:05 +020014504 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
14505 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020014506 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020014507 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020014508 vcpu->pre_pcpu = -1;
14509 }
14510}
14511
Feng Wuefc64402015-09-18 22:29:51 +080014512/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080014513 * This routine does the following things for vCPU which is going
14514 * to be blocked if VT-d PI is enabled.
14515 * - Store the vCPU to the wakeup list, so when interrupts happen
14516 * we can find the right vCPU to wake up.
14517 * - Change the Posted-interrupt descriptor as below:
14518 * 'NDST' <-- vcpu->pre_pcpu
14519 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
14520 * - If 'ON' is set during this process, which means at least one
14521 * interrupt is posted for this vCPU, we cannot block it, in
14522 * this case, return 1, otherwise, return 0.
14523 *
14524 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070014525static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080014526{
Feng Wubf9f6ac2015-09-18 22:29:55 +080014527 unsigned int dest;
14528 struct pi_desc old, new;
14529 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
14530
14531 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080014532 !irq_remapping_cap(IRQ_POSTING_CAP) ||
14533 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080014534 return 0;
14535
Paolo Bonzini8b306e22017-06-06 12:57:05 +020014536 WARN_ON(irqs_disabled());
14537 local_irq_disable();
14538 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
14539 vcpu->pre_pcpu = vcpu->cpu;
14540 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
14541 list_add_tail(&vcpu->blocked_vcpu_list,
14542 &per_cpu(blocked_vcpu_on_cpu,
14543 vcpu->pre_pcpu));
14544 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
14545 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080014546
14547 do {
14548 old.control = new.control = pi_desc->control;
14549
Feng Wubf9f6ac2015-09-18 22:29:55 +080014550 WARN((pi_desc->sn == 1),
14551 "Warning: SN field of posted-interrupts "
14552 "is set before blocking\n");
14553
14554 /*
14555 * Since vCPU can be preempted during this process,
14556 * vcpu->cpu could be different with pre_pcpu, we
14557 * need to set pre_pcpu as the destination of wakeup
14558 * notification event, then we can find the right vCPU
14559 * to wakeup in wakeup handler if interrupts happen
14560 * when the vCPU is in blocked state.
14561 */
14562 dest = cpu_physical_id(vcpu->pre_pcpu);
14563
14564 if (x2apic_enabled())
14565 new.ndst = dest;
14566 else
14567 new.ndst = (dest << 8) & 0xFF00;
14568
14569 /* set 'NV' to 'wakeup vector' */
14570 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020014571 } while (cmpxchg64(&pi_desc->control, old.control,
14572 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080014573
Paolo Bonzini8b306e22017-06-06 12:57:05 +020014574 /* We should not block the vCPU if an interrupt is posted for it. */
14575 if (pi_test_on(pi_desc) == 1)
14576 __pi_post_block(vcpu);
14577
14578 local_irq_enable();
14579 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080014580}
14581
Yunhong Jiangbc225122016-06-13 14:19:58 -070014582static int vmx_pre_block(struct kvm_vcpu *vcpu)
14583{
14584 if (pi_pre_block(vcpu))
14585 return 1;
14586
Yunhong Jiang64672c92016-06-13 14:19:59 -070014587 if (kvm_lapic_hv_timer_in_use(vcpu))
14588 kvm_lapic_switch_to_sw_timer(vcpu);
14589
Yunhong Jiangbc225122016-06-13 14:19:58 -070014590 return 0;
14591}
14592
14593static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080014594{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020014595 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080014596 return;
14597
Paolo Bonzini8b306e22017-06-06 12:57:05 +020014598 WARN_ON(irqs_disabled());
14599 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020014600 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020014601 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080014602}
14603
Yunhong Jiangbc225122016-06-13 14:19:58 -070014604static void vmx_post_block(struct kvm_vcpu *vcpu)
14605{
Yunhong Jiang64672c92016-06-13 14:19:59 -070014606 if (kvm_x86_ops->set_hv_timer)
14607 kvm_lapic_switch_to_hv_timer(vcpu);
14608
Yunhong Jiangbc225122016-06-13 14:19:58 -070014609 pi_post_block(vcpu);
14610}
14611
Feng Wubf9f6ac2015-09-18 22:29:55 +080014612/*
Feng Wuefc64402015-09-18 22:29:51 +080014613 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
14614 *
14615 * @kvm: kvm
14616 * @host_irq: host irq of the interrupt
14617 * @guest_irq: gsi of the interrupt
14618 * @set: set or unset PI
14619 * returns 0 on success, < 0 on failure
14620 */
14621static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
14622 uint32_t guest_irq, bool set)
14623{
14624 struct kvm_kernel_irq_routing_entry *e;
14625 struct kvm_irq_routing_table *irq_rt;
14626 struct kvm_lapic_irq irq;
14627 struct kvm_vcpu *vcpu;
14628 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010014629 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080014630
14631 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080014632 !irq_remapping_cap(IRQ_POSTING_CAP) ||
14633 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080014634 return 0;
14635
14636 idx = srcu_read_lock(&kvm->irq_srcu);
14637 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010014638 if (guest_irq >= irq_rt->nr_rt_entries ||
14639 hlist_empty(&irq_rt->map[guest_irq])) {
14640 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
14641 guest_irq, irq_rt->nr_rt_entries);
14642 goto out;
14643 }
Feng Wuefc64402015-09-18 22:29:51 +080014644
14645 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
14646 if (e->type != KVM_IRQ_ROUTING_MSI)
14647 continue;
14648 /*
14649 * VT-d PI cannot support posting multicast/broadcast
14650 * interrupts to a vCPU, we still use interrupt remapping
14651 * for these kind of interrupts.
14652 *
14653 * For lowest-priority interrupts, we only support
14654 * those with single CPU as the destination, e.g. user
14655 * configures the interrupts via /proc/irq or uses
14656 * irqbalance to make the interrupts single-CPU.
14657 *
14658 * We will support full lowest-priority interrupt later.
14659 */
14660
Radim Krčmář371313132016-07-12 22:09:27 +020014661 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080014662 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
14663 /*
14664 * Make sure the IRTE is in remapped mode if
14665 * we don't handle it in posted mode.
14666 */
14667 ret = irq_set_vcpu_affinity(host_irq, NULL);
14668 if (ret < 0) {
14669 printk(KERN_INFO
14670 "failed to back to remapped mode, irq: %u\n",
14671 host_irq);
14672 goto out;
14673 }
14674
Feng Wuefc64402015-09-18 22:29:51 +080014675 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080014676 }
Feng Wuefc64402015-09-18 22:29:51 +080014677
14678 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
14679 vcpu_info.vector = irq.vector;
14680
hu huajun2698d822018-04-11 15:16:40 +080014681 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080014682 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
14683
14684 if (set)
14685 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080014686 else
Feng Wuefc64402015-09-18 22:29:51 +080014687 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080014688
14689 if (ret < 0) {
14690 printk(KERN_INFO "%s: failed to update PI IRTE\n",
14691 __func__);
14692 goto out;
14693 }
14694 }
14695
14696 ret = 0;
14697out:
14698 srcu_read_unlock(&kvm->irq_srcu, idx);
14699 return ret;
14700}
14701
Ashok Rajc45dcc72016-06-22 14:59:56 +080014702static void vmx_setup_mce(struct kvm_vcpu *vcpu)
14703{
14704 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
14705 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
14706 FEATURE_CONTROL_LMCE;
14707 else
14708 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
14709 ~FEATURE_CONTROL_LMCE;
14710}
14711
Ladi Prosek72d7b372017-10-11 16:54:41 +020014712static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
14713{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014714 /* we need a nested vmexit to enter SMM, postpone if run is pending */
14715 if (to_vmx(vcpu)->nested.nested_run_pending)
14716 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020014717 return 1;
14718}
14719
Ladi Prosek0234bf82017-10-11 16:54:40 +020014720static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
14721{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014722 struct vcpu_vmx *vmx = to_vmx(vcpu);
14723
14724 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
14725 if (vmx->nested.smm.guest_mode)
14726 nested_vmx_vmexit(vcpu, -1, 0, 0);
14727
14728 vmx->nested.smm.vmxon = vmx->nested.vmxon;
14729 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070014730 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020014731 return 0;
14732}
14733
14734static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
14735{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014736 struct vcpu_vmx *vmx = to_vmx(vcpu);
14737 int ret;
14738
14739 if (vmx->nested.smm.vmxon) {
14740 vmx->nested.vmxon = true;
14741 vmx->nested.smm.vmxon = false;
14742 }
14743
14744 if (vmx->nested.smm.guest_mode) {
14745 vcpu->arch.hflags &= ~HF_SMM_MASK;
Sean Christophersona633e412018-09-26 09:23:47 -070014746 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014747 vcpu->arch.hflags |= HF_SMM_MASK;
14748 if (ret)
14749 return ret;
14750
14751 vmx->nested.smm.guest_mode = false;
14752 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020014753 return 0;
14754}
14755
Ladi Prosekcc3d9672017-10-17 16:02:39 +020014756static int enable_smi_window(struct kvm_vcpu *vcpu)
14757{
14758 return 0;
14759}
14760
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020014761static inline int vmx_has_valid_vmcs12(struct kvm_vcpu *vcpu)
14762{
14763 struct vcpu_vmx *vmx = to_vmx(vcpu);
14764
14765 /*
14766 * In case we do two consecutive get/set_nested_state()s while L2 was
14767 * running hv_evmcs may end up not being mapped (we map it from
14768 * nested_vmx_run()/vmx_vcpu_run()). Check is_guest_mode() as we always
14769 * have vmcs12 if it is true.
14770 */
14771 return is_guest_mode(vcpu) || vmx->nested.current_vmptr != -1ull ||
14772 vmx->nested.hv_evmcs;
14773}
14774
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014775static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
14776 struct kvm_nested_state __user *user_kvm_nested_state,
14777 u32 user_data_size)
14778{
14779 struct vcpu_vmx *vmx;
14780 struct vmcs12 *vmcs12;
14781 struct kvm_nested_state kvm_state = {
14782 .flags = 0,
14783 .format = 0,
14784 .size = sizeof(kvm_state),
14785 .vmx.vmxon_pa = -1ull,
14786 .vmx.vmcs_pa = -1ull,
14787 };
14788
14789 if (!vcpu)
14790 return kvm_state.size + 2 * VMCS12_SIZE;
14791
14792 vmx = to_vmx(vcpu);
14793 vmcs12 = get_vmcs12(vcpu);
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020014794
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020014795 if (nested_vmx_allowed(vcpu) && vmx->nested.enlightened_vmcs_enabled)
14796 kvm_state.flags |= KVM_STATE_NESTED_EVMCS;
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020014797
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014798 if (nested_vmx_allowed(vcpu) &&
14799 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
14800 kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
14801 kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
14802
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020014803 if (vmx_has_valid_vmcs12(vcpu)) {
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014804 kvm_state.size += VMCS12_SIZE;
14805
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014806 if (is_guest_mode(vcpu) &&
14807 nested_cpu_has_shadow_vmcs(vmcs12) &&
14808 vmcs12->vmcs_link_pointer != -1ull)
14809 kvm_state.size += VMCS12_SIZE;
14810 }
14811
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014812 if (vmx->nested.smm.vmxon)
14813 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
14814
14815 if (vmx->nested.smm.guest_mode)
14816 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
14817
14818 if (is_guest_mode(vcpu)) {
14819 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
14820
14821 if (vmx->nested.nested_run_pending)
14822 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
14823 }
14824 }
14825
14826 if (user_data_size < kvm_state.size)
14827 goto out;
14828
14829 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
14830 return -EFAULT;
14831
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020014832 if (!vmx_has_valid_vmcs12(vcpu))
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014833 goto out;
14834
14835 /*
14836 * When running L2, the authoritative vmcs12 state is in the
14837 * vmcs02. When running L1, the authoritative vmcs12 state is
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020014838 * in the shadow or enlightened vmcs linked to vmcs01, unless
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020014839 * need_vmcs12_sync is set, in which case, the authoritative
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014840 * vmcs12 state is in the vmcs12 already.
14841 */
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020014842 if (is_guest_mode(vcpu)) {
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014843 sync_vmcs12(vcpu, vmcs12);
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020014844 } else if (!vmx->nested.need_vmcs12_sync) {
14845 if (vmx->nested.hv_evmcs)
14846 copy_enlightened_to_vmcs12(vmx);
14847 else if (enable_shadow_vmcs)
14848 copy_shadow_to_vmcs12(vmx);
14849 }
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014850
14851 if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
14852 return -EFAULT;
14853
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014854 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
14855 vmcs12->vmcs_link_pointer != -1ull) {
14856 if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
14857 get_shadow_vmcs12(vcpu), sizeof(*vmcs12)))
14858 return -EFAULT;
14859 }
14860
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014861out:
14862 return kvm_state.size;
14863}
14864
14865static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
14866 struct kvm_nested_state __user *user_kvm_nested_state,
14867 struct kvm_nested_state *kvm_state)
14868{
14869 struct vcpu_vmx *vmx = to_vmx(vcpu);
14870 struct vmcs12 *vmcs12;
14871 u32 exit_qual;
14872 int ret;
14873
14874 if (kvm_state->format != 0)
14875 return -EINVAL;
14876
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020014877 if (kvm_state->flags & KVM_STATE_NESTED_EVMCS)
14878 nested_enable_evmcs(vcpu, NULL);
14879
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014880 if (!nested_vmx_allowed(vcpu))
14881 return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
14882
14883 if (kvm_state->vmx.vmxon_pa == -1ull) {
14884 if (kvm_state->vmx.smm.flags)
14885 return -EINVAL;
14886
14887 if (kvm_state->vmx.vmcs_pa != -1ull)
14888 return -EINVAL;
14889
14890 vmx_leave_nested(vcpu);
14891 return 0;
14892 }
14893
14894 if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
14895 return -EINVAL;
14896
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014897 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
14898 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
14899 return -EINVAL;
14900
14901 if (kvm_state->vmx.smm.flags &
14902 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
14903 return -EINVAL;
14904
Paolo Bonzini5bea5122018-09-18 15:19:17 +020014905 /*
14906 * SMM temporarily disables VMX, so we cannot be in guest mode,
14907 * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
14908 * must be zero.
14909 */
14910 if (is_smm(vcpu) ? kvm_state->flags : kvm_state->vmx.smm.flags)
14911 return -EINVAL;
14912
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014913 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
14914 !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
14915 return -EINVAL;
14916
14917 vmx_leave_nested(vcpu);
14918 if (kvm_state->vmx.vmxon_pa == -1ull)
14919 return 0;
14920
14921 vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
14922 ret = enter_vmx_operation(vcpu);
14923 if (ret)
14924 return ret;
14925
Vitaly Kuznetsova1b0c1c2018-10-16 18:50:07 +020014926 /* Empty 'VMXON' state is permitted */
14927 if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
14928 return 0;
14929
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020014930 if (kvm_state->vmx.vmcs_pa != -1ull) {
14931 if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
14932 !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
14933 return -EINVAL;
Vitaly Kuznetsova1b0c1c2018-10-16 18:50:07 +020014934
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020014935 set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
14936 } else if (kvm_state->flags & KVM_STATE_NESTED_EVMCS) {
14937 /*
14938 * Sync eVMCS upon entry as we may not have
14939 * HV_X64_MSR_VP_ASSIST_PAGE set up yet.
14940 */
14941 vmx->nested.need_vmcs12_sync = true;
14942 } else {
14943 return -EINVAL;
14944 }
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014945
14946 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
14947 vmx->nested.smm.vmxon = true;
14948 vmx->nested.vmxon = false;
14949
14950 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
14951 vmx->nested.smm.guest_mode = true;
14952 }
14953
14954 vmcs12 = get_vmcs12(vcpu);
14955 if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
14956 return -EFAULT;
14957
Liran Alon392b2f22018-06-23 02:35:01 +030014958 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014959 return -EINVAL;
14960
14961 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
14962 return 0;
14963
14964 vmx->nested.nested_run_pending =
14965 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
14966
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014967 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
14968 vmcs12->vmcs_link_pointer != -1ull) {
14969 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
14970 if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12))
14971 return -EINVAL;
14972
14973 if (copy_from_user(shadow_vmcs12,
14974 user_kvm_nested_state->data + VMCS12_SIZE,
14975 sizeof(*vmcs12)))
14976 return -EFAULT;
14977
14978 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
14979 !shadow_vmcs12->hdr.shadow_vmcs)
14980 return -EINVAL;
14981 }
14982
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014983 if (check_vmentry_prereqs(vcpu, vmcs12) ||
14984 check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
14985 return -EINVAL;
14986
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014987 vmx->nested.dirty_vmcs12 = true;
Sean Christophersona633e412018-09-26 09:23:47 -070014988 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014989 if (ret)
14990 return -EINVAL;
14991
14992 return 0;
14993}
14994
Kees Cook404f6aa2016-08-08 16:29:06 -070014995static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080014996 .cpu_has_kvm_support = cpu_has_kvm_support,
14997 .disabled_by_bios = vmx_disabled_by_bios,
14998 .hardware_setup = hardware_setup,
14999 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030015000 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080015001 .hardware_enable = hardware_enable,
15002 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080015003 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020015004 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080015005
Wanpeng Lib31c1142018-03-12 04:53:04 -070015006 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070015007 .vm_alloc = vmx_vm_alloc,
15008 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070015009
Avi Kivity6aa8b732006-12-10 02:21:36 -080015010 .vcpu_create = vmx_create_vcpu,
15011 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030015012 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080015013
Sean Christopherson6d6095b2018-07-23 12:32:44 -070015014 .prepare_guest_switch = vmx_prepare_switch_to_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -080015015 .vcpu_load = vmx_vcpu_load,
15016 .vcpu_put = vmx_vcpu_put,
15017
Paolo Bonzinia96036b2015-11-10 11:55:36 +010015018 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060015019 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080015020 .get_msr = vmx_get_msr,
15021 .set_msr = vmx_set_msr,
15022 .get_segment_base = vmx_get_segment_base,
15023 .get_segment = vmx_get_segment,
15024 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020015025 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080015026 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020015027 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020015028 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030015029 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080015030 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080015031 .set_cr3 = vmx_set_cr3,
15032 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080015033 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080015034 .get_idt = vmx_get_idt,
15035 .set_idt = vmx_set_idt,
15036 .get_gdt = vmx_get_gdt,
15037 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010015038 .get_dr6 = vmx_get_dr6,
15039 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030015040 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010015041 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030015042 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080015043 .get_rflags = vmx_get_rflags,
15044 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080015045
Avi Kivity6aa8b732006-12-10 02:21:36 -080015046 .tlb_flush = vmx_flush_tlb,
Junaid Shahidfaff8752018-06-29 13:10:05 -070015047 .tlb_flush_gva = vmx_flush_tlb_gva,
Avi Kivity6aa8b732006-12-10 02:21:36 -080015048
Avi Kivity6aa8b732006-12-10 02:21:36 -080015049 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020015050 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080015051 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040015052 .set_interrupt_shadow = vmx_set_interrupt_shadow,
15053 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020015054 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030015055 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030015056 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020015057 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030015058 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020015059 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030015060 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010015061 .get_nmi_mask = vmx_get_nmi_mask,
15062 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030015063 .enable_nmi_window = enable_nmi_window,
15064 .enable_irq_window = enable_irq_window,
15065 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040015066 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080015067 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030015068 .get_enable_apicv = vmx_get_enable_apicv,
15069 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080015070 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010015071 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080015072 .hwapic_irr_update = vmx_hwapic_irr_update,
15073 .hwapic_isr_update = vmx_hwapic_isr_update,
Liran Alone6c67d82018-09-04 10:56:52 +030015074 .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt,
Yang Zhanga20ed542013-04-11 19:25:15 +080015075 .sync_pir_to_irr = vmx_sync_pir_to_irr,
15076 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030015077
Izik Eiduscbc94022007-10-25 00:29:55 +020015078 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070015079 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080015080 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080015081 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030015082
Avi Kivity586f9602010-11-18 13:09:54 +020015083 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020015084
Sheng Yang17cc3932010-01-05 19:02:27 +080015085 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080015086
15087 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080015088
15089 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000015090 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020015091
15092 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080015093
15094 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100015095
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020015096 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Leonid Shatz326e7422018-11-06 12:14:25 +020015097 .write_l1_tsc_offset = vmx_write_l1_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020015098
15099 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020015100
15101 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080015102 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000015103 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080015104 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020015105 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010015106
15107 .check_nested_events = vmx_check_nested_events,
Sean Christophersond264ee02018-08-27 15:21:12 -070015108 .request_immediate_exit = vmx_request_immediate_exit,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020015109
15110 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080015111
15112 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
15113 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
15114 .flush_log_dirty = vmx_flush_log_dirty,
15115 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040015116 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020015117
Feng Wubf9f6ac2015-09-18 22:29:55 +080015118 .pre_block = vmx_pre_block,
15119 .post_block = vmx_post_block,
15120
Wei Huang25462f72015-06-19 15:45:05 +020015121 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080015122
15123 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070015124
15125#ifdef CONFIG_X86_64
15126 .set_hv_timer = vmx_set_hv_timer,
15127 .cancel_hv_timer = vmx_cancel_hv_timer,
15128#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080015129
15130 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020015131
Jim Mattson8fcc4b52018-07-10 11:27:20 +020015132 .get_nested_state = vmx_get_nested_state,
15133 .set_nested_state = vmx_set_nested_state,
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020015134 .get_vmcs12_pages = nested_get_vmcs12_pages,
15135
Ladi Prosek72d7b372017-10-11 16:54:41 +020015136 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020015137 .pre_enter_smm = vmx_pre_enter_smm,
15138 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020015139 .enable_smi_window = enable_smi_window,
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +020015140
15141 .nested_enable_evmcs = nested_enable_evmcs,
Avi Kivity6aa8b732006-12-10 02:21:36 -080015142};
15143
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020015144static void vmx_cleanup_l1d_flush(void)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020015145{
15146 if (vmx_l1d_flush_pages) {
15147 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
15148 vmx_l1d_flush_pages = NULL;
15149 }
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020015150 /* Restore state so sysfs ignores VMX */
15151 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020015152}
15153
Thomas Gleixnera7b90202018-07-13 16:23:18 +020015154static void vmx_exit(void)
15155{
15156#ifdef CONFIG_KEXEC_CORE
15157 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
15158 synchronize_rcu();
15159#endif
15160
15161 kvm_exit();
15162
15163#if IS_ENABLED(CONFIG_HYPERV)
15164 if (static_branch_unlikely(&enable_evmcs)) {
15165 int cpu;
15166 struct hv_vp_assist_page *vp_ap;
15167 /*
15168 * Reset everything to support using non-enlightened VMCS
15169 * access later (e.g. when we reload the module with
15170 * enlightened_vmcs=0)
15171 */
15172 for_each_online_cpu(cpu) {
15173 vp_ap = hv_get_vp_assist_page(cpu);
15174
15175 if (!vp_ap)
15176 continue;
15177
15178 vp_ap->current_nested_vmcs = 0;
15179 vp_ap->enlighten_vmentry = 0;
15180 }
15181
15182 static_branch_disable(&enable_evmcs);
15183 }
15184#endif
15185 vmx_cleanup_l1d_flush();
15186}
15187module_exit(vmx_exit);
15188
Avi Kivity6aa8b732006-12-10 02:21:36 -080015189static int __init vmx_init(void)
15190{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010015191 int r;
15192
15193#if IS_ENABLED(CONFIG_HYPERV)
15194 /*
15195 * Enlightened VMCS usage should be recommended and the host needs
15196 * to support eVMCS v1 or above. We can also disable eVMCS support
15197 * with module parameter.
15198 */
15199 if (enlightened_vmcs &&
15200 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
15201 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
15202 KVM_EVMCS_VERSION) {
15203 int cpu;
15204
15205 /* Check that we have assist pages on all online CPUs */
15206 for_each_online_cpu(cpu) {
15207 if (!hv_get_vp_assist_page(cpu)) {
15208 enlightened_vmcs = false;
15209 break;
15210 }
15211 }
15212
15213 if (enlightened_vmcs) {
15214 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
15215 static_branch_enable(&enable_evmcs);
15216 }
15217 } else {
15218 enlightened_vmcs = false;
15219 }
15220#endif
15221
15222 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Thomas Gleixnera7b90202018-07-13 16:23:18 +020015223 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030015224 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080015225 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080015226
Thomas Gleixnera7b90202018-07-13 16:23:18 +020015227 /*
Thomas Gleixner7db92e12018-07-13 16:23:19 +020015228 * Must be called after kvm_init() so enable_ept is properly set
15229 * up. Hand the parameter mitigation value in which was stored in
15230 * the pre module init parser. If no parameter was given, it will
15231 * contain 'auto' which will be turned into the default 'cond'
15232 * mitigation mode.
Thomas Gleixnera7b90202018-07-13 16:23:18 +020015233 */
Thomas Gleixner7db92e12018-07-13 16:23:19 +020015234 if (boot_cpu_has(X86_BUG_L1TF)) {
15235 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
15236 if (r) {
15237 vmx_exit();
15238 return r;
15239 }
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020015240 }
15241
Dave Young2965faa2015-09-09 15:38:55 -070015242#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080015243 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
15244 crash_vmclear_local_loaded_vmcss);
15245#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070015246 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080015247
He, Qingfdef3ad2007-04-30 09:45:24 +030015248 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080015249}
Thomas Gleixnera7b90202018-07-13 16:23:18 +020015250module_init(vmx_init);